In this article, we will cover how to differentiate a polynomial and set the derivatives in Python.
numpy.polynomial.polynomial.polyder method
The Numpy library provides the numpy.polynomial.polynomial.polyder() method to differentiate a polynomial and set the derivatives. The polynomial coefficients c differentiated m times along the axis. The result is multiplied by scl at each iteration (the scaling factor is used for a linear change of variable). The argument c is an array of coefficients ranging from low to high degree along each axis. For example, [5,6,7] represents the polynomial 5 + 6*x + 7*x**2, whereas [[5,6],[5,6]] represents 5 + 5*x + 6*y + 6*x*y if axis=0 is x and axis=1 is y.
Syntax: polynomial.polynomial.polyder(c, m=1, scl=1, axis=0)
Parameters:
- c: array like object.A collection of polynomial coefficients. I
- m: (optional) The number of derivatives taken must not be negative. Standard value is 1.
- scl: (optional) Each differentiation is multiplied by scl. The end result is multiplication by scl**m.. Standard value is 1.
- axis: (optional), int. The axis along which the derivative is computed. The default is 0. If it’s along the columns axis should be set to 1.
Returns: Polynomial coefficients of the derivative.
Example 1:
In this example, the NumPy package is imported and an array is created which represents the coefficients of a polynomial. numpy.polynomial.polynomial.polyder() is used to differentiate a polynomial and set the derivatives. The shape, datatype and dimension of the array are found by using the .shape, .dtype and .ndim attributes.
Python3
import numpy as np from numpy.polynomial import polynomial as P # Creating an array of polynomial coefficients c = np.array([ 5 , 6 , 7 , 8 ]) print (c) # shape of the array is print ( "Shape of the array is : " ,c.shape) # dimension of the array print ( "The dimension of the array is : " ,c.ndim) # Datatype of the array print ( "Datatype of our Array is : " ,c.dtype) # To differentiate a polynomial. print (P.polyder(c, 3 )) |
Output:
[5 6 7 8] Shape of the array is : (4,) The dimension of the array is : 1 Datatype of our Array is : int64 [48.]
Example 2:
In this example derivative of the polynomial is found along the columns axis as set to ‘1’.
Python3
# import packages import numpy as np from numpy.polynomial import polynomial as P # Creating an array of polynomial coefficients array = np.array([[ 5 , 6 , 7 ]]) print (array) # shape of the array is print ( "Shape of the array is : " ,array.shape) # dimension of the array print ( "The dimension of the array is : " ,array.ndim) # Datatype of the array print ( "Datatype of our Array is : " ,array.dtype) # differentiating the polynomial along columns print (P.polyder(array,m = 2 ,axis = 1 )) |
Output:
[[5 6 7]] Shape of the array is : (1, 3) The dimension of the array is : 2 Datatype of our Array is : int64 [[14.]]