diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2024-01-17 23:28:48 +0100 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2024-01-17 23:28:48 +0100 |
| commit | 9cc61497ed8a2336f33407d3262181e4ac3b46cb (patch) | |
| tree | aac75b57b0fffea64abcd23cbac4d27c875fee48 /src/commonsense.rs | |
| parent | 77cf9aa7535a1d9481f0bd3caeea26e2b85c5019 (diff) | |
add commonsense and expression_functions
Diffstat (limited to 'src/commonsense.rs')
| -rw-r--r-- | src/commonsense.rs | 89 |
1 files changed, 89 insertions, 0 deletions
diff --git a/src/commonsense.rs b/src/commonsense.rs new file mode 100644 index 0000000..df688fb --- /dev/null +++ b/src/commonsense.rs @@ -0,0 +1,89 @@ +use crate::complex::Complex; + +pub fn add(a: Complex, b: Complex) -> Complex { + a + b +} + +pub fn sub(a: Complex, b: Complex) -> Complex { + a - b +} + +pub fn mul(a: Complex, b: Complex) -> Complex { + a * b +} + +pub fn div(a: Complex, b: Complex) -> Complex { + a / b +} + +pub fn pow(a: Complex, b: Complex) -> Complex { + a.pow(b) +} + +pub fn sqrt(a: Complex) -> Complex { + a.sqrt() +} + +pub fn sin(a: Complex) -> Complex { + a.sin() +} + +pub fn cos(a: Complex) -> Complex { + a.cos() +} + +pub fn tan(a: Complex) -> Complex { + a.tan() +} + +#[macro_export] +macro_rules! commonsense_functions { + {$($x:expr => $y:expr), *} => { + { + let mut h : HashMap<String, Box<dyn Function>> = HashMap::new(); + h.extend(functions!{ + "sqrt" => &crate::commonsense::sqrt, + "sin" => &crate::commonsense::sin, + "cos" => &crate::commonsense::cos, + "tan" => &crate::commonsense::tan + }); + $( + h.insert($x.to_string(), Box::new($y)); + )* + h + } + }; +} + +#[macro_export] +macro_rules! commonsense_operations { + {$($x:expr => $y:expr), *} => { + vec![ + Operation::new('+', Box::new(&crate::commonsense::add)), + Operation::new('-', Box::new(&crate::commonsense::sub)), + Operation::new('*', Box::new(&crate::commonsense::mul)), + Operation::new('/', Box::new(&crate::commonsense::div)), + Operation::new('^', Box::new(&crate::commonsense::pow)) + $( + Operation::new($x, Box::new($y)), + )*] + }; +} + +#[macro_export] +macro_rules! commonsense_variables { + {$($x:expr => $y:expr), *} => { + { + let mut h : HashMap<String, Complex> = HashMap::new(); + h.extend(variables!{ + "pi" => Complex::new(std::f64::consts::PI, 0.0), + "e" => Complex::new(std::f64::consts::E, 0.0) + }); + $( + h.insert($x.to_string(), $y); + )* + h + } + }; +} + |