On Mon, Mar 09, 2009 at 10:38:44AM -0700, Nick Alexander wrote:
> On 9-Mar-09, at 8:48 AM, Noel wrote:
> > What's the best way of listing all polynomials of a given degree with
> > coefficients in a finite field?
>
> If you want a one liner, you could use
>
> sage: [ GF(3)['x'](list(t)) for t in (GF(3)^2) ]
> [0, 1, 2, x, x + 1, x + 2, 2*x, 2*x + 1, 2*x + 2]
Nice one. Here is a variant for homogeneous multivariate polynomials.
It's longer than one line :-) But the two first functions are standard
utilies that I would like to see in Sage (volunteers?)
Cheers,
Nicolas
K = IntegerModRing(2)
P = K['x,y']
n = 2 # num variables
def term(exponents):
"""
Return the term of P with the given exponent vector.
This should be a method of P, with a fast implementation using the
internal data structure!!!
Should this be term or monomial? The names in
CombinatorialFreeModule should be named accordingly
EXAMPLES:
sage: term([3,2])
x^3*y^2
"""
return prod((x^e for (x,e) in zip(P.gens(), exponents)), P(1))
def basis(d):
"""
Returns a basis of the homogeneous component of P of degree d.
This should be a method of P, available under:
EXAMPLES:
sage: b = P.basis().subset(degree=d)
sage: b.cardinality()
sage: 4
sage: list(b)
sage: [x^3, x^2*y, x*y^2, y^3]
"""
return IntegerVectors(d, n).map(term)
def all_polynomials(d):
"""
Returns all the homogeneous polynomials of P of degree d
This should be a method of P, available under something like:
EXAMPLES:
sage: P.subset(degree = 3, homogeneous = True)
[0,
y^3,
x*y^2,
x*y^2 + y^3,
x^2*y,
x^2*y + y^3,
x^2*y + x*y^2,
x^2*y + x*y^2 + y^3,
x^3,
x^3 + y^3,
x^3 + x*y^2,
x^3 + x*y^2 + y^3,
x^3 + x^2*y,
x^3 + x^2*y + y^3,
x^3 + x^2*y + x*y^2,
x^3 + x^2*y + x*y^2 + y^3]
"""
monomials = list(basis(d))
#
def coeff_to_poly(coeffs):
return sum( (c*m for (c,m) in zip(coeffs, monomials)), P.zero_element())
# We are missing CartesianPower here
return CartesianProduct(*[K for i in
range(exponents.cardinality())]).map(coeff_to_poly)
count(all_polynomials(3))
16
list(all_polynomials(3))
[0,
y^3,
x*y^2,
x*y^2 + y^3,
x^2*y,
x^2*y + y^3,
x^2*y + x*y^2,
x^2*y + x*y^2 + y^3,
x^3,
x^3 + y^3,
x^3 + x*y^2,
x^3 + x*y^2 + y^3,
x^3 + x^2*y,
x^3 + x^2*y + y^3,
x^3 + x^2*y + x*y^2,
x^3 + x^2*y + x*y^2 + y^3]
--
Nicolas M. ThiƩry "Isil" <[email protected]>
http://Nicolas.Thiery.name/
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at http://groups.google.com/group/sage-devel
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---