Skip to content

Commit 39b3809

Browse files
Update main.rs
Implement command `inverse` of matrix.
1 parent 1953610 commit 39b3809

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/main.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1577,6 +1577,26 @@ impl Executor {
15771577
self.stack.push(Type::Matrix(transposed_data, (cols, rows)))
15781578
}
15791579

1580+
"inverse" => {
1581+
let (matrix, (rows, cols)) = self.pop_stack().get_matrix();
1582+
let matrix = nalgebra::DMatrix::from_row_slice(
1583+
rows,
1584+
cols,
1585+
&matrix.iter().map(|x| x.to_f64()).collect::<Vec<f64>>(),
1586+
);
1587+
let inversed_matrix = if let Some(i) = matrix.try_inverse() {
1588+
i
1589+
} else {
1590+
self.stack.push(Type::Error("no-inverse".to_string()));
1591+
return;
1592+
};
1593+
1594+
self.stack.push(Type::Matrix(
1595+
inversed_matrix.iter().map(|x| Fraction::new(*x)).collect(),
1596+
(inversed_matrix.nrows(), inversed_matrix.ncols()),
1597+
))
1598+
}
1599+
15801600
"sim-equation" => {
15811601
let (matrix, (rows, cols)) = self.pop_stack().get_matrix();
15821602
let constants: Vec<Fraction> = {

0 commit comments

Comments
 (0)