eval

Function eval 

Source
pub fn eval<Coeff, Value, Coeffs, Values>(
    coeffs: Coeffs,
    values: &Values,
    degree: Power,
) -> Result<Value, Error>
where Value: Zero + AddAssign + AddAssign<Coeff> + for<'v> MulAssign<&'v Value>, Coeffs: IntoIterator<Item = Coeff>, Values: SequenceRef<OwnedItem = Value> + ?Sized,
Expand description

Evaluates a polynomial for the given values.

The number of coefficients of the polynomial is determined by the length of the values (the number of variables) and the degree. The coefficients have to be iterable. Only the expected number of coefficients are taken from the iterator.

§Errors

If the iterator of coefficients has fewer elements than expected, an error is returned.

§Examples

The vector of coefficients for the polynomial $f(x) = x^2 - x + 2$ is [1, -1, 2]. Evaluating $f(0)$, $f(1)$ and $f(2)$:

use nutils_poly;

let coeffs = [1, -1, 2];
assert_eq!(nutils_poly::eval(&coeffs, &[0], 2), Ok(2)); // f(0) = 2
assert_eq!(nutils_poly::eval(&coeffs, &[1], 2), Ok(2)); // f(1) = 2
assert_eq!(nutils_poly::eval(&coeffs, &[2], 2), Ok(4)); // f(2) = 4

Let $g(x) = x^2 + x + 1$ be another polynomial. Since eval() consumes only the expected number of coefficients, we can chain the coefficients for $f$ and $g$ in a single iterator and call eval() twice to obtain the values for $f$ and $g$:

use nutils_poly;

let mut coeffs = [1, -1, 2, 1, 1, 1].into_iter();
// Evaluate `f`, consumes the first three coefficients.
assert_eq!(nutils_poly::eval(&mut coeffs, &[2], 2), Ok(4)); // f(2) = 4
// Evaluate `g`, consumes the last three coefficients.
assert_eq!(nutils_poly::eval(&mut coeffs, &[2], 2), Ok(7)); // g(2) = 7