42 #ifndef __Teuchos_MatrixMarket_Raw_Adder_hpp
43 #define __Teuchos_MatrixMarket_Raw_Adder_hpp
46 #include "Teuchos_ArrayRCP.hpp"
47 #include "Teuchos_CommHelpers.hpp"
49 #include "Teuchos_MatrixMarket_Banner.hpp"
50 #include "Teuchos_MatrixMarket_CoordDataReader.hpp"
61 namespace MatrixMarket {
86 template<
class Scalar,
class Ordinal>
96 Element () : rowIndex_ (-1), colIndex_ (-1), value_ (0) {}
99 Element (
const Ordinal i,
const Ordinal j,
const Scalar& Aij) :
100 rowIndex_ (i), colIndex_ (j), value_ (Aij) {}
104 return rowIndex_ == rhs.rowIndex_ && colIndex_ == rhs.colIndex_;
109 return ! (*
this == rhs);
114 if (rowIndex_ < rhs.rowIndex_)
116 else if (rowIndex_ > rhs.rowIndex_)
119 return colIndex_ < rhs.colIndex_;
128 template<
class BinaryFunction>
132 std::invalid_argument,
133 "Attempt to merge elements at different locations in the sparse "
134 "matrix. The current element is at (" <<
rowIndex() <<
", "
135 <<
colIndex() <<
") and the element you asked me to merge with it "
137 "probably indicates a bug in the sparse matrix reader.");
139 value_ = f (rhs.value_, value_);
151 std::invalid_argument,
152 "Attempt to merge elements at different locations in the sparse "
153 "matrix. The current element is at (" <<
rowIndex() <<
", "
154 <<
colIndex() <<
") and the element you asked me to merge with it "
156 "probably indicates a bug in the sparse matrix reader.");
162 value_ += rhs.value_;
173 Scalar
value()
const {
return value_; }
176 Ordinal rowIndex_, colIndex_;
190 template<
class Scalar,
class Ordinal>
192 operator<< (std::ostream& out, const Element<Scalar, Ordinal>& elt)
211 if (! STS::isOrdinal) {
216 out << std::scientific;
219 out << std::setbase (10);
225 const double numDigitsAsDouble =
228 const int numDigits =
static_cast<int> (numDigitsAsDouble + 0.5);
233 out << std::setprecision (numDigits + 1);
235 out << elt.rowIndex () <<
" " << elt.colIndex () <<
" ";
236 if (STS::isComplex) {
237 out << STS::real (elt.value ()) <<
" " << STS::imag (elt.value ());
281 template<
class Scalar,
class Ordinal>
284 typedef Ordinal index_type;
285 typedef Scalar value_type;
287 typedef typename std::vector<element_type>::size_type size_type;
302 expectedNumEntries_(0),
327 Adder (
const Ordinal expectedNumRows,
328 const Ordinal expectedNumCols,
329 const Ordinal expectedNumEntries,
330 const bool tolerant=
false,
331 const bool debug=
false) :
332 expectedNumRows_(expectedNumRows),
333 expectedNumCols_(expectedNumCols),
334 expectedNumEntries_(expectedNumEntries),
338 tolerant_ (tolerant),
366 const bool countAgainstTotal=
true)
369 const bool indexPairOutOfRange = i < 1 || j < 1 ||
370 i > expectedNumRows_ || j > expectedNumCols_;
373 std::invalid_argument,
"Matrix is " << expectedNumRows_ <<
" x "
374 << expectedNumCols_ <<
", so entry A(" << i <<
"," << j <<
") = "
375 << Aij <<
" is out of range.");
376 if (countAgainstTotal) {
378 std::invalid_argument,
"Cannot add entry A(" << i <<
"," << j
379 <<
") = " << Aij <<
" to matrix; already have expected number "
380 "of entries " << expectedNumEntries_ <<
".");
391 seenNumRows_ = std::max (seenNumRows_, i);
392 seenNumCols_ = std::max (seenNumCols_, j);
393 if (countAgainstTotal) {
408 print (std::ostream& out,
const bool doMerge,
const bool replace=
false)
413 std::sort (elts_.begin(), elts_.end());
416 typedef std::ostream_iterator<element_type> iter_type;
417 std::copy (elts_.begin(), elts_.end(), iter_type (out,
"\n"));
442 std::pair<size_type, size_type>
445 typedef typename std::vector<element_type>::iterator iter_type;
454 std::sort (elts_.begin(), elts_.end());
461 size_type numUnique = 0;
462 iter_type cur = elts_.begin();
463 if (cur == elts_.end()) {
464 return std::make_pair (numUnique, size_type (0));
467 iter_type next = cur;
470 while (next != elts_.end()) {
473 cur->merge (*next, replace);
485 const size_type numRemoved = elts_.size() - numUnique;
486 elts_.resize (numUnique);
487 return std::make_pair (numUnique, numRemoved);
535 size_type& numRemovedElts,
539 const bool replace=
false)
544 std::pair<size_type, size_type> mergeResult =
merge (replace);
552 const Ordinal nrows = tolerant_ ? seenNumRows_ : expectedNumRows_;
561 typedef typename std::vector<element_type>::const_iterator iter_type;
563 for (iter_type it = elts_.begin(); it != elts_.end(); ++it) {
564 const Ordinal i = it->rowIndex ();
565 const Ordinal j = it->colIndex ();
566 const Scalar Aij = it->value ();
569 "current matrix entry's row index " << i <<
" is less then what "
570 "should be the current row index lower bound " << curRow <<
".");
571 for (Ordinal k = curRow+1; k <= i; ++k) {
577 static_cast<size_t> (curInd) >= elts_.size (),
578 std::logic_error,
"The current index " << curInd <<
" into ind "
579 "and val is >= the number of matrix entries " << elts_.size ()
585 for (Ordinal k = curRow+1; k <= nrows; ++k) {
595 numUniqueElts = mergeResult.first;
596 numRemovedElts = mergeResult.second;
615 const Ordinal
numRows()
const {
return seenNumRows_; }
620 const Ordinal
numCols()
const {
return seenNumCols_; }
623 Ordinal expectedNumRows_, expectedNumCols_, expectedNumEntries_;
624 Ordinal seenNumRows_, seenNumCols_, seenNumEntries_;
629 std::vector<element_type> elts_;
635 #endif // #ifndef __Teuchos_MatrixMarket_Raw_Adder_hpp
std::pair< size_type, size_type > merge(const bool replace=false)
Merge duplicate elements.
const std::vector< element_type > & getEntries() const
A temporary const view of the entries of the matrix.
bool operator<(const Element &rhs) const
Lexicographic order first by row index, then by column index.
Element(const Ordinal i, const Ordinal j, const Scalar &Aij)
Create a sparse matrix entry at (i,j) with value Aij.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
Scalar value() const
Value (A(rowIndex(), colIndex()) of this Element.
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
void clear()
Clear all the added matrix entries and reset metadata.
void mergeAndConvertToCSR(size_type &numUniqueElts, size_type &numRemovedElts, Teuchos::ArrayRCP< Ordinal > &rowptr, Teuchos::ArrayRCP< Ordinal > &colind, Teuchos::ArrayRCP< Scalar > &values, const bool replace=false)
Merge duplicate elements and convert to zero-based CSR.
Element()
Default constructor: an invalid entry of the matrix.
This structure defines some basic traits for a scalar field type.
Templated Parameter List class.
Ordinal rowIndex() const
Row index (zero-based) of this Element.
Adder()
Default constructor.
bool operator==(const Element &rhs)
Ignore the matrix value for comparisons.
const Ordinal numRows() const
Computed number of rows.
void print(std::ostream &out, const bool doMerge, const bool replace=false)
Print the sparse matrix data.
Stores one entry of a sparse matrix.
void operator()(const Ordinal i, const Ordinal j, const Scalar &Aij, const bool countAgainstTotal=true)
Add an entry to the sparse matrix.
const Ordinal numCols() const
Computed number of columns.
void merge(const Element &rhs, const BinaryFunction &f)
Merge rhs into this Element, using custom binary function.
bool operator!=(const Element &rhs)
Ignore the matrix value for comparisons.
void merge(const Element &rhs, const bool replace=false)
Merge rhs into this Element, either by addition or replacement.
Ordinal colIndex() const
Column index (zero-based) of this Element.
Adder(const Ordinal expectedNumRows, const Ordinal expectedNumCols, const Ordinal expectedNumEntries, const bool tolerant=false, const bool debug=false)
Standard constructor.
Reference-counted smart pointer for managing arrays.
To be used with Checker for "raw" sparse matrix input.