@ -23,7 +23,7 @@ namespace sparse {
/*!
* A sparse matrix class with a constant number of non - zero entries on the non - diagonal fields
* and a sepe rate dense storage for the diagonal elements .
* and a sepa rate dense storage for the diagonal elements .
* NOTE : Addressing * is * zero - based , so the valid range for getValue and addNextValue is 0. . ( rows - 1 )
* where rows is the first argument to the constructor .
*/
@ -49,10 +49,10 @@ public:
* @ param rows The number of rows of the matrix
*/
StaticSparseMatrix ( uint_fast64_t rows )
: internal_s tatus ( MatrixStatus : : UnInitialized ) ,
current_s ize ( 0 ) , last_r ow ( 0 ) , value_s torage ( nullptr ) ,
diagonal_s torage ( nullptr ) , column_i ndications ( nullptr ) ,
row_i ndications ( nullptr ) , row_c ount ( rows ) , non_zero_entry_c ount ( 0 ) { }
: internalS tatus ( MatrixStatus : : UnInitialized ) ,
currentS ize ( 0 ) , lastR ow ( 0 ) , valueS torage ( nullptr ) ,
diagonalS torage ( nullptr ) , columnI ndications ( nullptr ) ,
rowI ndications ( nullptr ) , rowC ount ( rows ) , nonZeroEntryC ount ( 0 ) { }
/ / ! Copy Constructor
/*!
@ -60,14 +60,14 @@ public:
* @ param ssm A reference to the matrix to be copied .
*/
StaticSparseMatrix ( const StaticSparseMatrix < T > & ssm )
: internal_s tatus ( ssm . internal_s tatus ) ,
current_s ize ( ssm . current_size ) , last_r ow ( ssm . last_r ow ) ,
row_c ount ( ssm . row_c ount ) ,
non_zero_entry_c ount ( ssm . non_zero_entry_c ount ) {
: internalS tatus ( ssm . internalS tatus ) ,
currentS ize ( ssm . currentSize ) , lastR ow ( ssm . lastR ow ) ,
rowC ount ( ssm . rowC ount ) ,
nonZeroEntryC ount ( ssm . nonZeroEntryC ount ) {
pantheios : : log_DEBUG ( " StaticSparseMatrix::CopyConstructor: Using copy constructor. " ) ;
/ / Check whether copying the matrix is safe .
if ( ! ssm . hasError ( ) ) {
pantheios : : log_ERROR ( " StaticSparseMatrix::CopyCtor: Throwing invalid_argument: Can not C opy from matrix in error state. " ) ;
pantheios : : log_ERROR ( " StaticSparseMatrix::CopyConstructor: Throwing invalid_argument: Can not c opy from matrix in error state. " ) ;
throw mrmc : : exceptions : : invalid_argument ( ) ;
} else {
/ / Try to prepare the internal storage and throw an error in case
@ -81,19 +81,19 @@ public:
/ / copy them seperately in order to invoke copy their copy
/ / constructor . This may not be necessary , but it is safer to
/ / do so in any case .
for ( uint_fast64_t i = 0 ; i < non_zero_entry_c ount ; + + i ) {
for ( uint_fast64_t i = 0 ; i < nonZeroEntryC ount ; + + i ) {
/ / use T ( ) to force use of the copy constructor for complex T types
value_s torage [ i ] = T ( ssm . value_s torage [ i ] ) ;
valueS torage [ i ] = T ( ssm . valueS torage [ i ] ) ;
}
for ( uint_fast64_t i = 0 ; i < = row_c ount ; + + i ) {
for ( uint_fast64_t i = 0 ; i < = rowC ount ; + + i ) {
/ / use T ( ) to force use of the copy constructor for complex T types
diagonal_s torage [ i ] = T ( ssm . diagonal_s torage [ i ] ) ;
diagonalS torage [ i ] = T ( ssm . diagonalS torage [ i ] ) ;
}
/ / The elements that are not of the value type but rather the
/ / index type may be copied with memcpy .
memcpy ( column_i ndications , ssm . column_i ndications , sizeof ( column_i ndications [ 0 ] ) * non_zero_entry_c ount ) ;
memcpy ( row_i ndications , ssm . row_i ndications , sizeof ( row_i ndications [ 0 ] ) * ( row_c ount + 1 ) ) ;
memcpy ( columnI ndications , ssm . columnI ndications , sizeof ( columnI ndications [ 0 ] ) * nonZeroEntryC ount ) ;
memcpy ( rowI ndications , ssm . rowI ndications , sizeof ( rowI ndications [ 0 ] ) * ( rowC ount + 1 ) ) ;
}
}
}
@ -104,21 +104,21 @@ public:
*/
~ StaticSparseMatrix ( ) {
setState ( MatrixStatus : : UnInitialized ) ;
if ( value_s torage ! = NULL ) {
if ( valueS torage ! = NULL ) {
/ / free ( value_storage ) ;
delete [ ] value_s torage ;
delete [ ] valueS torage ;
}
if ( column_i ndications ! = NULL ) {
if ( columnI ndications ! = NULL ) {
/ / free ( column_indications ) ;
delete [ ] column_i ndications ;
delete [ ] columnI ndications ;
}
if ( row_i ndications ! = NULL ) {
if ( rowI ndications ! = NULL ) {
/ / free ( row_indications ) ;
delete [ ] row_i ndications ;
delete [ ] rowI ndications ;
}
if ( diagonal_s torage ! = NULL ) {
if ( diagonalS torage ! = NULL ) {
/ / free ( diagonal_storage ) ;
delete [ ] diagonal_s torage ;
delete [ ] diagonalS torage ;
}
}
@ -128,27 +128,28 @@ public:
* NOTE : Calling this method before any other member function is mandatory .
* This version is to be used together with addNextValue ( ) . For
* initialization from an Eigen SparseMatrix , use initialize ( Eigen : : SparseMatrix < T > & ) .
* @ param non_zero_entries
* @ param nonZeroEntries The number of non - zero entries that are not on the
* diagonal .
*/
void initialize ( uint_fast64_t non_zero_e ntries ) {
void initialize ( uint_fast64_t nonZeroE ntries ) {
/ / Check whether initializing the matrix is safe .
if ( internal_s tatus ! = MatrixStatus : : UnInitialized ) {
if ( internalS tatus ! = MatrixStatus : : UnInitialized ) {
triggerErrorState ( ) ;
pantheios : : log_ERROR ( " StaticSparseMatrix::initialize: Throwing invalid state for status flag != 0 (is " , pantheios : : integer ( internal_s tatus ) , " - Already initialized? " ) ;
pantheios : : log_ERROR ( " StaticSparseMatrix::initialize: Throwing invalid state for status flag != 0 (is " , pantheios : : integer ( internalS tatus ) , " - Already initialized? " ) ;
throw mrmc : : exceptions : : invalid_state ( " StaticSparseMatrix::initialize: Invalid state for status flag != 0 - Already initialized? " ) ;
} else if ( row_c ount = = 0 ) {
} else if ( rowC ount = = 0 ) {
triggerErrorState ( ) ;
pantheios : : log_ERROR ( " StaticSparseMatrix::initialize: Throwing invalid_argument for row_count = 0 " ) ;
throw mrmc : : exceptions : : invalid_argument ( " mrmc::StaticSparseMatrix::initialize: Matrix with 0 rows is not reasonable " ) ;
} else if ( ( ( row_count * row_count ) - row_count ) < non_zero_e ntries ) {
} else if ( ( ( rowCount * rowCount ) - rowCount ) < nonZeroE ntries ) {
triggerErrorState ( ) ;
pantheios : : log_ERROR ( " StaticSparseMatrix::initialize: Throwing invalid_argument: More non-zero entries than entries in target matrix " ) ;
throw mrmc : : exceptions : : invalid_argument ( " mrmc::StaticSparseMatrix::initialize: More non-zero entries than entries in target matrix " ) ;
} else {
/ / If it is safe , initialize necessary members and prepare the
/ / internal storage .
non_zero_entry_count = non_zero_e ntries ;
last_r ow = 0 ;
nonZeroEntryCount = nonZeroE ntries ;
lastR ow = 0 ;
if ( ! prepareInternalStorage ( ) ) {
triggerErrorState ( ) ;
@ -166,21 +167,21 @@ public:
* This version is only to be used when copying an Eigen sparse matrix . For
* initialization with addNextValue ( ) and finalize ( ) use initialize ( uint_fast32_t )
* instead .
* @ param eigen_sparse_m atrix The Eigen sparse matrix to be copied .
* @ param eigenSparseM atrix The Eigen sparse matrix to be copied .
* * NOTE * Has to be in compressed form !
*/
template < int _Options , typename _Index >
void initialize ( const Eigen : : SparseMatrix < T , _Options , _Index > & eigen_sparse_m atrix ) {
void initialize ( const Eigen : : SparseMatrix < T , _Options , _Index > & eigenSparseM atrix) {
/ / Throw an error in case the matrix is not in compressed format .
if ( ! eigen_sparse_m atrix . isCompressed ( ) ) {
if ( ! eigenSparseM atrix . isCompressed ( ) ) {
triggerErrorState ( ) ;
pantheios : : log_ERROR ( " StaticSparseMatrix::initialize: Throwing invalid_argument: eigen_sparse_matrix is not in Compressed form. " ) ;
throw mrmc : : exceptions : : invalid_argument ( " StaticSparseMatrix::initialize: Throwing invalid_argument: eigen_sparse_matrix is not in Compressed form. " ) ;
}
/ / Compute the actual ( i . e . non - diagonal ) number of non - zero entries .
non_zero_entry_c ount = getEigenSparseMatrixCorrectNonZeroEntryCount ( eigen_sparse_m atrix ) ;
last_r ow = 0 ;
nonZeroEntryC ount = getEigenSparseMatrixCorrectNonZeroEntryCount ( eigenSparseM atrix ) ;
lastR ow = 0 ;
/ / Try to prepare the internal storage and throw an error in case of
/ / failure .
@ -191,29 +192,28 @@ public:
throw std : : bad_alloc ( ) ;
} else {
/ / Get necessary pointers to the contents of the Eigen matrix .
const T * valuePtr = eigen_sparse_m atrix . valuePtr ( ) ;
const _Index * indexPtr = eigen_sparse_m atrix . innerIndexPtr ( ) ;
const _Index * outerPtr = eigen_sparse_m atrix . outerIndexPtr ( ) ;
const T * valuePtr = eigenSparseM atrix . valuePtr ( ) ;
const _Index * indexPtr = eigenSparseM atrix . innerIndexPtr ( ) ;
const _Index * outerPtr = eigenSparseM atrix . outerIndexPtr ( ) ;
/ / If the given matrix is in RowMajor format , copying can simply
/ / be done by adding all values in order .
/ / Direct copying is , however , prevented because we have to
/ / separate the diagonal entries from others .
if ( isEigenRowMajor ( eigen_sparse_m atrix ) ) {
if ( isEigenRowMajor ( eigenSparseM atrix ) ) {
/ / Because of the RowMajor format outerSize evaluates to the
/ / number of rows .
const _Index rowCount = eigen_sparse_m atrix . outerSize ( ) ;
const _Index rowCount = eigenSparseM atrix . outerSize ( ) ;
for ( _Index row = 0 ; row < rowCount ; + + row ) {
for ( _Index col = outerPtr [ row ] ; col < outerPtr [ row + 1 ] ;
+ + col ) {
for ( _Index col = outerPtr [ row ] ; col < outerPtr [ row + 1 ] ; + + col ) {
addNextValue ( row , indexPtr [ col ] , valuePtr [ col ] ) ;
}
}
} else {
const _Index entryCount = eigen_sparse_m atrix . nonZeros ( ) ;
const _Index entryCount = eigenSparseM atrix . nonZeros ( ) ;
/ / Because of the ColMajor format outerSize evaluates to the
/ / number of columns .
const _Index colCount = eigen_sparse_m atrix . outerSize ( ) ;
const _Index colCount = eigenSparseM atrix . outerSize ( ) ;
/ / Create an array to remember which elements have to still
/ / be searched in each column and initialize it with the starting
@ -237,17 +237,17 @@ public:
addNextValue ( currentRow , currentColumn ,
valuePtr [ positions [ currentColumn ] ] ) ;
/ / Remember that we found one more non - zero element .
i + + ;
+ + i ;
/ / Mark this position as " used " .
positions [ currentColumn ] + + ;
+ + positions [ currentColumn ] ;
}
/ / Now we can advance to the next column and also row ,
/ / in case we just iterated through the last column .
currentColumn + + ;
+ + currentColumn ;
if ( currentColumn = = colCount ) {
currentColumn = 0 ;
currentRow + + ;
+ + currentRow ;
}
}
delete [ ] positions ;
@ -267,31 +267,31 @@ public:
void addNextValue ( const uint_fast64_t row , const uint_fast64_t col , const T & value ) {
/ / Check whether the given row and column positions are valid and throw
/ / error otherwise .
if ( ( row > row_c ount ) | | ( col > row_c ount ) ) {
if ( ( row > rowC ount ) | | ( col > rowC ount ) ) {
triggerErrorState ( ) ;
pantheios : : log_ERROR ( " StaticSparseMatrix::addNextValue: Throwing out_of_range: row or col not in 0 .. rows (is " ,
pantheios : : integer ( row ) , " x " , pantheios : : integer ( col ) , " , max is " ,
pantheios : : integer ( row_c ount ) , " x " , pantheios : : integer ( row_c ount ) , " ). " ) ;
pantheios : : integer ( rowC ount ) , " x " , pantheios : : integer ( rowC ount ) , " ). " ) ;
throw mrmc : : exceptions : : out_of_range ( " StaticSparseMatrix::addNextValue: row or col not in 0 .. rows " ) ;
}
if ( row = = col ) { / / Set a diagonal element .
diagonal_s torage [ row ] = value ;
diagonalS torage [ row ] = value ;
} else { / / Set a non - diagonal element .
/ / If we switched to another row , we have to adjust the missing
/ / entries in the row_indications array .
if ( row ! = last_r ow ) {
for ( uint_fast64_t i = last_r ow + 1 ; i < = row ; + + i ) {
row_i ndications [ i ] = current_s ize ;
if ( row ! = lastR ow ) {
for ( uint_fast64_t i = lastR ow + 1 ; i < = row ; + + i ) {
rowI ndications [ i ] = currentS ize ;
}
last_r ow = row ;
lastR ow = row ;
}
/ / Finally , set the element and increase the current size .
value_storage [ current_s ize ] = value ;
column_indications [ current_s ize ] = col ;
valueStorage [ currentS ize ] = value ;
columnIndications [ currentS ize ] = col ;
current_size + + ;
+ + currentSize ;
}
}
@ -305,18 +305,18 @@ public:
if ( ! isInitialized ( ) ) {
triggerErrorState ( ) ;
pantheios : : log_ERROR ( " StaticSparseMatrix::finalize: Throwing invalid state for internal state not Initialized (is " ,
pantheios : : integer ( internal_s tatus ) , " - Already finalized? " ) ;
pantheios : : integer ( internalS tatus ) , " - Already finalized? " ) ;
throw mrmc : : exceptions : : invalid_state ( " StaticSparseMatrix::finalize: Invalid state for internal state not Initialized - Already finalized? " ) ;
} else if ( current_size ! = non_zero_entry_c ount ) {
} else if ( currentSize ! = nonZeroEntryC ount ) {
triggerErrorState ( ) ;
pantheios : : log_ERROR ( " StaticSparseMatrix::finalize: Throwing invalid_state: Wrong call count for addNextValue " ) ;
throw mrmc : : exceptions : : invalid_state ( " StaticSparseMatrix::finalize: Wrong call count for addNextValue " ) ;
} else {
/ / Fill in the missing entries in the row_indications array .
/ / ( Can happen because of empty rows at the end . )
if ( last_row ! = row_c ount ) {
for ( uint_fast64_t i = last_r ow + 1 ; i < = row_c ount ; + + i ) {
row_i ndications [ i ] = current_s ize ;
if ( lastRow ! = rowC ount ) {
for ( uint_fast64_t i = lastR ow + 1 ; i < = rowC ount ; + + i ) {
rowI ndications [ i ] = currentS ize ;
}
}
@ -324,7 +324,7 @@ public:
/ / array . This eases iteration work , as now the indices of row i
/ / are always between row_indications [ i ] and row_indications [ i + 1 ] ,
/ / also for the first and last row .
row_indications [ row_c ount + 1 ] = non_zero_entry_c ount ;
rowIndications [ rowC ount + 1 ] = nonZeroEntryC ount ;
setState ( MatrixStatus : : ReadReady ) ;
}
@ -342,37 +342,37 @@ public:
*/
inline bool getValue ( uint_fast64_t row , uint_fast64_t col , T * const target ) {
/ / Check for illegal access indices .
if ( ( row > row_c ount ) | | ( col > row_c ount ) ) {
if ( ( row > rowC ount ) | | ( col > rowC ount ) ) {
pantheios : : log_ERROR ( " StaticSparseMatrix::getValue: row or col not in 0 .. rows (is " , pantheios : : integer ( row ) , " x " ,
pantheios : : integer ( col ) , " , max is " , pantheios : : integer ( row_c ount ) , " x " , pantheios : : integer ( row_c ount ) , " ). " ) ;
pantheios : : integer ( col ) , " , max is " , pantheios : : integer ( rowC ount ) , " x " , pantheios : : integer ( rowC ount ) , " ). " ) ;
throw mrmc : : exceptions : : out_of_range ( " StaticSparseMatrix::getValue: row or col not in 0 .. rows " ) ;
return false ;
}
/ / Read elements on the diagonal directly .
if ( row = = col ) {
* target = diagonal_s torage [ row ] ;
* target = diagonalS torage [ row ] ;
return true ;
}
/ / In case the element is not on the diagonal , we have to iterate
/ / over the accessed row to find the element .
uint_fast64_t row_start = row_i ndications [ row ] ;
uint_fast64_t row_end = row_i ndications [ row + 1 ] ;
while ( row_start < row_e nd ) {
uint_fast64_t rowStart = rowI ndications [ row ] ;
uint_fast64_t rowEnd = rowI ndications [ row + 1 ] ;
while ( rowStart < rowE nd ) {
/ / If the lement is found , write the content to the specified
/ / position and return true .
if ( column_indications [ row_s tart ] = = col ) {
* target = value_storage [ row_s tart ] ;
if ( columnIndications [ rowS tart ] = = col ) {
* target = valueStorage [ rowS tart ] ;
return true ;
}
/ / If the column of the current element is already larger than the
/ / requested column , the requested element cannot be contained
/ / in the matrix and we may therefore stop searching .
if ( column_indications [ row_s tart ] > col ) {
if ( columnIndications [ rowS tart ] > col ) {
break ;
}
row_start + + ;
+ + rowStart ;
}
/ / Set 0 as the content and return false in case the element was not found .
@ -384,7 +384,7 @@ public:
* Returns the number of rows of the matrix .
*/
uint_fast64_t getRowCount ( ) const {
return row_c ount ;
return rowC ount ;
}
/*!
@ -393,7 +393,7 @@ public:
* @ return A pointer to the value storage of the matrix .
*/
T * getStoragePointer ( ) const {
return value_s torage ;
return valueS torage ;
}
/*!
@ -401,7 +401,7 @@ public:
* @ return A pointer to the storage of elements on the diagonal .
*/
T * getDiagonalStoragePointer ( ) const {
return diagonal_s torage ;
return diagonalS torage ;
}
/*!
@ -411,7 +411,7 @@ public:
* entries in the value storage for each row .
*/
uint_fast64_t * getRowIndicationsPointer ( ) const {
return row_i ndications ;
return rowI ndications ;
}
/*!
@ -421,7 +421,7 @@ public:
* element that is not on the diagonal .
*/
uint_fast64_t * getColumnIndicationsPointer ( ) const {
return column_i ndications ;
return columnI ndications ;
}
/*!
@ -431,7 +431,7 @@ public:
* reading access .
*/
bool isReadReady ( ) {
return ( internal_s tatus = = MatrixStatus : : ReadReady ) ;
return ( internalS tatus = = MatrixStatus : : ReadReady ) ;
}
/*!
@ -440,7 +440,7 @@ public:
* @ return True iff the matrix was initialized previously .
*/
bool isInitialized ( ) {
return ( internal_s tatus = = MatrixStatus : : Initialized | | internal_s tatus = = MatrixStatus : : ReadReady ) ;
return ( internalS tatus = = MatrixStatus : : Initialized | | internalS tatus = = MatrixStatus : : ReadReady ) ;
}
/*!
@ -448,7 +448,7 @@ public:
* @ return The internal state of the matrix .
*/
MatrixStatus getState ( ) {
return internal_s tatus ;
return internalS tatus ;
}
/*!
@ -456,7 +456,7 @@ public:
* @ return True iff the internal state of the matrix signals an error .
*/
bool hasError ( ) {
return ( internal_s tatus = = MatrixStatus : : Error ) ;
return ( internalS tatus = = MatrixStatus : : Error ) ;
}
/*!
@ -468,11 +468,11 @@ public:
/ / Check whether it is safe to export this matrix .
if ( ! isReadReady ( ) ) {
triggerErrorState ( ) ;
pantheios : : log_ERROR ( " StaticSparseMatrix::toEigenSparseMatrix: Throwing invalid state for internal state not ReadReady (is " , pantheios : : integer ( internal_s tatus ) , " ). " ) ;
pantheios : : log_ERROR ( " StaticSparseMatrix::toEigenSparseMatrix: Throwing invalid state for internal state not ReadReady (is " , pantheios : : integer ( internalS tatus ) , " ). " ) ;
throw mrmc : : exceptions : : invalid_state ( " StaticSparseMatrix::toEigenSparseMatrix: Invalid state for internal state not ReadReady. " ) ;
} else {
/ / Create a
int_fast32_t eigenRows = static_cast < int_fast32_t > ( row_c ount ) ;
int_fast32_t eigenRows = static_cast < int_fast32_t > ( rowC ount ) ;
Eigen : : SparseMatrix < T , Eigen : : RowMajor , int_fast32_t > * mat = new Eigen : : SparseMatrix < T , Eigen : : RowMajor , int_fast32_t > ( eigenRows , eigenRows ) ;
/ / There are two ways of converting this matrix to Eigen ' s format .
@ -497,24 +497,24 @@ public:
/ / Prepare the triplet storage .
typedef Eigen : : Triplet < T > IntTriplet ;
std : : vector < IntTriplet > tripletList ;
tripletList . reserve ( non_zero_entry_count + row_c ount ) ;
tripletList . reserve ( nonZeroEntryCount + rowC ount ) ;
/ / First , iterate over all elements that are not on the diagonal
/ / and add the corresponding triplet .
uint_fast64_t row_s tart ;
uint_fast64_t row_e nd ;
for ( uint_fast64_t row = 0 ; row < = row_c ount ; + + row ) {
row_start = row_i ndications [ row ] ;
row_end = row_i ndications [ row + 1 ] ;
while ( row_start < row_e nd ) {
tripletList . push_back ( IntTriplet ( row , column_indications [ row_start ] , value_storage [ row_s tart ] ) ) ;
+ + row_s tart ;
uint_fast64_t rowS tart ;
uint_fast64_t rowE nd ;
for ( uint_fast64_t row = 0 ; row < = rowC ount ; + + row ) {
rowStart = rowI ndications [ row ] ;
rowEnd = rowI ndications [ row + 1 ] ;
while ( rowStart < rowE nd ) {
tripletList . push_back ( IntTriplet ( row , columnIndications [ rowStart ] , valueStorage [ rowS tart ] ) ) ;
+ + rowS tart ;
}
}
/ / Then add the elements on the diagonal .
for ( uint_fast64_t i = 0 ; i < = row_c ount ; + + i ) {
tripletList . push_back ( IntTriplet ( i , i , diagonal_s torage [ i ] ) ) ;
for ( uint_fast64_t i = 0 ; i < = rowC ount ; + + i ) {
tripletList . push_back ( IntTriplet ( i , i , diagonalS torage [ i ] ) ) ;
}
/ / Let Eigen create a matrix from the given list of triplets .
@ -524,23 +524,23 @@ public:
/ / Reserve the average number of non - zero elements per row for each
/ / row .
mat - > reserve ( Eigen : : VectorXi : : Constant ( eigenRows , static_cast < int_fast32_t > ( ( non_zero_entry_count + row_c ount ) / eigenRows ) ) ) ;
mat - > reserve ( Eigen : : VectorXi : : Constant ( eigenRows , static_cast < int_fast32_t > ( ( nonZeroEntryCount + rowC ount ) / eigenRows ) ) ) ;
/ / Iterate over the all non - zero elements in this matrix and add
/ / them to the matrix individually .
uint_fast64_t row_s tart ;
uint_fast64_t row_e nd ;
for ( uint_fast64_t row = 0 ; row < = row_c ount ; + + row ) {
row_start = row_i ndications [ row ] ;
row_end = row_i ndications [ row + 1 ] ;
uint_fast64_t rowS tart ;
uint_fast64_t rowE nd ;
for ( uint_fast64_t row = 0 ; row < = rowC ount ; + + row ) {
rowStart = rowI ndications [ row ] ;
rowEnd = rowI ndications [ row + 1 ] ;
/ / Insert the element on the diagonal .
mat - > insert ( row , row ) = diagonal_s torage [ row ] ;
mat - > insert ( row , row ) = diagonalS torage [ row ] ;
/ / Insert the elements that are not on the diagonal
while ( row_start < row_e nd ) {
mat - > insert ( row , column_indications [ row_s tart ] ) = value_storage [ row_s tart ] ;
+ + row_s tart ;
while ( rowStart < rowE nd ) {
mat - > insert ( row , columnIndications [ rowS tart ] ) = valueStorage [ rowS tart ] ;
+ + rowS tart ;
}
}
# endif / / MRMC_USE_TRIPLETCONVERT
@ -561,7 +561,7 @@ public:
* @ returns The number of non - zero entries that are not on the diagonal .
*/
uint_fast64_t getNonZeroEntryCount ( ) const {
return non_zero_entry_c ount ;
return nonZeroEntryC ount ;
}
/*!
@ -573,24 +573,24 @@ public:
*/
bool makeStateAbsorbing ( const uint_fast64_t state ) {
/ / Check whether the accessed state exists .
if ( state > row_c ount ) {
pantheios : : log_ERROR ( " StaticSparseMatrix::makeStateFinal: state not in 0 .. rows (is " , pantheios : : integer ( state ) , " , max is " , pantheios : : integer ( row_c ount ) , " ). " ) ;
if ( state > rowC ount ) {
pantheios : : log_ERROR ( " StaticSparseMatrix::makeStateFinal: state not in 0 .. rows (is " , pantheios : : integer ( state ) , " , max is " , pantheios : : integer ( rowC ount ) , " ). " ) ;
throw mrmc : : exceptions : : out_of_range ( " StaticSparseMatrix::makeStateFinal: state not in 0 .. rows " ) ;
return false ;
}
/ / Iterate over the elements in the row that are not on the diagonal
/ / and set them to zero .
uint_fast64_t row_start = row_i ndications [ state ] ;
uint_fast64_t row_end = row_i ndications [ state + 1 ] ;
uint_fast64_t rowStart = rowI ndications [ state ] ;
uint_fast64_t rowEnd = rowI ndications [ state + 1 ] ;
while ( row_start < row_e nd ) {
value_storage [ row_s tart ] = mrmc : : misc : : constGetZero ( value_s torage ) ;
row_start + + ;
while ( rowStart < rowE nd ) {
valueStorage [ rowS tart ] = mrmc : : misc : : constGetZero ( valueS torage ) ;
+ + rowStart ;
}
/ / Set the element on the diagonal to one .
diagonal_s torage [ state ] = mrmc : : misc : : constGetOne ( diagonal_s torage ) ;
diagonalS torage [ state ] = mrmc : : misc : : constGetOne ( diagonalS torage ) ;
return true ;
}
@ -600,10 +600,14 @@ public:
*/
uint_fast64_t getSizeInMemory ( ) {
uint_fast64_t size = sizeof ( * this ) ;
size + = sizeof ( T ) * non_zero_entry_count ; / / add value_storage size
size + = sizeof ( T ) * ( row_count + 1 ) ; / / add diagonal_storage size
size + = sizeof ( uint_fast64_t ) * non_zero_entry_count ; / / add column_indications size
size + = sizeof ( uint_fast64_t ) * ( row_count + 1 ) ; / / add row_indications size
/ / Add value_storage size .
size + = sizeof ( T ) * nonZeroEntryCount ;
/ / Add diagonal_storage size .
size + = sizeof ( T ) * ( rowCount + 1 ) ;
/ / Add column_indications size .
size + = sizeof ( uint_fast64_t ) * nonZeroEntryCount ;
/ / Add row_indications size .
size + = sizeof ( uint_fast64_t ) * ( rowCount + 1 ) ;
return size ;
}
@ -612,51 +616,51 @@ private:
/*!
* The number of rows of the matrix .
*/
uint_fast64_t row_c ount ;
uint_fast64_t rowC ount ;
/*!
* The number of non - zero elements that are not on the diagonal .
*/
uint_fast64_t non_zero_entry_c ount ;
uint_fast64_t nonZeroEntryC ount ;
/*!
* Stores all non - zero values that are not on the diagonal .
*/
T * value_s torage ;
T * valueS torage ;
/*!
* Stores all elements on the diagonal , even the ones that are zero .
*/
T * diagonal_s torage ;
T * diagonalS torage ;
/*!
* Stores the column for each non - zero element that is not on the diagonal .
*/
uint_fast64_t * column_i ndications ;
uint_fast64_t * columnI ndications ;
/*!
* Array containing the boundaries ( indices ) in the value_storage array
* for each row . All elements of value_storage with indices between the
* i - th and the ( i + 1 ) - st element of this array belong to row i .
*/
uint_fast64_t * row_i ndications ;
uint_fast64_t * rowI ndications ;
/*!
* The internal status of the matrix .
*/
MatrixStatus internal_s tatus ;
MatrixStatus internalS tatus ;
/*!
* Stores the current number of non - zero elements that have been added to
* the matrix . Used for correctly inserting elements in the matrix .
*/
uint_fast64_t current_s ize ;
uint_fast64_t currentS ize ;
/*!
* Stores the row in which the last element was inserted . Used for correctly
* inserting elements in the matrix .
*/
uint_fast64_t last_r ow ;
uint_fast64_t lastR ow ;
/*!
* Sets the internal status to signal an error .
@ -671,7 +675,7 @@ private:
* @ param new_state The new state to be switched to .
*/
void setState ( const MatrixStatus new_state ) {
internal_status = ( internal_s tatus = = MatrixStatus : : Error ) ? internal_s tatus : new_state ;
internalStatus = ( internalS tatus = = MatrixStatus : : Error ) ? internalS tatus : new_state ;
}
/*!
@ -681,20 +685,20 @@ private:
*/
bool prepareInternalStorage ( ) {
/ / Set up the arrays for the elements that are not on the diagonal .
value_s torage = new ( std : : nothrow ) T [ non_zero_entry_c ount ] ( ) ;
column_i ndications = new ( std : : nothrow ) uint_fast64_t [ non_zero_entry_c ount ] ( ) ;
valueS torage = new ( std : : nothrow ) T [ nonZeroEntryC ount ] ( ) ;
columnI ndications = new ( std : : nothrow ) uint_fast64_t [ nonZeroEntryC ount ] ( ) ;
/ / Set up the row_indications array and reserve one element more than
/ / there are rows in order to put a sentinel element at the end ,
/ / which eases iteration process .
row_i ndications = new ( std : : nothrow ) uint_fast64_t [ row_c ount + 1 ] ( ) ;
rowI ndications = new ( std : : nothrow ) uint_fast64_t [ rowC ount + 1 ] ( ) ;
/ / Set up the array for the elements on the diagonal .
diagonal_s torage = new ( std : : nothrow ) T [ row_c ount ] ( ) ;
diagonalS torage = new ( std : : nothrow ) T [ rowC ount ] ( ) ;
/ / Return whether all the allocations could be made without error .
return ( ( value_s torage ! = NULL ) & & ( column_i ndications ! = NULL )
& & ( row_i ndications ! = NULL ) & & ( diagonal_s torage ! = NULL ) ) ;
return ( ( valueS torage ! = NULL ) & & ( columnI ndications ! = NULL )
& & ( rowI ndications ! = NULL ) & & ( diagonalS torage ! = NULL ) ) ;
}
/*!
@ -728,17 +732,16 @@ private:
* the given Eigen matrix .
*/
template < typename _Scalar , int _Options , typename _Index >
_Index getEigenSparseMatrixCorrectNonZeroEntryCount (
const Eigen : : SparseMatrix < _Scalar , _Options , _Index > & eigen_sparse_matrix ) {
_Index getEigenSparseMatrixCorrectNonZeroEntryCount ( const Eigen : : SparseMatrix < _Scalar , _Options , _Index > & eigen_sparse_matrix ) {
const _Index * indexPtr = eigen_sparse_matrix . innerIndexPtr ( ) ;
const _Index * outerPtr = eigen_sparse_matrix . outerIndexPtr ( ) ;
const _Index entryCount = eigen_sparse_matrix . nonZeros ( ) ;
const _Index outerCount = eigen_sparse_matrix . outerSize ( ) ;
uint_fast64_t diag_non_z eros = 0 ;
uint_fast64_t diagNonZ eros = 0 ;
/ / For RowMajor , row is the current R ow and col the column and for
/ / For RowMajor , row is the current r ow and col the column and for
/ / ColMajor , row is the current column and col the row , but this is
/ / not important as we are only looking for elements on the diagonal .
_Index innerStart = 0 ;
@ -761,11 +764,11 @@ private:
/ / Check whether we have found an element on the diagonal .
if ( ( innerStart = = innerEnd ) & & ( indexPtr [ innerStart ] = = row ) ) {
+ + diag_non_z eros ;
+ + diagNonZ eros ;
}
}
return static_cast < _Index > ( entryCount - diag_non_z eros ) ;
return static_cast < _Index > ( entryCount - diagNonZ eros ) ;
}
} ;