1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//! Public `np::poly` wrappers over `Array<T>`.
//!
//! Coefficients are always highest power first and 1-d. Arithmetic is done in `f64`
//! internally (also for `f32` arrays).
//!
//! v0.1 limitation: rustnum has no complex dtype, so `roots` returns `(re, im)`.
use kernels;
use crate::;
/// Coefficients as `Vec<f64>`; panics unless the array is 1-d.
/// Build a 1-d `Array<F>` from `f64` data.
/// Evaluate a polynomial at `x`, like `np.polyval(p, x)`.
///
/// `p` holds the coefficients highest power first (`[1, 0, -1]` is `x^2 - 1`) and is evaluated
/// with Horner's rule. `x` may be a scalar or an array of any shape; the result has the shape of
/// `x`.
///
/// # Panics
/// If `p` is not 1-d.
///
/// # Examples
/// ```
/// use rustnum as np;
///
/// let p = np::array![3.0, 0.0, 1.0]; // 3x^2 + 1
/// assert_eq!(np::poly::polyval(&p, 2.0).item(), 13.0);
///
/// let q = np::array![1.0, 0.0, -1.0]; // x^2 - 1
/// let y = np::poly::polyval(&q, &np::array![1.0, 2.0, 3.0]);
/// assert_eq!(y.to_vec(), vec![0.0, 3.0, 8.0]);
/// ```
/// Least-squares polynomial fit of degree `deg`, like `np.polyfit(x, y, deg)`.
///
/// Returns the `deg + 1` coefficients, highest power first. `x` and `y` are 1-d and equally long.
/// The Vandermonde columns are scaled to unit norm and solved by column-pivoted Householder QR.
/// When the system is rank deficient (e.g. fewer points than `deg + 1`) NumPy warns and returns the
/// minimum-norm solution; this returns a basic solution (free coefficients zero) that fits equally
/// well. Weights (`w=`), `cov=` and 2-d `y` are not supported in v0.1.
///
/// # Panics
/// If `x` or `y` is not 1-d, `x` is empty, or the lengths differ (`expected x and y to have same length`).
///
/// # Examples
/// ```
/// use rustnum as np;
///
/// let x = np::array![0.0, 1.0, 2.0, 3.0];
/// let y = np::array![1.0, 3.0, 7.0, 13.0]; // 1 + x + x^2
/// let c = np::poly::polyfit(&x, &y, 2);
/// let c: Vec<f64> = c.to_vec();
/// for (got, want) in c.iter().zip([1.0, 1.0, 1.0]) {
/// assert!((got - want).abs() < 1e-10);
/// }
/// ```
/// Sum of two polynomials, like `np.polyadd(a, b)`.
///
/// The constant terms line up, so the shorter input is padded on the left. Leading zeros in the
/// result are kept, as in NumPy.
///
/// # Panics
/// If either input is not 1-d.
///
/// # Examples
/// ```
/// use rustnum as np;
///
/// let s = np::poly::polyadd(&np::array![1.0, 2.0, 3.0], &np::array![1.0, 1.0]);
/// assert_eq!(s.to_vec(), vec![1.0, 3.0, 4.0]);
/// ```
/// Difference of two polynomials, like `np.polysub(a, b)`.
///
/// # Panics
/// If either input is not 1-d.
///
/// # Examples
/// ```
/// use rustnum as np;
///
/// let d = np::poly::polysub(&np::array![1.0, 2.0, 3.0], &np::array![1.0, 1.0]);
/// assert_eq!(d.to_vec(), vec![1.0, 1.0, 2.0]);
/// ```
/// Product of two polynomials, like `np.polymul(a, b)` (a convolution of the coefficients).
///
/// # Panics
/// If either input is not 1-d or is empty.
///
/// # Examples
/// ```
/// use rustnum as np;
///
/// // (x + 1)(x - 1) = x^2 - 1
/// let p = np::poly::polymul(&np::array![1.0, 1.0], &np::array![1.0, -1.0]);
/// assert_eq!(p.to_vec(), vec![1.0, 0.0, -1.0]);
/// ```
/// Polynomial division `u / v`, like `np.polydiv(u, v)`; returns `(quotient, remainder)`.
///
/// Follows NumPy exactly, including dropping leading remainder coefficients that are
/// numerically zero (`|r[0]| <= 1e-8`).
///
/// # Panics
/// If either input is not 1-d or empty, or the leading coefficient of `v` is zero.
///
/// # Examples
/// ```
/// use rustnum as np;
///
/// // (x^2 - 3x + 2) / (x - 1) = x - 2, remainder 0
/// let (q, r) = np::poly::polydiv(&np::array![1.0, -3.0, 2.0], &np::array![1.0, -1.0]);
/// assert_eq!(q.to_vec(), vec![1.0, -2.0]);
/// assert_eq!(r.to_vec(), vec![0.0]);
/// ```
/// `m`-th derivative of a polynomial, like `np.polyder(p, m)`.
///
/// `m = 0` returns `p` unchanged. Differentiating a constant gives an empty array, as in NumPy.
///
/// # Panics
/// If `p` is not 1-d.
///
/// # Examples
/// ```
/// use rustnum as np;
///
/// let p = np::array![3.0, 2.0, 1.0]; // 3x^2 + 2x + 1
/// assert_eq!(np::poly::polyder(&p, 1).to_vec(), vec![6.0, 2.0]);
/// assert_eq!(np::poly::polyder(&p, 2).to_vec(), vec![6.0]);
/// ```
/// `m`-fold antiderivative of a polynomial, like `np.polyint(p, m, k)`.
///
/// `k` holds the integration constants, lowest integration first. An empty slice means all zeros,
/// a single value is used for every integration, and otherwise `k.len()` must be at least `m`
/// (extra values are ignored). The result has `m` more coefficients than `p`.
///
/// # Panics
/// If `p` is not 1-d, or `k` has more than one value but fewer than `m`
/// (`k must be a scalar or a rank-1 array of length 1 or >m.`).
///
/// # Examples
/// ```
/// use rustnum as np;
///
/// let p = np::array![3.0, 2.0, 1.0];
/// assert_eq!(np::poly::polyint(&p, 1, &[]).to_vec(), vec![1.0, 1.0, 1.0, 0.0]);
/// assert_eq!(np::poly::polyint(&p, 1, &[5.0]).to_vec(), vec![1.0, 1.0, 1.0, 5.0]);
/// ```
/// Roots of a polynomial, like `np.roots(p)`; returns `(re, im)` arrays of length `degree`.
///
/// Computed as the eigenvalues of the companion matrix (Francis double-shift QR with balancing,
/// with an Aberth-Ehrlich fallback). Leading zero coefficients are ignored and trailing zeros give
/// zero roots at the end. Complex-conjugate pairs appear with the positive imaginary part first;
/// the order otherwise follows NumPy/LAPACK in nearly all cases (not guaranteed in general).
///
/// **v0.1 limitation:** no complex dtype, so the roots are returned as two real arrays.
/// NumPy returns a real array when every root is real; check `im` for that.
///
/// # Panics
/// If `p` is not 1-d.
///
/// # Examples
/// ```
/// use rustnum as np;
///
/// // x^2 - 3x + 2 = (x - 2)(x - 1)
/// let (re, im) = np::poly::roots(&np::array![1.0, -3.0, 2.0]);
/// let re: Vec<f64> = re.to_vec();
/// assert!((re[0] - 2.0).abs() < 1e-12 && (re[1] - 1.0).abs() < 1e-12);
/// assert_eq!(im.to_vec(), vec![0.0, 0.0]);
///
/// // x^2 + 1 = 0 -> +i, -i
/// let (_, im) = np::poly::roots(&np::array![1.0, 0.0, 1.0]);
/// assert!(im.to_vec()[0] > 0.0);
/// ```