Polynomial

Struct Polynomial 

Source
pub struct Polynomial<const NUM_VARIABLES: usize, const NUM_TERMS: usize> { /* private fields */ }
Expand description

Represents a polynomial with a fixed number of variables and terms.

The polynomial is defined as a sum of NUM_TERMS terms, where each term operates on NUM_VARIABLES variables. Each term is an instance of Term<N>, encapsulating a coefficient and a set of variable functions.

§Type Parameters

  • NUM_VARIABLES: Number of variables in the polynomial.
  • NUM_TERMS: Number of terms in the polynomial.

§Example

use const_poly::VarFunction::*;
use const_poly::{const_poly, Polynomial};

// Create a polynomial, f(x,y) = 3 * sin(x) * cos(y)
const POLY: Polynomial<2, 1> = const_poly!({[3.0, [Sin, Cos]]});

// Evaluate at x = π/2, y = 0 => 3 * sin(π/2) * cos(0) = 3 * 1 * 1 = 3
let result = POLY.evaluate([1.57079632679, 0.0]);
assert!((result - 3.0).abs() < 1e-6);

Implementations§

Source§

impl<const NUM_VARIABLES: usize, const NUM_TERMS: usize> Polynomial<NUM_VARIABLES, NUM_TERMS>

Source

pub const fn new(terms: [Term<NUM_VARIABLES>; NUM_TERMS]) -> Self

Creates a new Polynomial from an array of terms.

§Parameters
  • terms: An array containing NUM_TERMS terms, each of which operates on NUM_VARIABLES variables.
§Returns

A new Polynomial instance encapsulating the given terms.

§Example
use const_poly::VarFunction::*;
use const_poly::{const_poly, Polynomial};

//define polynomial f(x,y) = x*y + 2*Sin(x)*Sin(y)
const POLY: Polynomial<2, 2> = const_poly!({[1.0, [Identity, Identity]],
                                            [2.0, [Sin,      Cos]]});
Source

pub const fn evaluate(&self, vars: [f64; NUM_VARIABLES]) -> f64

Evaluates the polynomial at the given variable values.

This computes the sum of the evaluations of all constituent terms.

§Parameters
  • vars: An array of NUM_VARIABLES floating-point values representing the variables.
§Returns

The floating-point result of evaluating the polynomial.

Auto Trait Implementations§

§

impl<const NUM_VARIABLES: usize, const NUM_TERMS: usize> Freeze for Polynomial<NUM_VARIABLES, NUM_TERMS>

§

impl<const NUM_VARIABLES: usize, const NUM_TERMS: usize> RefUnwindSafe for Polynomial<NUM_VARIABLES, NUM_TERMS>

§

impl<const NUM_VARIABLES: usize, const NUM_TERMS: usize> Send for Polynomial<NUM_VARIABLES, NUM_TERMS>

§

impl<const NUM_VARIABLES: usize, const NUM_TERMS: usize> Sync for Polynomial<NUM_VARIABLES, NUM_TERMS>

§

impl<const NUM_VARIABLES: usize, const NUM_TERMS: usize> Unpin for Polynomial<NUM_VARIABLES, NUM_TERMS>

§

impl<const NUM_VARIABLES: usize, const NUM_TERMS: usize> UnwindSafe for Polynomial<NUM_VARIABLES, NUM_TERMS>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.