rename solve to decompose and rearrange stuff

master
Andrew Cassidy 1 year ago
parent 543769f691
commit bddc3e1977

@ -2,18 +2,16 @@ use crate::util::checked_inv;
use crate::Matrix;
use crate::Vector;
use num_traits::real::Real;
use num_traits::{One, Zero};
use std::iter::{Product, Sum};
use std::ops::Index;
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct LUDecomp<T: Copy, const N: usize> {
pub struct LUDecomposition<T: Copy, const N: usize> {
pub lu: Matrix<T, N, N>,
pub idx: Vector<usize, N>,
pub parity: T,
}
impl<T: Copy + Default, const N: usize> LUDecomp<T, N>
impl<T: Copy + Default, const N: usize> LUDecomposition<T, N>
where
T: Real + Default + Sum + Product,
{
@ -133,12 +131,12 @@ where
}
}
pub trait LUSolve<T, const N: usize>
pub trait LUDecomposable<T, const N: usize>
where
T: Copy + Default + Real + Product + Sum,
{
#[must_use]
fn lu(&self) -> Option<LUDecomp<T, N>>;
fn lu(&self) -> Option<LUDecomposition<T, N>>;
#[must_use]
fn inverse(&self) -> Option<Matrix<T, N, N>>;
@ -147,6 +145,57 @@ where
fn det(&self) -> T;
#[must_use]
fn solve<const M: usize>(&self, b: &Matrix<T, N, M>) -> Option<Matrix<T, N, M>>;
}
impl<T, const N: usize> LUDecomposable<T, N> for Matrix<T, N, N>
where
T: Copy + Default + Real + Sum + Product,
{
fn lu(&self) -> Option<LUDecomposition<T, N>> {
LUDecomposition::decompose(self)
}
fn inverse(&self) -> Option<Matrix<T, N, N>> {
match N {
1 => Some(Self::fill(checked_inv(self[0])?)),
2 => {
let mut result = Self::default();
result[(0, 0)] = self[(1, 1)];
result[(1, 1)] = self[(0, 0)];
result[(1, 0)] = -self[(1, 0)];
result[(0, 1)] = -self[(0, 1)];
Some(result * checked_inv(self.det())?)
}
_ => Some(self.lu()?.inverse()),
}
}
fn det(&self) -> T {
match N {
1 => self[0],
2 => (self[(0, 0)] * self[(1, 1)]) - (self[(0, 1)] * self[(1, 0)]),
3 => {
// use rule of Sarrus
(0..N) // starting column
.map(|i| {
let dn = (0..N)
.map(|j| -> T { self[(j, (j + i) % N)] })
.product::<T>();
let up = (0..N)
.map(|j| -> T { self[(N - j - 1, (j + i) % N)] })
.product::<T>();
dn - up
})
.sum::<T>()
}
_ => {
// use LU decomposition
self.lu().map_or(T::zero(), |lu| lu.det())
}
}
}
fn solve<const M: usize>(&self, b: &Matrix<T, N, M>) -> Option<Matrix<T, N, M>> {
Some(self.lu()?.solve(b))
}

@ -1,9 +1,9 @@
extern crate core;
pub mod decompose;
pub mod index;
mod macros;
mod matrix;
pub mod solve;
mod util;
pub use matrix::{Matrix, Vector};

@ -1,13 +1,10 @@
use crate::impl_matrix_op;
use crate::index::Index2D;
use crate::util::checked_inv;
use num_traits::real::Real;
use num_traits::{Num, NumOps, One, Zero};
use std::fmt::Debug;
use std::iter::{zip, Flatten, Product, Sum};
use crate::solve::{LUDecomp, LUSolve};
use std::ops::{Add, AddAssign, Deref, DerefMut, Index, IndexMut, Mul, MulAssign, Neg};
/// A 2D array of values which can be operated upon.
@ -401,55 +398,6 @@ impl<T: Copy, const N: usize> Matrix<T, N, N> {
}
}
impl<T, const N: usize> LUSolve<T, N> for Matrix<T, N, N>
where
T: Copy + Default + Real + Sum + Product,
{
fn lu(&self) -> Option<LUDecomp<T, N>> {
LUDecomp::decompose(self)
}
fn inverse(&self) -> Option<Matrix<T, N, N>> {
match N {
1 => Some(Self::fill(checked_inv(self[0])?)),
2 => {
let mut result = Self::default();
result[(0, 0)] = self[(1, 1)];
result[(1, 1)] = self[(0, 0)];
result[(1, 0)] = -self[(1, 0)];
result[(0, 1)] = -self[(0, 1)];
Some(result * checked_inv(self.det())?)
}
_ => Some(self.lu()?.inverse()),
}
}
fn det(&self) -> T {
match N {
1 => self[0],
2 => (self[(0, 0)] * self[(1, 1)]) - (self[(0, 1)] * self[(1, 0)]),
3 => {
// use rule of Sarrus
(0..N) // starting column
.map(|i| {
let dn = (0..N)
.map(|j| -> T { self[(j, (j + i) % N)] })
.product::<T>();
let up = (0..N)
.map(|j| -> T { self[(N - j - 1, (j + i) % N)] })
.product::<T>();
dn - up
})
.sum::<T>()
}
_ => {
// use LU decomposition
self.lu().map_or(T::zero(), |lu| lu.det())
}
}
}
}
// Index
impl<I, T, const M: usize, const N: usize> Index<I> for Matrix<T, M, N>
where

@ -1,4 +1,4 @@
use num_traits::{Num, NumOps, One, Zero};
use num_traits::{Num, One, Zero};
use std::ops::Div;
pub fn checked_div<L: Num + Div<R, Output = T>, R: Num + Zero, T>(num: L, den: R) -> Option<T> {

@ -29,10 +29,6 @@ impl<T: Copy + Approx, const M: usize, const N: usize> Approx for Matrix<T, M, N
}
}
pub fn approx<T: Approx>(left: &T, right: &T) -> bool {
T::approx(left, right)
}
macro_rules! assert_approx {
($left:expr, $right:expr $(,)?) => {
match (&$left, &$right) {

@ -6,8 +6,8 @@ use generic_parameterize::parameterize;
use num_traits::real::Real;
use num_traits::Zero;
use std::fmt::Debug;
use std::iter::{zip, Product, Sum};
use vector_victor::solve::{LUDecomp, LUSolve};
use std::iter::{Product, Sum};
use vector_victor::decompose::{LUDecomposable, LUDecomposition};
use vector_victor::{Matrix, Vector};
#[parameterize(S = (f32, f64), M = [1,2,3,4])]
@ -19,7 +19,7 @@ fn test_lu_identity<S: Default + Approx + Real + Debug + Product + Sum, const M:
let i = Matrix::<S, M, M>::identity();
let ones = Vector::<S, M>::fill(S::one());
let decomp = i.lu().expect("Singular matrix encountered");
let LUDecomp { lu, idx, parity } = decomp;
let LUDecomposition { lu, idx, parity } = decomp;
assert_eq!(lu, i, "Incorrect LU decomposition");
assert!(
(0..M).eq(idx.elements().cloned()),

@ -1,14 +1,10 @@
#[macro_use]
mod common;
use common::Approx;
use generic_parameterize::parameterize;
use num_traits::real::Real;
use num_traits::Zero;
use std::fmt::Debug;
use std::iter::{zip, Product, Sum};
use std::ops;
use vector_victor::{Matrix, Vector};
use vector_victor::Matrix;
#[parameterize(S = (i32, f32, f64, u32), M = [1,4], N = [1,4])]
#[test]

Loading…
Cancel
Save