opensrdk_symbolic_computation/expression/matrix_expression/operations/
inv.rs1use std::collections::HashMap;
2
3use opensrdk_linear_algebra::Matrix;
4
5use crate::{BracketsLevel, ConstantValue, Expression, MatrixExpression};
6
7impl Expression {
8 pub fn inv(self) -> Expression {
9 if let Expression::Constant(v) = self {
10 let inv = |v: Matrix| v.getrf().unwrap().getri().unwrap().into();
11 return match v {
12 ConstantValue::Scalar(v) => v.abs().into(),
13 ConstantValue::Tensor(v) => inv(v.reduce_1dimension_rank().to_mat()),
14 ConstantValue::Matrix(v) => return inv(v),
15 };
16 }
17
18 MatrixExpression::Inv(self.into()).into()
19 }
20}
21
22impl MatrixExpression {
23 pub(crate) fn diff_inv(v: &Expression, symbols: &[&str]) -> Vec<Expression> {
24 v.differential(symbols)
25 .into_iter()
26 .map(|d_v_d_symbol| {
27 let v_inv = v.clone().inv();
28 let d_v_inv_d_v = -v_inv.clone().dot(v_inv, &[[1, 0]]);
29
30 d_v_inv_d_v.dot(d_v_d_symbol, &[[0, 0], [1, 1]])
31 })
32 .collect()
33 }
34
35 pub(crate) fn tex_code_inv(v: &Expression, symbols: &HashMap<&str, &str>) -> String {
36 format!(
37 r"{{{}^{{-1}}}}",
38 v._tex_code(symbols, BracketsLevel::ForOperation)
39 )
40 }
41}