TESTS:
sage: float(RR['x,y'](0)) # indirect doctest
0.0
TESTS:
sage: type(RR['x,y'])
<class 'sage.rings.polynomial.multi_polynomial_ring.MPolynomialRing_polydict_domain'>
sage: type(RR['x, y'](0))
<class 'sage.rings.polynomial.multi_polynomial_element.MPolynomial_polydict'>
sage: int(RR['x,y'](0)) # indirect doctest
0
sage: int(RR['x,y'](10))
10
sage: int(RR['x,y'].gen())
...
TypeError...
Facilitates iterating over the monomials of self, returning tuples of the form (coeff, mon) for each non-zero monomial.
Note
This function creates the entire list upfront because Cython doesn’t (yet) support iterators.
EXAMPLES:
sage: P.<x,y,z> = PolynomialRing(QQ,3)
sage: f = 3*x^3*y + 16*x + 7
sage: [(c,m) for c,m in f]
[(3, x^3*y), (16, x), (7, 1)]
sage: f = P.random_element(12,14)
sage: sum(c*m for c,m in f) == f
True
TESTS:
sage: long(RR['x,y'](0)) # indirect doctest
0L
EXAMPLES:
sage: R.<x,y> = PolynomialRing(QQ)
sage: f = (x^2*y + 2*x - 3)
sage: g = (x + 1)*f
sage: g % f
0
sage: (g+1) % f
1
sage: M = x*y
sage: N = x^2*y^3
sage: M.divides(N)
True
TESTS:
sage: CDF(RR['x,y'](0)) # indirect doctest
0
TESTS:
sage: CC(RR['x,y'](0)) # indirect doctest
0
Factor a multivariate polynomial over a non-prime field by reducing to the case of a prime field and gcd’s over the non-prime field. Note that proof=False for this function.
EXAMPLES:
sage: k.<a> = GF(9)
sage: R.<x,y> = PolynomialRing(k)
sage: f = (x-a)*(y-a)
sage: f._factor_over_nonprime_finite_field()
(y + (-a)) * (x + (-a))
Given an ExpressionTreeBuilder, return an Expression representing this value.
EXAMPLES:
sage: from sage.ext.fast_callable import ExpressionTreeBuilder
sage: etb = ExpressionTreeBuilder(vars=['x','y','z'])
sage: K.<x,y,z> = QQ[]
sage: v = K.random_element(degree=3, terms=4); v
-6/5*x*y*z + 2*y*z^2 - x
sage: v._fast_callable_(etb)
add(add(add(0, mul(-6/5, mul(mul(ipow(v_0, 1), ipow(v_1, 1)), ipow(v_2, 1)))), mul(2, mul(ipow(v_1, 1), ipow(v_2, 2)))), mul(-1, ipow(v_0, 1)))
TESTS:
sage: v = K(0)
sage: vf = fast_callable(v)
sage: type(v(0r, 0r, 0r))
<type 'sage.rings.rational.Rational'>
sage: type(vf(0r, 0r, 0r))
<type 'sage.rings.rational.Rational'>
sage: K.<x,y,z> = QQ[]
sage: from sage.ext.fast_eval import fast_float
sage: fast_float(K(0)).op_list()
[('load_const', 0.0), 'return']
sage: fast_float(K(17)).op_list()
[('load_const', 0.0), ('load_const', 17.0), 'add', 'return']
sage: fast_float(y).op_list()
[('load_const', 0.0), ('load_const', 1.0), ('load_arg', 1), ('ipow', 1), 'mul', 'add', 'return']
Returns a quickly-evaluating function on floats.
EXAMPLES:
sage: K.<x,y,z> = QQ[]
sage: f = (x+2*y+3*z^2)^2 + 42
sage: f(1, 10, 100)
901260483
sage: ff = f._fast_float_()
sage: ff(0, 0, 1)
51.0
sage: ff(0, 1, 0)
46.0
sage: ff(1, 10, 100)
901260483.0
sage: ff_swapped = f._fast_float_('z', 'y', 'x')
sage: ff_swapped(100, 10, 1)
901260483.0
sage: ff_extra = f._fast_float_('x', 'A', 'y', 'B', 'z', 'C')
sage: ff_extra(1, 7, 10, 13, 100, 19)
901260483.0
Currently, we use a fairly unoptimized method that evaluates one monomial at a time, with no sharing of repeated computations and with useless additions of 0 and multiplications by 1:
sage: list(ff)
['push 0.0', 'push 12.0', 'load 1', 'load 2', 'dup', 'mul', 'mul', 'mul', 'add', 'push 4.0', 'load 0', 'load 1', 'mul', 'mul', 'add', 'push 42.0', 'add', 'push 1.0', 'load 0', 'dup', 'mul', 'mul', 'add', 'push 9.0', 'load 2', 'dup', 'mul', 'dup', 'mul', 'mul', 'add', 'push 6.0', 'load 0', 'load 2', 'dup', 'mul', 'mul', 'mul', 'add', 'push 4.0', 'load 1', 'dup', 'mul', 'mul', 'add']
TESTS:
sage: from sage.ext.fast_eval import fast_float
sage: list(fast_float(K(0), old=True))
['push 0.0']
sage: list(fast_float(K(17), old=True))
['push 0.0', 'push 17.0', 'add']
sage: list(fast_float(y, old=True))
['push 0.0', 'push 1.0', 'load 1', 'mul', 'add']
TESTS:
sage: ZZ(RR['x,y'](0)) # indirect doctest
0
sage: ZZ(RR['x,y'](0.0))
0
sage: ZZ(RR['x,y'](0.5))
...
TypeError...
Returns a Magma string representation of self valid in the given magma session.
EXAMPLES:
sage: k.<b> = GF(25); R.<x,y> = k[]
sage: f = y*x^2*b + x*(b+1) + 1
sage: magma = Magma() # so var names same below
sage: magma(f) # optional - magma
b*x^2*y + b^22*x + 1
sage: f._magma_init_(magma) # optional - magma
'_sage_[...]!((_sage_[...]!(_sage_[...]))*_sage_[...]^2*_sage_[...]+(_sage_[...]!(_sage_[...] + 1))*_sage_[...]+(_sage_[...]!(1))*1)'
A more complicated nested example:
sage: R.<x,y> = QQ[]; S.<z,w> = R[]; f = (2/3)*x^3*z + w^2 + 5
sage: f._magma_init_(magma) # optional - magma
'_sage_[...]!((_sage_[...]!((1)*1))*_sage_[...]^2+(_sage_[...]!((2/3)*_sage_[...]^3))*_sage_[...]+(_sage_[...]!((5)*1))*1)'
sage: magma(f) # optional - magma
w^2 + 2/3*x^3*z + 5
TESTS:
sage: RR(RR['x,y'](0)) # indirect doctest
0.000000000000000
Return a dict of coefficient entries suitable for construction of a MPolynomial_polydict with the given variables.
EXAMPLES:
sage: R = Integers(10)['x,y,z']['t,s']
sage: t,s = R.gens()
sage: x,y,z = R.base_ring().gens()
sage: (x+y+2*z*s+3*t)._mpoly_dict_recursive(['z','t','s'])
{(1, 0, 1): 2, (0, 1, 0): 3, (0, 0, 0): x + y}
TESTS:
sage: R = Qp(7)['x,y,z,t,p']; S = ZZ['x,z,t']['p']
sage: R(S.0)
p
sage: R = QQ['x,y,z,t,p']; S = ZZ['x']['y,z,t']['p']
sage: z = S.base_ring().gen(1)
sage: R(z)
z
sage: R = QQ['x,y,z,t,p']; S = ZZ['x']['y,z,t']['p']
sage: z = S.base_ring().gen(1); p = S.0; x = S.base_ring().base_ring().gen()
sage: R(z+p)
z + p
sage: R = Qp(7)['x,y,z,p']; S = ZZ['x']['y,z,t']['p'] # shouldn't work, but should throw a better error
sage: R(S.0)
p
See trac 2601:
sage: R.<a,b,c> = PolynomialRing(QQ, 3)
sage: a._mpoly_dict_recursive(['c', 'b', 'a'])
{(0, 0, 1): 1}
sage: testR.<a,b,c> = PolynomialRing(QQ,3)
sage: id_ringA = ideal([a^2-b,b^2-c,c^2-a])
sage: id_ringB = ideal(id_ringA.gens()).change_ring(PolynomialRing(QQ,'c,b,a'))
Given a multivariate polynomial over a nonprime finite field
, compute the norm of the polynomial down to
, which
is the product of the conjugates by the Frobenius action on
coefficients, where Frobenius acts by p-th power.
This is (currently) an internal function used in factoring over finite fields.
EXAMPLES:
sage: k.<a> = GF(9)
sage: R.<x,y> = PolynomialRing(k)
sage: f = (x-a)*(y-a)
sage: f._norm_over_nonprime_finite_field()
x^2*y^2 - x^2*y - x*y^2 - x^2 + x*y - y^2 + x + y + 1
TESTS:
sage: QQ(RR['x,y'](0)) # indirect doctest
0
sage: QQ(RR['x,y'](0.5)) # indirect doctest
...
TypeError...
TESTS:
sage: RR(RR['x,y'](0)) # indirect doctest
0.000000000000000
EXAMPLES:
sage: R.<x,y> = QQ[]
sage: f = x^3 + y
sage: g = f._symbolic_(SR); g
x^3 + y
sage: g(x=2,y=2)
10
sage: g = SR(f)
sage: g(x=2,y=2)
10
Returns the named of the arguments of self, in the order they are accepted from call.
EXAMPLES:
sage: R.<x,y> = ZZ[]
sage: x.args()
(x, y)
Return a copy of this polynomial but with coefficients in R, if at all possible.
INPUT:
EXAMPLES:
sage: R.<x,y> = QQ[]
sage: f = x^3 + 3/5*y + 1
sage: f.change_ring(GF(7))
x^3 + 2*y + 1
sage: R.<x,y> = GF(9,'a')[]
sage: (x+2*y).change_ring(GF(3))
x - y
Return the nonzero coefficients of this polynomial in a list. The returned list is decreasingly ordered by the term ordering of self.parent(), i.e. the list of coefficients matches the list of monomials returned by monomials().
EXAMPLES:
sage: R.<x,y,z> = PolynomialRing(QQ,3,order='degrevlex')
sage: f=23*x^6*y^7 + x^3*y+6*x^7*z
sage: f.coefficients()
[23, 6, 1]
sage: R.<x,y,z> = PolynomialRing(QQ,3,order='lex')
sage: f=23*x^6*y^7 + x^3*y+6*x^7*z
sage: f.coefficients()
[6, 23, 1]
Test the same stuff with base ring – different implementation:
sage: R.<x,y,z> = PolynomialRing(ZZ,3,order='degrevlex')
sage: f=23*x^6*y^7 + x^3*y+6*x^7*z
sage: f.coefficients()
[23, 6, 1]
sage: R.<x,y,z> = PolynomialRing(ZZ,3,order='lex')
sage: f=23*x^6*y^7 + x^3*y+6*x^7*z
sage: f.coefficients()
[6, 23, 1]
AUTHOR:
Returns the content of this polynomial. Here, we define content as the gcd of the coefficients in the base ring.
EXAMPLES:
sage: R.<x,y>=ZZ[]
sage: f=4*x+6*y
sage: f.content()
2
sage: f.content().parent()
Integer Ring
The formal derivative of this polynomial, with respect to variables supplied in args.
Multiple variables and iteration counts may be supplied; see documentation for the global derivative() function for more details.
See also
_derivative()
EXAMPLES:
Polynomials implemented via Singular:
sage: R.<x, y> = PolynomialRing(FiniteField(5))
sage: f = x^3*y^5 + x^7*y
sage: type(f)
<type 'sage.rings.polynomial.multi_polynomial_libsingular.MPolynomial_libsingular'>
sage: f.derivative(x)
2*x^6*y - 2*x^2*y^5
sage: f.derivative(y)
x^7
Generic multivariate polynomials:
sage: R.<t> = PowerSeriesRing(QQ)
sage: S.<x, y> = PolynomialRing(R)
sage: f = (t^2 + O(t^3))*x^2*y^3 + (37*t^4 + O(t^5))*x^3
sage: type(f)
<class 'sage.rings.polynomial.multi_polynomial_element.MPolynomial_polydict'>
sage: f.derivative(x) # with respect to x
(2*t^2 + O(t^3))*x*y^3 + (111*t^4 + O(t^5))*x^2
sage: f.derivative(y) # with respect to y
(3*t^2 + O(t^3))*x^2*y^2
sage: f.derivative(t) # with respect to t (recurses into base ring)
(2*t + O(t^2))*x^2*y^3 + (148*t^3 + O(t^4))*x^3
sage: f.derivative(x, y) # with respect to x and then y
(6*t^2 + O(t^3))*x*y^2
sage: f.derivative(y, 3) # with respect to y three times
(6*t^2 + O(t^3))*x^2
sage: f.derivative() # can't figure out the variable
...
ValueError: must specify which variable to differentiate with respect to
Polynomials over the symbolic ring (just for fun....):
sage: x = var("x")
sage: S.<u, v> = PolynomialRing(SR)
sage: f = u*v*x
sage: f.derivative(x) == u*v
True
sage: f.derivative(u) == v*x
True
Return a list of partial derivatives of this polynomial, ordered by the variables of self.parent().
EXAMPLES:
sage: P.<x,y,z> = PolynomialRing(ZZ,3)
sage: f = x*y + 1
sage: f.gradient()
[y, x, 0]
Return self if self is homogeneous. Otherwise return a homogenized polynomial for self. If a string is given, return a polynomial in one more variable named after the string such that setting that variable equal to 1 yields self. This variable is added to the end of the variables. If a variable in self.parent() is given, this variable is used to homogenize the polynomial. If an integer is given, the variable with this index is used for homogenization.
INPUT:
OUTPUT:
a multivariate polynomial
EXAMPLES:
sage: P.<x,y> = PolynomialRing(QQ,2)
sage: f = x^2 + y + 1 + 5*x*y^10
sage: g = f.homogenize('z'); g
5*x*y^10 + x^2*z^9 + y*z^10 + z^11
sage: g.parent()
Multivariate Polynomial Ring in x, y, z over Rational Field
sage: f.homogenize(x)
2*x^11 + x^10*y + 5*x*y^10
sage: f.homogenize(0)
2*x^11 + x^10*y + 5*x*y^10
sage: x, y = Zmod(3)['x', 'y'].gens()
sage: (x + x^2).homogenize(y)
x^2 + x*y
sage: x, y = Zmod(3)['x', 'y'].gens()
sage: (x + x^2).homogenize(y).parent()
Multivariate Polynomial Ring in x, y over Ring of integers modulo 3
sage: x, y = GF(3)['x', 'y'].gens()
sage: (x + x^2).homogenize(y)
x^2 + x*y
sage: x, y = GF(3)['x', 'y'].gens()
sage: (x + x^2).homogenize(y).parent()
Multivariate Polynomial Ring in x, y over Finite Field of size 3
TESTS:
sage: R = PolynomialRing(QQ, 'x', 5)
sage: p = R.random_element()
sage: q1 = p.homogenize()
sage: q2 = p.homogenize()
sage: q1.parent() is q2.parent()
True
Returns True if this polynomial is a generator of its parent.
EXAMPLES:
sage: R.<x,y>=ZZ[]
sage: x.is_generator()
True
sage: (x+y-y).is_generator()
True
sage: (x*y).is_generator()
False
sage: R.<x,y>=QQ[]
sage: x.is_generator()
True
sage: (x+y-y).is_generator()
True
sage: (x*y).is_generator()
False
Return True if self is a homogeneous polynomial.
TESTS:
sage: from sage.rings.polynomial.multi_polynomial import MPolynomial
sage: P.<x, y> = PolynomialRing(QQ, 2)
sage: MPolynomial.is_homogeneous(x+y)
True
sage: MPolynomial.is_homogeneous(P(0))
True
sage: MPolynomial.is_homogeneous(x+y^2)
False
sage: MPolynomial.is_homogeneous(x^2 + y^2)
True
sage: MPolynomial.is_homogeneous(x^2 + y^2*x)
False
sage: MPolynomial.is_homogeneous(x^2*y + y^2*x)
True
Note
This is a generic implementation which is likely overridden by subclasses.
Return the Jacobian ideal of the polynomial self.
EXAMPLES:
sage: R.<x,y,z> = QQ[]
sage: f = x^3 + y^3 + z^3
sage: f.jacobian_ideal()
Ideal (3*x^2, 3*y^2, 3*z^2) of Multivariate Polynomial Ring in x, y, z over Rational Field
Returns a new element of self.parent() obtained by applying the function f to all of the coefficients of self.
EXAMPLES:
sage: k.<a> = GF(9); R.<x,y> = k[]; f = x*a + 2*x^3*y*a + a
sage: f.map_coefficients(lambda a : a + 1)
(-a + 1)*x^3*y + (a + 1)*x + (a + 1)
Return the Newton polytope of this polynomial.
You should have the optional polymake package installed.
EXAMPLES:
sage: R.<x,y> = QQ[]
sage: f = 1 + x*y + x^3 + y^3
sage: P = f.newton_polytope()
sage: P
A Polyhedron with 3 vertices.
sage: P.is_simple()
True
TESTS:
sage: R.<x,y> = QQ[]
sage: R(0).newton_polytope()
A Polyhedron.
sage: R(1).newton_polytope()
A Polyhedron with 1 vertex.
Let var be one of the variables of the parent of self. This returns self viewed as a univariate polynomial in var over the polynomial ring generated by all the other variables of the parent.
EXAMPLES:
sage: R.<x,w,z> = QQ[]
sage: f = x^3 + 3*w*x + w^5 + (17*w^3)*x + z^5
sage: f.polynomial(x)
x^3 + (17*w^3 + 3*w)*x + w^5 + z^5
sage: parent(f.polynomial(x))
Univariate Polynomial Ring in x over Multivariate Polynomial Ring in w, z over Rational Field
sage: f.polynomial(w)
w^5 + 17*x*w^3 + 3*x*w + z^5 + x^3
sage: f.polynomial(z)
z^5 + w^5 + 17*x*w^3 + x^3 + 3*x*w
sage: R.<x,w,z,k> = ZZ[]
sage: f = x^3 + 3*w*x + w^5 + (17*w^3)*x + z^5 +x*w*z*k + 5
sage: f.polynomial(x)
x^3 + (17*w^3 + w*z*k + 3*w)*x + w^5 + z^5 + 5
sage: f.polynomial(w)
w^5 + 17*x*w^3 + (x*z*k + 3*x)*w + z^5 + x^3 + 5
sage: f.polynomial(z)
z^5 + x*w*k*z + w^5 + 17*x*w^3 + x^3 + 3*x*w + 5
sage: f.polynomial(k)
x*w*z*k + w^5 + z^5 + 17*x*w^3 + x^3 + 3*x*w + 5
sage: R.<x,y>=GF(5)[]
sage: f=x^2+x+y
sage: f.polynomial(x)
x^2 + x + y
sage: f.polynomial(y)
y + x^2 + x