1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
use variable::Variable; use std::collections::HashMap; use std::ops::Add; use std::ops::Sub; use std::ops::Mul; use std::ops::Div; type VariableValues = HashMap<char, f64>; /// 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`. #[derive(Clone)] pub enum Term { /// 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); /// ``` Variable(Variable), /// 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); /// ``` Constant(f64), /// 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); /// ``` Sum(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); /// ``` Difference(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); /// ``` Product(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); /// ``` Quotient(Vec<Term>), // Look into limiting vector sizes to avoid confusion (due to bad input). /// 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." Sine(Box<Term>), // TODO: Verify that this is what we want (this uses heap memory). /// 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." Cosine(Box<Term>), // TODO: Verify that this is what we want (this uses heap memory). /// 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." Tangent(Box<Term>), // TODO: Verify that this is what we want (this uses heap memory). /// 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." ArcSine(Box<Term>), // TODO: Verify that this is what we want (this uses heap memory). /// 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." ArcCosine(Box<Term>), // TODO: Verify that this is what we want (this uses heap memory). /// 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." ArcTangent(Box<Term>) // TODO: Verify that this is what we want (this uses heap memory). } impl Term { /// 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 evaluate(&self, values: &VariableValues) -> Result<f64, String> { self.eval(Some(values)) } /// 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); /// ``` pub fn reduce(&self) -> Result<f64, String> { self.eval(None) } fn eval(&self, values: Option<&VariableValues>) -> Result<f64, String> { use Term::*; match *self { Constant(value) => Ok(value), Sum(ref terms) => { let mut sum = 0.0; for term in terms { match term.eval(values) { Ok(value) => { sum += value; }, Err(e) => { return Err(e); } }; } Ok(sum) // dim sum for a twosome }, Difference(ref terms) => { let first = terms[0].eval(values); if first.is_err() { return first; } let mut difference = first.unwrap(); for term in terms[1..].iter() { match term.eval(values) { Ok(value) => { difference -= value; }, Err(e) => { return Err(e); } }; } Ok(difference) }, Product(ref terms) => { let mut product = 1.0; for term in terms { match term.eval(values) { Ok(value) => { product *= value; }, Err(e) => { return Err(e); } }; } Ok(product) }, Quotient(ref terms) => { let first = terms[0].eval(values); if first.is_err() { return first; } let mut quotient = first.unwrap(); for term in terms[1..].iter() { match term.eval(values) { Ok(dividend) => { if dividend.abs() < 0.00000000000000001 { return Err("Attempted division by zero.".to_string()); } quotient /= dividend; }, Err(e) => { return Err(e); } }; } Ok(quotient) }, Variable(ref variable) => { if let Some(v) = values { if let Some(value) = v.get(&variable.symbol) { Ok(*value) } else { Err(format!("No value provided for variable {}", variable.symbol)) } } else { Err(format!("No variable values provided (looking for {})", variable.symbol)) } }, Sine(ref term) => { match term.eval(values) { Ok(value) => Ok(value.sin()), Err(e) => Err(e) } }, Cosine(ref term) => { match term.eval(values) { Ok(value) => Ok(value.cos()), Err(e) => Err(e) } }, ArcSine(ref term) => { match term.eval(values) { Ok(value) => Ok(value.asin()), Err(e) => Err(e) } }, ArcCosine(ref term) => { match term.eval(values) { Ok(value) => Ok(value.acos()), Err(e) => Err(e) } }, Tangent(ref term) => { match term.eval(values) { Ok(value) => Ok(value.tan()), Err(e) => Err(e) } }, ArcTangent(ref term) => { match term.eval(values) { Ok(value) => Ok(value.atan()), Err(e) => Err(e) } } } } } impl<'a, 'b> Add<&'b Term> for &'a Term { type Output = Term; fn add(self, another: &'b Term) -> Term { Term::Sum(vec!(self.clone(), another.clone())) } } impl Add for Term { type Output = Term; fn add(self, another: Term) -> Term { &self + &another } } impl<'a, 'b> Sub<&'b Term> for &'a Term { type Output = Term; fn sub(self, another: &'b Term) -> Term { Term::Difference(vec!(self.clone(), another.clone())) } } impl Sub for Term { type Output = Term; fn sub(self, another: Term) -> Term { &self - &another } } impl<'a, 'b> Mul<&'b Term> for &'a Term { type Output = Term; fn mul(self, another: &'b Term) -> Term { Term::Product(vec!(self.clone(), another.clone())) } } impl Mul for Term { type Output = Term; fn mul(self, another: Term) -> Term { &self * &another } } impl<'a, 'b> Div<&'b Term> for &'a Term { type Output = Term; fn div(self, another: &'b Term) -> Term { Term::Quotient(vec!(self.clone(), another.clone())) } } impl Div for Term { type Output = Term; fn div(self, another: Term) -> Term { &self / &another } }