Polynomial

Polynomial()

A Symbolica polynomial with rational coefficients.

Methods

Name Description
adjoin Adjoin the coefficient ring of this polynomial R[a] with b, whose minimal polynomial
approximate_roots Approximate all complex roots of a univariate polynomial, given a maximal number of iterations
coefficient_list Get the coefficient list, optionally in the variables vars.
contains Check if the polynomial contains the given variable.
content Get the content, i.e., the GCD of the coefficients.
degree Get the degree of the polynomial in var.
derivative Take a derivative in x.
evaluate Evaluate the polynomial at point input.
evaluate_complex Evaluate the polynomial at point input with complex input.
extended_gcd Compute the extended GCD of two polynomials, yielding the GCD and the Bezout coefficients s and t
factor Factorize the polynomial.
factor_square_free Compute the square-free factorization of the polynomial.
format Convert the polynomial into a human-readable string, with tunable settings.
gcd Compute the greatest common divisor (GCD) of two or more polynomials.
get_variables Get the list of variables in the internal ordering of the polynomial.
groebner_basis Compute the Groebner basis of a polynomial system.
integrate Integrate the polynomial in x.
interpolate Perform Newton interpolation in the variable x given the sample points
isolate_roots Isolate the real roots of the polynomial. The result is a list of intervals with rational bounds that contain exactly one root,
lcoeff Get the leading coefficient.
nterms Get the number of terms.
parse Parse a polynomial with rational coefficients from a string.
primitive Get the primitive part of the polynomial, i.e., the polynomial divided
quot_rem Divide self by rhs, returning the quotient and remainder.
reduce Completely reduce the polynomial w.r.t the polynomials gs.
reorder Set a new variable ordering for the polynomial.
replace Replace the variable x with a polynomial v.
resultant Compute the resultant of two polynomials with respect to the variable var.
simplify_algebraic_number Find the minimal polynomial for the algebraic number represented by this polynomial
to_expression Convert the polynomial to an expression.
to_finite_field Convert the coefficients of the polynomial to a finite field with prime prime.
to_latex Convert the polynomial into a LaTeX string.
to_number_field Convert the polynomial to a number field defined by the minimal polynomial minimal_poly.

adjoin

Polynomial.adjoin(b, new_symbol=None)

Adjoin the coefficient ring of this polynomial R[a] with b, whose minimal polynomial is R[a][b] and form R[b]. Also return the new representation of a and b.

b must be irreducible over R and R[a]; this is not checked.

If new_symbol is provided, the variable of the new extension will be renamed to it. Otherwise, the variable of the new extension will be the same as that of b.

Examples

>>> from symbolica import *
>>> sqrt2 = P('a^2-2')
>>> sqrt23 = P('b^2-a-3')
>>> (min_poly, rep2, rep23) = sqrt2.adjoin(sqrt23)
>>>
>>> # convert to number field
>>> a = P('a^2+b').replace(S('a'), rep2).replace(S('b'), rep23).to_number_field(min_poly)

approximate_roots

Polynomial.approximate_roots(max_iterations, tolerance)

Approximate all complex roots of a univariate polynomial, given a maximal number of iterations and a given tolerance. Returns the roots and their multiplicity.

Examples

>>> p = E('x^10+9x^7+4x^3+2x+1').to_polynomial()
>>> for (r, m) in p.approximate_roots(1000, 1e-10):
>>>     print(r, m)

coefficient_list

Polynomial.coefficient_list(xs)

Get the coefficient list, optionally in the variables xs.

Examples

>>> from symbolica import *
>>> x = S('x')
>>> p = E('x*y+2*x+x^2').to_polynomial()
>>> for n, pp in p.coefficient_list(x):
>>>     print(n, pp)

contains

Polynomial.contains(var)

Check if the polynomial contains the given variable.

content

Polynomial.content()

Get the content, i.e., the GCD of the coefficients.

Examples

>>> from symbolica import *
>>> p = E('3x^2+6x+9').to_polynomial()
>>> print(p.content())

degree

Polynomial.degree(var)

Get the degree of the polynomial in var.

derivative

Polynomial.derivative(x)

Take a derivative in x.

Examples

>>> from symbolica import *
>>> x = S('x')
>>> p = E('x^2+2').to_polynomial()
>>> print(p.derivative(x))

evaluate

Polynomial.evaluate(inputs)

Evaluate the polynomial at point input.

Examples

>>> from symbolica import *
>>> P('x*y+2*x+x^2').evaluate([2., 3.])

Yields 14.0.

evaluate_complex

Polynomial.evaluate_complex(inputs)

Evaluate the polynomial at point input with complex input.

Examples

>>> from symbolica import *
>>> P('x*y+2*x+x^2').evaluate([2+1j, 3+2j])

Yields 11+13j.

extended_gcd

Polynomial.extended_gcd(rhs)

Compute the extended GCD of two polynomials, yielding the GCD and the Bezout coefficients s and t such that self * s + rhs * t = gcd(self, rhs).

Examples

>>> from symbolica import *
>>> E('(1+x)(20+x)').to_polynomial().extended_gcd(E('x^2+2').to_polynomial())

yields (1, 1/67-7/402*x, 47/134+7/402*x).

factor

Polynomial.factor()

Factorize the polynomial.

Examples

>>> from symbolica import *
>>> p = E('(x+1)(x+2)(x+3)(x+4)(x+5)(x^2+6)(x^3+7)(x+8)(x^4+9)(x^5+x+10)').expand().to_polynomial()
>>> print('Factorization of {}:'.format(p))
>>> for f, exp in p.factor():
>>>     print(' ({})^{}'.format(f, exp))

factor_square_free

Polynomial.factor_square_free()

Compute the square-free factorization of the polynomial.

Examples

>>> from symbolica import *
>>> p = E('3*(2*x^2+y)(x^3+y)^2(1+4*y)^2(1+x)').expand().to_polynomial()
>>> print('Square-free factorization of {}:'.format(p))
>>> for f, exp in p.factor_square_free():
>>>     print(' ({})^{}'.format(f, exp))

format

Polynomial.format(
    mode=PrintMode.Symbolica,
    terms_on_new_line=False,
    color_top_level_sum=True,
    color_builtin_symbols=True,
    print_finite_field=True,
    symmetric_representation_for_finite_field=False,
    explicit_rational_polynomial=False,
    number_thousands_separator=None,
    multiplication_operator='*',
    double_star_for_exponentiation=False,
    square_brackets_for_function=False,
    num_exp_as_superscript=True,
    precision=None,
    show_namespaces=False,
    max_terms=None,
    custom_print_mode=None,
)

Convert the polynomial into a human-readable string, with tunable settings.

Examples

>>> p = FiniteFieldPolynomial.parse("3*x^2+2*x+7*x^3", ['x'], 11)
>>> print(p.format(symmetric_representation_for_finite_field=True))

Yields z³⁴+x^(x+2)+y⁴+f(x,x²)+128_378_127_123 z^(2/3) w² x⁻¹ y⁻¹+3/5.

gcd

Polynomial.gcd(rhs)

Compute the greatest common divisor (GCD) of two polynomials.

get_variables

Polynomial.get_variables()

Get the list of variables in the internal ordering of the polynomial.

groebner_basis

Polynomial.groebner_basis(_cls, system, grevlex=True, print_stats=False)

Compute the Groebner basis of a polynomial system.

If grevlex=True, reverse graded lexicographical ordering is used, otherwise the ordering is lexicographical.

If print_stats=True intermediate statistics will be printed.

Examples

>>> basis = Polynomial.groebner_basis(
>>>     [E("a b c d - 1").to_polynomial(),
>>>     E("a b c + a b d + a c d + b c d").to_polynomial(),
>>>     E("a b + b c + a d + c d").to_polynomial(),
>>>     E("a + b + c + d").to_polynomial()],
>>>     grevlex=True,
>>>     print_stats=True
>>> )
>>> for p in basis:
>>>     print(p)

integrate

Polynomial.integrate(x)

Integrate the polynomial in x.

Examples

>>> from symbolica import *
>>> x = S('x')
>>> p = E('x^2+2').to_polynomial()
>>> print(p.integrate(x))

interpolate

Polynomial.interpolate(_cls, x, sample_points, values)

Perform Newton interpolation in the variable x given the sample points sample_points and the values values.

Examples

>>> x, y = S('x', 'y')
>>> a = Polynomial.interpolate(
>>>         x, [4, 5], [(y**2+5).to_polynomial(), (y**3).to_polynomial()])
>>> print(a)

yields 25-5*x+5*y^2-y^2*x-4*y^3+y^3*x.

isolate_roots

Polynomial.isolate_roots(refine=None)

Isolate the real roots of the polynomial. The result is a list of intervals with rational bounds that contain exactly one root, and the multiplicity of that root. Optionally, the intervals can be refined to a given precision.

Examples

>>> from symbolica import *
>>> p = E('2016+5808*x+5452*x^2+1178*x^3+-753*x^4+-232*x^5+41*x^6').to_polynomial()
>>> for a, b, n in p.isolate_roots():
>>>     print('({},{}): {}'.format(a, b, n))

yields

(-56/45,-77/62): 1
(-98/79,-119/96): 1
(-119/96,-21/17): 1
(-7/6,0): 1
(0,6): 1
(6,12): 1

lcoeff

Polynomial.lcoeff()

Get the leading coefficient.

Examples

>>> from symbolica import Expression
>>> p = E('3x^2+6x+9').to_polynomial().lcoeff()
>>> print(p)

Yields 3.

nterms

Polynomial.nterms()

Get the number of terms in the polynomial.

parse

Polynomial.parse(_cls, input, vars, default_namespace='python')

Parse a polynomial with integer coefficients from a string. The input must be written in an expanded format and a list of all the variables must be provided.

If these requirements are too strict, use Expression.to_polynomial() or RationalPolynomial.parse() instead.

Examples

>>> e = Polynomial.parse('3*x^2+y+y*4', ['x', 'y'])

Raises

Name Type Description
ValueError If the input is not a valid Symbolica polynomial.

primitive

Polynomial.primitive()

Get the primitive part of the polynomial, i.e., the polynomial divided by its content.

Examples

>>> from symbolica import Expression
>>> p = E('3x^2+6x+9').to_polynomial().primitive()
>>> print(p)

Yields x^2+2*x+3.

quot_rem

Polynomial.quot_rem(rhs)

Divide self by rhs, returning the quotient and remainder.

reduce

Polynomial.reduce(gs, grevlex=True)

Completely reduce the polynomial w.r.t the polynomials gs.

If grevlex=True, reverse graded lexicographical ordering is used, otherwise the ordering is lexicographical.

Examples

>>> E('y^2+x').to_polynomial().reduce([E('x').to_polynomial()])

yields y^2

reorder

Polynomial.reorder(vars)

Reorder the polynomial in-place to use the given variable order.

replace

Polynomial.replace(x, v)

Replace the variable x with a polynomial v.

Examples

>>> from symbolica import *
>>> x = S('x')
>>> p = E('x*y+2*x+x^2').to_polynomial()
>>> r = E('y+1').to_polynomial())
>>> p.replace(x, r)

resultant

Polynomial.resultant(rhs, var)

Compute the resultant of two polynomials with respect to the variable var.

simplify_algebraic_number

Polynomial.simplify_algebraic_number(minimal_poly)

Find the minimal polynomial for the algebraic number represented by this polynomial expressed in the number field defined by minimal_poly.

Examples

>>> from symbolica import *
>>> (min_poly, rep2, rep23) = P('a^2-2').adjoin(P('b^2-3'))
>>> rep2.simplify_algebraic_number(min_poly)

Yields b^2-2.

to_expression

Polynomial.to_expression()

Convert the polynomial to an expression.

Examples

>>> from symbolica import *
>>> e = E('x*y+2*x+x^2')
>>> p = e.to_polynomial()
>>> print((e - p.to_expression()).expand())

to_finite_field

Polynomial.to_finite_field(prime)

Convert the coefficients of the polynomial to a finite field with prime prime.

to_latex

Polynomial.to_latex()

Convert the polynomial into a LaTeX string.

to_number_field

Polynomial.to_number_field(minimal_poly)

Convert the polynomial to a number field defined by the minimal polynomial minimal_poly.

Examples

>>> from symbolica import *
>>> a = P('a').to_number_field(P('a^2-2'))
>>> print(a * a)

Yields 2.