Enum cassie::Term [−][src]
pub enum Term { Variable(Variable), Constant(f64), Sum(Vec<Term>), Difference(Vec<Term>), Product(Vec<Term>), Quotient(Vec<Term>), Sine(Box<Term>), Cosine(Box<Term>), Tangent(Box<Term>), ArcSine(Box<Term>), ArcCosine(Box<Term>), ArcTangent(Box<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.
#Examples
use cassie::{Term, Variable}; use std::collections::HashMap; let mut bindings = HashMap::new(); bindings.insert('φ', 68.0); let f = Variable::named('φ'); let f = Term::Variable(f); assert!(f.evaluate(&bindings).unwrap() - 68.0 < 0.00001);
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.
#Examples
use cassie::Term; let c = Term::Constant(24.0); assert!(c.reduce().unwrap() - 24.0 < 0.00001);
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.
#Examples
use cassie::Term; let a = Term::Constant(24.0); let b = Term::Constant(72.0); let y = Term::Sum(vec!(a, b)); // Notice that this is very ugly; see below assert!(y.reduce().unwrap() - 108.0 < 0.00001); let c = Term::Constant(12.0); let d = Term::Constant(27.0); let z = c + d; // Preferred assert!(z.reduce().unwrap() - 39.0 < 0.00001);
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.
#Examples
use cassie::Term; let a = Term::Constant(24.0); let b = Term::Constant(72.0); let y = Term::Difference(vec!(a, b)); // Notice that this is very ugly; see below assert!((y.reduce().unwrap() + 48.0).abs() < 0.00001); let c = Term::Constant(12.0); let d = Term::Constant(27.0); let z = c - d; // Preferred assert!((z.reduce().unwrap() + 15.0).abs() < 0.00001);
Product(Vec<Term>)
Represents a product of terms.
All terms are multiplied together after evaluation, with evaluation proceeding in ascending index order.
#Examples
use cassie::Term; let a = Term::Constant(8.0); let b = Term::Constant(9.0); let y = Term::Product(vec!(a, b)); // Notice that this is very ugly; see below assert!((y.reduce().unwrap() - 72.0).abs() < 0.00001); let c = Term::Constant(3.0); let d = Term::Constant(-1.0); let z = c * d; // Preferred assert!((z.reduce().unwrap() + 3.0).abs() < 0.00001);
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 more or less unstable; it is only due to typing constraints that simplification is implemented for more than two subterms. Consider using Term::Product
instead, if possible.
#Examples
use cassie::Term; let a = Term::Constant(63.0); let b = Term::Constant(3.0); let y = Term::Quotient(vec!(a, b)); // Notice that this is very ugly; see below assert!((y.reduce().unwrap() - 21.0).abs() < 0.00001); let c = Term::Constant(3.0); let d = Term::Constant(-1.0); let z = c / d; // Preferred assert!((z.reduce().unwrap() + 3.0).abs() < 0.00001);
Sine(Box<Term>)
Represents the sine function.
The associated term is evaluated and passed to a sine function to obtain a result.
Like any self-respecting sine function, this performs operations "in radians."
Cosine(Box<Term>)
Represents the cosine function.
The associated term is evaluated and passed to a cosine function to obtain a result.
Like any self-respecting cosine function, this performs operations "in radians."
Tangent(Box<Term>)
Represents the tangent function.
The associated term is evaluated and passed to a tangent function to obtain a result.
Like any self-respecting cosine function, this performs operations "in radians."
ArcSine(Box<Term>)
Represents the inverse sine function.
The associated term is evaluated and passed to an inverse sine function to obtain a result.
Like any self-respecting trigonometric function, this performs operations "in radians."
ArcCosine(Box<Term>)
Represents the inverse cosine function.
The associated term is evaluated and passed to an inverse cosine function to obtain a result.
Like any self-respecting trigonometric function, this performs operations "in radians."
ArcTangent(Box<Term>)
Represents the inverse tangent function.
The associated term is evaluated and passed to an inverse tangent function to obtain a result.
Like any self-respecting trigonometric function, this performs operations "in radians."
Methods
impl Term
[src]
impl Term
pub fn evaluate(&self, values: &HashMap<char, f64>) -> Result<f64, String>
[src]
pub fn evaluate(&self, values: &HashMap<char, f64>) -> Result<f64, String>
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);
pub fn reduce(&self) -> Result<f64, String>
[src]
pub fn reduce(&self) -> Result<f64, String>
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]
impl Clone for Term
fn clone(&self) -> Term
[src]
fn clone(&self) -> Term
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
impl<'a, 'b> Add<&'b Term> for &'a Term
[src]
impl<'a, 'b> Add<&'b Term> for &'a Term
type Output = Term
The resulting type after applying the +
operator.
fn add(self, another: &'b Term) -> Term
[src]
fn add(self, another: &'b Term) -> Term
Performs the +
operation.
impl Add for Term
[src]
impl Add for Term
type Output = Term
The resulting type after applying the +
operator.
fn add(self, another: Term) -> Term
[src]
fn add(self, another: Term) -> Term
Performs the +
operation.
impl<'a, 'b> Sub<&'b Term> for &'a Term
[src]
impl<'a, 'b> Sub<&'b Term> for &'a Term
type Output = Term
The resulting type after applying the -
operator.
fn sub(self, another: &'b Term) -> Term
[src]
fn sub(self, another: &'b Term) -> Term
Performs the -
operation.
impl Sub for Term
[src]
impl Sub for Term
type Output = Term
The resulting type after applying the -
operator.
fn sub(self, another: Term) -> Term
[src]
fn sub(self, another: Term) -> Term
Performs the -
operation.
impl<'a, 'b> Mul<&'b Term> for &'a Term
[src]
impl<'a, 'b> Mul<&'b Term> for &'a Term
type Output = Term
The resulting type after applying the *
operator.
fn mul(self, another: &'b Term) -> Term
[src]
fn mul(self, another: &'b Term) -> Term
Performs the *
operation.
impl Mul for Term
[src]
impl Mul for Term
type Output = Term
The resulting type after applying the *
operator.
fn mul(self, another: Term) -> Term
[src]
fn mul(self, another: Term) -> Term
Performs the *
operation.
impl<'a, 'b> Div<&'b Term> for &'a Term
[src]
impl<'a, 'b> Div<&'b Term> for &'a Term
type Output = Term
The resulting type after applying the /
operator.
fn div(self, another: &'b Term) -> Term
[src]
fn div(self, another: &'b Term) -> Term
Performs the /
operation.
impl Div for Term
[src]
impl Div for Term