86. plugins.polynomial
— Polynomials¶
This module defines the class Polynomial, representing a polynomial in n variables.
86.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
86.2. Functions defined in module plugins.polynomial¶
-
plugins.polynomial.
factor
(sym, exp)[source]¶ Return a string with a variable to some exponent
- Parameters
- 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'