Expand description
§poly_it
A no-std library for manipulating polynomials with slice support and minimal allocation (or no allocation).
At the end of the day the classical representation method for the polynomial is its coefficients and this library leverages this by means of slices. Take this example case:
extern crate poly_it;
use poly_it::Polynomial;
pub fn main() {
let p1 = Polynomial::new(vec![1, 2, 3]);
let p2 = Polynomial::new(vec![3, 2, 1]);
let arr = [1, 2];
let vec = vec![1, 2, 3];
assert_eq!(
format!("{}", p2 - &p1),
"2 + -2x^2"
);
assert_eq!(
format!("{}", p1.clone() + arr.as_slice()),
"2 + 4x + 3x^2"
);
assert_eq!(
format!("{}", p1 * vec.as_slice()),
"1 + 4x + 10x^2 + 12x^3 + 9x^4"
);
}
This example illustrates several things:
- Binary operations with slices.
- Using operations with owned or referenced polynomials.
- That
+ -
is possibly heretical.
As to the third point, this has to do with future proofing in cases where the underlying numerical type might not have ordering, but for the second point, see below for a quick summary as to the how and the why.
§Minimal Allocation
This crate attempts to minimize allocations by reusing the underlying allocated space of the polynomials when an owned Polynomial is passed to a unary or binary operation. If more than one owned polynomial is passed, as would be the case with:
let x = Polynomial::new(vec![1, 2, 3]) * Polynomial::new(vec![3, 2, 1]);
the polynomial whose data vector has the highest capacity will be selected. If a new allocation is desired, use references.
§Stack Storage
By default this crate has the alloc
feature enabled and sets the default storage mechanism for Polynomial
to Vec
. It is however possible to have array backing for a polynomial with the tinyvec
feature:
extern crate poly_it;
use poly_it::{Polynomial, storage::tinyvec::ArrayVec};
pub fn main() {
let p1 = Polynomial::new(ArrayVec::from([1, 2, 3]));
}
§Polynomial Division
Polynomial division is defined similar to polynomial long division, in the sense that for two polynomials $A$ and $B$, $A / B$ produces a quotient $Q$ and a remainder $R$ such that: $$ A \approx BQ + R $$ up to the precision of the underlying numeric type (integer types can be seen as infinitely precise as their results conform to their mathematical closures i.e. 5 + 6 is exactly 11 and no precision loss occurs).
A notable consequence of this is that $R$ may be of the same degree as $A$, even if $B$ has a degree greater than 0. As an example case, consider the division $(-10x + 1) / (7x + 2)$ for the 32 bit integer type. This will produce $Q = -1$ and $R = -3x + 3$.
Re-exports§
pub use num_traits;
Modules§
- storage
- This module provides storage solution abstractions. This is done via the
StorageProvider
andStorage
traits. Conceptually the storage type represent the specific instance of storage in question, e.g. aVec<f64>
, whereas the storage provider represents the underlying container or mechanism, e.g.Vec
and provides a means to create different instances using that container or mechanism type.
Structs§
- Polynomial
- A polynomial.