Skip to main content

Linalg

Namespace: MATHLIB  ·  Library: Math Library

Python equivalent

numpy.linalg / scipy.linalg in MNE-Python.

#include <math/linalg.h>

class UTILSLIB::Linalg

Static linear-algebra utilities (condition numbers, block-diagonal sparse assembly, xyz reductions, stable index sort) layered on top of Eigen for use across MATHLIB, FWDLIB and INVERSELIB.

Static Eigen-based linear-algebra helpers used across MATHLIB and the inverse solvers.

src/examples/ex_math/main.cpp
MatrixXd tall(3, 2);
tall << 1, 2, 3, 4, 5, 6;
const MatrixXd tallPinv = Linalg::pinv(tall); // Moore-Penrose, 2 x 3

MatrixXd square(2, 2);
square << 4, 1, 2, 3;
VectorXd singular;
const double condition = Linalg::getConditionNumber(square, singular); // s_max / s_min

Public Methods​

~Linalg()​

Destroys the Linalg object.


Static Methods​

combine_xyz(vec)​

Compute the three Cartesian components of a vector together.

Parameters:

  • vec : const Eigen::VectorXd & Input row vector [ x1 y1 z1 ... x_n y_n z_n ].

Returns:

  • Eigen::VectorXd — Output vector [x1^2+y1^2+z1^2 ... x_n^2+y_n^2+z_n^2 ].

getConditionNumber(A, s)​

Returns the condition number of a given matrix.

Parameters:

  • A : const Eigen::MatrixXd & Matrix to compute the condition number from.

  • s : Eigen::VectorXd & Singular values of A.

Returns:

  • double — the condition number.

getConditionSlope(A, s)​

Returns the condition slope of a given matrix.

Parameters:

  • A : const Eigen::MatrixXd & Matrix to compute the condition number from.

  • s : Eigen::VectorXd & Singular values of A.

Returns:

  • double — the condition slope.

get_whitener(A, pca, ch_type, eig, eigvec)​

Returns the whitener of a given matrix.

Parameters:

  • A : Eigen::MatrixXd & Matrix to compute the whitener from.

  • pca : bool Perform a PCA.

  • ch_type : QString Channel type string.

  • eig : Eigen::VectorXd & Eigenvalues.

  • eigvec : Eigen::MatrixXd & Eigenvectors (transposed).


get_whitener(A, pca, ch_type, eig, eigvec)​

Returns the whitener of a given matrix.

Parameters:

  • A : Eigen::MatrixXd & Matrix to compute the whitener from.

  • pca : bool Perform a PCA.

  • ch_type : const std::string & Channel type string.

  • eig : Eigen::VectorXd & Eigenvalues.

  • eigvec : Eigen::MatrixXd & Eigenvectors (transposed).


intersect(v1, v2, idx_sel)​

Find the intersection of two vectors.

Parameters:

  • v1 : const Eigen::VectorXi & Input vector 1.

  • v2 : const Eigen::VectorXi & Input vector 2.

  • idx_sel : Eigen::VectorXi & Index of intersection based on v1.

Returns:

  • Eigen::VectorXi — the sorted, unique values that are in both of the input arrays.

make_block_diag(A, n)​

Make a sparse block diagonal matrix.

Returns a sparse block diagonal, diagonalized from the elements in "A". "A" is ma x na, comprising bdn=(na/"n") blocks of submatrices. Each submatrix is ma x "n", and these submatrices are placed down the diagonal of the matrix.

Parameters:

  • A : const Eigen::MatrixXd & Matrix which should be diagonalized.

  • n : qint32 Columns of the submatrices.

Returns:

  • Eigen::SparseMatrix< double > — A sparse block diagonal, diagonalized from the elements in "A".

pinv(a)​

Creates the pseudo inverse of a matrix.

Parameters:

  • a : const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > & Matrix to compute the pseudo inverse of.

Returns:

  • Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > — the pseudo inverse of a.

rank(A, tol)​

Returns the rank of a matrix A.

Parameters:

  • A : const Eigen::MatrixXd & Matrix to get the rank from.

  • tol : double Relative threshold: biggest singular value multiplied with tol is smallest singular value considered non-zero.

Returns:

  • qint32 — rank of matrix A.

sort(v, desc)​

Sorts a vector (ascending order) in place and returns the track of the original indices.

Parameters:

  • v : Eigen::Matrix< T, Eigen::Dynamic, 1 > & Vector to sort; it's sorted in place.

  • desc : bool If true its sorted in a descending order, otherwise ascending (default = true).

Returns:

  • Eigen::VectorXi — Vector of the original indices in the new order.

sort(v_prime, mat, desc)​

Sorts a vector and a corresponding matrix in place and returns the track of the original indices.

The matrix is sorted along the columns using the vector values for comparison.

Parameters:

  • v_prime : Eigen::Matrix< T, Eigen::Dynamic, 1 > & Vector to sort (sorted in place).

  • mat : Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > & Matrix to sort (sorted in place).

  • desc : bool If true its sorted in a descending order, otherwise ascending (default = true).

Returns:

  • Eigen::VectorXi — Vector of the original indices in the new order.

sortrows(A, column)​

Sort rows in ascending order.

Parameters:

  • A : const std::vector< Eigen::Triplet< T > > & Triplet vector to sort.

  • column : qint32 Sorts the triplet vector based on the column specified.

Returns:

  • std::vector< Eigen::Triplet< T > > — Sorted copy of the triplet vector.

compareIdxValuePairBiggerThan(lhs, rhs)​

Compares two index-value-pairs (greater-than).

Parameters:

  • lhs : const std::pair< int, T > & Left index-value pair.

  • rhs : const std::pair< int, T > & Right index-value pair.

Returns:

  • bool — True if the value of lhs is greater than the value of rhs.

compareIdxValuePairSmallerThan(lhs, rhs)​

Compares two index-value-pairs (less-than).

Parameters:

  • lhs : const std::pair< int, T > & Left index-value pair.

  • rhs : const std::pair< int, T > & Right index-value pair.

Returns:

  • bool — True if the value of lhs is less than the value of rhs.

compareTripletFirstEntry(lhs, rhs)​

Compares triplet first entry (row).

Parameters:

  • lhs : const Eigen::Triplet< T > & Left triplet.

  • rhs : const Eigen::Triplet< T > & Right triplet.

Returns:

  • bool — True if the row index of lhs is less than that of rhs.

compareTripletSecondEntry(lhs, rhs)​

Compares triplet second entry (column).

Parameters:

  • lhs : const Eigen::Triplet< T > & Left triplet.

  • rhs : const Eigen::Triplet< T > & Right triplet.

Returns:

  • bool — True if the column index of lhs is less than that of rhs.

Example​

Used in examples​

ex_clustered_inverse_mne​

Example of a Clustered Inverse Minimum Norm Estimation application.

src/examples/ex_clustered_inverse_mne/main.cpp, line 230
double t_dConditionNumber = Linalg::getConditionNumber(t_Fwd.sol->data, s);
double t_dConditionNumberClustered = Linalg::getConditionNumber(t_clusteredFwd.sol->data, s);

ex_clustered_inverse_mne_raw​

Example of the computation of a clustered inverse mne raw.

src/examples/ex_clustered_inverse_mne_raw/main.cpp, line 559
double t_dConditionNumber = Linalg::getConditionNumber(t_Fwd.sol->data, s);
double t_dConditionNumberClustered = Linalg::getConditionNumber(t_clusteredFwd.sol->data, s);

ex_math​

Math library on inputs with known answers.

src/examples/ex_math/main.cpp, line 92
MatrixXd tall(3, 2);
tall << 1, 2, 3, 4, 5, 6;
const MatrixXd tallPinv = Linalg::pinv(tall); // Moore-Penrose, 2 x 3

Also used in ex_inverse_mne_raw.

Authors of this file​