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§
Implementations§
Source§impl Expr
impl Expr
Sourcepub fn unary(op: &str, arg: &Expr) -> Expr
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.
Sourcepub fn binary(op: &str, l: &Expr, r: &Expr) -> Expr
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.
Sourcepub fn ternary(op: &str, l: &Expr, c: &Expr, r: &Expr) -> Expr
pub fn ternary(op: &str, l: &Expr, c: &Expr, r: &Expr) -> Expr
Creates a ternary operation: op(l, c, r).
Sourcepub fn to_variable(&self) -> Result<Variable>
pub fn to_variable(&self) -> Result<Variable>
Converts a variable Expr to a Variable type needed by the next stage.
pub fn add(&self, other: &Expr) -> Expr
pub fn sub(&self, other: &Self) -> Expr
pub fn mul(&self, other: &Self) -> Expr
pub fn div(&self, other: &Self) -> Expr
pub fn rem(&self, other: &Self) -> Expr
pub fn bitand(&self, other: &Self) -> Expr
pub fn bitor(&self, other: &Self) -> Expr
pub fn bitxor(&self, other: &Self) -> Expr
pub fn min(&self, other: &Expr) -> Expr
pub fn max(&self, other: &Expr) -> Expr
pub fn pow(&self, other: &Expr) -> Expr
Sourcepub fn ifelse(&self, true_val: &Expr, false_val: &Expr) -> Expr
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.
Sourcepub fn sum(&self, var: &Expr, start: &Expr, end: &Expr) -> Expr
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.
Sourcepub fn prod(&self, var: &Expr, start: &Expr, end: &Expr) -> Expr
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.
pub fn not(&self) -> Expr
pub fn neg(&self) -> Expr
pub fn round(&self) -> Expr
pub fn trunc(&self) -> Expr
pub fn floor(&self) -> Expr
pub fn ceil(&self) -> Expr
Sourcepub fn heaviside(&self) -> Expr
pub fn heaviside(&self) -> Expr
Heaviside functions. It returns 1 if self is >=0; otherwise returns 0.
It can be used for conditional operations.
pub fn sqrt(&self) -> Expr
pub fn abs(&self) -> Expr
pub fn sin(&self) -> Expr
pub fn cos(&self) -> Expr
pub fn tan(&self) -> Expr
pub fn csc(&self) -> Expr
pub fn sec(&self) -> Expr
pub fn cot(&self) -> Expr
pub fn sinh(&self) -> Expr
pub fn cosh(&self) -> Expr
pub fn tanh(&self) -> Expr
pub fn csch(&self) -> Expr
pub fn sech(&self) -> Expr
pub fn coth(&self) -> Expr
pub fn asin(&self) -> Expr
pub fn acos(&self) -> Expr
pub fn atan(&self) -> Expr
pub fn asinh(&self) -> Expr
pub fn acosh(&self) -> Expr
pub fn atanh(&self) -> Expr
pub fn sinc(&self) -> Expr
pub fn cbrt(&self) -> Expr
pub fn exp(&self) -> Expr
pub fn ln(&self) -> Expr
pub fn log10(&self) -> Expr
pub fn exp_m1(&self) -> Expr
pub fn ln_1p(&self) -> Expr
pub fn log2(&self) -> Expr
pub fn exp2(&self) -> Expr
pub fn erf(&self) -> Expr
pub fn erfc(&self) -> Expr
pub fn gamma(&self) -> Expr
pub fn si(&self) -> Expr
pub fn ci(&self) -> Expr
pub fn shi(&self) -> Expr
pub fn chi(&self) -> Expr
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Expr
impl<'de> Deserialize<'de> for Expr
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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