void MathUtilities::extractTripletData( const SparseMatrixsc& matrix, VectorXi& rows, VectorXi& cols, VectorXs& vals ) { rows.resize( matrix.nonZeros() ); cols.resize( matrix.nonZeros() ); vals.resize( matrix.nonZeros() ); int flat_index{ 0 }; for( int outer_index = 0; outer_index < matrix.outerSize(); ++outer_index ) { for( Eigen::SparseMatrix<double>::InnerIterator it( matrix, outer_index ); it; ++it ) { rows( flat_index ) = it.row(); cols( flat_index ) = it.col(); vals( flat_index++ ) = it.value(); } } assert( flat_index == matrix.nonZeros() ); }
// TODO: Use utility class here void MathUtilities::deserialize( SparseMatrixsc& A, std::istream& stm ) { assert( stm.good() ); VectorXi col_ptr; VectorXi row_ind; VectorXs val; // Read the number of rows in the matrix int rows{ -1 }; stm.read((char*)&rows,sizeof(int)); assert( rows >= 0 ); // Read the number of columns in the matrix int cols{ -1 }; stm.read((char*)&cols,sizeof(int)); assert( cols >= 0 ); // Read the column pointer array col_ptr.resize(cols+1); stm.read((char*)col_ptr.data(),col_ptr.size()*sizeof(int)); // Read the number of nonzeros in the array int nnz{ -1 }; stm.read((char*)&nnz,sizeof(int)); assert( nnz >= 0 ); // Read the row-index array row_ind.resize(nnz); stm.read((char*)row_ind.data(),row_ind.size()*sizeof(int)); // Read the value array val.resize(nnz); stm.read((char*)val.data(),val.size()*sizeof(scalar)); A.resize(rows,cols); A.reserve(nnz); for( int col = 0; col < cols; ++col ) { A.startVec(col); for( int curel = col_ptr(col); curel < col_ptr(col+1); ++curel ) { int row = row_ind(curel); scalar curval = val(curel); A.insertBack(row,col) = curval; } } A.finalize(); }
void MathUtilities::extractDataCCS( const SparseMatrixsc& A, VectorXi& col_ptr, VectorXi& row_ind, VectorXs& val ) { col_ptr.resize( A.cols() + 1 ); row_ind.resize( A.nonZeros() ); val.resize( A.nonZeros() ); col_ptr(0) = 0; for( int col = 0; col < A.outerSize(); ++col ) { col_ptr(col+1) = col_ptr(col); for( SparseMatrixsc::InnerIterator it(A,col); it; ++it ) { const int row{ it.row() }; val(col_ptr(col+1)) = it.value(); row_ind(col_ptr(col+1)) = row; ++col_ptr(col+1); } } assert( col_ptr( col_ptr.size() - 1 ) == row_ind.size() ); }