NumPy also provides methods to work with polynomials, and includes a package called numpy.polynomial
to create, manipulate, and fit the polynomials. A common application of this would be interpolation and extrapolation. In this section, our focus is still on using ndarray
with NumPy functions instead of using polynomial instances. (Don't worry, we will still show you the usage of the polynomial class.)
As we stated in the matrix class section, using ndarray
with NumPy functions is preferred since ndarray
can be accepted in any functions while matrix and polynomial objects need to be converted, especially when communicating to other programs. Both of them provided handy properties, but in most cases ndarray
would be good enough.
In this section, we will cover how to calculate the coefficients based on a set of roots, and how to solve a polynomial equation, and finally we will evaluate integrals and derivatives. Let's start by calculating the coefficients of a polynomial:
In [66]: root = np.array([1,2,3,4]) In [67]: np.poly(root) Out[67]: array([ 1, -10, 35, -50, 24])
numpy.poly()
returned a one-dimensional array of polynomial coefficients whose roots are the given array root
from the highest to lowest exponents. In this example, we take the root array [1,2,3,4]
and return the polynomial, which is equivalent to x4 - 10*3 + 35*2 - 50x + 24.
One thing we need be careful about is that the input roots array should be a one-dimensional or square two-dimensional array, or a ValueError
will be triggered. Of course, we can also perform the opposite operation: calculating the roots based on the coefficients using numpy.roots()
:
In [68]: np.roots([1,-10,35,-50,24]) Out[68]: array([ 4., 3., 2., 1.])
Now, let's say we have the equation y = x4 - 10*3 + 35*2 - 50x + 24, and we want to know the value of y when x = 5. We can use numpy.polyval()
to calculate this:
In [69]: np.polyval([1,-10,35,-50,24], 5) Out[69]: 24
numpy.polyval()
takes two input parameters, the first being the coefficient array of the polynomial, and the second one being the specific point value to evaluate the given polynomial. We can also input a sequence of x
, and the result will return an ndarray
whose values correspond to the given x
sequence.
Next we will talk about integrals and derivatives. We will continue the example of x4 - 10x3 + 35x2 - 50x + 24:
In [70]: coef = np.array([1,-10,35,-50,24]) In [71]: integral = np.polyint(coef) In [72]: integral Out[72]: array([ 0.2 , -2.5 , 11.6667, -25. , 24. , 0. ]) In [73]: np.polyder(integral) == coef Out[73]: array([ True, True, True, True, True], dtype=bool) In [74]: np.polyder(coef, 5) Out[74]: array([], dtype=int32)
In this example, we use numpy.polyint()
for the integral calculus and the result is equivalent to:
The default integration constant is 0, but we can specify it using the input parameter k
. You can do some exercises on this by yourself for the integral of a different k
.
Let's go back to the previous example-after doing the integration, we performed the differential calculus right away using numpy.polyder()
and we compared the derivatives to the original coef
array. We got five True
boolean arrays, which verified that both are identical.
We can also specify the order of differentiation (the default is 1) in numpy.polyder()
. As we expected, when we calculate the fifth-order derivative of a fourth-order polynomial, it returns an empty array.
Now we will repeat these examples using an instance of the polynomial class to see the differences in the usage. The very first step in using the numpy.polynomial
class is to initialize a polynomial instance. Let's start:
In [75]: from numpy.polynomial import polynomial In [76]: p = polynomial.Polynomial(coef) In [77]: p Out[77]: Polynomial([ 1., -10., 35., -50., 24.], [-1, 1], [-1, 1])
Note that beside the returned type of p
is an instance of the class Polynomial
, and there are three parts returned. The first part is the coefficient of the polynomial. The second one is domain
, which represents the input value interval in the polynomial (the default is [-1, 1]
). And the third one is window
, which maps the domain interval to the corresponding interval based on the polynomial (the default is also [-1, 1]
):
In [78]: p.coef Out[78]: array([ 1., -10., 35., -50., 24.]) In [79]: p.roots() Out[79]: array([ 0.25 , 0.3333, 0.5 , 1. ])
With the Polynomial
instance, we can simply call the coef
property to show the ndarray
of the coefficient. The roots()
method will show the roots. Next we will evaluate the polynomial of a specific value, 5
:
In [80]: polynomial.polyval(p, 5) Out[80]: Polynomial([ 5.], [-1., 1.], [-1., 1.])
The integration and derivation is also done with the built-in functions in the Polynomial
class as roots()
, but the function names change to integ()
and derive()
:
In [81]: p.integ() Out[81]: Polynomial([ 0. , 1. , -5. , 11.6667, -12.5 , 4.8 ], [-1., 1.], [-1., 1.]) In [82]: p.integ().deriv() == p Out[82]: True
The polynomial package also provides special polynomials such as Chebyshev, Legendre, and Hermite. For more details on these, please refer to http://docs.scipy.org/doc/numpy-1.10.0/reference/routines.polynomials.classes.html .
In summary, for most cases ndarray
and NumPy functions can solve problems related to polynomials. They are also a more preferred way since there is less conversion between types in the program, meaning fewer potential issues. However, when dealing with special polynomials, we still need the polynomial package. We are almost done with the math part. In the next section, we will talk about the application of linear algebra.
18.189.180.43