#polynomial #arithmetic #vector #coefficients #perform #evaluation

polynomials

Tools to perform polynomial arithmetic and evaluation

7 releases

0.2.4 Oct 7, 2020
0.2.3 Jul 30, 2020
0.1.1 Jul 19, 2020

#1419 in Math

Download history 11/week @ 2023-12-09 14/week @ 2023-12-16 13/week @ 2023-12-23 1/week @ 2023-12-30 13/week @ 2024-01-06 18/week @ 2024-01-13 6/week @ 2024-01-20 1/week @ 2024-01-27 11/week @ 2024-02-03 16/week @ 2024-02-10 20/week @ 2024-02-17 27/week @ 2024-02-24 28/week @ 2024-03-02 33/week @ 2024-03-09 31/week @ 2024-03-16 28/week @ 2024-03-23

129 downloads per month
Used in 8 crates (4 directly)

Custom license

17KB
384 lines

Polynomials

A minimal implementation of polynomial arithmetic, that does not rely on the standard library. (i.e. can be built with no_std)

Documentation

Detailed documentation can be found here.

Usage

A Polynomial is a vector of coefficients. To create a new polynomial:

let mut p = Polynomial::new();

You can add coefficients to this polynomial, just like adding elements to a vector:

// 3x^2 + 2x + 1
p.push(1);
p.push(2);
p.push(3);

Or you can instantiate a vector with the poly! macro, similar to a vector:

let p = poly![1, 2, 3];

You can multiply or divide a polynomial by a constant:

let new = p * 2;
assert_eq!(new, poly![2, 4, 6]);
assert_eq!(new / 2, poly![1, 2, 3]);

You can multiply a polynomial by another polynomial:

// (x + 1)(x - 1) = x^2 - 1
let a = poly![1, 1]; // x + 1
let b = poly![1, -1]; // x - 1
assert_eq!(a * b, poly![-1, 0, 1]);

And of course, you can evaluate a polynomial at some value:

let p = poly![1, 1]; // x + 1
assert_eq!(p.eval(7).unwrap(), 8); // 1*7 + 1

Dependencies

~0.5–1MB
~24K SLoC