Skip to content

Add calculation for tridiagonal matrices (solve, factorize, det, rcond) #196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jun 28, 2020

Conversation

doraneko94
Copy link
Contributor

I added the calculation for tridiagonal matrices, which might be important for solving differential equations.

First, I made a struct of TriDiagonal for LU factorization and solving equation using LAPACK. It represents a tridiagonal matrix as 3 one-dimensional Array (diagonal, sub-diagonal and super-diagonal elements).
And this struct also holds the layout and 1-norm of the raw matrix. They are used in some methods (solve_tridiagonal, rcond_tridiagonal, etc.)

Second, LUFactorizedTriDiagonal struct contains additional 1D Array of the second super-diagonal of matrix U and ipiv, just like LUFactorized.

These structs and ArrayBase<S, Ix2> have lu (*gttrf), rcond (*gtcon), solve (*gttrs) like methods using LAPACK and a det like method using a recurrent relation implemented in Rust.

I also added an example and some test functions. Please confirm that you can use them just like any other methods implemented for general or triangular matrices.

@doraneko94
Copy link
Contributor Author

I removed a 1-norm field n1 from Tridiagonal and add it to LUFactorizedTridiagonal (with renaming to anom ).
This is because I wanted to implement Index / IndexMut trait to Tridiagonal so that it can be updated the value by indexing.
Thus, when you call .lu_tridiagonal(), 1-norm is calculated too to construct a LUFactorizedTridiagonal.

Copy link
Member

@termoshtt termoshtt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work :)

}

/// An interface for making a Tridiagonal struct.
pub trait ToTridiagonal<A: Scalar> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub trait ToTridiagonal<A: Scalar> {
pub trait ExtractTridiagonal<A: Scalar> {

Comment on lines +30 to +37
pub trait TridiagIndex {
fn to_tuple(&self) -> (i32, i32);
}
impl TridiagIndex for [Ix; 2] {
fn to_tuple(&self) -> (i32, i32) {
(self[0] as i32, self[1] as i32)
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think ndarray::NdIndex<Ix2> could be better for it, but it does not enough iterface for implementing this PR, e.g. Into<Ix2>. I keep this code as it is.

@termoshtt termoshtt merged commit e005ae1 into rust-ndarray:master Jun 28, 2020
@termoshtt
Copy link
Member

Thank a lot :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants