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 >
100 typename MatrixCooVec_::const_iterator itGlobale =
matrixCooVec_.end();
102 typename MatrixCooVec_::const_iterator itSelectb =
matrixCooVec_.begin();
103 typename MatrixCooVec_::const_iterator itSelecte = itSelectb;
107 typename MatrixCooVec_::iterator itStore =
matrixCooVec_.begin();
110 while (itSelectb != itGlobale) {
113 itSelecte = std::upper_bound( itSelectb, itGlobale, *itSelectb, TripleLessThan_( ) );
119 *itStore = tmpTriple;
123 itSelectb = itSelecte;
150 typename MatrixCooVec_::const_iterator iter =
matrixCooVec_.begin();
151 typename MatrixCooVec_::const_iterator itere =
matrixCooVec_.end();
153 for ( ; iter != itere; ++iter ) {
159 if ( row >= low and row < high ) {
164 if ( col >= low and col < high ) {
170 nnzRowsOut[rowLoc]++;
194 typename MatrixCooVec_::iterator tripEraseIter;
195 typename MatrixCooVec_::iterator tripEraseIterE;
199 row, TripleRowIndexLessThan_( ) );
202 tripEraseIterE = std::upper_bound( tripEraseIter,
matrixCooVec_.end(),
203 *tripEraseIter, TripleRowLessThan_( ) );
206 typename MatrixCooVec_::iterator tripEraseIterDiag;
207 tripEraseIterDiag = std::find_if( tripEraseIter, tripEraseIterE, TripleDiagEntry_( ) );
210 for ( ; tripEraseIter != tripEraseIterE; ++tripEraseIter ) {
211 (*tripEraseIter).second =
static_cast< ValueType >( 0.0 );
215 Triple_ tmpTrip = std::make_pair( std::make_pair ( row, row ), factor );
217 if ( tripEraseIterDiag != tripEraseIterE ) {
219 *tripEraseIterDiag = tmpTrip;
242 typename MatrixCooVec_::const_iterator tvIter =
matrixCooVec_.begin();
243 typename MatrixCooVec_::const_iterator tvEnd =
matrixCooVec_.end();
250 rowPtrs.reserve( nr + 1 );
252 int previousRow = -1;
255 for ( ; tvIter != tvEnd; ++tvIter ) {
256 const IndexType row = (*tvIter).first.first;
257 const IndexType col = (*tvIter).first.second;
259 values.push_back( tvIter -> second );
260 colIndices.push_back( col );
261 if ( row == previousRow ) index++;
262 else rowPtrs.push_back( index++ );
267 rowPtrs.push_back( values.size() );
276 bool onlyUpperTriangle =
false )
286 typename MatrixCooVec_::const_iterator tvIter =
matrixCooVec_.begin();
287 typename MatrixCooVec_::const_iterator tvEnd =
matrixCooVec_.end();
294 for ( ; tvIter != tvEnd; ++tvIter ) {
295 const IndexType row = (*tvIter).first.first ;
296 const IndexType col = (*tvIter).first.second ;
298 if ( not onlyUpperTriangle or col >= row ) {
299 values.push_back( (*tvIter).second );
300 rowIndices.push_back( row );
301 colIndices.push_back( col );
320 typename MatrixCooVec_::iterator tripEraseIter =
matrixCooVec_.begin( );
321 typename MatrixCooVec_::iterator tripEraseIterE =
matrixCooVec_.end( );
324 for ( ; tripEraseIter != tripEraseIterE; ++tripEraseIter ) {
325 (*tripEraseIter).second =
static_cast< ValueType >(0.0);
332 std::ostream &
write( std::ostream & out )
338 typename MatrixCooVec_::const_iterator tvIter =
matrixCooVec_.begin();
339 typename MatrixCooVec_::const_iterator tvEnd =
matrixCooVec_.end();
340 for ( ; tvIter != tvEnd; ++tvIter ) {
341 const IndexType row = (*tvIter).first.first;
342 const IndexType col = (*tvIter).first.second;
344 out << row <<
" " << col <<
" "
345 << std::setprecision(12)
346 << (*tvIter).second << std::endl;
362 typename MatrixCooVec_::const_iterator iter =
matrixCooVec_.begin();
363 typename MatrixCooVec_::const_iterator last =
matrixCooVec_.end();
364 for ( ; iter != last; ++iter ) {
365 const IndexType iRow = (*iter).first.first;
366 rowSums[ iRow ] += std::fabs( (*iter).second );
369 return *std::max_element( rowSums.begin(), rowSums.end() );
383 typename MatrixCooVec_::const_iterator iter =
matrixCooVec_.begin();
384 typename MatrixCooVec_::const_iterator last =
matrixCooVec_.end();
385 for ( ; iter != last; ++iter ) {
388 sumOfSquares += val * val;
391 return std::sqrt( sumOfSquares );
422 if ( val > maxVal ) {
427 if ( val < minVal ) {
434 ValueType scalar = ( minVal + maxVal ) / 2.0;
442 typedef std::pair<IndexPair_,ValueType>
Triple_;
480 IndexPair_ firstTripleIndxPair = firstTriple.first;
481 IndexPair_ secondTripleIndxPair = secondTriple.first;
483 return (firstTripleIndxPair < secondTripleIndxPair);
492 IndexType firstRowIndex = firstTriple.first.first;
493 IndexType secondRowIndex = secondTriple.first.first;
495 return (firstRowIndex < secondRowIndex);
504 IndexType tripleRowIndex = triple.first.first;
506 return (tripleRowIndex < row);
515 IndexType tripleRowIndex = triple.first.first;
516 IndexType tripleColIndex = triple.first.second;
518 return (tripleRowIndex == tripleColIndex);
528 for ( ; itb != ite; ++itb) {
529 val += (*itb).second;
532 Triple_ combinedTriple = std::make_pair(tmpPair, val);
534 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 <i,j, values>
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