interpolate::poly::polynomial
On this page
Power series c0 + c1 x + ... + cn x^n, ascending coefficients as in
numpy.polynomial.
Structs
PolynomialSource
pub struct PolynomialA polynomial with real coefficients, lowest power first. Trailing
zeros are dropped, so == compares polynomials, not spellings. It is
a Curve (at is Horner's rule) and takes the arithmetic operators:
+ - * with another polynomial or an f64 on either side, / and
% (long division), ** with an integer, ∘ (composition) and
unary -.
use interpolate::{Curve, Polynomial};
let p = Polynomial::new([1, -3, 2]); // 1 - 3x + 2x^2
let x = Polynomial::x();
assert(p == 2.0 * x ** 2 - 3.0 * x + 1.0);
assert(p.at(2.0) == 3.0 && f"{p}" == "1 - 3x + 2x^2");
assert((p ∘ (x + 1.0)).at(1.0) == p.at(2.0));
The polynomial with coefficients
coef, constant term first.nabiuse interpolate::Polynomial; assert(Polynomial::new([0, 0, 1]).degree() == 2); assert(Polynomial::new([5, 0, 0]).degree() == 0);The constant
v.nabiuse interpolate::{Curve, Polynomial}; assert(Polynomial::constant(4.0).at(9.0) == 4.0);The identity
x, for writing polynomials as expressions.nabiuse interpolate::Polynomial; let x = Polynomial::x(); assert((x * x - 1.0).coefficients()[0] == -1.0);The monic polynomial
(x - r0)(x - r1)...with the given roots.nabiuse interpolate::Polynomial; let p = Polynomial::from_roots([1, 2]); assert(p == Polynomial::new([2, -3, 1]));The degree; 0 for a constant, the zero polynomial included.
nabiuse interpolate::Polynomial; assert(Polynomial::new([1, 2, 3]).degree() == 2);The coefficients, constant term first.
nabiuse interpolate::Polynomial; assert(Polynomial::new([1, 2]).coefficients()[1] == 2.0);The coefficient of
x^j(0 past the degree).nabiuse interpolate::Polynomial; let p = Polynomial::new([1, 2]); assert(p.coef(1) == 2.0 && p.coef(7) == 0.0);The
m-th derivative.nabiuse interpolate::Polynomial; let p = Polynomial::new([1, 1, 1, 1]); assert(p.deriv(1) == Polynomial::new([1, 2, 3])); assert(p.deriv(4) == Polynomial::constant(0.0));The
m-th antiderivative; each integration adds the constantk(its value at 0).nabiuse interpolate::{Curve, Polynomial}; let p = Polynomial::new([0, 2]); // 2x assert(p.integ(1) == Polynomial::new([0, 0, 1])); assert(p.integ(1, 5.0).at(0.0) == 5.0);The integral from
atob.nabiuse interpolate::Polynomial; assert(Polynomial::new([0, 0, 3]).integrate(0.0..2.0) == 8.0);
Sourcepub fn roots(§self) throws (InterpError) -> Vector<Complex<f64>> Every root, complex ones in conjugate pairs: the eigenvalues of the companion matrix (Hessenberg QR, via
linalg::eig), each polished by Newton steps that are kept only when they shrink|p|. Simple roots come out to aboutn epsrelative to the coefficients; a root of multiplicity m to abouteps^(1/m). The zero polynomial and constants have no roots.nabiuse interpolate::Polynomial; let r = (try Polynomial::new([1, 0, 1]).roots()); // 1 + x^2 assert(r.len() == 2 && (r[0].im.abs() - 1.0).abs() < 1e-15 && r[0].re.abs() < 1e-15);
Sourcepub fn real_roots(§self) throws (InterpError) -> Vector<f64> The real roots, rising: the roots whose imaginary part is below
1e-8 (1 + |z|), a tolerance that keeps the real ones a companion eigensolver spreads off the axis (double roots).nabiuse interpolate::Polynomial; let r = try Polynomial::from_roots([3, -1, 2]).real_roots(); assert((r[0] + 1.0).abs() < 1e-14 && (r[2] - 3.0).abs() < 1e-14);
Sourcepub fn fit(§x: Vector<f64>, y: Vector<f64>, deg: usize) throws (InterpError) -> Polynomial The least-squares polynomial of degree
degthrough(x, y). The fit runs onxmapped to[-1, 1](a well-conditioned Vandermonde matrix, solved by pivoted QR inlinalg::lstsq) and the result is mapped back by composition, which costs some digits for high degrees on intervals far from the origin; useChebyshev::fitthere.nabiuse interpolate::Polynomial; let p = try Polynomial::fit([0, 1, 2, 3], [1, 3, 5, 7], 1); assert((p.coef(0) - 1.0).abs() < 1e-14 && (p.coef(1) - 2.0).abs() < 1e-14);
Sourcepub fn divmod(§self, d: Polynomial) throws (InterpError) -> (Polynomial, Polynomial) Quotient and remainder of long division:
self = q d + rwithdeg r < deg d. A zero divisor isBadArgument.nabiuse interpolate::Polynomial; let p = Polynomial::new([-1, 0, 1]); // x^2 - 1 let (q, r) = try p.divmod(Polynomial::new([-1, 1])); assert(q == Polynomial::new([1, 1]) && r == Polynomial::constant(0.0));Whether every coefficient is 0.
nabiuse interpolate::Polynomial; assert(Polynomial::new([0, 0]).is_zero());
Trait implementations 14
impl Curve for Polynomialimpl Display for Polynomialimpl ops::Add for Polynomialimpl ops::Add<f64> for Polynomialimpl ops::Sub for Polynomialimpl ops::Sub<f64> for Polynomialimpl ops::Mul for Polynomialimpl ops::Mul<f64> for Polynomialimpl ops::Div for Polynomialimpl ops::Div<f64> for Polynomialimpl ops::Rem for Polynomialimpl ops::Neg for Polynomialimpl ops::Pow<u32> for Polynomialimpl ops::Compose<Polynomial> for Polynomial