    

 ## On this page

  

 

 # SparseMatrix Class Reference

 Last update: 16.07.2025 

encapsulates sparse matrix classes and algorithms [More...](class_sparse_matrix.xhtml#details)

## <a name="pub-types"></a>Public Types

<a id="a1b5357921e32b1f6d262754172689b02"></a>typedef Eigen::SparseMatrix&lt; [TScalar](class_sparse_matrix.xhtml#ad33f729eda6e45412aac71cee702012f) &gt; [Base](class_sparse_matrix.xhtml#a1b5357921e32b1f6d262754172689b02) the ancestor class type   
 <a id="a353dbc81e718cd4640b7c719b512892f"></a>typedef Base::Index [TEigenIndex](class_sparse_matrix.xhtml#a353dbc81e718cd4640b7c719b512892f) index type for Eigen matrices   
 <a id="ad33f729eda6e45412aac71cee702012f"></a>typedef number [TScalar](class_sparse_matrix.xhtml#ad33f729eda6e45412aac71cee702012f) the data type of the coefficients (real number)   
 <a id="ab423d248b103558b5903c702c13335ae"></a>typedef number [TUnsignedIndex](class_sparse_matrix.xhtml#ab423d248b103558b5903c702c13335ae) an unsigned index type   
 ## <a name="pub-methods"></a>Public Member Functions

[SparseMatrix](class_sparse_matrix.xhtml) [\_\_neg\_\_](class_sparse_matrix.xhtml#ae8b8bc59ac6df86a5e2b7a529754e5c2) () const char \* [\_\_str\_\_](class_sparse_matrix.xhtml#adb0255d0035cd7783ce87f41a3cae5cc) () transforms this objects into a string, required by SWIG [More...](#adb0255d0035cd7783ce87f41a3cae5cc)  
  [Assign](class_sparse_matrix.xhtml#a37225e979eb1257d6498c58b1902ebcf) ([SparseMatrix](class_sparse_matrix.xhtml) other, bool keep\_data=true) assigns a new matrix to this object [More...](#a37225e979eb1257d6498c58b1902ebcf)  
 [SparseMatrix](class_sparse_matrix.xhtml) [Col](class_sparse_matrix.xhtml#aded57b8e31e935d93fefe03f80a4da0a) (int col) const extracts a column of this matrix (as a copy, not a reference!) [More...](#aded57b8e31e935d93fefe03f80a4da0a)  
 [TUnsignedIndex](class_sparse_matrix.xhtml#ab423d248b103558b5903c702c13335ae) [Cols](class_sparse_matrix.xhtml#a2ec082b93696e449a0eb430f1f00196e) () [TUnsignedIndex](class_sparse_matrix.xhtml#ab423d248b103558b5903c702c13335ae) [InnerSize](class_sparse_matrix.xhtml#a9c5c5e3890bd6be32b732fa324ca0954) () [TUnsignedIndex](class_sparse_matrix.xhtml#ab423d248b103558b5903c702c13335ae) [NonZeros](class_sparse_matrix.xhtml#ac83d198cac5c9247233b7024b3f2742a) () [Matrix](class_matrix.xhtml) [operator\*](class_sparse_matrix.xhtml#ac20eea8e7bdf72e549208fcb4ad002f9) ([Matrix](class_matrix.xhtml) x) const overrides the sparsematrix-densematrix product. [More...](#ac20eea8e7bdf72e549208fcb4ad002f9)  
 [Matrix](class_matrix.xhtml) [operator\*](class_sparse_matrix.xhtml#ab20c65b6af96b99b1154078d249d8e38) ([MatrixBlock](class_matrix_block.xhtml) x) const overrides the sparsematrix-densematrix product. [More...](#ab20c65b6af96b99b1154078d249d8e38)  
 [SparseMatrix](class_sparse_matrix.xhtml) [operator\*](class_sparse_matrix.xhtml#a5753e8d3adb4ab7a445a13e64e537c49) ([SparseMatrix](class_sparse_matrix.xhtml) x) const overrides the sparsematrix-densematrix product. [More...](#a5753e8d3adb4ab7a445a13e64e537c49)  
 [SparseMatrix](class_sparse_matrix.xhtml) [operator\*](class_sparse_matrix.xhtml#a1c232ae958ce411881b151d1f185f0f0) ([SymSparseMatrix](class_sym_sparse_matrix.xhtml) x) const overrides the sparsematrix-densematrix product. [More...](#a1c232ae958ce411881b151d1f185f0f0)  
 [SparseMatrix](class_sparse_matrix.xhtml) [operator\*](class_sparse_matrix.xhtml#a431a04a11674d950508f3439410e552a) (number x) const overrides the sparsematrix-scalar product. [More...](#a431a04a11674d950508f3439410e552a)  
 [SparseMatrix](class_sparse_matrix.xhtml) [operator+](class_sparse_matrix.xhtml#aae646a4650f9980281054ab377fdc7da) ([SparseMatrix](class_sparse_matrix.xhtml) x) const overrides the sparsematrix-sparsematrix sum. [More...](#aae646a4650f9980281054ab377fdc7da)  
 [SparseMatrix](class_sparse_matrix.xhtml) [operator-](class_sparse_matrix.xhtml#a3bd5687e27d7ce922c4caaf27928215a) ([SparseMatrix](class_sparse_matrix.xhtml) x) const overrides the sparsematrix-sparsematrix subtraction. [More...](#a3bd5687e27d7ce922c4caaf27928215a)  
 [SparseMatrix](class_sparse_matrix.xhtml) [operator/](class_sparse_matrix.xhtml#a5949931c82a009b294722821b241c9cd) (number x) const overrides the sparsematrix-scalar division. [More...](#a5949931c82a009b294722821b241c9cd)  
 [TUnsignedIndex](class_sparse_matrix.xhtml#ab423d248b103558b5903c702c13335ae) [OuterSize](class_sparse_matrix.xhtml#ac8a32a382f45637a59b805a83a1399e0) ()  [Print](class_sparse_matrix.xhtml#a6a84471f08e9842cce4e162251b007fd) (string format="%g ", TUnsignedIndex rows=20, [TUnsignedIndex](class_sparse_matrix.xhtml#ab423d248b103558b5903c702c13335ae) cols=20) const  [Prune](class_sparse_matrix.xhtml#a611c0976718f1af2c5a0ee6a0f5e34a0) ([TScalar](class_sparse_matrix.xhtml#ad33f729eda6e45412aac71cee702012f) reference) sets all elements with absolute value &lt;= reference to zero and removes from storage [More...](#a611c0976718f1af2c5a0ee6a0f5e34a0)  
  [Prune](class_sparse_matrix.xhtml#a95e65c4fd2e14633c28dc24b75811ef7) ([TScalar](class_sparse_matrix.xhtml#ad33f729eda6e45412aac71cee702012f) reference, [TScalar](class_sparse_matrix.xhtml#ad33f729eda6e45412aac71cee702012f) epsilon) sets all elements with absolute value &lt;= reference\*epsilon to zero and removes from storage [More...](#a95e65c4fd2e14633c28dc24b75811ef7)  
 [TUnsignedIndex](class_sparse_matrix.xhtml#ab423d248b103558b5903c702c13335ae) [Rows](class_sparse_matrix.xhtml#a3c3320817fc198207404606c52e47fa3) () <a id="a82fdaa2aa8c930c663007796e9aaa03f"></a> [SetZero](class_sparse_matrix.xhtml#a82fdaa2aa8c930c663007796e9aaa03f) () sets the object to zero (clears it)   
  [SparseMatrix](class_sparse_matrix.xhtml#ad511a44fa858c31088fc81ab3c87fadb) ([SparseMatrix](class_sparse_matrix.xhtml) ar, bool keep\_data=true) destructor [More...](#ad511a44fa858c31088fc81ab3c87fadb)  
  [SparseMatrix](class_sparse_matrix.xhtml#a023309859f06d1a3d62ad7db794c1e71) ([SparseMatrix](class_sparse_matrix.xhtml) ar) copy constructor [More...](#a023309859f06d1a3d62ad7db794c1e71)  
  [SparseMatrix](class_sparse_matrix.xhtml#a883abf2e70947dcc777928a1893cbea1) ([SymSparseMatrix](class_sym_sparse_matrix.xhtml) ar, bool keep\_data=true) copy constructor [More...](#a883abf2e70947dcc777928a1893cbea1)  
  [SparseMatrix](class_sparse_matrix.xhtml#a61c85bfe820f533d4bd20c5ab8f036a8) ([SymSparseMatrix](class_sym_sparse_matrix.xhtml) ar) copy constructor [More...](#a61c85bfe820f533d4bd20c5ab8f036a8)  
  [Swap](class_sparse_matrix.xhtml#a22650e9fcd4c542332969018cfac399c) ([SparseMatrix](class_sparse_matrix.xhtml) other) swaps the contents of this object [More...](#a22650e9fcd4c542332969018cfac399c)  
 [SparseMatrix](class_sparse_matrix.xhtml) [Transpose](class_sparse_matrix.xhtml#a0db67d3df1b6115c2e30688695bc42d2) () <a id="details" name="details"></a>## Detailed Description

encapsulates sparse matrix classes and algorithms

The default sparse matrix class.

A class that represents storage for sparse linalg objects.

The storage scheme is column\_major (FORTRAN). It basically provides basic features of the Eigen::SparseMatrix class, i.e. basic operators and assembly methods.

The storage scheme is described in the Eigen doc in detail. It should be noted that one can view any column\_major sparse matrix as a set of 'ncols' sparse vectors. The sparse vectors are labeled by the index i ("outer iterator"), the access of the elements within these vectors is labeled by the Eigen::InnerIterator object it ("inner iterator"/ "inner loop").



## Constructor &amp; Destructor Documentation

<a id="ad511a44fa858c31088fc81ab3c87fadb"></a>## [◆ ](#ad511a44fa858c31088fc81ab3c87fadb)SparseMatrix() \[1/4\]

 [SparseMatrix](class_sparse_matrix.xhtml)  ( [SparseMatrix](class_sparse_matrix.xhtml)  *ar*,    bool  *keep\_data* = `true`   ) 

destructor

copy constructor

Parameters arthe source matrix keep\_dataif true, the data is copied. if false, the source matrix is empty on output 



<a id="a023309859f06d1a3d62ad7db794c1e71"></a>## [◆ ](#a023309859f06d1a3d62ad7db794c1e71)SparseMatrix() \[2/4\]

 [SparseMatrix](class_sparse_matrix.xhtml)  ( [SparseMatrix](class_sparse_matrix.xhtml)  *ar*) 

copy constructor

Parameters arthe source matrix 



<a id="a883abf2e70947dcc777928a1893cbea1"></a>## [◆ ](#a883abf2e70947dcc777928a1893cbea1)SparseMatrix() \[3/4\]

 [SparseMatrix](class_sparse_matrix.xhtml)  ( [SymSparseMatrix](class_sym_sparse_matrix.xhtml)  *ar*,    bool  *keep\_data* = `true`   ) 

copy constructor

Parameters arthe source matrix keep\_dataif true, the data is copied. if false, the source matrix is empty on output 



<a id="a61c85bfe820f533d4bd20c5ab8f036a8"></a>## [◆ ](#a61c85bfe820f533d4bd20c5ab8f036a8)SparseMatrix() \[4/4\]

 [SparseMatrix](class_sparse_matrix.xhtml)  ( [SymSparseMatrix](class_sym_sparse_matrix.xhtml)  *ar*) 

copy constructor

Parameters arthe source matrix 



## Member Function Documentation

<a id="ae8b8bc59ac6df86a5e2b7a529754e5c2"></a>## [◆ ](#ae8b8bc59ac6df86a5e2b7a529754e5c2)\_\_neg\_\_()

 [SparseMatrix](class_sparse_matrix.xhtml) \_\_neg\_\_  ( )  const

Returnsunary minus of this matrix (return B = -A) 



<a id="adb0255d0035cd7783ce87f41a3cae5cc"></a>## [◆ ](#adb0255d0035cd7783ce87f41a3cae5cc)\_\_str\_\_()

 char\* \_\_str\_\_  ( ) 

transforms this objects into a string, required by SWIG

Returnsa 0-terminated string. 



<a id="a37225e979eb1257d6498c58b1902ebcf"></a>## [◆ ](#a37225e979eb1257d6498c58b1902ebcf)Assign()

 Assign  ( [SparseMatrix](class_sparse_matrix.xhtml)  *other*,    bool  *keep\_data* = `true`   ) 

assigns a new matrix to this object

Parameters otherthe other sparse matrix keep\_dataif false, then the other matrix will be cleared 



<a id="aded57b8e31e935d93fefe03f80a4da0a"></a>## [◆ ](#aded57b8e31e935d93fefe03f80a4da0a)Col()

 [SparseMatrix](class_sparse_matrix.xhtml) Col  ( int  *col*)  const

extracts a column of this matrix (as a copy, not a reference!)

Parameters colthe column index Returnsthe column as sparse matrix 



<a id="a2ec082b93696e449a0eb430f1f00196e"></a>## [◆ ](#a2ec082b93696e449a0eb430f1f00196e)Cols()

 [TUnsignedIndex](class_sparse_matrix.xhtml#ab423d248b103558b5903c702c13335ae) Cols  ( ) 

Returnsthe number of columns 



<a id="a9c5c5e3890bd6be32b732fa324ca0954"></a>## [◆ ](#a9c5c5e3890bd6be32b732fa324ca0954)InnerSize()

 [TUnsignedIndex](class_sparse_matrix.xhtml#ab423d248b103558b5903c702c13335ae) InnerSize  ( ) 

Returnsthe number of inner elements 



<a id="ac83d198cac5c9247233b7024b3f2742a"></a>## [◆ ](#ac83d198cac5c9247233b7024b3f2742a)NonZeros()

 [TUnsignedIndex](class_sparse_matrix.xhtml#ab423d248b103558b5903c702c13335ae) NonZeros  ( ) 

Returnsthe number of nonzeros 



<a id="ac20eea8e7bdf72e549208fcb4ad002f9"></a>## [◆ ](#ac20eea8e7bdf72e549208fcb4ad002f9)operator\*() \[1/5\]

 [Matrix](class_matrix.xhtml) operator\*  ( [Matrix](class_matrix.xhtml)  *x*)  const

overrides the sparsematrix-densematrix product.

Parameters xthe other matrix Returns(\*this)\*x 



<a id="ab20c65b6af96b99b1154078d249d8e38"></a>## [◆ ](#ab20c65b6af96b99b1154078d249d8e38)operator\*() \[2/5\]

 [Matrix](class_matrix.xhtml) operator\*  ( [MatrixBlock](class_matrix_block.xhtml)  *x*)  const

overrides the sparsematrix-densematrix product.

Parameters xthe other matrix Returns(\*this)\*x 



<a id="a5753e8d3adb4ab7a445a13e64e537c49"></a>## [◆ ](#a5753e8d3adb4ab7a445a13e64e537c49)operator\*() \[3/5\]

 [SparseMatrix](class_sparse_matrix.xhtml) operator\*  ( [SparseMatrix](class_sparse_matrix.xhtml)  *x*)  const

overrides the sparsematrix-densematrix product.

Parameters xthe other matrix Returns(\*this)\*x 



<a id="a1c232ae958ce411881b151d1f185f0f0"></a>## [◆ ](#a1c232ae958ce411881b151d1f185f0f0)operator\*() \[4/5\]

 [SparseMatrix](class_sparse_matrix.xhtml) operator\*  ( [SymSparseMatrix](class_sym_sparse_matrix.xhtml)  *x*)  const

overrides the sparsematrix-densematrix product.

Parameters xthe other matrix Returns(\*this)\*x 



<a id="a431a04a11674d950508f3439410e552a"></a>## [◆ ](#a431a04a11674d950508f3439410e552a)operator\*() \[5/5\]

 [SparseMatrix](class_sparse_matrix.xhtml) operator\*  ( number  *x*)  const

overrides the sparsematrix-scalar product.

Parameters xthe other matrix Returns(\*this)\*x 



<a id="aae646a4650f9980281054ab377fdc7da"></a>## [◆ ](#aae646a4650f9980281054ab377fdc7da)operator+()

 [SparseMatrix](class_sparse_matrix.xhtml) operator+  ( [SparseMatrix](class_sparse_matrix.xhtml)  *x*)  const

overrides the sparsematrix-sparsematrix sum.

Parameters xthe other matrix Returns(\*this)+x 



<a id="a3bd5687e27d7ce922c4caaf27928215a"></a>## [◆ ](#a3bd5687e27d7ce922c4caaf27928215a)operator-()

 [SparseMatrix](class_sparse_matrix.xhtml) operator-  ( [SparseMatrix](class_sparse_matrix.xhtml)  *x*)  const

overrides the sparsematrix-sparsematrix subtraction.

Parameters xthe other matrix Returns(\*this)-x 



<a id="a5949931c82a009b294722821b241c9cd"></a>## [◆ ](#a5949931c82a009b294722821b241c9cd)operator/()

 [SparseMatrix](class_sparse_matrix.xhtml) operator/  ( number  *x*)  const

overrides the sparsematrix-scalar division.

Parameters xthe other matrix Returns(\*this)/x 



<a id="ac8a32a382f45637a59b805a83a1399e0"></a>## [◆ ](#ac8a32a382f45637a59b805a83a1399e0)OuterSize()

 [TUnsignedIndex](class_sparse_matrix.xhtml#ab423d248b103558b5903c702c13335ae) OuterSize  ( ) 

Returnsthe number of sparse column vectors 



<a id="a6a84471f08e9842cce4e162251b007fd"></a>## [◆ ](#a6a84471f08e9842cce4e162251b007fd)Print()

 Print  ( string  *format* = `"%g "`,    [TUnsignedIndex](class_sparse_matrix.xhtml#ab423d248b103558b5903c702c13335ae)  *rows* = `20`,    [TUnsignedIndex](class_sparse_matrix.xhtml#ab423d248b103558b5903c702c13335ae)  *cols* = `20`   )  const

prints the sparse matrix up to the given rows and columns (internally a temporary dense matrix will be created)

Parameters formatthe format of a single element, see *printf* for details rowsonly print the first *rows* rows colsonly print the first *cols* columns 



<a id="a611c0976718f1af2c5a0ee6a0f5e34a0"></a>## [◆ ](#a611c0976718f1af2c5a0ee6a0f5e34a0)Prune() \[1/2\]

 Prune  ( [TScalar](class_sparse_matrix.xhtml#ad33f729eda6e45412aac71cee702012f)  *reference*) 

sets all elements with absolute value &lt;= reference to zero and removes from storage

Parameters referencethe reference value 



<a id="a95e65c4fd2e14633c28dc24b75811ef7"></a>## [◆ ](#a95e65c4fd2e14633c28dc24b75811ef7)Prune() \[2/2\]

 Prune  ( [TScalar](class_sparse_matrix.xhtml#ad33f729eda6e45412aac71cee702012f)  *reference*,    [TScalar](class_sparse_matrix.xhtml#ad33f729eda6e45412aac71cee702012f)  *epsilon*   ) 

sets all elements with absolute value &lt;= reference\*epsilon to zero and removes from storage

Parameters referencethe reference value epsilonthe tolerance epsilon 



<a id="a3c3320817fc198207404606c52e47fa3"></a>## [◆ ](#a3c3320817fc198207404606c52e47fa3)Rows()

 [TUnsignedIndex](class_sparse_matrix.xhtml#ab423d248b103558b5903c702c13335ae) Rows  ( ) 

Returnsthe number of rows 



<a id="a22650e9fcd4c542332969018cfac399c"></a>## [◆ ](#a22650e9fcd4c542332969018cfac399c)Swap()

 Swap  ( [SparseMatrix](class_sparse_matrix.xhtml)  *other*) 

swaps the contents of this object

Parameters otherthe other sparse matrix 



<a id="a0db67d3df1b6115c2e30688695bc42d2"></a>## [◆ ](#a0db67d3df1b6115c2e30688695bc42d2)Transpose()

 [SparseMatrix](class_sparse_matrix.xhtml) Transpose  ( ) 

Returnsthe transpose