Crate poly_it

source ·
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:

  1. Binary operations with slices.
  2. Using operations with owned or referenced polynomials.
  3. 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]));
}

Re-exports§

Modules§

  • This module provides storage solution abstractions. This is done via the StorageProvider and Storage traits. Conceptually the storage type represent the specific instance of storage in question, e.g. a Vec<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§