From 5ca4efc7c9aa3889cba5823ddc698004baeda826 Mon Sep 17 00:00:00 2001 From: Ivan Schuetz Date: Sun, 16 Jun 2024 09:15:05 +0200 Subject: [PATCH] solve an equation system (wip) nalgebra seems not to work to solve 4x3 matrix? ndarray c dependencies error https://github.com/rust-ndarray/ndarray-linalg/issues/373 --- Cargo.lock | 143 +++++++++++++++++++++++++++++++++++++++++++++++++++-- Cargo.toml | 2 + src/alg.rs | 61 +++++++++++++++++------ 3 files changed, 187 insertions(+), 19 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3fac7e1..0cff239 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -169,6 +169,15 @@ dependencies = [ "libc", ] +[[package]] +name = "approx" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f2a05fd1bd10b2527e20a2cd32d8873d115b8b39fe219ee25f42a8aca6ba278" +dependencies = [ + "num-traits", +] + [[package]] name = "approx" version = "0.5.1" @@ -1017,7 +1026,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aa66539aa93d8522b146bf82de429714ea6370a6061fc1f1ff7bcacd4e64c6c4" dependencies = [ "accesskit_winit", - "approx", + "approx 0.5.1", "bevy_a11y", "bevy_app", "bevy_derive", @@ -1207,6 +1216,27 @@ dependencies = [ "thiserror", ] +[[package]] +name = "cauchy" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ff11ddd2af3b5e80dd0297fee6e56ac038d9bdc549573cdb51bd6d2efe7f05e" +dependencies = [ + "num-complex", + "num-traits", + "rand", + "serde", +] + +[[package]] +name = "cblas-sys" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6feecd82cce51b0204cf063f0041d69f24ce83f680d87514b004248e7b0fa65" +dependencies = [ + "libc", +] + [[package]] name = "cc" version = "1.0.99" @@ -1870,7 +1900,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cc32c2334f00ca5ac3695c5009ae35da21da8c62d255b5b96d56e2597a637a38" dependencies = [ "ab_glyph", - "approx", + "approx 0.5.1", "xi-unicode", ] @@ -2115,6 +2145,17 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "katexit" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb1304c448ce2c207c2298a34bc476ce7ae47f63c23fa2b498583b26be9bc88c" +dependencies = [ + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "khronos-egl" version = "6.0.0" @@ -2141,6 +2182,28 @@ dependencies = [ "bitflags 1.3.2", ] +[[package]] +name = "lapack-sys" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "447f56c85fb410a7a3d36701b2153c1018b1d2b908c5fbaf01c1b04fac33bcbe" +dependencies = [ + "libc", +] + +[[package]] +name = "lax" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f96a229d9557112e574164f8024ce703625ad9f88a90964c1780809358e53da" +dependencies = [ + "cauchy", + "katexit", + "lapack-sys", + "num-traits", + "thiserror", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -2217,6 +2280,8 @@ version = "0.1.0" dependencies = [ "bevy", "nalgebra", + "ndarray", + "ndarray-linalg", ] [[package]] @@ -2362,7 +2427,7 @@ version = "0.32.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7b5c17de023a86f59ed79891b2e5d5a94c705dbe904a5b5c9c952ea6221b03e4" dependencies = [ - "approx", + "approx 0.5.1", "matrixmultiply", "nalgebra-macros", "num-complex", @@ -2383,6 +2448,38 @@ dependencies = [ "syn 1.0.109", ] +[[package]] +name = "ndarray" +version = "0.15.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "adb12d4e967ec485a5f71c6311fe28158e9d6f4bc4a447b474184d0f91a8fa32" +dependencies = [ + "approx 0.4.0", + "cblas-sys", + "libc", + "matrixmultiply", + "num-complex", + "num-integer", + "num-traits", + "rawpointer", +] + +[[package]] +name = "ndarray-linalg" +version = "0.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b0e8dda0c941b64a85c5deb2b3e0144aca87aced64678adfc23eacea6d2cc42" +dependencies = [ + "cauchy", + "katexit", + "lax", + "ndarray", + "num-complex", + "num-traits", + "rand", + "thiserror", +] + [[package]] name = "ndk" version = "0.8.0" @@ -2467,6 +2564,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "73f88a1307638156682bada9d7604135552957b7818057dcef22705b4d509495" dependencies = [ "num-traits", + "rand", + "serde", ] [[package]] @@ -2769,6 +2868,12 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "ppv-lite86" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de" + [[package]] name = "presser" version = "0.3.1" @@ -2814,6 +2919,36 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "17fd96390ed3feda12e1dfe2645ed587e0bea749e319333f104a33ff62f77a0b" +[[package]] +name = "rand" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c" +dependencies = [ + "getrandom", +] + [[package]] name = "range-alloc" version = "0.1.3" @@ -3049,7 +3184,7 @@ version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "061507c94fc6ab4ba1c9a0305018408e312e17c041eb63bef8aa726fa33aceae" dependencies = [ - "approx", + "approx 0.5.1", "num-complex", "num-traits", "paste", diff --git a/Cargo.toml b/Cargo.toml index 42a1d92..456de71 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/alg.rs b/src/alg.rs index 0bcb7dc..a0e15e7 100644 --- a/src/alg.rs +++ b/src/alg.rs @@ -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 = array![[3., 2., -1.], [2., -2., 4.], [-2., 1., -2.]]; + let b: Array1 = 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), + // } + } + }