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 377 378 379 380 381 382 383 384 385 386 387
//! A simple, safe crate for parsing properly-formatted math strings which represent parametric functions into Rust functions that compute them. Ported from an earlier version for Javascript. //! //! # Example Usage //! //! Simply create a Parametrizer struct by passing a string to one of its constructors and call the evaluate method: //! ``` //! use crate::parametrizer::Parametrizer; //! //! let division = Parametrizer::new("4\\2").unwrap(); //! let subtraction = Parametrizer::new("15-3*t").unwrap(); //! let spaces = Parametrizer::new("6 + T").unwrap(); //! let sin = Parametrizer::new("sin(t*t + t - 1)").unwrap(); //! //! assert_eq!(2, division.evaluate(8)); //! assert_eq!(6, subtraction.evaluate(3)); //! assert_eq!(8, spaces.evaluate(2)); //! assert_eq!(11.0_f64.sin(), sin.evaluate(3.0)); //! ``` //! //! ``` //! use crate::parametrizer::Parametrizer; //! //! let constant = Parametrizer::new("1.35").unwrap(); //! //! assert_eq!(1.35, constant.evaluate(2.0)); //! assert_eq!(1.35, constant.evaluate(3.4)); //! ``` //! //! ``` //! use crate::parametrizer::Parametrizer; //! //! let variable = Parametrizer::new("t").unwrap(); //! //! assert_eq!(3.0, variable.evaluate(3.0)); //! assert_ne!(4.2, variable.evaluate(1.25)); //! ``` //! //! ``` //! use crate::parametrizer::Parametrizer; //! //! let addition = Parametrizer::new("1+t").unwrap(); //! //! assert_eq!(9.0, addition.evaluate(8.0)); //! assert_eq!(1.16, addition.evaluate(0.16)); //! ``` //! //! ``` //! use crate::parametrizer::Parametrizer; //! //! let equation = Parametrizer::new("13+((2*t)+5)").unwrap(); //! //! assert_eq!(20, equation.evaluate(1)); //! assert_eq!(30, equation.evaluate(6)); //! ``` //! //! ``` //! use crate::parametrizer::Parametrizer; //! //! let division = Parametrizer::new("6/t").unwrap(); //! //! assert_eq!(2, division.evaluate(3)); //! assert_eq!(3, division.evaluate(2)); //! ``` //! //! ``` //! use crate::parametrizer::Parametrizer; //! //! let equation = Parametrizer::new("13-t").unwrap(); //! let negation = Parametrizer::new("-t").unwrap(); //! //! assert_eq!(10, equation.evaluate(3)); //! assert_eq!(-9, negation.evaluate(9)); //! ``` //! //! ``` //! use crate::parametrizer::Parametrizer; //! //! let dynamic_rand = Parametrizer::new("rd(2+t<4*t)").unwrap(); //! let computed_rand = Parametrizer::new("rc(4<8)").unwrap(); //! //! assert_eq!(computed_rand.evaluate(2), computed_rand.evaluate(4)); //! assert!(4 <= dynamic_rand.evaluate(2)); //! assert!(16 > dynamic_rand.evaluate(4)); //! ``` //! //! ``` //! use crate::parametrizer::Parametrizer; //! //! //Piecewise functions //! let p1 = Parametrizer::new("p2>0|4>2|8>6").unwrap(); //! let p2 = Parametrizer::new("p2*t>0|4>2").unwrap(); //! //! //Looping piecewise functions //! let p3 = Parametrizer::new("p[10]18>0|23>4").unwrap(); //! //! assert_eq!(2, p1.evaluate(1)); //! assert_eq!(4, p1.evaluate(5)); //! assert_eq!(2, p2.evaluate(1)); //! assert_eq!(4, p2.evaluate(9)); //! //! assert_eq!(18, p3.evaluate(23)); //! assert_eq!(23, p3.evaluate(106)); //! ``` //! //! The underlying terms are public to allow for the manual composition of terms in code to avoid //! the string parsing overhead. See the `term` module documentation for more information. See the //! `Parametrizer` struct's implementation documentation to see more usage examples. extern crate num; use num::Num; use num::ToPrimitive; use num::FromPrimitive; use std::cmp::PartialOrd; use std::str::FromStr; use std::fmt; pub mod term; pub trait Number: Num + ToPrimitive + FromPrimitive + PartialOrd + FromStr + Copy + Send + Sync + 'static {} impl<T: Num + ToPrimitive + FromPrimitive + PartialOrd + FromStr + Copy + Send + Sync + 'static> Number for T {} ///An error which describes why parametrization failed. Contains the param string which failed as ///well as the reason for failure. #[derive(Debug)] pub struct ParametrizerError { param: String, reason: &'static str } impl fmt::Display for ParametrizerError { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { return write!(f, "Parametrizer failed to parse string: {}, with failure reason: {}", self.param, self.reason); } } ///A pair containing a function on 64-bit float numbers and a shorthand associated with it. pub struct ParametrizerFunction { shorthand: String, function: fn(f64) -> f64 } impl ParametrizerFunction { ///Function for creating a ParametrizerFunction pair for use in Parametrizer /// /// # Examples /// /// ``` /// use crate::parametrizer::ParametrizerFunction; /// /// let pair = ParametrizerFunction::new("Sin".to_string(), f64::sin); /// /// assert_eq!("sin(", pair.shorthand()); /// assert_eq!(2.0_f64.sin(), (pair.function())(2.0)); /// ``` pub fn new(identifier: String, function: fn(f64) -> f64) -> ParametrizerFunction { let shorthand = identifier.to_lowercase(); let shorthand = format!("{}(", shorthand); return ParametrizerFunction { shorthand, function }; } ///A function which returns the shorthand for the function as parsed by parametrize_string, ///i.e. adds a "(" to the end of the user-defined identifier. pub fn shorthand(&self) -> &String { return &self.shorthand; } ///Returns the stored function pub fn function(&self) -> fn(f64) -> f64 { return self.function; } } ///Main struct for parametrizing strings. Contains a pointer to the top-level term, which will ///contain pointers to lower leves for recursive evaluations pub struct Parametrizer<T: Number> { //The top-level term for the parametrized function. Must be placed on the heap as the //recursion could be of theoretically unbounded depth term: Box<dyn term::Term<T> + Send + Sync> } impl<T: Number> Parametrizer<T> { ///Default constructor. Formats the param string before parsing to handle uppercase letters, spaces, ///and the like, which may cause some performance slowdown. Already properly formatted strings can ///be parsed using Parametrizer::quick_new.Supports sine and cosine via "sin" and "cos". /// /// # Examples /// ``` /// use crate::parametrizer::Parametrizer; /// /// let division = Parametrizer::new("4\\2").unwrap(); /// let subtraction = Parametrizer::new("15-3*t").unwrap(); /// let spaces = Parametrizer::new("6 + T").unwrap(); /// let sin = Parametrizer::new("sin(t*t + t - 1)").unwrap(); /// /// assert_eq!(2, division.evaluate(8)); /// assert_eq!(6, subtraction.evaluate(3)); /// assert_eq!(8, spaces.evaluate(2)); /// assert_eq!(11.0_f64.sin(), sin.evaluate(3.0)); /// ``` /// /// ``` /// use crate::parametrizer::Parametrizer; /// /// let constant = Parametrizer::new("1.35").unwrap(); /// /// assert_eq!(1.35, constant.evaluate(2.0)); /// assert_eq!(1.35, constant.evaluate(3.4)); /// ``` /// /// ``` /// use crate::parametrizer::Parametrizer; /// /// let variable = Parametrizer::new("t").unwrap(); /// /// assert_eq!(3.0, variable.evaluate(3.0)); /// assert_ne!(4.2, variable.evaluate(1.25)); /// ``` /// /// ``` /// use crate::parametrizer::Parametrizer; /// /// let addition = Parametrizer::new("1+t").unwrap(); /// /// assert_eq!(9.0, addition.evaluate(8.0)); /// assert_eq!(1.16, addition.evaluate(0.16)); /// ``` /// /// ``` /// use crate::parametrizer::Parametrizer; /// /// let equation = Parametrizer::new("13+((2*t)+5)").unwrap(); /// /// assert_eq!(20, equation.evaluate(1)); /// assert_eq!(30, equation.evaluate(6)); /// ``` /// /// ``` /// use crate::parametrizer::Parametrizer; /// /// let division = Parametrizer::new("6/t").unwrap(); /// /// assert_eq!(2, division.evaluate(3)); /// assert_eq!(3, division.evaluate(2)); /// ``` /// /// ``` /// use crate::parametrizer::Parametrizer; /// /// let equation = Parametrizer::new("13-t").unwrap(); /// let negation = Parametrizer::new("-t").unwrap(); /// /// assert_eq!(10, equation.evaluate(3)); /// assert_eq!(-9, negation.evaluate(9)); /// ``` /// /// ``` /// use crate::parametrizer::Parametrizer; /// /// let dynamic_rand = Parametrizer::new("rd(2+t<4*t)").unwrap(); /// let computed_rand = Parametrizer::new("rc(4<8)").unwrap(); /// /// assert_eq!(computed_rand.evaluate(2), computed_rand.evaluate(4)); /// assert!(4 <= dynamic_rand.evaluate(2)); /// assert!(16 > dynamic_rand.evaluate(4)); /// ``` pub fn new(param: &str) -> Result<Parametrizer<T>, ParametrizerError> { return Parametrizer::new_functions(param, vec![ ParametrizerFunction::new("sin".to_string(), f64::sin), ParametrizerFunction::new("cos".to_string(), f64::cos) ]); } ///Constructor which allows for the user to define additional functions using a vector of ///ParametrizerFunction structs. Formats the param string like Parametrizer::new, with similar ///potential slowdown. Note that sine and cosine are not supported by default, but can be ///included in the user-defined list. /// /// # Examples /// ``` /// use crate::parametrizer::Parametrizer; /// use crate::parametrizer::ParametrizerFunction; /// /// fn square(t: f64) -> f64 /// { /// /// return t * t; /// /// } /// /// let logarithm_and_square = Parametrizer::new_functions("Log( square(t) + 3 )", vec![ /// /// ParametrizerFunction::new("LOG".to_string(), f64::ln), /// ParametrizerFunction::new("square".to_string(), square) /// /// ]).unwrap(); /// /// assert_eq!(7.0_f64.ln(), logarithm_and_square.evaluate(2.0)); /// assert_eq!(28.0_f64.ln(), logarithm_and_square.evaluate(5.0)); /// ``` pub fn new_functions(param: &str, functions: Vec<ParametrizerFunction>) -> Result<Parametrizer<T>, ParametrizerError> { let term = term::create_parametrization::<T>(param, &functions[..])?; return Ok(Parametrizer::<T> { term }); } ///Constructor which skips the added string formatting of Parametrizer::new and ///Parametrizer::new_functions, potentially speeding up parsing at the cost of unpredictable ///behavior when a string is not formatted exactly correctly. (I.e., includes extra spaces ///or capital letters.) Requires users to specify a vector of ParametrizerFunctions as in ///the case for Parametrizer::new_functions, and does not include sine or cosine by default. /// /// # Examples /// ``` /// use crate::parametrizer::Parametrizer; /// use crate::parametrizer::ParametrizerFunction; /// /// let division = Parametrizer::quick_new("4/2", Vec::new()).unwrap(); /// let subtraction = Parametrizer::quick_new("15+(-3*t)", Vec::new()).unwrap(); /// let spaces = Parametrizer::quick_new("6+t", Vec::new()).unwrap(); /// let sin = Parametrizer::quick_new("sin(t*t+t+-1)", vec![ ParametrizerFunction::new("sin".to_string(), f64::sin) ]).unwrap(); /// let log = Parametrizer::quick_new("log(t+3)", vec![ ParametrizerFunction::new("log".to_string(), f64::ln) /// ]).unwrap(); /// /// assert_eq!(2, division.evaluate(8)); /// assert_eq!(6, subtraction.evaluate(3)); /// assert_eq!(8, spaces.evaluate(2)); /// assert_eq!(11.0_f64.sin(), sin.evaluate(3.0)); /// assert_eq!(8.0_f64.ln(), log.evaluate(5.0)); /// ``` pub fn quick_new(param: &str, functions: Vec<ParametrizerFunction>) -> Result<Parametrizer<T>, ParametrizerError> { let term = term::quick_parametrization::<T>(param, &functions[..])?; return Ok(Parametrizer::<T> { term }); } ///Used to compute the parametric function at a specific point. As the parsing is done once at ///creation time, the only overhead is due to pointers and recursion. pub fn evaluate(&self, t: T) -> T { return (*self.term).evaluate(t); } }