Skip to content

Commit

Permalink
solve an equation system (wip)
Browse files Browse the repository at this point in the history
nalgebra seems not to work to solve 4x3 matrix?

ndarray c dependencies error
rust-ndarray/ndarray-linalg#373
  • Loading branch information
ivnsch committed Jun 16, 2024
1 parent 15395d0 commit 5ca4efc
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 19 deletions.
143 changes: 139 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ edition = "2021"
# bevy = "0.14.0-rc.2"
bevy = { version = "0.13.2" }
nalgebra = { version = "0.32" }
ndarray = "0.15.6"
ndarray-linalg = "0.16.0"
61 changes: 46 additions & 15 deletions src/alg.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,49 @@
#[cfg(test)]
use nalgebra::{Matrix3, Vector3};
mod tests {
use nalgebra::{Matrix3, Vector3};
use ndarray::{array, Array1, Array2};
use ndarray_linalg::Solve;

#[test]
fn multiply_vector_matrix() {
let v = Vector3::new(1, 2, 3);

#[rustfmt::skip]
let m = Matrix3::new(
11, 12, 13,
21, 22, 23,
31, 32, 33
);

let res = m * v;

assert_eq!(res, Vector3::new(74, 134, 194));
}

#[test]
fn solve_equations_system() {
let a: Array2<f64> = array![[3., 2., -1.], [2., -2., 4.], [-2., 1., -2.]];
let b: Array1<f64> = array![1., -2., 0.];
let x = a.solve_into(b).unwrap();
assert!(x.abs_diff_eq(&array![1., -2., -2.], 1e-9));

#[test]
fn multiply_vector_matrix() {
let v = Vector3::new(1, 2, 3);

#[rustfmt::skip]
let m = Matrix3::new(
11, 12, 13,
21, 22, 23,
31, 32, 33
);

let res = m * v;

assert_eq!(res, Vector3::new(74, 134, 194));
// // Define your 4x3 matrix A and 4-dimensional vector b
// #[rustfmt::skip]
// let a = Array2::from_shape_vec((4, 3), vec![
// 1.0, 2.0, 3.0,
// 4.0, 5.0, 6.0,
// 7.0, 8.0, 9.0,
// 10.0, 11.0, 12.0,
// ]).unwrap();

// let b = Array1::from(vec![10.0, 20.0, 30.0, 40.0]);

// // Solve the equation Ax = b
// // a.solv
// match a.solve_into(b) {
// Ok(x) => println!("Solution x: {:?}", x),
// Err(e) => println!("Error: {}", e),
// }
}

}

0 comments on commit 5ca4efc

Please sign in to comment.