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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
//! # Numerical Polynomial Operations
//!
//! This module provides a `Polynomial` struct and associated functions for numerical
//! operations on polynomials with `f64` coefficients. It supports evaluation,
//! differentiation, arithmetic (addition, subtraction, multiplication, division),
//! and finding real roots.
use std::ops::Add;
use std::ops::Div;
use std::ops::Mul;
use std::ops::Sub;
use serde::Deserialize;
use serde::Serialize;
use crate::numerical::real_roots;
/// Represents a polynomial with f64 coefficients for numerical operations.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Polynomial {
/// Coefficients of the polynomial, from highest degree to lowest.
pub coeffs: Vec<f64>,
}
impl Polynomial {
/// Creates a new `Polynomial` from coefficients (highest degree first).
#[must_use]
pub const fn new(coeffs: Vec<f64>) -> Self {
Self { coeffs }
}
/// Evaluates the polynomial at a given point `x` using Horner's method.
///
/// Horner's method is an efficient algorithm for evaluating polynomials.
/// For a polynomial `P(x) = c_n*x^n + c_{n-1}*x^{n-1} + ... + c_1*x + c_0`,
/// it computes `P(x) = (...((c_n*x + c_{n-1})*x + c_{n-2})*x + ... + c_1)*x + c_0`.
///
/// # Arguments
/// * `x` - The point at which to evaluate the polynomial.
///
/// # Returns
/// The value of the polynomial at `x` as an `f64`.
#[must_use]
pub fn eval(
&self,
x: f64,
) -> f64 {
self.coeffs.iter().fold(0.0, |acc, &c| acc.mul_add(x, c))
}
/// Finds the real roots of the polynomial.
///
/// This method combines Sturm's theorem for root isolation with Newton's method
/// for refining the roots. Sturm's theorem provides disjoint intervals, each
/// containing exactly one real root, which are then used as starting points
/// for Newton's method to converge to the root.
///
/// # Returns
/// A `Result` containing a `Vec<f64>` of the real roots found, or an error string
/// if root isolation or refinement fails.
///
/// # Errors
/// Returns an error if root isolation or refinement fails.
pub fn find_roots(&self) -> Result<Vec<f64>, String> {
let derivative = self.derivative();
let isolating_intervals = real_roots::isolate_real_roots(self, 1e-9)?;
let mut roots = Vec::new();
for (a, b) in isolating_intervals {
let mut guess = f64::midpoint(a, b);
for _ in 0..30 {
let f_val = self.eval(guess);
let f_prime_val = derivative.eval(guess);
if f_prime_val.abs() < 1e-12 {
break;
}
let next_guess = guess - f_val / f_prime_val;
if (next_guess - guess).abs() < 1e-12 {
guess = next_guess;
break;
}
guess = next_guess;
}
roots.push(guess);
}
Ok(roots)
}
/// Returns the derivative of the polynomial.
///
/// The derivative is computed by applying the power rule to each term.
/// For a term `c*x^n`, its derivative is `(c*n)*x^(n-1)`.
///
/// # Returns
/// A new `Polynomial` representing the derivative.
#[must_use]
pub fn derivative(&self) -> Self {
if self.coeffs.len() <= 1 {
return Self { coeffs: vec![0.0] };
}
let mut new_coeffs = Vec::with_capacity(self.coeffs.len() - 1);
let n = (self.coeffs.len() - 1) as f64;
for (i, &c) in self.coeffs.iter().enumerate().take(self.coeffs.len() - 1) {
new_coeffs.push(c * (n - i as f64));
}
Self { coeffs: new_coeffs }
}
/// Performs polynomial long division.
///
/// This method divides the current polynomial (dividend) by another polynomial (divisor).
///
/// # Arguments
/// * `divisor` - The polynomial to divide by.
///
/// # Returns
/// A tuple `(quotient, remainder)` as `Polynomial`s.
#[must_use]
pub fn long_division(
mut self,
divisor: &Self,
) -> (Self, Self) {
let mut quotient = vec![0.0; self.coeffs.len()];
let divisor_lead = divisor.coeffs[0];
while self.coeffs.len() >= divisor.coeffs.len() {
let lead_coeff = self.coeffs[0];
let q_coeff = lead_coeff / divisor_lead;
let deg_diff = self.coeffs.len() - divisor.coeffs.len();
quotient[deg_diff] = q_coeff;
for i in 0..divisor.coeffs.len() {
self.coeffs[i] -= divisor.coeffs[i] * q_coeff;
}
self.coeffs.remove(0);
}
(Self { coeffs: quotient }, self)
}
/// Returns the degree of the polynomial.
#[must_use]
pub const fn degree(&self) -> usize {
if self.coeffs.is_empty() {
return 0;
}
self.coeffs.len() - 1
}
/// Checks if the polynomial is zero (within epsilon).
#[must_use]
pub fn is_zero(
&self,
epsilon: f64,
) -> bool {
self.coeffs.iter().all(|&c| c.abs() < epsilon)
}
/// Returns the indefinite integral of the polynomial (constant of integration = 0).
#[must_use]
pub fn integral(&self) -> Self {
if self.coeffs.is_empty() {
return Self { coeffs: vec![0.0] };
}
let mut new_coeffs = Vec::with_capacity(self.coeffs.len() + 1);
let d = self.degree() as f64;
for (i, &c) in self.coeffs.iter().enumerate() {
new_coeffs.push(c / (d - i as f64 + 1.0));
}
new_coeffs.push(0.0);
Self { coeffs: new_coeffs }
}
}
impl Add for Polynomial {
type Output = Self;
fn add(
self,
rhs: Self,
) -> Self {
let max_len = self.coeffs.len().max(rhs.coeffs.len());
let mut new_coeffs = vec![0.0; max_len];
let self_pad = max_len - self.coeffs.len();
let rhs_pad = max_len - rhs.coeffs.len();
for (i, var) in new_coeffs.iter_mut().enumerate().take(max_len) {
let c1 = if i >= self_pad {
self.coeffs[i - self_pad]
} else {
0.0
};
let c2 = if i >= rhs_pad {
rhs.coeffs[i - rhs_pad]
} else {
0.0
};
*var = c1 + c2;
}
Self { coeffs: new_coeffs }
}
}
impl Sub for Polynomial {
type Output = Self;
fn sub(
self,
rhs: Self,
) -> Self {
let max_len = self.coeffs.len().max(rhs.coeffs.len());
let mut new_coeffs = vec![0.0; max_len];
let self_pad = max_len - self.coeffs.len();
let rhs_pad = max_len - rhs.coeffs.len();
for (i, var) in new_coeffs.iter_mut().enumerate().take(max_len) {
let c1 = if i >= self_pad {
self.coeffs[i - self_pad]
} else {
0.0
};
let c2 = if i >= rhs_pad {
rhs.coeffs[i - rhs_pad]
} else {
0.0
};
*var = c1 - c2;
}
Self { coeffs: new_coeffs }
}
}
impl Mul for Polynomial {
type Output = Self;
fn mul(
self,
rhs: Self,
) -> Self {
if self.coeffs.is_empty() || rhs.coeffs.is_empty() {
return Self { coeffs: vec![] };
}
let mut new_coeffs = vec![0.0; self.coeffs.len() + rhs.coeffs.len() - 1];
for (i, &c1) in self.coeffs.iter().enumerate() {
for (j, &c2) in rhs.coeffs.iter().enumerate() {
new_coeffs[i + j] += c1 * c2;
}
}
Self { coeffs: new_coeffs }
}
}
impl Div for Polynomial {
type Output = Self;
fn div(
self,
rhs: Self,
) -> Self {
self.long_division(&rhs).0
}
}
impl Mul<f64> for Polynomial {
type Output = Self;
fn mul(
self,
rhs: f64,
) -> Self {
let new_coeffs = self.coeffs.iter().map(|&c| c * rhs).collect();
Self { coeffs: new_coeffs }
}
}
impl Div<f64> for Polynomial {
type Output = Self;
fn div(
self,
rhs: f64,
) -> Self {
let new_coeffs = self.coeffs.iter().map(|&c| c / rhs).collect();
Self { coeffs: new_coeffs }
}
}
impl Polynomial {
/// Divides the polynomial by a scalar.
///
/// # Arguments
/// * `rhs` - The scalar to divide by.
///
/// # Returns
/// A `Result` containing the new `Polynomial`, or an error if the scalar is zero.
///
/// # Errors
/// Returns an error if the divisor scalar is zero.
pub fn div_scalar(
self,
rhs: f64,
) -> Result<Self, String> {
if rhs == 0.0 {
return Err("Division by \
zero scalar"
.to_string());
}
let new_coeffs = self.coeffs.iter().map(|&c| c / rhs).collect();
Ok(Self { coeffs: new_coeffs })
}
}