pub fn matrix_solve<T>(A: &Array2<T>, b: &Array1<T>) -> Array1<T>
where
    T: NdFloat,
{
    let matrix_dimension = A.nrows();
    // solve Ax = b for x, where A is a square matrix
    let (L, U, P) = LU_decomp(A);
    // first solve Ly = Pb
    let pivotized_b = P.dot(b);
    let mut y: Array1<T> = pivotized_b.clone();
    for i in 1..matrix_dimension {
        y[i] = y[i] - L.slice(s![i, 0..i]).dot(&y.slice(s![0..i]));
    }
    // then solve Ux = y
    let mut x: Array1<T> = y.clone();
    x[matrix_dimension - 1] =
        x[matrix_dimension - 1] / U[[matrix_dimension - 1, matrix_dimension - 1]];
    for i in (0..matrix_dimension - 1).rev() {
        x[i] = (x[i]
            - U.slice(s![i, i + 1..matrix_dimension])
                .dot(&x.slice(s![i + 1..matrix_dimension])))
            / U[[i, i]];
    }
    x
}
/// Solves linear equation A*x = b by LU decomposition
Solves linear equation A*x = b by LU decomposition
Solves linear equation A * x = b by LU decomposition
pub fn matrix_solve<T>(A: &Array2<T>, b: &Array1<T>) -> Array1<T>
where
    T: NdFloat,
{
    let matrix_dimension = A.nrows();
    // solve Ax = b for x, where A is a square matrix
    let (L, U, P) = LU_decomp(A);
    // first solve Ly = Pb
    let pivotized_b = P.dot(b);
    let mut y: Array1<T> = pivotized_b.clone();
    for i in 1..matrix_dimension {
        y[i] = y[i] - L.slice(s![i, 0..i]).dot(&y.slice(s![0..i]));
    }
    // then solve Ux = y
    let mut x: Array1<T> = y.clone();
    x[matrix_dimension - 1] =
        x[matrix_dimension - 1] / U[[matrix_dimension - 1, matrix_dimension - 1]];
    for i in (0..matrix_dimension - 1).rev() {
        x[i] = (x[i]
            - U.slice(s![i, i + 1..matrix_dimension])
                .dot(&x.slice(s![i + 1..matrix_dimension])))
            / U[[i, i]];
    }
    x
}
pub fn matrix_solve < T > ( A : & Array2 < T > , b : & Array1 < T > ) -> Array1 < T > where T : NdFloat , { let matrix_dimension = A . nrows ( ) ; // solve Ax = b for x, where A is a square matrix let ( L , U , P ) = LU_decomp ( A ) ; // first solve Ly = Pb let pivotized_b = P . dot ( b ) ; let mut y : Array1 < T > = pivotized_b . clone ( ) ; for i in 1 .. matrix_dimension { y [ i ] = y [ i ] - L . slice ( s ! [ i 0 i ] ) . dot ( & y . slice ( s ! [ 0 i ] ) ) ; } // then solve Ux = y let mut x : Array1 < T > = y . clone ( ) ; x [ matrix_dimension - 1 ] = x [ matrix_dimension - 1 ] / U [ [ matrix_dimension - 1 , matrix_dimension - 1 ] ] ; for i in ( 0 .. matrix_dimension - 1 ) . rev ( ) { x [ i ] = ( x [ i ] - U . slice ( s ! [ i i 1 matrix_dimension ] ) . dot ( & x . slice ( s ! [ i 1 matrix_dimension ] ) ) ) / U [ [ i , i ] ] ; } x }
Solves linear equation A*x = b by LU decomposition
Solves linear equation A * x = b by LU decomposition
A
&Array2<T>
b
&Array1<T>
A
&Array2<T>
b
&Array1<T>