pub struct TriangMat {
pub kind: usize,
pub data: Vec<f64>,
}
Expand description
Compact Triangular Matrix.
TriangMat stores and manipulates triangular matrices.
It also compactly represents square symmetric and antisymmetric matrices.
TriangMat is typically produced by some matrix calculations,
so the end-type for its data is f64.
The actual length of its data is triangmat.len() = n*(n+1)/2
.
The dimension of the implied nxn matrix is n = triangmat.dim()
.
The kind (field) of the TriangMat is encoded as 0..5. This enables
trivial transpositions by: (kind+3) % 6
.
Fields§
§kind: usize
Matrix kind encoding: 0=plain, 1=antisymmetric, 2=symmetric, +3=transposed
data: Vec<f64>
Packed 1d vector of triangular matrix data of size (n+1)*n/2
Implementations§
source§impl TriangMat
impl TriangMat
Implementation of associated functions for struct TriangleMat. End type is f64, as triangular matrices will be mostly computed
sourcepub fn dim(&self) -> usize
pub fn dim(&self) -> usize
Dimension of the implied full (square) matrix
from the quadratic equation: n^2 + n - 2l = 0
sourcepub fn sum(&self) -> f64
pub fn sum(&self) -> f64
Sum of the elements:
when applied to the wedge product a∧b, returns det(a,b)
sourcepub fn unit(n: usize) -> Self
pub fn unit(n: usize) -> Self
New unit (symmetric) TriangMat matrix with total data size n*(n+1)/2
sourcepub fn eigenvalues(&self) -> Vec<f64>
pub fn eigenvalues(&self) -> Vec<f64>
Eigenvalues from Cholesky L matrix
sourcepub fn determinant(&self) -> f64
pub fn determinant(&self) -> f64
Determinant from Cholesky L matrix
sourcepub fn eigenvectors(&self) -> Vec<Vec<f64>>
pub fn eigenvectors(&self) -> Vec<Vec<f64>>
Normalized full eigenvectors from triangular covariance matrix.
Can be used together with eigenvalues for Principal Components Analysis.
Covariance matrix is symmetric, so
we use its rows as eigenvectors (no need to transpose it)
sourcepub fn rowcol(s: usize) -> (usize, usize)
pub fn rowcol(s: usize) -> (usize, usize)
Translates subscripts to a 1d vector, i.e. natural numbers, to a pair of (row,column) coordinates within a lower/upper triangular matrix. Enables memory efficient representation of triangular matrices as one flat vector.
sourcepub fn project(&self, index: &[usize]) -> Self
pub fn project(&self, index: &[usize]) -> Self
Project symmetric/antisymmetric triangmat to a smaller one of the same kind, into a subspace specified by an ascending index of dimensions. Deletes all rows and columns of the missing dimensions.
sourcepub fn to_triangle(&self) -> Vec<Vec<f64>>
pub fn to_triangle(&self) -> Vec<Vec<f64>>
Unpacks flat TriangMat Vec to triangular Vec
sourcepub fn cholesky(&self) -> Result<Self, RE>
pub fn cholesky(&self) -> Result<Self, RE>
Efficient Cholesky-Banachiewicz matrix decomposition into LL'
,
where L is the returned lower triangular matrix and L’ its upper triangular transpose.
Expects as input a symmetric positive definite matrix
in TriangMat compact form, such as a covariance matrix produced by covar
.
The computations are all done on the compact form,
making this implementation memory efficient for large (symmetric) matrices.
Reports errors if the above conditions are not satisfied.
sourcepub fn mahalanobis<U>(&self, d: &[U]) -> Result<f64, RE>
pub fn mahalanobis<U>(&self, d: &[U]) -> Result<f64, RE>
Mahalanobis scaled magnitude m(d) of a (column) vector d.
Self is the decomposed lower triangular matrix L, as returned by cholesky
decomposition of covariance/comediance positive definite matrix: C = LL’,
where ’ denotes transpose. Mahalanobis distance is defined as:
m(d) = sqrt(d'inv(C)d) = sqrt(d'inv(LL')d) = sqrt(d'inv(L')inv(L)d)
,
where inv()
denotes matrix inverse, which is not explicitly computed.
Let x = inv(L)d
( and therefore also x' = d'inv(L')
).
Substituting x into the above definition: `m(d) = sqrt(x’x) = |x|.
We obtain x by setting Lx = d and solving by forward substitution.
All the calculations are done in the compact triangular form.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for TriangMat
impl RefUnwindSafe for TriangMat
impl Send for TriangMat
impl Sync for TriangMat
impl Unpin for TriangMat
impl UnwindSafe for TriangMat
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§impl<T> Pointable for T
impl<T> Pointable for T
source§impl<T> Printing<T> for Twhere
T: Display,
impl<T> Printing<T> for Twhere
T: Display,
source§fn to_str(self) -> String
fn to_str(self) -> String
printing.rs
.source§fn to_plainstr(self) -> String
fn to_plainstr(self) -> String
printing.rs
.