Maple number theory functions and Sage

Stephen Forrest, 28 November 2007

In the course of learning Sage I have tried to compile a list of number-theoretic functions in Maple along with their more-or-less direct Sage equivalents, if such equivalents exist.

Note this is not all of Maple's number theory functions: it is essentially the numtheory package plus a few important top-level functions like modp, irem, and iquo. There are more top-level rountines, and more functionality is available in other packages, such as GaussInt.

In the second column below I use the following abbreviations and colours:

As I'm not yet familiar with Sage, I have undoubtedly missed some obvious analogues. My main source for Sage information was the Sage Tutorial and Reference Manual; any claim that a Sage equivalent does not exist comes from my failure to find it in these documents.

Please contact me if you have any questions, corrections, or other comments about this list.

Table 1: Maple number theory functions and Sage equivalents
Maple function=?Sage equivalentNotes
igcd(n)Ygcd(n)
ilcm(n)Ylcm(n)
ilog(n,b)Yn.exact_log(b)largest integer k s.t. b^k ≤ n
iquo(m,n)Ym // ninteger division
irem(m,n)Ym % ninteger remainder
isprime(n)Yn.isprime()
ithprime(n)Ynth_prime(n)
modp(m,n)YMod(m,n)
mods(m,n)N symmetric modulus (i.e. signed)
nextprime(n)Ynext_prime(n)
prevprime(n)Yprevious_prime(n)
Maple numtheory package
numtheory:-bigomega(n)Ysloane.A001222(n) # of prime divisors with multiplicity
numtheory:-cfrac(x)Pcontinued_fraction(x)Maple has more functionality:
  • can convert both to list and dedicated
    CFRAC data structure
  • for real input, supports simple & centered expansions
  • also supports rational polynomials and series
  • numtheory:-cfracpol? continued fraction expansions for all real roots
    of a rational polynomial
    numtheory:-cyclotomic(a,b)Ycyclotomic_polynomial(a,b)
    numtheory:-divisors(n)Ydivisors(n)
    numtheory:-factorEQ(x,d)YK.<r> = QuadraticField(d)
    K.factor_integer(x)
    factor integers in Z[sqrt(d)]
    numtheory:-factorset(n)Yprime_factors(n)
    numtheory:-fermat(n)N information about nth Fermat number
    numtheory:-GIgcd(x)? GCD of Gaussian integers
    numtheory:-imagunit(n)YR=IntegerModRing(n)
    sqrt( R(-1) )
    square root of -1 (mod n)
    numtheory:-index(x,a,n)Y(see numtheory:-mlog(x,a,n)) 
    numtheory:-integral_basis(x)Yt = QQ['z'].0
    K = NumberField(x,'s')
    K.integral_basis()
    compute integral basis of algebraic number fields
    given irreducible polynom or algebraic number
    numtheory:-invcfrac(x)? convert CFRAC to surd
    numtheory:-invphi(n)N inverse of phi function:
    returns list L s.t. phi(x)=n for x in L
    numtheory:-issqrfree(n)Yn.is_squarefree()
    numtheory:-jacobi(m,n)Ykronecker_symbol(m,n)
    numtheory:-kroneckerN Inhomogenous Diophantine approximation
    numtheory:-lambda(n)N Carmichael λ function (Sloane's A002322)
    numtheory:-legendre(m,n)Ykronecker_symbol(m,n)
    numtheory:-mcombine(a,ra,b,rb)YCRT(ra,rb,a,b)Chinese remaindering
    numtheory:-migcdex(N,a,b1,...,bn)N solve modulo N extended GCD problem
    numtheory:-mipolys(n,p,m)N number of monic irreducible polynomials of
    degree n over GF(p^m)
    numtheory:-mersenne([n])Ysloane.A000668(n)Mersenne primes
    numtheory:-minkowskiN solve Minkowski's linear forms
    (homogeneous Diophantine approximation)
    numtheory:-mlog(x,a,n)Pb=Mod(a,n)
    discrete_log_generic(x,b)
    Maple can also return characteristic of
    domain of answer
    numtheory:-mobius(n)Ymoebius(n)Möbius function
    numtheory:-mroot(x,n,p)YR = IntegerModRing(p)
    R(x).nth_root(n)
    nth root of x (mod p)
    numtheory:-msqrt(x, n)YR = IntegerModRing(n)
    sqrt(R(x))
    numtheory:-nearestp? nearby lattice point problem
    numtheory:-nthconver(x,n)Yconvergent(x,n)nth rational convergent
    numtheory:-nthdenom? denom of nth convergent of
    continued fraction (w.o. computing convergent)
    numtheory:-nthnumer? numer of nth convergent of
    continued fraction (w.o. computing convergent)
    numtheory:-nthpow(m,n)? largest b s.t. b^n divides m
    numtheory:-order(m,n)YR = IntegerModRing(n)
    R(m).multiplicative_order()
    numtheory:-pdexpand(x)N data structure for periodic decimal
    expansion of rational x
    numtheory:-phi(n)Yeuler_phi(n)
    numtheory:-pi(n)Yprime_pi(n)
    numtheory:-pprimrootN pseudo-primitive root
    numtheory:-primroot(n)Yprimitive_root(n)
    numtheory:-quadresY?
    numtheory:-rootsunity(p,n)Pfor p prime:
    R = IntegerModRing(n)
    zeta(p, all=True)
    for p composite, unknown
    numtheory:-safeprime(n)N smallest prime p ≥ n s.t. (p-1)/2 is prime
    numtheory:-sigma(n)Ysigma(n)
    numtheory:-sigma[k](n)Ysigma(n,k)
    numtheory:-sq2factor(n)Y R.<sqrt2> = QuadraticField(2)
    R.factor_integer(n)
    factor integers in Z[sqrt(2)]
    numtheory:-sum2sqr(n)Ptwo_squares(n) Maple returns multiple results; Sage doesn't
    numtheory:-tau(n)Ysloane.A000005(n)number of divisors
    numtheory:-thueN solve Thue equation or inequality