opensurgsim
Classes | Namespaces | Typedefs | Functions
SparseMatrix.h File Reference

Definitions of useful sparse matrix functions. More...

#include <Eigen/Sparse>
#include "SurgSim/Framework/Assert.h"
#include "SurgSim/Math/Matrix.h"

Go to the source code of this file.

Classes

class  SurgSim::Math::Operation< DerivedSub, SparseType, StorageOrder >
 Helper class to run operation a column/row of a matrix to a chunk of memory where the size is dynamically defined. More...
 
class  SurgSim::Math::Operation< DerivedSub, SparseType, Eigen::ColMajor >
 Specialization for column major storage. More...
 
class  SurgSim::Math::Operation< DerivedSub, SparseType, Eigen::RowMajor >
 Specialization for row major storage. More...
 

Namespaces

 SurgSim
 Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui needs glew but we need to call glewInit() from a osg callback, using this call we avoid getting warnings about redefinitions.
 

Typedefs

typedef Eigen::SparseMatrix< double > SurgSim::Math::SparseMatrix
 A sparse matrix.
 

Functions

template<int Opt, typename StorageIndex >
void SurgSim::Math::blockWithSearch (const Eigen::Ref< const Matrix > &subMatrix, Eigen::Index rowStart, Eigen::Index columnStart, Eigen::Index n, Eigen::Index m, Eigen::SparseMatrix< double, Opt, StorageIndex > *matrix, void(Operation< Matrix, Eigen::SparseMatrix< double, Opt, StorageIndex >>::*op)(double *, StorageIndex, Eigen::Index, Eigen::Index, const Eigen::Ref< const Matrix > &, Eigen::Index))
 Runs a given operation on a SparseMatrix block(i, j, n, m) from a (n x m) 'subMatrix' with searching for the block 1st element. More...
 
template<int Opt, typename StorageIndex >
void SurgSim::Math::blockOperation (const Eigen::Ref< const Matrix > &subMatrix, Eigen::Index rowStart, Eigen::Index columnStart, Eigen::SparseMatrix< double, Opt, StorageIndex > *matrix, void(Operation< Matrix, Eigen::SparseMatrix< double, Opt, StorageIndex >>::*op)(double *, const double &))
 Runs a given operation on a SparseMatrix block(i, j, n, m) from a (n x m) 'subMatrix' with searching for the block 1st element. More...
 
template<int Opt, typename StorageIndex >
void SurgSim::Math::addSubMatrix (const Eigen::Ref< const Matrix > &subMatrix, Eigen::Index blockIdRow, Eigen::Index blockIdCol, Eigen::SparseMatrix< double, Opt, StorageIndex > *matrix)
 Add a dense sub-matrix into a matrix that may need the memory locations to be initialized. More...
 
template<int Opt, typename StorageIndex >
void SurgSim::Math::addSubMatrixNoInitialize (const Eigen::Ref< const Matrix > &subMatrix, Eigen::Index blockIdRow, Eigen::Index blockIdCol, Eigen::SparseMatrix< double, Opt, StorageIndex > *matrix)
 Add a dense sub-matrix into a sparse matrix that does not need the memory locations to be initialized. More...
 
template<int Opt, typename StorageIndex >
void SurgSim::Math::assignSubMatrix (const Eigen::Ref< const Matrix > &subMatrix, Eigen::Index blockIdRow, Eigen::Index blockIdCol, Eigen::SparseMatrix< double, Opt, StorageIndex > *matrix)
 Assign a dense sub-matrix into a matrix that may need the memory locations to be initialized. More...
 
template<int Opt, typename StorageIndex >
void SurgSim::Math::assignSubMatrixNoInitialize (const Eigen::Ref< const Matrix > &subMatrix, Eigen::Index blockIdRow, Eigen::Index blockIdCol, Eigen::SparseMatrix< double, Opt, StorageIndex > *matrix)
 Assign a dense sub-matrix into a sparse matrix that does not need the memory locations to be initialized. More...
 
template<typename T , int Opt, typename StorageIndex >
void SurgSim::Math::zeroRow (Eigen::Index row, Eigen::SparseMatrix< T, Opt, StorageIndex > *matrix)
 Helper method to zero a row of a matrix specialized for Sparse Matrices. More...
 
template<typename T , int Opt, typename StorageIndex >
void SurgSim::Math::zeroColumn (Eigen::Index column, Eigen::SparseMatrix< T, Opt, StorageIndex > *matrix)
 Helper method to zero a column of a matrix specialized for Sparse Matrices. More...
 
template<typename T , int Opt, typename StorageIndex >
void SurgSim::Math::clearMatrix (Eigen::SparseMatrix< T, Opt, StorageIndex > *matrix)
 Helper method to zero all entries of a matrix specialized for Sparse Matrices. More...
 

Detailed Description

Definitions of useful sparse matrix functions.

Function Documentation

§ addSubMatrix()

template<int Opt, typename StorageIndex >
void SurgSim::Math::addSubMatrix ( const Eigen::Ref< const Matrix > &  subMatrix,
Eigen::Index  blockIdRow,
Eigen::Index  blockIdCol,
Eigen::SparseMatrix< double, Opt, StorageIndex > *  matrix 
)

Add a dense sub-matrix into a matrix that may need the memory locations to be initialized.

Template Parameters
Opt,StorageIndexType parameters defining the output matrix type SparseMatrix<double, Opt, StorageIndex>
Parameters
subMatrixThe sub-matrix
blockIdRow,blockIdColThe block indices in matrix
[in,out]matrixThe matrix to add the sub-matrix into
Note
This is a specialization of addSubMatrix for sparse matrices.
See also
addSubMatrixNoInitialize

§ addSubMatrixNoInitialize()

template<int Opt, typename StorageIndex >
void SurgSim::Math::addSubMatrixNoInitialize ( const Eigen::Ref< const Matrix > &  subMatrix,
Eigen::Index  blockIdRow,
Eigen::Index  blockIdCol,
Eigen::SparseMatrix< double, Opt, StorageIndex > *  matrix 
)

Add a dense sub-matrix into a sparse matrix that does not need the memory locations to be initialized.

Template Parameters
Opt,StorageIndexType parameters defining the output matrix type SparseMatrix<double, Opt, StorageIndex>
Parameters
subMatrixThe sub-matrix
blockIdRow,blockIdColThe block indices in matrix
[in,out]matrixThe matrix to add the sub-matrix into
Note
This is a specialization of addSubMatrix for sparse matrices.
See also
addSubMatrix

§ assignSubMatrix()

template<int Opt, typename StorageIndex >
void SurgSim::Math::assignSubMatrix ( const Eigen::Ref< const Matrix > &  subMatrix,
Eigen::Index  blockIdRow,
Eigen::Index  blockIdCol,
Eigen::SparseMatrix< double, Opt, StorageIndex > *  matrix 
)

Assign a dense sub-matrix into a matrix that may need the memory locations to be initialized.

Template Parameters
Opt,StorageIndexType parameters defining the output matrix type SparseMatrix<double, Opt, Index>
Parameters
subMatrixThe sub-matrix
blockIdRow,blockIdColThe block indices in matrix
[in,out]matrixThe matrix to assign the sub-matrix into
See also
assignSubMatrixNoInitialize

§ assignSubMatrixNoInitialize()

template<int Opt, typename StorageIndex >
void SurgSim::Math::assignSubMatrixNoInitialize ( const Eigen::Ref< const Matrix > &  subMatrix,
Eigen::Index  blockIdRow,
Eigen::Index  blockIdCol,
Eigen::SparseMatrix< double, Opt, StorageIndex > *  matrix 
)

Assign a dense sub-matrix into a sparse matrix that does not need the memory locations to be initialized.

Template Parameters
Opt,StorageIndexType parameters defining the output matrix type SparseMatrix<double, Opt, Index>
Parameters
subMatrixThe sub-matrix
blockIdRow,blockIdColThe block indices in matrix
[in,out]matrixThe matrix to assign the sub-matrix into
See also
assignSubMatrix

§ blockOperation()

template<int Opt, typename StorageIndex >
void SurgSim::Math::blockOperation ( const Eigen::Ref< const Matrix > &  subMatrix,
Eigen::Index  rowStart,
Eigen::Index  columnStart,
Eigen::SparseMatrix< double, Opt, StorageIndex > *  matrix,
void(Operation< Matrix, Eigen::SparseMatrix< double, Opt, StorageIndex >>::*)(double *, const double &)  op 
)

Runs a given operation on a SparseMatrix block(i, j, n, m) from a (n x m) 'subMatrix' with searching for the block 1st element.


It supposes:

  • that both the SparseMatrix and the 'subMatrix' are using the same Scalar type, double.
    This function can be used to initialize the form of a sparse matrix as it will add entries when appropriate entries do not exist.
    Template Parameters
    Opt,StorageIndexType parameters defining the output matrix type SparseMatrix<double, Opt, StorageIndex>
    Parameters
    subMatrixThe sub matrix that will be copied into the SparseMatrix block
    rowStart,columnStartThe row and column indices to indicate where the block in the SparseMatrix starts
    [in,out]matrixThe sparse matrix in which the block needs to be set by 'subMatrix'
    opThe operation to run on the block
    Exceptions
    SurgSim::Framework::AssertionFailureIf one of the following conditions is met:
  • if 'subMatrix' is smaller than (n x m) in any dimension
  • if 'matrix' is nullptr or smaller than (n x m) in any dimension
  • if the requested block is out of range in 'matrix'.
  • if 'matrix' does not fulfill the requirement (i.e. is missing elements within the block).
    Note
    The receiving SparseMatrix must have a structure like the following:
    (xx x0 x)
    (xx 0x x)
    (x0[xx]x) -> The block must already contain all the coefficients but these rows and columns may
    (0x[xx]0) -> contains more coefficients before and after the block.
    (xx 00 x)

§ blockWithSearch()

template<int Opt, typename StorageIndex >
void SurgSim::Math::blockWithSearch ( const Eigen::Ref< const Matrix > &  subMatrix,
Eigen::Index  rowStart,
Eigen::Index  columnStart,
Eigen::Index  n,
Eigen::Index  m,
Eigen::SparseMatrix< double, Opt, StorageIndex > *  matrix,
void(Operation< Matrix, Eigen::SparseMatrix< double, Opt, StorageIndex >>::*)(double *, StorageIndex, Eigen::Index, Eigen::Index, const Eigen::Ref< const Matrix > &, Eigen::Index)  op 
)

Runs a given operation on a SparseMatrix block(i, j, n, m) from a (n x m) 'subMatrix' with searching for the block 1st element.


It supposes:

  • that the SparseMatrix already contains all the elements within the block (no insertion necessary)
  • that both the SparseMatrix and the 'subMatrix' are using the same Scalar type, double.
    This function will not change anything to the structure of the SparseMatrix, only change the values of the corresponding coefficients.
    Template Parameters
    Opt,StorageIndexType parameters defining the output matrix type SparseMatrix<double, Opt, StorageIndex>
    Parameters
    subMatrixThe sub matrix that will be copied into the SparseMatrix block
    rowStart,columnStartThe row and column indices to indicate where the block in the SparseMatrix starts
    n,mThe block size (Derived may be bigger but cannot be smaller in both dimension)
    [in,out]matrixThe sparse matrix in which the block needs to be set by 'subMatrix'
    opThe operation to run on the block
    Exceptions
    SurgSim::Framework::AssertionFailureIf one of the following conditions is met:
  • if 'subMatrix' is smaller than (n x m) in any dimension
  • if 'matrix' is nullptr or smaller than (n x m) in any dimension
  • if the requested block is out of range in 'matrix'.
  • if 'matrix' does not fulfill the requirement (i.e. is missing elements within the block).
    Note
    The receiving SparseMatrix must have a structure like the following:
    (xx x0 x)
    (xx 0x x)
    (x0[xx]x) -> The block must already contain all the coefficients but these rows and columns may
    (0x[xx]0) -> contains more coefficients before and after the block.
    (xx 00 x)

§ clearMatrix()

template<typename T , int Opt, typename StorageIndex >
void SurgSim::Math::clearMatrix ( Eigen::SparseMatrix< T, Opt, StorageIndex > *  matrix)
inline

Helper method to zero all entries of a matrix specialized for Sparse Matrices.

This allows the preservation of the the matrix form while still allowing the reset of the matrix entries to zero.

Parameters
[in,out]matrixThe matrix to set to zero

§ zeroColumn()

template<typename T , int Opt, typename StorageIndex >
void SurgSim::Math::zeroColumn ( Eigen::Index  column,
Eigen::SparseMatrix< T, Opt, StorageIndex > *  matrix 
)
inline

Helper method to zero a column of a matrix specialized for Sparse Matrices.

Parameters
columnThe column to set to zero
[in,out]matrixThe matrix to set the zero column on.

§ zeroRow()

template<typename T , int Opt, typename StorageIndex >
void SurgSim::Math::zeroRow ( Eigen::Index  row,
Eigen::SparseMatrix< T, Opt, StorageIndex > *  matrix 
)

Helper method to zero a row of a matrix specialized for Sparse Matrices.

Parameters
rowThe row to set to zero
[in,out]matrixThe matrix to set the zero row on.