Expr

Enum Expr 

Source
pub enum Expr {
    Tree {
        op: String,
        args: Vec<Expr>,
    },
    Const {
        val: f64,
    },
    Var {
        name: String,
    },
}
Expand description

The elements of the top-level expression trees

Expr is used to create variables and expressions to pass to Symjit to compile.

The Python/Sympy interface generates a JSON string, encoding the model, and pass it to the Rust code to deserialize. The Rust interface (Compiler) directly uses various functions to compose the trees.

§Examples

let x = Expr::var("x");     # create a new variable
let c = Expr::from(2.5);    # create a new constant (f64)
let expr = &x * &(x.sin() + &c)
...

Note that the overloaded operators expect &Expr; therefore, the need for taking reference (adding & in from the intermediate expressions).

Variants§

§

Tree

Fields

§args: Vec<Expr>
§

Const

Fields

§val: f64
§

Var

Fields

§name: String

Implementations§

Source§

impl Expr

Source

pub fn var(name: &str) -> Expr

Creates a new variable named name.

Source

pub fn unary(op: &str, arg: &Expr) -> Expr

Create a unary operation: op(arg).

To create a user-defined unary function, you need to register the function with Compiler using def_unary.

Source

pub fn binary(op: &str, l: &Expr, r: &Expr) -> Expr

Creates a binary operations op(l, r).

To create a user-defined binary function, you need to register the function with Compiler using def_binary.

Source

pub fn ternary(op: &str, l: &Expr, c: &Expr, r: &Expr) -> Expr

Creates a ternary operation: op(l, c, r).

Source

pub fn nary(op: &str, args: &[&Expr]) -> Expr

Creates an n-ary operation: op(args...).

Source

pub fn equation(lhs: &Expr, rhs: &Expr) -> Equation

Creates an equation lhs ~ rhs.

Source

pub fn to_variable(&self) -> Result<Variable>

Converts a variable Expr to a Variable type needed by the next stage.

Source

pub fn add(&self, other: &Expr) -> Expr

Source

pub fn sub(&self, other: &Self) -> Expr

Source

pub fn mul(&self, other: &Self) -> Expr

Source

pub fn div(&self, other: &Self) -> Expr

Source

pub fn rem(&self, other: &Self) -> Expr

Source

pub fn bitand(&self, other: &Self) -> Expr

Source

pub fn bitor(&self, other: &Self) -> Expr

Source

pub fn bitxor(&self, other: &Self) -> Expr

Source

pub fn eq(&self, other: &Expr) -> Expr

Comparison ==

Source

pub fn ne(&self, other: &Expr) -> Expr

Comparison !=

Source

pub fn lt(&self, other: &Expr) -> Expr

Comparison <

Source

pub fn le(&self, other: &Expr) -> Expr

Comparison <=

Source

pub fn gt(&self, other: &Expr) -> Expr

Comparison >

Source

pub fn ge(&self, other: &Expr) -> Expr

Comparison >=

Source

pub fn min(&self, other: &Expr) -> Expr

Source

pub fn max(&self, other: &Expr) -> Expr

Source

pub fn pow(&self, other: &Expr) -> Expr

Source

pub fn ifelse(&self, true_val: &Expr, false_val: &Expr) -> Expr

Ternary select operations: if self { true_val} else {false_valve} Note that this is not a short-circuited operation.

Source

pub fn sum(&self, var: &Expr, start: &Expr, end: &Expr) -> Expr

Sums self for var in start..=end.

§Examples
let x = Expr::var("x");
let i = Expr::var("i");
let p = i.sum(&i, &Expr::from(1), &x);
let mut comp = Compiler::new();
let mut func = comp.compile(&[x], &[p])?;
println!("{}", func.call([5]))  // prints [15.0]

Note that the range is start to end inclusive to remain consistent with SymPy usage.

§Warning

var should be a unique variable over the whole model.

Source

pub fn prod(&self, var: &Expr, start: &Expr, end: &Expr) -> Expr

Calculates the product of self for var in start..=end.

§Examples
let x = Expr::var("x");
let i = Expr::var("i");
let p = i.prod(&i, &Expr::from(1), &x); // this is the factorial function
let mut comp = Compiler::new();
let mut func = comp.compile(&[x], &[p])?;
println!("{}", func.call([5]))  // prints [120.0]

Note that the range is start to end inclusive to remain consistent with SymPy usage.

var should be a unique variable over the whole model.

Source

pub fn not(&self) -> Expr

Source

pub fn neg(&self) -> Expr

Source

pub fn square(&self) -> Expr

self^2

Source

pub fn cube(&self) -> Expr

self^3

Source

pub fn recip(&self) -> Expr

1 / self

Source

pub fn round(&self) -> Expr

Source

pub fn trunc(&self) -> Expr

Source

pub fn floor(&self) -> Expr

Source

pub fn ceil(&self) -> Expr

Source

pub fn fract(&self) -> Expr

The fractional part of a number: self - floor(self)

Source

pub fn heaviside(&self) -> Expr

Heaviside functions. It returns 1 if self is >=0; otherwise returns 0.

It can be used for conditional operations.

Source

pub fn sqrt(&self) -> Expr

Source

pub fn abs(&self) -> Expr

Source

pub fn sin(&self) -> Expr

Source

pub fn cos(&self) -> Expr

Source

pub fn tan(&self) -> Expr

Source

pub fn csc(&self) -> Expr

Source

pub fn sec(&self) -> Expr

Source

pub fn cot(&self) -> Expr

Source

pub fn sinh(&self) -> Expr

Source

pub fn cosh(&self) -> Expr

Source

pub fn tanh(&self) -> Expr

Source

pub fn csch(&self) -> Expr

Source

pub fn sech(&self) -> Expr

Source

pub fn coth(&self) -> Expr

Source

pub fn asin(&self) -> Expr

Source

pub fn acos(&self) -> Expr

Source

pub fn atan(&self) -> Expr

Source

pub fn asinh(&self) -> Expr

Source

pub fn acosh(&self) -> Expr

Source

pub fn atanh(&self) -> Expr

Source

pub fn sinc(&self) -> Expr

Source

pub fn cbrt(&self) -> Expr

Source

pub fn exp(&self) -> Expr

Source

pub fn ln(&self) -> Expr

Source

pub fn log10(&self) -> Expr

Source

pub fn exp_m1(&self) -> Expr

Source

pub fn ln_1p(&self) -> Expr

Source

pub fn log2(&self) -> Expr

Source

pub fn exp2(&self) -> Expr

Source

pub fn erf(&self) -> Expr

Source

pub fn erfc(&self) -> Expr

Source

pub fn gamma(&self) -> Expr

Source

pub fn lgam(&self) -> Expr

Log gamma

Source

pub fn si(&self) -> Expr

Source

pub fn ci(&self) -> Expr

Source

pub fn shi(&self) -> Expr

Source

pub fn chi(&self) -> Expr

Source§

impl Expr

Source

pub fn diff_var(&self) -> Option<String>

Extracts the differentiated variable from the lhs of a diff eq

Source

pub fn normal_var(&self) -> Option<String>

Extracts the regular variable from the lhs of an observable eq

Trait Implementations§

Source§

impl Add for &Expr

Source§

type Output = Expr

The resulting type after applying the + operator.
Source§

fn add(self, other: Self) -> Expr

Performs the + operation. Read more
Source§

impl BitAnd for &Expr

Source§

type Output = Expr

The resulting type after applying the & operator.
Source§

fn bitand(self, other: Self) -> Expr

Performs the & operation. Read more
Source§

impl BitOr for &Expr

Source§

type Output = Expr

The resulting type after applying the | operator.
Source§

fn bitor(self, other: Self) -> Expr

Performs the | operation. Read more
Source§

impl BitXor for &Expr

Source§

type Output = Expr

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, other: Self) -> Expr

Performs the ^ operation. Read more
Source§

impl Clone for Expr

Source§

fn clone(&self) -> Expr

Returns a duplicate of the value. Read more
1.0.0§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Expr

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Expr

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Div for &Expr

Source§

type Output = Expr

The resulting type after applying the / operator.
Source§

fn div(self, other: Self) -> Expr

Performs the / operation. Read more
Source§

impl From<f32> for Expr

Source§

fn from(val: f32) -> Expr

Converts to this type from the input type.
Source§

impl From<f64> for Expr

Source§

fn from(val: f64) -> Expr

Converts to this type from the input type.
Source§

impl From<i32> for Expr

Source§

fn from(val: i32) -> Expr

Converts to this type from the input type.
Source§

impl Mul for &Expr

Source§

type Output = Expr

The resulting type after applying the * operator.
Source§

fn mul(self, other: Self) -> Expr

Performs the * operation. Read more
Source§

impl Neg for &Expr

Source§

type Output = Expr

The resulting type after applying the - operator.
Source§

fn neg(self) -> Expr

Performs the unary - operation. Read more
Source§

impl Not for &Expr

Source§

type Output = Expr

The resulting type after applying the ! operator.
Source§

fn not(self) -> Expr

Performs the unary ! operation. Read more
Source§

impl Rem for &Expr

Source§

type Output = Expr

The resulting type after applying the % operator.
Source§

fn rem(self, other: Self) -> Expr

Performs the % operation. Read more
Source§

impl Sub for &Expr

Source§

type Output = Expr

The resulting type after applying the - operator.
Source§

fn sub(self, other: Self) -> Expr

Performs the - operation. Read more

Auto Trait Implementations§

§

impl Freeze for Expr

§

impl RefUnwindSafe for Expr

§

impl Send for Expr

§

impl Sync for Expr

§

impl Unpin for Expr

§

impl UnwindSafe for Expr

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

impl<T> CloneToUninit for T
where T: Clone,

§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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

§

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

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

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

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,