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
Generate a Chebyshev series with given roots in Python
To generate a Chebyshev series with given roots, use the chebyshev.chebfromroots() method in Python NumPy. The method returns a 1-D array of coefficients. If all roots are real then out is a real array, if some of the roots are complex, then out is complex even if all the coefficients in the result are real. The parameter roots is the sequence containing the roots.
Syntax
numpy.polynomial.chebyshev.chebfromroots(roots)
Parameters
roots ? Sequence containing the roots.
Return Value
Returns 1-D array of Chebyshev series coefficients ordered from low to high degree.
Example
Let's generate a Chebyshev series with roots (-1, 0, 1) ?
import numpy as np
from numpy.polynomial import chebyshev as C
# Generate a Chebyshev series with given roots
roots = (-1, 0, 1)
coefficients = C.chebfromroots(roots)
print("Roots:", roots)
print("Result...\n", coefficients)
# Get the datatype
print("\nType...\n", coefficients.dtype)
# Get the shape
print("\nShape...\n", coefficients.shape)
Roots: (-1, 0, 1) Result... [ 0. -0.25 0. 0.25] Type... float64 Shape... (4,)
Example with Complex Roots
Let's see what happens when we include complex roots ?
import numpy as np
from numpy.polynomial import chebyshev as C
# Generate Chebyshev series with complex roots
complex_roots = (1, -1, 1j, -1j)
coefficients = C.chebfromroots(complex_roots)
print("Complex roots:", complex_roots)
print("Result...\n", coefficients)
print("Type...\n", coefficients.dtype)
Complex roots: (1, -1, 1j, -1j) Result... [ 0. +0.j 0. +0.j -0.5 +0.j 0. +0.j 0.25+0.j] Type... complex128
How It Works
The chebfromroots() function constructs a Chebyshev polynomial from its roots. If the polynomial has roots r?, r?, ..., r?, it creates the polynomial (x - r?)(x - r?)...(x - r?) expressed as a Chebyshev series. The coefficients are ordered from low to high degree.
Conclusion
The chebyshev.chebfromroots() method efficiently generates Chebyshev series coefficients from given roots. The output datatype depends on the input roots ? real roots produce real coefficients, while complex roots produce complex coefficients.
