aboutsummaryrefslogtreecommitdiff
path: root/src/commonsense.rs
blob: df688fb8427044e81923ad0de5bf9dbdb27765f3 (plain)
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
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
        }
    };
}