Files
rmath/examples/benchmark.rs
2024-01-27 20:53:15 +08:00

42 lines
1.2 KiB
Rust

use std::env;
use num_bigint::BigInt;
use num_traits::One;
use num_traits::Zero;
use rmath::HMatrix;
use rmath::Rational;
use rmath::NumericalMatrix;
use rand::rngs::StdRng;
use rand::SeedableRng;
use rand::RngCore;
fn main( ) {
let size = env::args().skip(1).map(|it| {
it.parse::<u32>().unwrap()
}).take(1).collect::<Vec<u32>>()[0];
let mut rand = {
let seed = [
1,0,1,3,
2,5,0,0,
200,1,0,0,
210,30,0,0,
78,134,31,0,
253,11,7,0,
120,169,89,48,
200,0,202,0
];
StdRng::from_seed(seed)
};
let mtx = HMatrix::<Rational<BigInt>>::new(
usize::try_from(size).unwrap(),usize::try_from(size).unwrap(), |_| {
Rational::new(BigInt::from(rand.next_u32() % (size * 20)) - size * 10, BigInt::one())
});
let b = HMatrix::<Rational<BigInt>>::new(
usize::try_from(size).unwrap(),usize::try_from(size).unwrap(), |_| {
Rational::new(BigInt::from(rand.next_u32() % (size * 10)) - (size * 5), BigInt::one())
});
let lu = mtx.clone().lu().unwrap();
let x = lu.solve(&b).unwrap();
assert!((mtx * x - b).is_zero());
}