0
# Polynomial Operations
1
2
A sub-package for efficiently dealing with polynomials of different mathematical bases. NumPy provides convenience classes for six different polynomial types, each optimized for their respective mathematical foundations.
3
4
## Capabilities
5
6
### Convenience Classes
7
8
High-level polynomial classes providing consistent interfaces for creation, manipulation, and fitting across different polynomial bases.
9
10
```python { .api }
11
class Polynomial:
12
"""Power series polynomial class."""
13
def __init__(self, coef, domain=None, window=None, symbol='x'): ...
14
@classmethod
15
def fit(cls, x, y, deg, **kwargs): ...
16
def __call__(self, arg): ...
17
def deriv(self, m=1): ...
18
def integ(self, m=1, k=[], **kwargs): ...
19
def roots(self): ...
20
def degree(self): ...
21
22
class Chebyshev:
23
"""Chebyshev series polynomial class."""
24
def __init__(self, coef, domain=None, window=None, symbol='x'): ...
25
@classmethod
26
def fit(cls, x, y, deg, **kwargs): ...
27
def __call__(self, arg): ...
28
def deriv(self, m=1): ...
29
def integ(self, m=1, k=[], **kwargs): ...
30
def roots(self): ...
31
32
class Legendre:
33
"""Legendre series polynomial class."""
34
def __init__(self, coef, domain=None, window=None, symbol='x'): ...
35
@classmethod
36
def fit(cls, x, y, deg, **kwargs): ...
37
def __call__(self, arg): ...
38
def deriv(self, m=1): ...
39
def integ(self, m=1, k=[], **kwargs): ...
40
def roots(self): ...
41
42
class Laguerre:
43
"""Laguerre series polynomial class."""
44
def __init__(self, coef, domain=None, window=None, symbol='x'): ...
45
@classmethod
46
def fit(cls, x, y, deg, **kwargs): ...
47
def __call__(self, arg): ...
48
def deriv(self, m=1): ...
49
def integ(self, m=1, k=[], **kwargs): ...
50
def roots(self): ...
51
52
class Hermite:
53
"""Hermite series polynomial class."""
54
def __init__(self, coef, domain=None, window=None, symbol='x'): ...
55
@classmethod
56
def fit(cls, x, y, deg, **kwargs): ...
57
def __call__(self, arg): ...
58
def deriv(self, m=1): ...
59
def integ(self, m=1, k=[], **kwargs): ...
60
def roots(self): ...
61
62
class HermiteE:
63
"""HermiteE series polynomial class."""
64
def __init__(self, coef, domain=None, window=None, symbol='x'): ...
65
@classmethod
66
def fit(cls, x, y, deg, **kwargs): ...
67
def __call__(self, arg): ...
68
def deriv(self, m=1): ...
69
def integ(self, m=1, k=[], **kwargs): ...
70
def roots(self): ...
71
```
72
73
### Legacy Functions
74
75
Traditional polynomial functions for backward compatibility, primarily focused on power series.
76
77
```python { .api }
78
def poly(seq_of_zeros):
79
"""
80
Find the coefficients of a polynomial with the given sequence of roots.
81
82
Parameters:
83
- seq_of_zeros: array_like, sequence of polynomial zeros
84
85
Returns:
86
ndarray: Polynomial coefficients from highest to lowest degree
87
"""
88
89
def polyval(p, x):
90
"""
91
Evaluate a polynomial at specific values.
92
93
Parameters:
94
- p: array_like, polynomial coefficients (highest to lowest degree)
95
- x: array_like, values at which to evaluate the polynomial
96
97
Returns:
98
ndarray: Polynomial values at x
99
"""
100
101
def polyfit(x, y, deg, **kwargs):
102
"""
103
Least squares polynomial fit.
104
105
Parameters:
106
- x: array_like, x-coordinates of data points
107
- y: array_like, y-coordinates of data points
108
- deg: int, degree of fitting polynomial
109
110
Returns:
111
ndarray: Polynomial coefficients (highest to lowest degree)
112
"""
113
114
def polyder(p, m=1):
115
"""
116
Return the derivative of the specified order of a polynomial.
117
118
Parameters:
119
- p: array_like, polynomial coefficients
120
- m: int, order of derivative
121
122
Returns:
123
ndarray: Coefficients of derivative polynomial
124
"""
125
126
def polyint(p, m=1, k=None):
127
"""
128
Return an antiderivative (indefinite integral) of a polynomial.
129
130
Parameters:
131
- p: array_like, polynomial coefficients
132
- m: int, order of integration
133
- k: array_like, integration constants
134
135
Returns:
136
ndarray: Coefficients of integrated polynomial
137
"""
138
139
def roots(p):
140
"""
141
Return the roots of a polynomial with coefficients given in p.
142
143
Parameters:
144
- p: array_like, polynomial coefficients
145
146
Returns:
147
ndarray: Roots of the polynomial
148
"""
149
150
class poly1d:
151
"""
152
A one-dimensional polynomial class.
153
154
Note: This class is considered legacy. For new code, use
155
numpy.polynomial.Polynomial instead.
156
"""
157
def __init__(self, c_or_r, r=False, variable=None): ...
158
def __call__(self, val): ...
159
def deriv(self, m=1): ...
160
def integ(self, m=1, k=0): ...
161
@property
162
def roots(self): ...
163
@property
164
def coeffs(self): ...
165
```
166
167
## Usage Examples
168
169
### Modern Polynomial Classes
170
171
```python
172
import numpy as np
173
from numpy.polynomial import Polynomial, Chebyshev
174
175
# Create and work with power series polynomial
176
p = Polynomial([1, 2, 3]) # 1 + 2*x + 3*x^2
177
print(p(2)) # Evaluate at x=2
178
179
# Fit polynomial to data
180
x = np.linspace(0, 10, 11)
181
y = x**2 + 2*x + 1
182
fitted = Polynomial.fit(x, y, deg=2)
183
print(fitted.convert().coef) # Show coefficients
184
185
# Work with Chebyshev polynomials
186
cheb = Chebyshev.fit(x, y, deg=2)
187
print(cheb(x)) # Evaluate Chebyshev polynomial
188
```
189
190
### Legacy Polynomial Functions
191
192
```python
193
import numpy as np
194
195
# Traditional polynomial operations
196
coeffs = [3, 2, 1] # 3*x^2 + 2*x + 1
197
x_vals = [0, 1, 2, 3]
198
result = np.polyval(coeffs, x_vals)
199
200
# Fit polynomial to data (legacy)
201
x = np.array([0, 1, 2, 3])
202
y = np.array([1, 3, 7, 13])
203
coeffs = np.polyfit(x, y, deg=2)
204
205
# Find roots
206
roots = np.roots([1, -5, 6]) # roots of x^2 - 5x + 6
207
print(roots) # [2, 3]
208
```