void MathUtilities::extractLowerTriangularMatrix( const SparseMatrixsc& A, SparseMatrixsc& B ) { std::vector< Eigen::Triplet<scalar> > triplets; for( int col = 0; col < A.outerSize(); ++col ) { for( SparseMatrixsc::InnerIterator it( A, col ); it; ++it ) { if( col > it.row() ) { continue; } triplets.push_back( Eigen::Triplet<scalar>( it.row(), col, it.value() ) ); } } B.resize( A.rows(), A.cols() ); B.setFromTriplets( triplets.begin(), triplets.end() ); B.makeCompressed(); }
void MathUtilities::convertDenseToSparse( const bool filter_zeros, const MatrixXXsc& dense_matrix, SparseMatrixsc& sparse_matrix ) { std::vector<Eigen::Triplet<scalar>> triplets; for( int row = 0; row < dense_matrix.rows(); ++row ) { for( int col = 0; col < dense_matrix.cols(); ++col ) { if( dense_matrix( row, col ) != 0.0 || !filter_zeros ) { triplets.emplace_back( Eigen::Triplet<scalar>{ row, col, dense_matrix( row, col ) } ); } } } sparse_matrix.resize( dense_matrix.rows(), dense_matrix.cols() ); sparse_matrix.setFromTriplets( std::begin( triplets ), std::end( triplets ) ); sparse_matrix.makeCompressed(); }