1 #ifndef la_matrix_coo_h
2 #define la_matrix_coo_h
4 #define ALLOWED_MEM_OVERHEAD 2.e+6 // entries, this corresponds to 32 MB of
16 template<
typename INDT,
typename VALT >
39 template<
typename INDT,
typename VALT >
101 typename MatrixCooVec_::const_iterator itGlobale =
matrixCooVec_.end();
103 typename MatrixCooVec_::const_iterator itSelectb =
matrixCooVec_.begin();
104 typename MatrixCooVec_::const_iterator itSelecte = itSelectb;
108 typename MatrixCooVec_::iterator itStore =
matrixCooVec_.begin();
111 while (itSelectb != itGlobale) {
114 itSelecte = std::upper_bound( itSelectb, itGlobale, *itSelectb, TripleLessThan_( ) );
120 *itStore = tmpTriple;
124 itSelectb = itSelecte;
151 typename MatrixCooVec_::const_iterator iter =
matrixCooVec_.begin();
152 typename MatrixCooVec_::const_iterator itere =
matrixCooVec_.end();
154 for ( ; iter != itere; ++iter ) {
160 if ( row >= low and row < high ) {
165 if ( col >= low and col < high ) {
171 nnzRowsOut[rowLoc]++;
195 typename MatrixCooVec_::iterator tripEraseIter;
196 typename MatrixCooVec_::iterator tripEraseIterE;
200 row, TripleRowIndexLessThan_( ) );
203 tripEraseIterE = std::upper_bound( tripEraseIter,
matrixCooVec_.end(),
204 *tripEraseIter, TripleRowLessThan_( ) );
207 typename MatrixCooVec_::iterator tripEraseIterDiag;
208 tripEraseIterDiag = std::find_if( tripEraseIter, tripEraseIterE, TripleDiagEntry_( ) );
211 for ( ; tripEraseIter != tripEraseIterE; ++tripEraseIter ) {
212 (*tripEraseIter).second =
static_cast< ValueType >( 0.0 );
216 Triple_ tmpTrip = std::make_pair( std::make_pair ( row, row ), factor );
218 if ( tripEraseIterDiag != tripEraseIterE ) {
220 *tripEraseIterDiag = tmpTrip;
243 typename MatrixCooVec_::const_iterator tvIter =
matrixCooVec_.begin();
244 typename MatrixCooVec_::const_iterator tvEnd =
matrixCooVec_.end();
251 rowPtrs.reserve( nr + 1 );
253 int previousRow = -1;
256 for ( ; tvIter != tvEnd; ++tvIter ) {
257 const IndexType row = (*tvIter).first.first;
258 const IndexType col = (*tvIter).first.second;
260 values.push_back( tvIter -> second );
261 colIndices.push_back( col );
262 if ( row == previousRow ) index++;
263 else rowPtrs.push_back( index++ );
268 rowPtrs.push_back( values.size() );
277 bool onlyUpperTriangle =
false )
287 typename MatrixCooVec_::const_iterator tvIter =
matrixCooVec_.begin();
288 typename MatrixCooVec_::const_iterator tvEnd =
matrixCooVec_.end();
295 for ( ; tvIter != tvEnd; ++tvIter ) {
296 const IndexType row = (*tvIter).first.first ;
297 const IndexType col = (*tvIter).first.second ;
299 if ( not onlyUpperTriangle or col >= row ) {
300 values.push_back( (*tvIter).second );
301 rowIndices.push_back( row );
302 colIndices.push_back( col );
321 typename MatrixCooVec_::iterator tripEraseIter =
matrixCooVec_.begin( );
322 typename MatrixCooVec_::iterator tripEraseIterE =
matrixCooVec_.end( );
325 for ( ; tripEraseIter != tripEraseIterE; ++tripEraseIter ) {
326 (*tripEraseIter).second =
static_cast< ValueType >(0.0);
333 std::ostream &
write( std::ostream & out )
339 typename MatrixCooVec_::const_iterator tvIter =
matrixCooVec_.begin();
340 typename MatrixCooVec_::const_iterator tvEnd =
matrixCooVec_.end();
341 for ( ; tvIter != tvEnd; ++tvIter ) {
342 const IndexType row = (*tvIter).first.first;
343 const IndexType col = (*tvIter).first.second;
345 out << row <<
" " << col <<
" "
346 << std::setprecision(12)
347 << (*tvIter).second << std::endl;
363 typename MatrixCooVec_::const_iterator iter =
matrixCooVec_.begin();
364 typename MatrixCooVec_::const_iterator last =
matrixCooVec_.end();
365 for ( ; iter != last; ++iter ) {
366 const IndexType iRow = (*iter).first.first;
367 rowSums[ iRow ] += std::fabs( (*iter).second );
370 return *std::max_element( rowSums.begin(), rowSums.end() );
384 typename MatrixCooVec_::const_iterator iter =
matrixCooVec_.begin();
385 typename MatrixCooVec_::const_iterator last =
matrixCooVec_.end();
386 for ( ; iter != last; ++iter ) {
389 sumOfSquares += val * val;
392 return std::sqrt( sumOfSquares );
423 if ( val > maxVal ) {
428 if ( val < minVal ) {
435 ValueType scalar = ( minVal + maxVal ) / 2.0;
443 typedef std::pair<IndexPair_,ValueType>
Triple_;
481 IndexPair_ firstTripleIndxPair = firstTriple.first;
482 IndexPair_ secondTripleIndxPair = secondTriple.first;
484 return (firstTripleIndxPair < secondTripleIndxPair);
493 IndexType firstRowIndex = firstTriple.first.first;
494 IndexType secondRowIndex = secondTriple.first.first;
496 return (firstRowIndex < secondRowIndex);
505 IndexType tripleRowIndex = triple.first.first;
507 return (tripleRowIndex < row);
516 IndexType tripleRowIndex = triple.first.first;
517 IndexType tripleColIndex = triple.first.second;
519 return (tripleRowIndex == tripleColIndex);
529 for ( ; itb != ite; ++itb) {
530 val += (*itb).second;
533 Triple_ combinedTriple = std::make_pair(tmpPair, val);
535 return combinedTriple;
void prepareAssembly(const IndexType length)
void finishAssembly()
Finalize matrix assembly - build the assembled input and truncate matrix.
std::ostream & write(std::ostream &out)
debug-output
void zeroRow(const IndexType row, const ValueType factor)
Zero a row and set diagonal to scalar.
std::pair< IndexType, IndexType > IndexPair_
pair of indices
Triple_ combineTriples_(typename MatrixCooVec_::const_iterator itb, typename MatrixCooVec_::const_iterator ite)
merge range of triples by addition
void getEntry(IndexType index, IndexType &row, IndexType &col, ValueType &val)
Get specific entry.
MatrixCoo(unsigned nRows=0, unsigned nCols=0)
returns true if the index pair of the first entry is smaller than of the second entry ...
void erase()
erase all contents
IndexType nnz() const
Return number of non-zeros.
MatrixCooVec_ matrixCooVec_
matrix in coordinate format
bool operator()(Triple_ triple)
bool operator()(Triple_ firstTriple, Triple_ secondTriple)
returns true if the entry is on diagonal, i.e. row == column
ValueType frobeniusNorm()
Return Frobenius norm of matrix .
unsigned sortedSize_
number of sorted entries
bool operator()(Triple_ firstTriple, Triple_ secondTriple)
returns true if the first entry has smaller ROW index than the second one
IndexType nRows_
number of rows of matrix
bool sorted_
state variable - is matrix sorted?
void insert(const IndexType i, const IndexType j, const ValueType value)
Insert a value to unassembled vector.
std::pair< IndexPair_, ValueType > Triple_
triple
returns true if the row of an entry is smaller than a prescribed index
void extractArrays(std::vector< IndexType > &rowIndices, std::vector< IndexType > &colIndices, std::vector< ValueType > &values, bool onlyUpperTriangle=false)
Fill independent arrays with column indices, row pointers and values.
void getIntervalCounts(IndexType low, IndexType high, std::vector< IndexType > &nnzRowsIn, std::vector< IndexType > &nnzRowsOut)
Determine number of nonzeros within a block [low,high) - used for PETSc preallocation.
Temporary storage for a sparse matrix.
double infNorm()
Compute maximal row sum (infinity-norm) of a matrix in coordinate format.
void clear()
destroy all contents
IndexType nCols_
number of columns of matrix
void setCompressedRowArrays(std::vector< IndexType > &colIndices, std::vector< IndexType > &rowPtrs, std::vector< ValueType > &values)
std::vector< Triple_ > MatrixCooVec_
vector of triples
bool operator()(Triple_ triple, IndexType row)
IndexType deduceNumRows_()
#define ALLOWED_MEM_OVERHEAD