aboutsummaryrefslogtreecommitdiff
path: root/src/math/commonsense.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/math/commonsense.rs')
-rw-r--r--src/math/commonsense.rs90
1 files changed, 90 insertions, 0 deletions
diff --git a/src/math/commonsense.rs b/src/math/commonsense.rs
new file mode 100644
index 0000000..e75ccc5
--- /dev/null
+++ b/src/math/commonsense.rs
@@ -0,0 +1,90 @@
+use crate::math::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), *} => {
+ {
+ use std::sync::Arc;
+ let mut h : HashMap<String, Arc<dyn Function>> = HashMap::new();
+ h.extend(functions!{
+ "sqrt" => &$crate::math::commonsense::sqrt,
+ "sin" => &$crate::math::commonsense::sin,
+ "cos" => &$crate::math::commonsense::cos,
+ "tan" => &$crate::math::commonsense::tan
+ });
+ $(
+ h.insert($x.to_string(), Arc::new($y));
+ )*
+ h
+ }
+ };
+}
+
+#[macro_export]
+macro_rules! commonsense_operations {
+ {$($x:expr => $y:expr), *} => {
+ vec![
+ Operation::new('+', $crate::math::commonsense::add),
+ Operation::new('-', $crate::math::commonsense::sub),
+ Operation::new('*', $crate::math::commonsense::mul),
+ Operation::new('/', $crate::math::commonsense::div),
+ Operation::new('^', $crate::math::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
+ }
+ };
+}
+