Skip to content

Commit

Permalink
CHGE: Fix all errors in Generic LinearAlgebra
Browse files Browse the repository at this point in the history
  • Loading branch information
Axect committed Oct 24, 2024
1 parent d2a93b4 commit cae9177
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 61 deletions.
6 changes: 3 additions & 3 deletions src/fuga/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ pub use crate::traits::{
fp::{FPMatrix, FPVector},
general::Algorithm,
math::{InnerProduct, LinearOp, MatrixProduct, Norm, Normed, Vector, VectorProduct},
matrix::MatrixTrait,
matrix::{MatrixTrait, LinearAlgebra},
mutable::{MutFP, MutMatrix},
num::Real,
pointer::{MatrixPtr, Oxide, Redox, RedoxCommon},
Expand Down Expand Up @@ -234,11 +234,11 @@ pub use crate::numerical::integral::Integral::{
pub use crate::statistics::stat::QType::{
Type1, Type2, Type3, Type4, Type5, Type6, Type7, Type8, Type9,
};
pub use crate::structure::matrix::{
pub use crate::traits::matrix::{
Form::{Diagonal, Identity},
SolveKind::{LU, WAZ},
UPLO::{Upper, Lower}
};
pub use crate::structure::matrix::UPLO::{Upper, Lower};
pub use crate::structure::dataframe::DType::*;
pub use crate::structure::ad::AD::*;
pub use crate::numerical::spline::SlopeMethod::{Akima, Quadratic};
Expand Down
2 changes: 1 addition & 1 deletion src/numerical/newton.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::numerical::utils::jacobian;
use crate::structure::matrix::*;
use crate::structure::ad::*;
use crate::traits::{
math::{Norm, Normed, Vector},
mutable::MutFP,
matrix::LinearAlgebra,
};

/// Newton-Raphson Method
Expand Down
4 changes: 2 additions & 2 deletions src/numerical/optimize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@
pub use self::OptMethod::{GaussNewton, GradientDescent, LevenbergMarquardt};
use self::OptOption::{InitParam, MaxIter};
use crate::numerical::utils::jacobian;
use crate::structure::matrix::{LinearAlgebra, Matrix};
use crate::traits::matrix::MatrixTrait;
use crate::structure::matrix::Matrix;
use crate::traits::matrix::{MatrixTrait, LinearAlgebra};
use crate::structure::ad::{AD, ADVec};
use crate::util::useful::max;
use std::collections::HashMap;
Expand Down
1 change: 1 addition & 0 deletions src/numerical/spline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ use crate::structure::matrix::*;
use crate::structure::polynomial::*;
#[allow(unused_imports)]
use crate::structure::vector::*;
use crate::traits::matrix::LinearAlgebra;
#[allow(unused_imports)]
use crate::util::non_macro::*;
use crate::util::useful::zip_range;
Expand Down
4 changes: 2 additions & 2 deletions src/prelude/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ pub use crate::traits::{
fp::{FPMatrix, FPVector},
general::Algorithm,
math::{InnerProduct, LinearOp, MatrixProduct, Vector, VectorProduct},
matrix::MatrixTrait,
matrix::{MatrixTrait, PQLU, QR, WAZD},
mutable::{MutFP, MutMatrix},
num::Real,
pointer::{MatrixPtr, Oxide, Redox, RedoxCommon},
Expand All @@ -170,7 +170,7 @@ pub use crate::structure::{
ad::AD::*,
matrix::{
combine, diag, gemm, gemv, gen_householder, inv_l, inv_u, matrix, ml_matrix, py_matrix,
r_matrix, Col, Matrix, Row, Shape, PQLU, QR, WAZD,
r_matrix, Col, Matrix, Row, Shape,
},
polynomial::{Polynomial,poly,Calculus,lagrange_polynomial,legendre_polynomial},
vector::*,
Expand Down
79 changes: 40 additions & 39 deletions src/prelude/simpler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ use crate::numerical::{
};
#[cfg(feature = "parquet")]
use crate::structure::dataframe::{DataFrame, WithParquet};
use crate::structure::matrix::{self, Matrix};
use crate::structure::matrix::Matrix;
use crate::structure::polynomial;
use crate::traits::math::{Norm, Normed};
use crate::traits::matrix::{MatrixTrait, LinearAlgebra, PQLU, WAZD, QR, Form, SolveKind};
#[cfg(feature = "parquet")]
use arrow2::io::parquet::write::CompressionOptions;
#[cfg(feature = "parquet")]
Expand All @@ -28,22 +29,22 @@ pub fn integrate<F: Fn(f64) -> f64 + Copy>(f: F, (a, b): (f64, f64)) -> f64 {
}

/// Simple Linear algebra
pub trait SimplerLinearAlgebra {
fn back_subs(&self, b: &Vec<f64>) -> Vec<f64>;
fn forward_subs(&self, b: &Vec<f64>) -> Vec<f64>;
fn lu(&self) -> matrix::PQLU;
fn waz_diag(&self) -> Option<matrix::WAZD>;
fn waz(&self) -> Option<matrix::WAZD>;
fn qr(&self) -> matrix::QR;
pub trait SimplerLinearAlgebra<M:MatrixTrait> {
fn back_subs(&self, b: &[f64]) -> Vec<f64>;
fn forward_subs(&self, b: &[f64]) -> Vec<f64>;
fn lu(&self) -> PQLU<M>;
fn waz_diag(&self) -> Option<WAZD<M>>;
fn waz(&self) -> Option<WAZD<M>>;
fn qr(&self) -> QR<M>;
#[cfg(feature = "O3")]
fn cholesky(&self) -> Matrix;
fn rref(&self) -> Matrix;
fn cholesky(&self) -> M;
fn rref(&self) -> M;
fn det(&self) -> f64;
fn block(&self) -> (Matrix, Matrix, Matrix, Matrix);
fn inv(&self) -> Matrix;
fn pseudo_inv(&self) -> Matrix;
fn solve(&self, b: &Vec<f64>) -> Vec<f64>;
fn solve_mat(&self, m: &Matrix) -> Matrix;
fn block(&self) -> (M, M, M, M);
fn inv(&self) -> M;
fn pseudo_inv(&self) -> M;
fn solve(&self, b: &[f64]) -> Vec<f64>;
fn solve_mat(&self, m: &M) -> M;
fn is_symmetric(&self) -> bool;
}

Expand Down Expand Up @@ -74,73 +75,73 @@ impl SimpleNorm for Matrix {
}
}

impl SimplerLinearAlgebra for Matrix {
fn back_subs(&self, b: &Vec<f64>) -> Vec<f64> {
matrix::LinearAlgebra::back_subs(self, b)
impl SimplerLinearAlgebra<Matrix> for Matrix {
fn back_subs(&self, b: &[f64]) -> Vec<f64> {
LinearAlgebra::back_subs(self, b)
}

fn forward_subs(&self, b: &Vec<f64>) -> Vec<f64> {
matrix::LinearAlgebra::forward_subs(self, b)
fn forward_subs(&self, b: &[f64]) -> Vec<f64> {
LinearAlgebra::forward_subs(self, b)
}

fn lu(&self) -> matrix::PQLU {
matrix::LinearAlgebra::lu(self)
fn lu(&self) -> PQLU<Matrix> {
LinearAlgebra::lu(self)
}

fn waz_diag(&self) -> Option<matrix::WAZD> {
matrix::LinearAlgebra::waz(self, matrix::Form::Diagonal)
fn waz_diag(&self) -> Option<WAZD<Matrix>> {
LinearAlgebra::waz(self, Form::Diagonal)
}

fn waz(&self) -> Option<matrix::WAZD> {
matrix::LinearAlgebra::waz(self, matrix::Form::Identity)
fn waz(&self) -> Option<WAZD<Matrix>> {
LinearAlgebra::waz(self, Form::Identity)
}

fn qr(&self) -> matrix::QR {
matrix::LinearAlgebra::qr(self)
fn qr(&self) -> QR<Matrix> {
LinearAlgebra::qr(self)
}

#[cfg(feature = "O3")]
fn cholesky(&self) -> Matrix {
matrix::LinearAlgebra::cholesky(self, matrix::UPLO::Lower)
LinearAlgebra::cholesky(self, UPLO::Lower)
}

fn rref(&self) -> Matrix {
matrix::LinearAlgebra::rref(self)
LinearAlgebra::rref(self)
}

fn det(&self) -> f64 {
matrix::LinearAlgebra::det(self)
LinearAlgebra::det(self)
}

fn block(&self) -> (Matrix, Matrix, Matrix, Matrix) {
matrix::LinearAlgebra::block(self)
LinearAlgebra::block(self)
}

fn inv(&self) -> Matrix {
matrix::LinearAlgebra::inv(self)
LinearAlgebra::inv(self)
}

fn pseudo_inv(&self) -> Matrix {
matrix::LinearAlgebra::pseudo_inv(self)
LinearAlgebra::pseudo_inv(self)
}

fn solve(&self, b: &Vec<f64>) -> Vec<f64> {
matrix::LinearAlgebra::solve(self, b, matrix::SolveKind::LU)
fn solve(&self, b: &[f64]) -> Vec<f64> {
LinearAlgebra::solve(self, b, SolveKind::LU)
}

fn solve_mat(&self, m: &Matrix) -> Matrix {
matrix::LinearAlgebra::solve_mat(self, m, matrix::SolveKind::LU)
LinearAlgebra::solve_mat(self, m, SolveKind::LU)
}

fn is_symmetric(&self) -> bool {
matrix::LinearAlgebra::is_symmetric(self)
LinearAlgebra::is_symmetric(self)
}
}

/// Simple solve
#[allow(non_snake_case)]
pub fn solve(A: &Matrix, m: &Matrix) -> Matrix {
matrix::solve(A, m, matrix::SolveKind::LU)
crate::traits::matrix::solve(A, m, SolveKind::LU)
}

/// Simple Chebyshev Polynomial (First Kind)
Expand Down
2 changes: 1 addition & 1 deletion src/statistics/stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ use std::fmt;
use self::QType::*;
//use crate::structure::dataframe::*;
use crate::structure::matrix::*;
use crate::traits::matrix::MatrixTrait;
use crate::traits::matrix::{MatrixTrait, LinearAlgebra};
use order_stat::kth_by;

/// Statistics Trait
Expand Down
8 changes: 4 additions & 4 deletions src/structure/matrix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3049,8 +3049,8 @@ impl LinearAlgebra<Matrix> for Matrix {
/// let (p,q,l,u) = (pqlu.p, pqlu.q, pqlu.l, pqlu.u);
/// assert_eq!(p, vec![1]); // swap 0 & 1 (Row)
/// assert_eq!(q, vec![1]); // swap 0 & 1 (Col)
/// assert_eq!(l, matrix(vec![1,0,0.5,1],2,2,Row));
/// assert_eq!(u, matrix(vec![4,3,0,-0.5],2,2,Row));
/// assert_eq!(l, matrix(vec![1.0,0.0,0.5,1.0],2,2,Row));
/// assert_eq!(u, matrix(vec![4.0,3.0,0.0,-0.5],2,2,Row));
/// }
/// ```
fn lu(&self) -> PQLU<Matrix> {
Expand Down Expand Up @@ -3563,7 +3563,7 @@ impl LinearAlgebra<Matrix> for Matrix {
SolveKind::LU => {
let lu = self.lu();
let (p, q, l, u) = lu.extract();
let mut v = b.clone();
let mut v = b.to_vec();
v.swap_with_perm(&p.into_iter().enumerate().collect());
let z = l.forward_subs(&v);
let mut y = u.back_subs(&z);
Expand All @@ -3575,7 +3575,7 @@ impl LinearAlgebra<Matrix> for Matrix {
None => panic!("Can't solve by WAZ with Singular matrix!"),
Some(obj) => obj,
};
let x = &wazd.w.t() * b;
let x = &wazd.w.t() * &b.to_vec();
let x = &wazd.z * &x;
x
}
Expand Down
19 changes: 10 additions & 9 deletions src/structure/sparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
//!
//! * Reference : Press, William H., and William T. Vetterling. *Numerical Recipes.* Cambridge: Cambridge Univ. Press, 2007.
use crate::structure::matrix::{Form, LinearAlgebra, Matrix, SolveKind, PQLU, QR, WAZD, SVD};
use crate::structure::matrix::Matrix;
use crate::traits::matrix::{Form, LinearAlgebra, SolveKind, PQLU, QR, WAZD, SVD};
use crate::traits::math::LinearOp;
//use crate::traits::math::{InnerProduct, LinearOp, Norm, Normed, Vector};
use crate::util::non_macro::zeros;
Expand Down Expand Up @@ -138,28 +139,28 @@ impl LinearOp<Vec<f64>, Vec<f64>> for SPMatrix {
/// Linear algebra for sparse matrix
///
/// **Caution** : In every ops in this trait, there is converting process to dense matrix
impl LinearAlgebra for SPMatrix {
fn back_subs(&self, _b: &Vec<f64>) -> Vec<f64> {
impl LinearAlgebra<Matrix> for SPMatrix {
fn back_subs(&self, _b: &[f64]) -> Vec<f64> {
unimplemented!()
}

fn forward_subs(&self, _b: &Vec<f64>) -> Vec<f64> {
fn forward_subs(&self, _b: &[f64]) -> Vec<f64> {
unimplemented!()
}

fn lu(&self) -> PQLU {
fn lu(&self) -> PQLU<Matrix> {
self.to_dense().lu()
}

fn waz(&self, _d_form: Form) -> Option<WAZD> {
fn waz(&self, _d_form: Form) -> Option<WAZD<Matrix>> {
unimplemented!()
}

fn qr(&self) -> QR {
fn qr(&self) -> QR<Matrix> {
self.to_dense().qr()
}

fn svd(&self) -> SVD {
fn svd(&self) -> SVD<Matrix> {
unimplemented!()
}

Expand Down Expand Up @@ -188,7 +189,7 @@ impl LinearAlgebra for SPMatrix {
self.to_dense().pseudo_inv()
}

fn solve(&self, _b: &Vec<f64>, _sk: SolveKind) -> Vec<f64> {
fn solve(&self, _b: &[f64], _sk: SolveKind) -> Vec<f64> {
unimplemented!()
}

Expand Down

0 comments on commit cae9177

Please sign in to comment.