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
pub mod assign;
pub mod differential;
pub mod operations;
pub mod size;
pub mod tex_code;
pub mod variable;

pub use assign::*;
pub use differential::*;
pub use operations::*;
use serde::{Deserialize, Serialize};
pub use size::*;
pub use tex_code::*;
pub use variable::*;

use crate::Expression;

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum MatrixExpression {
    T(Box<Expression>),
    Inv(Box<Expression>),
    Det(Box<Expression>),
}

impl Expression {
    pub fn matrix(self) -> Option<MatrixExpression> {
        match self {
            Expression::Matrix(t) => Some(*t),
            _ => None,
        }
    }

    pub fn into_matrix(self) -> MatrixExpression {
        match self {
            Expression::Matrix(t) => *t,
            _ => panic!("The expression is not a matrix expression."),
        }
    }
}

// impl TensorExpression {
//     pub fn to_mat(self) -> MatrixExpression {
//         let sizes = self.sizes();
//         if sizes.len() != 2 {
//             panic!("The rank of the argument must be 2.");
//         }

//         if let TensorExpression::Constant(v) = self {
//             return MatrixExpression::Constant(v.to_mat()).into();
//         }

//         MatrixExpression::Mat(self.into())
//     }
// }

impl From<MatrixExpression> for Expression {
    fn from(m: MatrixExpression) -> Self {
        Expression::Matrix(m.into())
    }
}

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

    use opensrdk_linear_algebra::Matrix;

    use crate::{Expression, MatrixExpression};

    #[test]
    fn it_works() {
        let len = 3usize;
        let a = Matrix::from(len, vec![1.0, 3.0, 4.0, 0.0, 1.0, 0.0, 0.0, 0.0, 3.0]).unwrap();
        let ea = Expression::from(a.clone());

        let a_t = a.clone().t();
        let ea_t = ea.clone().t();

        assert_eq!(Expression::from(a_t), ea_t);

        let a_inv = a.clone().getrf().unwrap().getri().unwrap();
        let ea_inv = ea.clone().inv();

        assert_eq!(Expression::from(a_inv), ea_inv);

        let a_det = a.clone().getrf().unwrap().0.trdet();
        let ea_det = ea.clone().det();

        assert_eq!(Expression::from(a_det), ea_det);
    }
}