Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Differentiate a polynomial in Python
To differentiate a polynomial, use the polynomial.polyder() method in Python NumPy. This method returns the polynomial coefficients differentiated m times along a specified axis. The coefficients are ordered from low to high degree, so [1, 2, 3] represents 1 + 2*x + 3*x².
Syntax
numpy.polynomial.polynomial.polyder(c, m=1, scl=1, axis=0)
Parameters
The method accepts the following parameters:
- c − Array of polynomial coefficients from low to high degree
- m − Number of derivatives to take (default: 1, must be non-negative)
- scl − Scaling factor applied at each differentiation (default: 1)
- axis − Axis over which the derivative is taken (default: 0)
Basic Example
Let's differentiate the polynomial 1 + 2x + 3x² + 4x³ ?
import numpy as np
from numpy.polynomial import polynomial as P
# Coefficients for 1 + 2x + 3x² + 4x³
c = np.array([1, 2, 3, 4])
print("Original coefficients:", c)
print("Polynomial: 1 + 2x + 3x² + 4x³")
# First derivative
result = P.polyder(c)
print("First derivative coefficients:", result)
print("Derivative: 2 + 6x + 12x²")
Original coefficients: [1 2 3 4] Polynomial: 1 + 2x + 3x² + 4x³ First derivative coefficients: [ 2. 6. 12.] Derivative: 2 + 6x + 12x²
Multiple Derivatives
You can compute higher-order derivatives by specifying the m parameter ?
import numpy as np
from numpy.polynomial import polynomial as P
c = np.array([1, 2, 3, 4]) # 1 + 2x + 3x² + 4x³
# First derivative
first = P.polyder(c, m=1)
print("First derivative:", first)
# Second derivative
second = P.polyder(c, m=2)
print("Second derivative:", second)
# Third derivative
third = P.polyder(c, m=3)
print("Third derivative:", third)
First derivative: [ 2. 6. 12.] Second derivative: [ 6. 24.] Third derivative: [24.]
Using Scaling Factor
The scaling factor is useful for linear changes of variables ?
import numpy as np
from numpy.polynomial import polynomial as P
c = np.array([1, 2, 3]) # 1 + 2x + 3x²
# Normal derivative
normal = P.polyder(c)
print("Normal derivative:", normal)
# With scaling factor of 2
scaled = P.polyder(c, scl=2)
print("Scaled derivative (scl=2):", scaled)
Normal derivative: [2. 6.] Scaled derivative (scl=2): [ 4. 12.]
How It Works
For a polynomial with coefficients [a?, a?, a?, ..., a?] representing a? + a?x + a?x² + ... + a?x?, the derivative has coefficients [a?, 2a?, 3a?, ..., na?] representing a? + 2a?x + 3a?x² + ... + na?x^(n-1).
Conclusion
Use numpy.polynomial.polynomial.polyder() to differentiate polynomials efficiently. The method supports multiple derivatives, scaling factors, and works with multi-dimensional coefficient arrays for complex polynomial operations.
