pub struct Compiler { /* private fields */ }Implementations§
Source§impl Compiler
The central hub of the Rust interface. It compiles a list of
variables and expressions into a callable object (of type Application).
impl Compiler
The central hub of the Rust interface. It compiles a list of
variables and expressions into a callable object (of type Application).
§Workflow
- Create terminals (variables and constants) and compose expressions using
Exprmethods:- Constructors:
var,from,unary,binary, … - Standard algebraic operations:
add,mul, … - Standard operators
+,-,*,/,%,&,|,^,!. - Unary functions such as
sin,exp, and other standard mathematical functions. - Binary functions such as
pow,min, … - IfElse operation
ifelse(cond, true_val, false_val). - Heavide function:
heaviside(x), which returns 1 ifx >= 0; otherwise 0. - Comparison methods
eq,ne,lt,le,gt, andge. - Looping constructs
sumandprod.
- Constructors:
- Create a new
Compilerobject (say,comp) using one of its constructors:new()orwith_compile_type(ty: CompilerType). - Fine-tune the optimization passes using
opt_level,simd,fastmath, andcsemethods (optional). - Define user-defined functions by called
comp.def_unaryandcomp.def_binary(optional). - Compile by calling
comp.compileorcomp.compile_params. The result is of typeApplication(say,app). - Execute the compiled code using one of the
app’scallfunctions:call(&[f64]): scalar call.call_params(&[f64], &[f64]): scalar call with parameters.call_simd(&[__m256d]): simd call.call_simd_params(&[__m256d], &[f64]): simd call with parameters.
- Optionally, generate a standalone fast function to execute.
§Examples
use anyhow::Result;
use symjit::{Compiler, Expr};
pub fn main() -> Result<()> {
let x = Expr::var("x");
let y = Expr::var("y");
let u = &x + &y;
let v = &x * &y;
let mut config = Config::default();
config.set_opt_level(2);
let mut comp = Compiler::with_config(config);
let mut app = comp.compile(&[x, y], &[u, v])?;
let res = app.call(&[3.0, 5.0]);
println!("{:?}", &res);
Ok(())
}pub fn with_config(config: Config) -> Compiler
Sourcepub fn compile(&mut self, states: &[Expr], obs: &[Expr]) -> Result<Application>
pub fn compile(&mut self, states: &[Expr], obs: &[Expr]) -> Result<Application>
Compiles a model.
states is a list of variables, created by Expr::var.
obs is a list of expressions.
Sourcepub fn compile_params(
&mut self,
states: &[Expr],
obs: &[Expr],
params: &[Expr],
) -> Result<Application>
pub fn compile_params( &mut self, states: &[Expr], obs: &[Expr], params: &[Expr], ) -> Result<Application>
Compiles a model with parameters.
states is a list of variables, created by Expr::var.
obs is a list of expressions.
params is a list of parameters, created by Expr::var.
Note: for scalar functions, the difference between states and params is mostly by convenion. However, they are different in SIMD cases, as params are always f64.
Source§impl Compiler
impl Compiler
Sourcepub fn translate(
&mut self,
json: String,
num_params: usize,
) -> Result<Application>
pub fn translate( &mut self, json: String, num_params: usize, ) -> Result<Application>
Compiles a Symbolica model.
json is the JSON-encoded output of Symbolica export_instructions.
Example:
let params = vec![parse!("x"), parse!("y")];
let eval = parse!("x + y^2")
.evaluator(&FunctionMap::new(), ¶ms, OptimizationSettings::default())?
let json = serde_json::to_string(&eval.export_instructions())?;
let mut comp = Compiler::new();
let mut app = comp.translate(&json)?;
assert!(app.evaluate_single(&[2.0, 3.0]) == 11.0);Auto Trait Implementations§
impl Freeze for Compiler
impl RefUnwindSafe for Compiler
impl Send for Compiler
impl Sync for Compiler
impl Unpin for Compiler
impl UnsafeUnpin for Compiler
impl UnwindSafe for Compiler
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
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>
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 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>
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