pypol module has some functions to work with polynomials. If you need other utility functions check the pypol.funcs and pypol.roots modules.
Make a 1 dimension polynomial from a list of coefficients.
Parameters: |
|
---|
Examples
We create the polynomials ,
and
.
>>> poly1d([3, -2, 4, -2])
+ 3x^3 - 2x^2 + 4x - 2
>>> poly1d([2, 0, 0, -2])
+ 2x^3 - 2
pay attention here:
>>> poly1d([3], right_hand_side=False)
+ 3x
because if you don’t do this:
>>> poly1d([3])
+ 3
3 will be interpreted as the right_hand_side of the polynomial.
An alternative solution may be:
>>> poly1d([3, 0])
+ 3x
Make a 1 dimension polynomial from a list of lists.
Parameters: |
|
---|---|
Return type: |
Examples
We want to create these two polynomials: and
:
>>> poly1d_2([[-1, 7], [2, 3], [-2, 2], [1, 1]])
- x^7 + 2x^3 - 2x^2 + x
>>> poly1d_2([[1, 1]])
+ x
This function is very useful when you need a polynomial with negative powers or with spread powers:
>>> poly1d_2([[1, -1], [2, -3], [3, 5]])
+ 3x^5 + x^-1 + 2x^-3
or:
>>> poly1d_2([[2, 9], [1, 2]])
+ 2x^9 + x^2
in this case, if you want to use poly1d() or polynomial() you can do this:
>>> poly1d([2, 0, 0, 0, 0, 0, 0, 1, 0, 0])
+ 2x^9 + x^2
>>> poly1d([2, 0, 0, 0, 0, 0, 0, 1, 0], right_hand_side=False)
+ 2x^9 + x^2
>>> polynomial('2x^9 + x^2')
+ 2x^9 + x^2
>>> polynomial('2x9 x2')
+ 2x^9 + x^2
Returns a Polynomial object.
Parameters: |
|
---|---|
Return type: |
Warning
With this function you cannot make polynomials with negative powers. In case you want to use negative powers, use poly1d_2() instead.
Examples
We want to make the polynomial :
>>> polynomial('2x^-1 + 2')
+ 2x + 1 ## Wrong!
>>> k = poly1d_2([[2, -1], [2, 0]])
>>> k
+ 2x^-1 + 2
>>> k.sort(key=k._key('x'))
>>> k
+ 2x^-1 + 2
Powers can be expressed using the ^ symbol. If a digit follows a letter then it is interpreted as an exponent. So the following expressions are equal:
>>> polynomial('2x^3y^2 + 1') == polynomial('2x3y2 + 1')
True
but if there is a white space after the letter then the digit is interpreted as a positive coefficient. So this:
>>> polynomial('2x3y 2 + 1')
represents this polynomial:
2x^3y + 3
>>> polynomial('2x3y 2 + 1')
+ 2x^3y + 3
Wrapper function that returns an AlgebraicFraction object.
Parameters s1, s2: | |
---|---|
two strings that represent a polynomial |
>>> algebraic_fraction('3x^2 - 4xy', 'x + y')
AlgebraicFraction(+ 3x² - 4xy, + x + y)
>>> algebraic_fraction('3x^2 - 4xy', 'x + y').terms
(+ 3x^2 - 4xy, + x + y)
See also
Simple function that returns a Polynomial object.
Parameters: | coeff – the coefficient of the polynomial |
---|---|
Key **vars: | the monomial’s letters |
>>> monomial(5, a=3, b=4)
+ 5a^3b^4
>>> m = monomial(5, a=3, b=4)
>>> m
+ 5a^3b^4
>>> type(m)
<class 'pypol.src.pypol.Polynomial'>
>>> m.monomials
((5, {'a': 3, 'b': 4}),)
This function is useful when you need a monomial. Without it you should do:
>>> Polynomial(((5, {'a': 3, 'b': 4}),))
+ 5a^3b^4
Either coeff or **vars is optional:
>>> monomial()
+ 1
>>> monomial(1)
+ 1
Equivalent to:
def monomial(coeff=1, **vars):
return Polynomial(((coeff, vars),))
New in version 0.2.
New in version 0.4: The *var parameter
Parses a string that represent a polynomial. It can parse integer coefficients, float coefficient and fractional coefficient.
Parameters: |
|
---|
An example:
>>> parse_polynomial('2x^3 - 3y + 2')
[(2, {'x': 3}), (-3, {'y': 1}), (2, {})]
>>> parse_polynomial('x3 - 3y2 + 2')
[(1, {'x': 3}), (-3, {'y': 2}), (2, {})]
See also
Returns the Greatest Common Divisor between two polynomials:
>>> gcd(polynomial('3x'), polynomial('6x^2'))
+ 3x
Returns the Least Common Multiple between two polynomials:
>>> lcm(polynomial('3x'), polynomial('6x^2'))
+ 6x^2
See also
pypol.funcs for other utility functions.