opensrdk_symbolic_computation/expression/matrix_expression/operations/
det.rs

1use std::collections::HashMap;
2
3use opensrdk_linear_algebra::Matrix;
4
5use crate::{BracketsLevel, ConstantValue, Expression, MatrixExpression};
6
7impl Expression {
8    pub fn det(self) -> Expression {
9        if let Expression::Constant(v) = self {
10            let det = |v: Matrix| v.getrf().unwrap().0.trdet().into();
11            return match v {
12                ConstantValue::Scalar(v) => v.abs().into(),
13                ConstantValue::Tensor(v) => det(v.reduce_1dimension_rank().to_mat()),
14                ConstantValue::Matrix(v) => return det(v),
15            };
16        }
17
18        MatrixExpression::Det(self.into()).into()
19    }
20}
21
22impl MatrixExpression {
23    pub(crate) fn diff_det(v: &Expression, symbols: &[&str]) -> Vec<Expression> {
24        v.differential(symbols)
25            .into_iter()
26            .map(|d_v_d_symbol| {
27                let v_det = v.clone().det();
28                let v_inv_t = v.clone().inv().t();
29                let d_v_det_d_v = v_det * v_inv_t;
30
31                d_v_det_d_v.dot(d_v_d_symbol, &[[0, 0], [1, 1]])
32            })
33            .collect()
34    }
35
36    pub(crate) fn tex_code_det(v: &Expression, symbols: &HashMap<&str, &str>) -> String {
37        format!(
38            r"\left\|{}\right\|",
39            v._tex_code(symbols, BracketsLevel::None)
40        )
41    }
42}