|
2 | 2 | //! & |
3 | 3 | //! Methods for tridiagonal matrices |
4 | 4 |
|
5 | | -use std::ops::{Index, IndexMut}; |
6 | | - |
7 | | -use cauchy::Scalar; |
8 | | -use ndarray::*; |
9 | | -use num_traits::One; |
10 | | - |
11 | 5 | use super::convert::*; |
12 | 6 | use super::error::*; |
13 | 7 | use super::lapack::*; |
14 | 8 | use super::layout::*; |
| 9 | +use cauchy::Scalar; |
| 10 | +use ndarray::*; |
| 11 | +use num_traits::One; |
15 | 12 |
|
16 | | -/// Represents a tridiagonal matrix as 3 one-dimensional vectors. |
17 | | -/// This struct also holds the layout of the raw matrix. |
18 | | -#[derive(Clone, PartialEq)] |
19 | | -pub struct Tridiagonal<A: Scalar> { |
20 | | - /// layout of raw matrix |
21 | | - pub l: MatrixLayout, |
22 | | - /// (n-1) sub-diagonal elements of matrix. |
23 | | - pub dl: Vec<A>, |
24 | | - /// (n) diagonal elements of matrix. |
25 | | - pub d: Vec<A>, |
26 | | - /// (n-1) super-diagonal elements of matrix. |
27 | | - pub du: Vec<A>, |
28 | | -} |
29 | | - |
30 | | -pub trait TridiagIndex { |
31 | | - fn to_tuple(&self) -> (i32, i32); |
32 | | -} |
33 | | -impl TridiagIndex for [Ix; 2] { |
34 | | - fn to_tuple(&self) -> (i32, i32) { |
35 | | - (self[0] as i32, self[1] as i32) |
36 | | - } |
37 | | -} |
38 | | - |
39 | | -impl<A, I> Index<I> for Tridiagonal<A> |
40 | | -where |
41 | | - A: Scalar, |
42 | | - I: TridiagIndex, |
43 | | -{ |
44 | | - type Output = A; |
45 | | - #[inline] |
46 | | - fn index(&self, index: I) -> &A { |
47 | | - let (n, _) = self.l.size(); |
48 | | - let (row, col) = index.to_tuple(); |
49 | | - assert!( |
50 | | - std::cmp::max(row, col) < n, |
51 | | - "ndarray: index {:?} is out of bounds for array of shape {}", |
52 | | - [row, col], |
53 | | - n |
54 | | - ); |
55 | | - match row - col { |
56 | | - 0 => &self.d[row as usize], |
57 | | - 1 => &self.dl[col as usize], |
58 | | - -1 => &self.du[row as usize], |
59 | | - _ => panic!( |
60 | | - "ndarray-linalg::tridiagonal: index {:?} is not tridiagonal element", |
61 | | - [row, col] |
62 | | - ), |
63 | | - } |
64 | | - } |
65 | | -} |
66 | | - |
67 | | -impl<A, I> IndexMut<I> for Tridiagonal<A> |
68 | | -where |
69 | | - A: Scalar, |
70 | | - I: TridiagIndex, |
71 | | -{ |
72 | | - #[inline] |
73 | | - fn index_mut(&mut self, index: I) -> &mut A { |
74 | | - let (n, _) = self.l.size(); |
75 | | - let (row, col) = index.to_tuple(); |
76 | | - assert!( |
77 | | - std::cmp::max(row, col) < n, |
78 | | - "ndarray: index {:?} is out of bounds for array of shape {}", |
79 | | - [row, col], |
80 | | - n |
81 | | - ); |
82 | | - match row - col { |
83 | | - 0 => &mut self.d[row as usize], |
84 | | - 1 => &mut self.dl[col as usize], |
85 | | - -1 => &mut self.du[row as usize], |
86 | | - _ => panic!( |
87 | | - "ndarray-linalg::tridiagonal: index {:?} is not tridiagonal element", |
88 | | - [row, col] |
89 | | - ), |
90 | | - } |
91 | | - } |
92 | | -} |
| 13 | +pub use lapack::tridiagonal::{LUFactorizedTridiagonal, Tridiagonal}; |
93 | 14 |
|
94 | 15 | /// An interface for making a Tridiagonal struct. |
95 | 16 | pub trait ExtractTridiagonal<A: Scalar> { |
@@ -188,23 +109,6 @@ pub trait SolveTridiagonalInplace<A: Scalar, D: Dimension> { |
188 | 109 | ) -> Result<&'a mut ArrayBase<S, D>>; |
189 | 110 | } |
190 | 111 |
|
191 | | -/// Represents the LU factorization of a tridiagonal matrix `A` as `A = P*L*U`. |
192 | | -#[derive(Clone)] |
193 | | -pub struct LUFactorizedTridiagonal<A: Scalar> { |
194 | | - /// A tridiagonal matrix which consists of |
195 | | - /// - l : layout of raw matrix |
196 | | - /// - dl: (n-1) multipliers that define the matrix L. |
197 | | - /// - d : (n) diagonal elements of the upper triangular matrix U. |
198 | | - /// - du: (n-1) elements of the first super-diagonal of U. |
199 | | - pub a: Tridiagonal<A>, |
200 | | - /// (n-2) elements of the second super-diagonal of U. |
201 | | - pub du2: Vec<A>, |
202 | | - /// 1-norm of raw matrix (used in .rcond_tridiagonal()). |
203 | | - pub anom: A::Real, |
204 | | - /// The pivot indices that define the permutation matrix `P`. |
205 | | - pub ipiv: Pivot, |
206 | | -} |
207 | | - |
208 | 112 | impl<A> SolveTridiagonal<A, Ix2> for LUFactorizedTridiagonal<A> |
209 | 113 | where |
210 | 114 | A: Scalar + Lapack, |
|
0 commit comments