1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use crate::numerical::{
    eigen,
    eigen::{Eigen, EigenMethod::Jacobi},
    integral,
    integral::Integral::GaussLegendre,
};
use crate::structure::matrix::{self, Matrix};
use crate::traits::math::{Norm, Normed};

/// Simple Norm
pub trait SimpleNorm: Normed {
    fn norm(&self) -> Self::Scalar;
    fn normalize(&self) -> Self;
}

/// Simple integrate
pub fn integrate<F: Fn(f64) -> f64>(f: F, (a, b): (f64, f64)) -> f64 {
    integral::integrate(f, (a, b), GaussLegendre(15))
}

/// 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;
    fn rref(&self) -> Matrix;
    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;
}

/// Simple Eigenpair
pub fn eigen(m: &Matrix) -> Eigen {
    eigen::eigen(m, Jacobi)
}

/// Simple L2 norm
impl SimpleNorm for Vec<f64> {
    fn norm(&self) -> Self::Scalar {
        Normed::norm(self, Norm::L2)
    }

    fn normalize(&self) -> Self {
        Normed::normalize(self, Norm::L2)
    }
}

/// Simple Frobenius norm
impl SimpleNorm for Matrix {
    fn norm(&self) -> Self::Scalar {
        Normed::norm(self, Norm::F)
    }

    fn normalize(&self) -> Self {
        unimplemented!()
    }
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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