FiffSparseMatrix
Namespace: FIFFLIB · Library: FIFF Library
#include <fiff/fiff_sparse_matrix.h>
class FIFFLIB::FiffSparseMatrix
Sparse FIFF matrix: CCS or RCS storage with the value / index / pointer triple as written by FiffStream::write_float_sparse_*.
Holds the three arrays the FIFF sparse-matrix tag stores on disk (values, inner indices, outer pointers) and the (nrow, ncol, nnz) shape, so it can be streamed in and out without conversion. Convert to an Eigen::SparseMatrix for arithmetic and back for serialization.
SparseMatrix<double> eigen(3, 4);
eigen.insert(0, 1) = 2.0;
eigen.insert(2, 3) = -1.5;
eigen.makeCompressed();
const FiffSparseMatrix fiffSparse = FiffSparseMatrix::fromEigenSparse(eigen); // FIFF storage layout
const SparseMatrix<double> backToEigen = fiffSparse.toEigenSparse();
Public Methods
FiffSparseMatrix()
Constructs an empty FiffSparseMatrix.
FiffSparseMatrix(mat, coding)
Constructs a FiffSparseMatrix wrapping an existing Eigen sparse matrix.
Parameters:
-
mat : Eigen::SparseMatrix< float > && Eigen sparse matrix to wrap (moved in).
-
coding : FIFFLIB::fiff_int_t FIFF storage coding (FIFFTS_MC_RCS or FIFFTS_MC_CCS).
FiffSparseMatrix(mat)
Default copy constructor.
Parameters:
- mat : const FiffSparseMatrix & Sparse matrix to copy.
FiffSparseMatrix(mat)
Default move constructor.
Parameters:
- mat : FiffSparseMatrix && Sparse matrix to move from; left in a valid but unspecified state.
operator=()
Default copy-assignment operator.
Returns:
- FiffSparseMatrix & — Reference to this object.
operator=()
Default move-assignment operator.
Returns:
- FiffSparseMatrix & — Reference to this object.
~FiffSparseMatrix()
Default destructor.
eigen()
Mutable access to the underlying Eigen sparse matrix.
Returns:
- Eigen::SparseMatrix< float > & — Reference to the internal Eigen::SparseMatrix<float>.
eigen()
Const access to the underlying Eigen sparse matrix.
Returns:
- const Eigen::SparseMatrix< float > & — Const reference to the internal Eigen::SparseMatrix<float>.
operator const Eigen::SparseMatrix< float > &()
Implicit conversion to const Eigen::SparseMatrix<float>&.
rows()
Number of rows.
Returns:
- int — Row count of the underlying Eigen sparse matrix.
cols()
Number of columns.
Returns:
- int — Column count of the underlying Eigen sparse matrix.
nonZeros()
Number of stored non-zero elements.
Returns:
- int — Count of explicitly stored entries in the underlying Eigen sparse matrix.
mne_add_upper_triangle_rcs()
Add the upper triangle to a lower-triangular sparse RCS matrix.
Returns:
- FiffSparseMatrix::UPtr — A unique pointer to the newly constructed
FiffSparseMatrixwith both triangles.
pickLowerTriangleRcs()
Extract only the lower triangle (including diagonal) from a square RCS matrix.
Returns:
- FiffSparseMatrix::UPtr — A unique pointer to the newly constructed
FiffSparseMatrixwith lower-triangle elements.
is_empty()
Check whether this sparse matrix is empty (no non-zero elements).
Returns:
- bool — true if the matrix has no data.
toEigenSparse()
Convert to Eigen::SparseMatrix<double> (cast from float).
Returns:
- Eigen::SparseMatrix< double > — A double-precision copy of the internal sparse matrix.
Static Methods
fiff_get_matrix_sparse_dims(tag)
Interpret dimensions and nz from matrix data.
Parameters:
- tag : const FIFFLIB::FiffTag::UPtr & The tag containing sparse matrix data.
Returns:
- std::vector< int > — A vector with the matrix dimension info, or empty on error.
fiff_get_float_sparse_matrix(tag)
Conversion of tag data into the standard sparse representation.
Parameters:
- tag : const FIFFLIB::FiffTag::UPtr & The tag containing sparse matrix data.
Returns:
- FiffSparseMatrix::UPtr — A unique pointer to the newly constructed
FiffSparseMatrix, or nullptr on error.
create_sparse_rcs(nrow, ncol, nnz, colindex, vals)
Create a sparse RCS matrix from row-based data.
Parameters:
-
nrow : int Number of rows.
-
ncol : int Number of columns.
-
nnz : *int ** Number of non-zero elements on each row.
-
colindex : *int *** Column indices of non-zero elements on each row.
-
vals : *float *** Values of non-zero elements on each row.
Returns:
- FiffSparseMatrix::UPtr — A unique pointer to the newly constructed
FiffSparseMatrix, or nullptr on error.
fromEigenSparse(mat)
Create a FiffSparseMatrix from an Eigen::SparseMatrix<double>.
The data is cast to float for internal storage.
Parameters:
- mat : const Eigen::SparseMatrix< double > & The Eigen sparse matrix to convert.
Returns:
- FiffSparseMatrix — The equivalent
FiffSparseMatrix.
fromEigenSparse(mat)
Create a FiffSparseMatrix from an Eigen::SparseMatrix<float>.
Parameters:
- mat : const Eigen::SparseMatrix< float > & The Eigen sparse matrix to wrap.
Returns:
- FiffSparseMatrix — The equivalent
FiffSparseMatrix.
Example
- Full source:
src/examples/ex_fiff_api/main.cpp - Build target:
ex_fiff_api(runs under CTest) - Data: mne-cpp-test-data
Used in examples
ex_fiff_api
FIFF library: measurement info, channels, digitizer, transforms, projectors, covariance, events,.
eigen.insert(0, 1) = 2.0;
eigen.insert(2, 3) = -1.5;
eigen.makeCompressed();
const FiffSparseMatrix fiffSparse = FiffSparseMatrix::fromEigenSparse(eigen); // FIFF storage layout
const SparseMatrix<double> backToEigen = fiffSparse.toEigenSparse();
ok &= expect(backToEigen.coeff(2, 3) == -1.5 && backToEigen.nonZeros() == 2, "FiffSparseMatrix round-trips an Eigen sparse matrix");
Authors of this file
- Christoph Dinh <christoph.dinh@mne-cpp.org>