Solve by Gauss-Jordan elimination
This commit is contained in:
commit
fb878327db
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
|||
/target
|
59
Cargo.lock
generated
Normal file
59
Cargo.lock
generated
Normal file
|
@ -0,0 +1,59 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
|
||||
|
||||
[[package]]
|
||||
name = "gauss-jordan"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"num-bigint",
|
||||
"num-rational",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-bigint"
|
||||
version = "0.4.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f93ab6289c7b344a8a9f60f88d80aa20032336fe78da341afc91c8a2341fc75f"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-integer"
|
||||
version = "0.1.44"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d2cc698a63b549a70bc047073d2949cce27cd1c7b0a4a862d08a8031bc2801db"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-rational"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d41702bd167c2df5520b384281bc111a4b5efcf7fbc4c9c222c815b07e0a6a6a"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"num-bigint",
|
||||
"num-integer",
|
||||
"num-traits",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-traits"
|
||||
version = "0.2.14"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
8
Cargo.toml
Normal file
8
Cargo.toml
Normal file
|
@ -0,0 +1,8 @@
|
|||
[package]
|
||||
name = "gauss-jordan"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
num-rational = "0.4"
|
||||
num-bigint = "0.4"
|
70
src/main.rs
Normal file
70
src/main.rs
Normal file
|
@ -0,0 +1,70 @@
|
|||
use num_bigint::BigInt;
|
||||
use num_rational::{BigRational, Ratio};
|
||||
|
||||
type Matrix = Vec<Vec<BigRational>>;
|
||||
|
||||
fn main() {
|
||||
let mut a: Matrix = vec![
|
||||
vec![
|
||||
Ratio::from_float::<f64>(10.0).unwrap(),
|
||||
Ratio::from_float::<f64>(7.0).unwrap(),
|
||||
Ratio::from_float::<f64>(8.1).unwrap(),
|
||||
Ratio::from_float::<f64>(7.2).unwrap(),
|
||||
Ratio::from_float::<f64>(32.0).unwrap(),
|
||||
],
|
||||
vec![
|
||||
Ratio::from_float::<f64>(7.08).unwrap(),
|
||||
Ratio::from_float::<f64>(5.04).unwrap(),
|
||||
Ratio::from_float::<f64>(6.0).unwrap(),
|
||||
Ratio::from_float::<f64>(5.0).unwrap(),
|
||||
Ratio::from_float::<f64>(23.0).unwrap(),
|
||||
],
|
||||
vec![
|
||||
Ratio::from_float::<f64>(8.0).unwrap(),
|
||||
Ratio::from_float::<f64>(5.98).unwrap(),
|
||||
Ratio::from_float::<f64>(9.89).unwrap(),
|
||||
Ratio::from_float::<f64>(9.0).unwrap(),
|
||||
Ratio::from_float::<f64>(33.0).unwrap(),
|
||||
],
|
||||
vec![
|
||||
Ratio::from_float::<f64>(6.99).unwrap(),
|
||||
Ratio::from_float::<f64>(4.99).unwrap(),
|
||||
Ratio::from_float::<f64>(9.0).unwrap(),
|
||||
Ratio::from_float::<f64>(9.98).unwrap(),
|
||||
Ratio::from_float::<f64>(31.0).unwrap(),
|
||||
],
|
||||
];
|
||||
|
||||
for i in 0..a.len() {
|
||||
for j in i..a.len() {
|
||||
let divisor = &a[j][i];
|
||||
a[j] = a[j].iter().map(|element| element / divisor).collect();
|
||||
}
|
||||
|
||||
for j in i + 1..a.len() {
|
||||
a[j] = a[j]
|
||||
.iter()
|
||||
.zip(&a[i])
|
||||
.map(|(element, previous)| element - previous)
|
||||
.collect();
|
||||
}
|
||||
}
|
||||
|
||||
for i in (0..a.len()).rev() {
|
||||
for j in (0..i).rev() {
|
||||
let multiplicator = &a[j][i];
|
||||
a[j] = a[j]
|
||||
.iter()
|
||||
.zip(&a[i])
|
||||
.map(|(element, subtractor)| element - subtractor * multiplicator)
|
||||
.collect();
|
||||
}
|
||||
}
|
||||
|
||||
let b: Vec<Vec<BigInt>> = a
|
||||
.iter()
|
||||
.map(|row| row.iter().map(|element| element.to_integer()).collect())
|
||||
.collect();
|
||||
|
||||
println!("result: {:?}", b);
|
||||
}
|
Loading…
Reference in a new issue