parametrizer

Struct Parametrizer

Source
pub struct Parametrizer<T: Number> { /* private fields */ }
Expand description

Main struct for parametrizing strings. Contains a pointer to the top-level term, which will contain pointers to lower leves for recursive evaluations

Implementations§

Source§

impl<T: Number> Parametrizer<T>

Source

pub fn new(param: &str) -> Result<Parametrizer<T>, ParametrizerError>

Default constructor. Formats the param string before parsing to handle uppercase letters, spaces, and the like, which may cause some performance slowdown. Already properly formatted strings can be parsed using Parametrizer::quick_new.Supports sine and cosine via “sin” and “cos”.

§Examples
use crate::parametrizer::Parametrizer;

let division = Parametrizer::new("4\\2").unwrap();
let subtraction = Parametrizer::new("15-3*t").unwrap();
let spaces = Parametrizer::new("6 + T").unwrap();
let sin = Parametrizer::new("sin(t*t + t - 1)").unwrap();

assert_eq!(2, division.evaluate(8));
assert_eq!(6, subtraction.evaluate(3));
assert_eq!(8, spaces.evaluate(2));
assert_eq!(11.0_f64.sin(), sin.evaluate(3.0));
use crate::parametrizer::Parametrizer;

let constant = Parametrizer::new("1.35").unwrap();

assert_eq!(1.35, constant.evaluate(2.0));
assert_eq!(1.35, constant.evaluate(3.4));
use crate::parametrizer::Parametrizer;

let variable = Parametrizer::new("t").unwrap();

assert_eq!(3.0, variable.evaluate(3.0));
assert_ne!(4.2, variable.evaluate(1.25));
use crate::parametrizer::Parametrizer;

let addition = Parametrizer::new("1+t").unwrap();

assert_eq!(9.0, addition.evaluate(8.0));
assert_eq!(1.16, addition.evaluate(0.16));
use crate::parametrizer::Parametrizer;

let equation = Parametrizer::new("13+((2*t)+5)").unwrap();

assert_eq!(20, equation.evaluate(1));
assert_eq!(30, equation.evaluate(6));
use crate::parametrizer::Parametrizer;

let division = Parametrizer::new("6/t").unwrap();

assert_eq!(2, division.evaluate(3));
assert_eq!(3, division.evaluate(2));
use crate::parametrizer::Parametrizer;

let equation = Parametrizer::new("13-t").unwrap();
let negation = Parametrizer::new("-t").unwrap();

assert_eq!(10, equation.evaluate(3));
assert_eq!(-9, negation.evaluate(9));
use crate::parametrizer::Parametrizer;

let dynamic_rand = Parametrizer::new("rd(2+t<4*t)").unwrap();
let computed_rand = Parametrizer::new("rc(4<8)").unwrap();

assert_eq!(computed_rand.evaluate(2), computed_rand.evaluate(4));
assert!(4 <= dynamic_rand.evaluate(2));
assert!(16 > dynamic_rand.evaluate(4));
Source

pub fn new_functions( param: &str, functions: Vec<ParametrizerFunction>, ) -> Result<Parametrizer<T>, ParametrizerError>

Constructor which allows for the user to define additional functions using a vector of ParametrizerFunction structs. Formats the param string like Parametrizer::new, with similar potential slowdown. Note that sine and cosine are not supported by default, but can be included in the user-defined list.

§Examples
use crate::parametrizer::Parametrizer;
use crate::parametrizer::ParametrizerFunction;

fn square(t: f64) -> f64
{

    return t * t;

}

let logarithm_and_square = Parametrizer::new_functions("Log( square(t) + 3 )", vec![

    ParametrizerFunction::new("LOG".to_string(), f64::ln),
    ParametrizerFunction::new("square".to_string(), square)

]).unwrap();

assert_eq!(7.0_f64.ln(), logarithm_and_square.evaluate(2.0));
assert_eq!(28.0_f64.ln(), logarithm_and_square.evaluate(5.0));
Source

pub fn quick_new( param: &str, functions: Vec<ParametrizerFunction>, ) -> Result<Parametrizer<T>, ParametrizerError>

Constructor which skips the added string formatting of Parametrizer::new and Parametrizer::new_functions, potentially speeding up parsing at the cost of unpredictable behavior when a string is not formatted exactly correctly. (I.e., includes extra spaces or capital letters.) Requires users to specify a vector of ParametrizerFunctions as in the case for Parametrizer::new_functions, and does not include sine or cosine by default.

§Examples
use crate::parametrizer::Parametrizer;
use crate::parametrizer::ParametrizerFunction;

let division = Parametrizer::quick_new("4/2", Vec::new()).unwrap();
let subtraction = Parametrizer::quick_new("15+(-3*t)", Vec::new()).unwrap();
let spaces = Parametrizer::quick_new("6+t", Vec::new()).unwrap();
let sin = Parametrizer::quick_new("sin(t*t+t+-1)", vec![ ParametrizerFunction::new("sin".to_string(), f64::sin) ]).unwrap();
let log = Parametrizer::quick_new("log(t+3)", vec![ ParametrizerFunction::new("log".to_string(), f64::ln)
]).unwrap();

assert_eq!(2, division.evaluate(8));
assert_eq!(6, subtraction.evaluate(3));
assert_eq!(8, spaces.evaluate(2));
assert_eq!(11.0_f64.sin(), sin.evaluate(3.0));
assert_eq!(8.0_f64.ln(), log.evaluate(5.0));
Source

pub fn evaluate(&self, t: T) -> T

Used to compute the parametric function at a specific point. As the parsing is done once at creation time, the only overhead is due to pointers and recursion.

Auto Trait Implementations§

§

impl<T> Freeze for Parametrizer<T>

§

impl<T> !RefUnwindSafe for Parametrizer<T>

§

impl<T> Send for Parametrizer<T>

§

impl<T> Sync for Parametrizer<T>

§

impl<T> Unpin for Parametrizer<T>

§

impl<T> !UnwindSafe for Parametrizer<T>

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V