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

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.

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.

Represents a sum of multiple terms.

To calculate the value of this term, the components are evaluated iteratively from the first to last index.

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.

Represents a product of terms.

All terms are multiplied together after evaluation, with evaluation proceeding in ascending index order.

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]

[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);

[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]

[src]

Returns a copy of the value. Read more

1.0.0
[src]

Performs copy-assignment from source. Read more

impl<'a, 'b> Add<&'b Term> for &'a Term
[src]

The resulting type after applying the + operator.

[src]

Performs the + operation.

impl Add for Term
[src]

The resulting type after applying the + operator.

[src]

Performs the + operation.