Enum cassie::Term
[−]
[src]
pub enum Term { Variable(Variable), Constant(f64), Sum(Vec<Term>), Difference(Vec<Term>), Product(Vec<Term>), Quotient(Vec<Term>), }
Terms are basic mathematical building blocks, from which are formed expressions and more complex entities.
The Term
data type (currently) represents basic polynomial components, which can be assigned a numeric value with Term::evaluate
/Term::reduce
.
Variants
Variable(Variable)
Represents a term which simply a variable, one of the two foundational term types.
The value of the variable is looked up against the given variable values when Term::evaluate
is called.
Constant(f64)
Represents a constant term, one of the two foundational term types.
The value of this term is fixed and is calculated by simply unpacking the associated value.
Sum(Vec<Term>)
Represents a sum of multiple terms.
To calculate the value of this term, the components are evaluated iteratively from the first to last index.
Difference(Vec<Term>)
Represents a difference of terms.
The first term is used as-is; all others have their signs inverted and are added to the first term in ascending order of index.
Product(Vec<Term>)
Represents a product of terms.
All terms are multiplied together after evaluation, with evaluation proceeding in ascending index order.
Quotient(Vec<Term>)
Represents a quotient of terms.
The first term is evaluated, then divided by each following term in order of ascending index (each term is used immediately after evaluation). Fairly aggressive sanity checks are performed to prevent division by zero; if this continues to pester you, consider multiplying by the inverse instead.
This variant should be considered unstable; it is only due to typing constraints that simplification is implemented for more than two subterms. Consider using Term::Product
instead, if possible.
Methods
impl Term
[src]
fn evaluate(&self, values: &HashMap<char, f64>) -> Result<f64, String>
[src]
Evaluates a term to its numerical value.
Examples
use cassie::{Term, Variable}; use std::collections::HashMap; let x: Variable = "x".parse().unwrap(); let x = Term::Variable(x); let c = Term::Constant(100.0); let s = x + c; let mut values = HashMap::new(); values.insert('x', 28.0); assert!((s.evaluate(&values).unwrap() - 128.0).abs() < 0.00001);
fn reduce(&self) -> Result<f64, String>
[src]
Evaluates a term to its numerical value, assuming only constants (no variables specified).
Panics
This method is functionally identical to using Term::evaluate
with an empty value table, so it inherits the panic conditions from Term::evaluate
.
Most significantly, if a variable is present in self
, this function will panic, since the variable value will not be resolved.
Examples
use cassie::Term; let c = Term::Constant(64.0); assert!((c.reduce().unwrap() - 64.0) < 0.00001); let b = Term::Constant(64.0); let a = Term::Constant(36.0); let c = &a + &b; assert!(a.reduce().unwrap() - 36.0 < 0.00001); assert!(b.reduce().unwrap() - 64.0 < 0.00001); assert!(c.reduce().unwrap() - 100.0 < 0.00001);
Trait Implementations
impl Clone for Term
[src]
fn clone(&self) -> Term
[src]
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
Performs copy-assignment from source
. Read more
impl<'a, 'b> Add<&'b Term> for &'a Term
[src]
type Output = Term
The resulting type after applying the +
operator.
fn add(self, another: &'b Term) -> Term
[src]
Performs the +
operation.