88. plugins.polynomial — Polynomials

This module defines the class Polynomial, representing a polynomial in n variables.

88.1. Classes defined in module plugins.polynomial

class plugins.polynomial.Polynomial(exp, coeff=None, symbol='xyz')[source]

A polynomial in n dimensions.

An n-dimensional polynomial is the sum of nterms terms, where each term is the product of a coefficient (a float constant) and a monomial in the n variables. A monomial is the product of each of the variables raised to a specific exponent. For example, in a 2-dim space, a polynomial in (x,y) could be:

2 + 3 * x - x * y - y**2

This contains four terms. We store the monomials as an int array with shape (nterms, ndim) specifying for each term the exponents of each of the variables in the monomial. For the above example this becomes: [(0,0), (1,0), (1,1), 0,1)]. The nterms coefficients are stored as floats: [2.0, 3.0, -1.0, -1.0].

Parameters:
  • exp (array_like) – An int array of shape (nterms,ndim) with the exponents of each of the ndim variables in each of the nterms terms of the polynomial.

  • coeff (array_like) – A float array with the coefficients of the terms. If not specified, all coefficients are set to 1.

  • symbol (str, optional) – A string of length at least ndim with the symbols to be used for each of the ndim independent variables. The default is set for a 3-d polynomial.

Examples

>>> p = Polynomial([(0,0),(1,0),(1,1),(0,2)],(2,3,-1,-1))
>>> p.exp
array([[0, 0],
       [1, 0],
       [1, 1],
       [0, 2]])
>>> p.coeff
array([ 2.,  3., -1., -1.])
>>> print(p.atoms())
['1', 'x', 'x*y', 'y**2']
>>> print(p)
2.0 +3.0*x -1.0*x*y -1.0*y**2
>>> print(repr(p))
Polynomial([[0, 0], [1, 0], [1, 1], [0, 2]], [2.0, 3.0, -1.0, -1.0], 'xyz')
>>> print(Polynomial([(2,0), (1,1), (0,2)], (1,2,1), 'ab'))
a**2 +2.0*a*b +b**2
degrees()[source]

Return the degree of the polynomial in each of the dimensions.

The degree is the maximal exponent for each of the dimensions.

degree()[source]

Return the total degree of the polynomial.

The degree is the sum of the degrees for all dimensions.

atoms()[source]

Return a human representation of the monomials

Returns:

list of str – A list of the monomials in the Polynomial

Examples

>>> Polynomial([(0,0),(1,0),(1,1),(0,2)]).atoms()
['1', 'x', 'x*y', 'y**2']
human()[source]

Return a human representation

Returns:

str – A string representation of the Polynomial

Examples

>>> Polynomial([(0,0),(1,0),(1,1),(0,2)]).human()
'1 +x +x*y +y**2'
evalAtoms(x)[source]

Evaluate the monomials at the given points

Parameters:

x (array_like) – An (npoints,ndim) array of points where the polynomial is to be evaluated.

Returns:

array – The (npoints,nterms) array of values of the nterms monomials at the npoints points.

Examples

>>> p = Polynomial([(0,0),(1,0),(1,1),(0,2)],(2,3,-1,-1))
>>> p.evalAtoms([[1,2],[3,0],[2,1]])
array([[1., 1., 2., 4.],
       [1., 3., 0., 0.],
       [1., 2., 2., 1.]])
eval(x)[source]

Evaluate the polynomial at the given points

Parameters:

x (array_like) – An (npoints,ndim) array of points where the polynomial is to be evaluated.

Returns:

array – The value of the polynomial at the npoints points.

Examples

>>> p = Polynomial([(0,0),(1,0),(1,1),(0,2)],(2,3,-1,-1))
>>> print(p.eval([[1,2],[3,0],[2,1]]))
[-1. 11.  5.]

88.2. Functions defined in module plugins.polynomial

plugins.polynomial.factor(sym, exp)[source]

Return a string with a variable to some exponent

Parameters:
  • sum (str) – The name of the variable

  • exp (int) – The exponent to which the variable is raised

Returns:

str – The expression with the variable sym raised to the power exp.

Examples

>>> factor('x', 3), factor('y', 1), factor('z', 0)
('x**3', 'y', '1')
plugins.polynomial.monomial(exp, symbol='xyz')[source]

Return the monomial for the given exponents.

Parameters:
  • exp (tuple of int) – The integer exponents to which to raise the ndim independent variables.

  • symbol (str) – A string of at least the same length as exp, containing the variables to use for each of the ndim independent variables. The default is set for a 3-dimensional space.

Returns:

str – A string representation of a monomial created by raising the symbols to the corresponding exponent.

Examples

>>> monomial((2,1))
'x**2*y'
>>> monomial((0,3,2))
'y**3*z**2'