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 determinant(&self) -> f64
pub fn determinant(&self) -> f64
Determinant of C = LL’ is the square of the product of the diagonal elements of L
sourcepub fn project(&self, index: &[usize]) -> Self
pub fn project(&self, index: &[usize]) -> Self
Projects to a smaller TriangMat of the same kind, in a subspace given by a subspace index. Deletes all the rows and columns of the other dimensions. The kept ones retain their original order.
sourcepub fn row(&self, r: usize) -> Vec<f64>
pub fn row(&self, r: usize) -> Vec<f64>
Copy one raw data row from TriangMat
To interpret the kind (plain, symmetric, assymetric, transposed),
use realrow,realcolumn,to_full
sourcepub fn transpose(&mut self)
pub fn transpose(&mut self)
Trivial implicit transposition of a mutable TriangMat.
The untransposed matrix is gone.
To keep the original, use clone_transpose
below
sourcepub fn clone_transpose(&self) -> TriangMat
pub fn clone_transpose(&self) -> TriangMat
Implicit transposition of a cloned TriangMat.
sourcepub fn realrow(&self, r: usize) -> Vec<f64>
pub fn realrow(&self, r: usize) -> Vec<f64>
One (short) row of a triangular matrix,
assumed to be zero filled at the end.
When the matrix is transposed (kind>2),
this will be a (short) column,
assumed to be zero filled upfront.
sourcepub fn realcolumn(&self, r: usize) -> Vec<f64>
pub fn realcolumn(&self, r: usize) -> Vec<f64>
One (short) column of a triangular matrix,
assumed to be zero filled upfront.
When the matrix is transposed (kind>2),
this will be a (short) row,
assumed to be zero filled at the end.
sourcepub fn to_full(&self) -> Vec<Vec<f64>>
pub fn to_full(&self) -> Vec<Vec<f64>>
Unpacks all kinds of TriangMat to equivalent full matrix form
For multiplications, use rmultv,lmultv,mult
instead, to save this unpacking.
sourcepub fn rmultv<U>(&self, v: &[U]) -> Vec<f64>
pub fn rmultv<U>(&self, v: &[U]) -> Vec<f64>
Postmultiply row vector v by triangular matrix self
.
When a column of self is shorter, it is as if padded with zeroes upfront.
When v is shorter, it is as if padded with zeroes at the end.
sourcepub fn lmultv<U>(&self, v: &[U]) -> Vec<f64>
pub fn lmultv<U>(&self, v: &[U]) -> Vec<f64>
Premultiply column vector v by triangular matrix self
.
When a row of self is shorter, it is as if padded with zeroes at the end.
When v is shorter, it is as if padded with zeroes upfront.
The output is (assumed to be) a column.
sourcepub fn mult(&self, other: &Self) -> Vec<Vec<f64>>
pub fn mult(&self, other: &Self) -> Vec<Vec<f64>>
General multiplication of two triangular matrices (of any kind). The triangular matrices are not expanded and incomplete rows/columns are not even padded (very effient).
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 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 never 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 more