Skip to content

Commit 6702f84

Browse files
committed
support symbolic q in number_of_irreducible_polynomials()
1 parent f65167c commit 6702f84

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

src/sage/combinat/q_analogues.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -976,28 +976,36 @@ def q_stirling_number2(n, k, q=None):
976976
q_int(k, q=q) * q_stirling_number2(n - 1, k, q=q))
977977

978978

979-
def number_of_irreducible_polynomials(q, n):
979+
def number_of_irreducible_polynomials(n, q=None):
980980
r"""
981-
Return the number of irreducible polynomials of degree ``n``
982-
over the finite field with ``q`` elements.
981+
Return the number of monic irreducible polynomials of degree ``n``
982+
over the finite field with ``q`` elements. If ``q`` is not given,
983+
the result is returned as a polynomial in `\QQ[q]`.
983984
984985
INPUT:
985986
986-
- ``q`` -- prime power
987987
- ``n`` -- positive integer
988+
- ``q`` -- ``None`` (default) or a prime power
988989
989990
OUTPUT: integer
990991
991992
EXAMPLES::
992993
993-
sage: number_of_irreducible_polynomials(2, 8)
994+
sage: number_of_irreducible_polynomials(8, q=2)
994995
30
995-
sage: number_of_irreducible_polynomials(9, 9)
996+
sage: number_of_irreducible_polynomials(9, q=9)
996997
43046640
997998
999+
::
1000+
1001+
sage: poly = number_of_irreducible_polynomials(12); poly
1002+
1/12*q^12 - 1/12*q^6 - 1/12*q^4 + 1/12*q^2
1003+
sage: poly(5) == number_of_irreducible_polynomials(12, q=5)
1004+
True
1005+
9981006
This function is *much* faster than enumerating the polynomials::
9991007
1000-
sage: num = number_of_irreducible_polynomials(101, 99)
1008+
sage: num = number_of_irreducible_polynomials(99, q=101)
10011009
sage: num.bit_length()
10021010
653
10031011
@@ -1006,9 +1014,14 @@ def number_of_irreducible_polynomials(q, n):
10061014
Classical formula `\frac1n \sum_{d\mid n} \mu(n/d) q^d` using the
10071015
Möbius function `\mu`; see :func:`moebius`.
10081016
"""
1017+
n = ZZ(n)
1018+
if n <= 0:
1019+
raise ValueError('n must be positive')
1020+
if q is None:
1021+
q = ZZ['q'].gen()
1022+
R = parent(q)
10091023
from sage.arith.misc import moebius
1010-
q, n = ZZ(q), ZZ(n)
1011-
r = ZZ.zero()
1012-
for d in n.divisors():
1013-
r += moebius(n//d) * q**d
1014-
return r // n
1024+
r = sum((moebius(n//d) * q**d for d in ZZ(n).divisors()), R.zero())
1025+
if R is ZZ:
1026+
return r // n
1027+
return r / n

0 commit comments

Comments
 (0)