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
Selected Reading
Raise a polynomial to a power in Python
To raise a polynomial to a power in Python, use the numpy.polynomial.polynomial.polypow() method. This function returns the polynomial raised to the specified power, where coefficients are ordered from low to high degree.
Syntax
numpy.polynomial.polynomial.polypow(c, pow, maxpower=16)
Parameters
The function accepts the following parameters:
- c − A 1-D array of polynomial coefficients ordered from low to high degree (e.g., [1,2,3] represents 1 + 2*x + 3*x²)
- pow − The power to which the polynomial will be raised
- maxpower − Maximum power allowed to limit series growth (default is 16)
Example
Let's raise a polynomial (4 + x + 6x²) to the power of 3:
from numpy.polynomial import polynomial as P
# Define polynomial coefficients and power
poly = (4, 1, 6) # represents 4 + x + 6x²
power = 3
# Display the polynomial
print("Polynomial coefficients:", poly)
print("Power:", power)
# Raise polynomial to the power
result = P.polypow(poly, power)
print("Result coefficients:", result)
Polynomial coefficients: (4, 1, 6) Power: 3 Result coefficients: [ 64. 48. 300. 145. 450. 108. 216.]
Understanding the Result
The result array represents the coefficients of the expanded polynomial (4 + x + 6x²)³:
from numpy.polynomial import polynomial as P
# Original polynomial
poly = (4, 1, 6)
result = P.polypow(poly, 3)
print("Original polynomial: 4 + x + 6x²")
print("Raised to power 3:")
print("Result: 64 + 48x + 300x² + 145x³ + 450x? + 108x? + 216x?")
print("Coefficients:", result)
Original polynomial: 4 + x + 6x² Raised to power 3: Result: 64 + 48x + 300x² + 145x³ + 450x? + 108x? + 216x? Coefficients: [ 64. 48. 300. 145. 450. 108. 216.]
Using maxpower Parameter
The maxpower parameter prevents excessive growth of polynomial degree:
from numpy.polynomial import polynomial as P
poly = (1, 1, 1) # 1 + x + x²
high_power = 10
# With default maxpower (16)
result1 = P.polypow(poly, high_power)
print("Degree with default maxpower:", len(result1) - 1)
# With limited maxpower
result2 = P.polypow(poly, high_power, maxpower=8)
print("Degree with maxpower=8:", len(result2) - 1)
Degree with default maxpower: 20 Degree with maxpower=8: 8
Conclusion
The polypow() function efficiently raises polynomials to any power, returning coefficients in ascending degree order. Use the maxpower parameter to control computational complexity for high powers.
Advertisements
