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
use crate::{BracketsLevel, ConstantValue, Expression, TranscendentalExpression};
use std::{collections::HashMap, ops::Mul};

impl Mul<Expression> for Expression {
    type Output = Self;

    fn mul(self, rhs: Expression) -> Self::Output {
        if !self.is_same_size(&rhs) {
            panic!("Cannot add expressions of different sizes");
        }
        // Merge constant
        if let Expression::Constant(vl) = &self {
            if vl == &ConstantValue::Scalar(0.0) {
                return 0.0.into();
            }
            if vl == &ConstantValue::Scalar(1.0) {
                return rhs;
            }
            if let Expression::Constant(vr) = rhs {
                return vl.mul(vr).into();
            }
        }
        if let Expression::Constant(vr) = &rhs {
            if vr == &ConstantValue::Scalar(0.0) {
                return 0.0.into();
            }
            if vr == &ConstantValue::Scalar(1.0) {
                return self;
            }
        }
        // Merge pow
        if let Expression::Transcendental(vl) = &self {
            if let TranscendentalExpression::Pow(vl, el) = vl.as_ref() {
                if let Expression::Transcendental(vr) = &rhs {
                    if let TranscendentalExpression::Pow(vr, er) = vr.as_ref() {
                        if vl.as_ref() == vr.as_ref() {
                            return vl.clone().pow(*el.clone() + *er.clone());
                        }
                    }
                }
                if vl.as_ref() == &rhs {
                    let one: Expression = 1.0.into();
                    return vl.clone().pow(*el.clone() + one);
                }
            }
        }
        if let Expression::Transcendental(vr) = &rhs {
            if let TranscendentalExpression::Pow(vr, er) = vr.as_ref() {
                if vr.as_ref() == &self {
                    let one: Expression = 1.0.into();
                    return vr.clone().pow(*er.clone() + one);
                }
            }
        }

        Expression::Mul(self.into(), rhs.into())
    }
}

impl Mul<Expression> for f64 {
    type Output = Expression;

    fn mul(self, rhs: Expression) -> Self::Output {
        Expression::Constant(ConstantValue::Scalar(self)) * rhs
    }
}

impl Mul<f64> for Expression {
    type Output = Self;

    fn mul(self, rhs: f64) -> Self::Output {
        self * Expression::Constant(ConstantValue::Scalar(rhs))
    }
}

impl Expression {
    pub(crate) fn diff_mul(
        l: &Box<Expression>,
        r: &Box<Expression>,
        variable_ids: &[&str],
    ) -> Vec<Expression> {
        l.differential(variable_ids)
            .into_iter()
            .zip(r.differential(variable_ids).into_iter())
            .map(|(li, ri)| li * r.as_ref().clone() + l.as_ref().clone() * ri)
            .collect()
    }

    pub(crate) fn tex_code_mul(
        l: &Box<Expression>,
        r: &Box<Expression>,
        symbols: &HashMap<&str, &str>,
        brackets_level: BracketsLevel,
    ) -> String {
        let inner = format!(
            r"{{{} \times {}}}",
            l._tex_code(symbols, BracketsLevel::ForMul),
            r._tex_code(symbols, BracketsLevel::ForMul)
        );

        match brackets_level {
            BracketsLevel::None | BracketsLevel::ForMul => inner,
            BracketsLevel::ForDiv | BracketsLevel::ForOperation => {
                format!(r"\left({}\right)", inner)
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use std::{collections::HashMap, ops::Add};

    use opensrdk_linear_algebra::sparse::SparseTensor;

    use crate::Expression;

    #[test]
    fn it_works() {
        let a1 = 5.0f64;
        let b1 = vec![a1; 8];
        let mut hash1 = HashMap::new();
        hash1.insert(vec![3, 2, 1], 2.0);
        hash1.insert(vec![1usize; 3], 3.0);
        hash1.insert(vec![4usize; 3], 4.0);
        hash1.insert(vec![5usize; 3], 2.0);
        let c1 = SparseTensor::from(vec![6usize; 3], hash1).unwrap();

        let ea1 = Expression::from(a1);
        let eb1 = Expression::from(b1.clone());
        let ec1 = Expression::from(c1.clone());

        let a2 = 5.0f64;
        let b2 = vec![a2; 8];
        let mut hash2 = HashMap::new();
        hash2.insert(vec![3usize; 3], 2.0);
        hash2.insert(vec![1usize; 3], 3.0);
        hash2.insert(vec![2, 1, 1], 4.0);
        hash2.insert(vec![5usize; 3], 2.0);
        let c2 = SparseTensor::from(vec![6usize; 3], hash2).unwrap();

        let ea2 = Expression::from(a2);
        let eb2 = Expression::from(b2.clone());
        let ec2 = Expression::from(c2.clone());

        let ea = ea1 * ea2;
        let eb = eb1 * eb2;
        let ec = ec1 * ec2;

        let a = Expression::from(a1 * a2);
        let b = Expression::from(
            b1.iter()
                .enumerate()
                .map(|(i, j)| j * b2[i])
                .collect::<Vec<f64>>(),
        );
        let c = Expression::from(c1 * c2);

        assert_eq!(ea, a);
        assert_eq!(eb, b);
        assert_eq!(ec, c);
    }
}