mirror of
https://github.com/drewcassidy/vector-victor.git
synced 2024-09-01 14:58:35 +00:00
rename solve to decompose and rearrange stuff
This commit is contained in:
parent
543769f691
commit
bddc3e1977
@ -2,18 +2,16 @@ use crate::util::checked_inv;
|
|||||||
use crate::Matrix;
|
use crate::Matrix;
|
||||||
use crate::Vector;
|
use crate::Vector;
|
||||||
use num_traits::real::Real;
|
use num_traits::real::Real;
|
||||||
use num_traits::{One, Zero};
|
|
||||||
use std::iter::{Product, Sum};
|
use std::iter::{Product, Sum};
|
||||||
use std::ops::Index;
|
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, PartialEq)]
|
#[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 lu: Matrix<T, N, N>,
|
||||||
pub idx: Vector<usize, N>,
|
pub idx: Vector<usize, N>,
|
||||||
pub parity: T,
|
pub parity: T,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Copy + Default, const N: usize> LUDecomp<T, N>
|
impl<T: Copy + Default, const N: usize> LUDecomposition<T, N>
|
||||||
where
|
where
|
||||||
T: Real + Default + Sum + Product,
|
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
|
where
|
||||||
T: Copy + Default + Real + Product + Sum,
|
T: Copy + Default + Real + Product + Sum,
|
||||||
{
|
{
|
||||||
#[must_use]
|
#[must_use]
|
||||||
fn lu(&self) -> Option<LUDecomp<T, N>>;
|
fn lu(&self) -> Option<LUDecomposition<T, N>>;
|
||||||
|
|
||||||
#[must_use]
|
#[must_use]
|
||||||
fn inverse(&self) -> Option<Matrix<T, N, N>>;
|
fn inverse(&self) -> Option<Matrix<T, N, N>>;
|
||||||
@ -147,6 +145,57 @@ where
|
|||||||
fn det(&self) -> T;
|
fn det(&self) -> T;
|
||||||
|
|
||||||
#[must_use]
|
#[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>> {
|
fn solve<const M: usize>(&self, b: &Matrix<T, N, M>) -> Option<Matrix<T, N, M>> {
|
||||||
Some(self.lu()?.solve(b))
|
Some(self.lu()?.solve(b))
|
||||||
}
|
}
|
@ -1,9 +1,9 @@
|
|||||||
extern crate core;
|
extern crate core;
|
||||||
|
|
||||||
|
pub mod decompose;
|
||||||
pub mod index;
|
pub mod index;
|
||||||
mod macros;
|
mod macros;
|
||||||
mod matrix;
|
mod matrix;
|
||||||
pub mod solve;
|
|
||||||
mod util;
|
mod util;
|
||||||
|
|
||||||
pub use matrix::{Matrix, Vector};
|
pub use matrix::{Matrix, Vector};
|
||||||
|
@ -1,13 +1,10 @@
|
|||||||
use crate::impl_matrix_op;
|
use crate::impl_matrix_op;
|
||||||
use crate::index::Index2D;
|
use crate::index::Index2D;
|
||||||
use crate::util::checked_inv;
|
|
||||||
|
|
||||||
use num_traits::real::Real;
|
|
||||||
use num_traits::{Num, NumOps, One, Zero};
|
use num_traits::{Num, NumOps, One, Zero};
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
use std::iter::{zip, Flatten, Product, Sum};
|
use std::iter::{zip, Flatten, Product, Sum};
|
||||||
|
|
||||||
use crate::solve::{LUDecomp, LUSolve};
|
|
||||||
use std::ops::{Add, AddAssign, Deref, DerefMut, Index, IndexMut, Mul, MulAssign, Neg};
|
use std::ops::{Add, AddAssign, Deref, DerefMut, Index, IndexMut, Mul, MulAssign, Neg};
|
||||||
|
|
||||||
/// A 2D array of values which can be operated upon.
|
/// 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
|
// Index
|
||||||
impl<I, T, const M: usize, const N: usize> Index<I> for Matrix<T, M, N>
|
impl<I, T, const M: usize, const N: usize> Index<I> for Matrix<T, M, N>
|
||||||
where
|
where
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use num_traits::{Num, NumOps, One, Zero};
|
use num_traits::{Num, One, Zero};
|
||||||
use std::ops::Div;
|
use std::ops::Div;
|
||||||
|
|
||||||
pub fn checked_div<L: Num + Div<R, Output = T>, R: Num + Zero, T>(num: L, den: R) -> Option<T> {
|
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 {
|
macro_rules! assert_approx {
|
||||||
($left:expr, $right:expr $(,)?) => {
|
($left:expr, $right:expr $(,)?) => {
|
||||||
match (&$left, &$right) {
|
match (&$left, &$right) {
|
||||||
|
@ -6,8 +6,8 @@ use generic_parameterize::parameterize;
|
|||||||
use num_traits::real::Real;
|
use num_traits::real::Real;
|
||||||
use num_traits::Zero;
|
use num_traits::Zero;
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
use std::iter::{zip, Product, Sum};
|
use std::iter::{Product, Sum};
|
||||||
use vector_victor::solve::{LUDecomp, LUSolve};
|
use vector_victor::decompose::{LUDecomposable, LUDecomposition};
|
||||||
use vector_victor::{Matrix, Vector};
|
use vector_victor::{Matrix, Vector};
|
||||||
|
|
||||||
#[parameterize(S = (f32, f64), M = [1,2,3,4])]
|
#[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 i = Matrix::<S, M, M>::identity();
|
||||||
let ones = Vector::<S, M>::fill(S::one());
|
let ones = Vector::<S, M>::fill(S::one());
|
||||||
let decomp = i.lu().expect("Singular matrix encountered");
|
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_eq!(lu, i, "Incorrect LU decomposition");
|
||||||
assert!(
|
assert!(
|
||||||
(0..M).eq(idx.elements().cloned()),
|
(0..M).eq(idx.elements().cloned()),
|
@ -1,14 +1,10 @@
|
|||||||
#[macro_use]
|
#[macro_use]
|
||||||
mod common;
|
mod common;
|
||||||
|
|
||||||
use common::Approx;
|
|
||||||
use generic_parameterize::parameterize;
|
use generic_parameterize::parameterize;
|
||||||
use num_traits::real::Real;
|
|
||||||
use num_traits::Zero;
|
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
use std::iter::{zip, Product, Sum};
|
|
||||||
use std::ops;
|
use std::ops;
|
||||||
use vector_victor::{Matrix, Vector};
|
use vector_victor::Matrix;
|
||||||
|
|
||||||
#[parameterize(S = (i32, f32, f64, u32), M = [1,4], N = [1,4])]
|
#[parameterize(S = (i32, f32, f64, u32), M = [1,4], N = [1,4])]
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
Reference in New Issue
Block a user