30 #ifndef CORE_DATATYPES_SPARSE_MATRIX_H
31 #define CORE_DATATYPES_SPARSE_MATRIX_H
36 #include <Eigen/SparseCore>
44 class SparseRowMatrixGeneric :
public MatrixBase<T>,
public Eigen::SparseMatrix<T, Eigen::RowMajor, index_type>
49 typedef Eigen::SparseMatrix<T, Eigen::RowMajor, index_type>
EigenBase;
61 if (rowCounter[nrows] != nnz)
62 THROW_INVALID_ARGUMENT(
"Invalid sparse row matrix array: row accumulator array does not match number of non-zero elements.");
63 std::vector<Triplet> triplets;
64 triplets.reserve(nnz);
70 while (j < rowCounter[i + 1])
75 triplets.push_back(
Triplet(i, columnCounter[j], 0));
80 this->setFromTriplets(triplets.begin(), triplets.end());
84 template<
typename OtherDerived>
90 template<
typename OtherDerived>
93 this->EigenBase::operator=(other);
102 virtual size_t nrows()
const {
return this->rows(); }
103 virtual size_t ncols()
const {
return this->cols(); }
110 visitor.
visit(*
this);
115 if (this->cols() != this->rows())
118 if (this->rows() * this->cols() > 1e7)
119 THROW_INVALID_ARGUMENT(
"Dangerous call! This poorly implememented method will convert your sparse matrix to dense. It needs to be rewritten. To avoid memory wastage, throwing an exception here.");
121 return this->isApprox(this->transpose(),1e-16);
124 virtual T
get(
int i,
int j)
const override
126 return this->coeff(i,j);
128 virtual void put(
int i,
int j,
const T& val)
override
130 this->coeffRef(i,j) = val;
135 class NonZeroIterator :
public std::iterator<std::forward_iterator_tag, value_type>
138 typedef NonZeroIterator my_type;
140 typedef typename std::iterator<std::forward_iterator_tag, value_type> my_base;
142 typedef typename my_base::pointer pointer;
145 const matrix_type* matrix_;
146 typedef typename matrix_type::EigenBase::InnerIterator IteratorPrivate;
147 boost::scoped_ptr<IteratorPrivate> impl_;
161 explicit NonZeroIterator(
const matrix_type* matrix = 0) : matrix_(matrix),
165 if (matrix && matrix->nonZeros() > 0)
167 impl_.reset(
new IteratorPrivate(matrix,0));
191 throw std::out_of_range(
"Cannot dereference end iterator");
194 return impl_->value();
195 throw std::logic_error(
"null iterator impl");
198 const pointer operator->()
const
203 my_type& operator++()
218 my_type operator++(
int)
227 if (isEnd_ && rhs.isEnd_)
230 if (isEnd_ != rhs.isEnd_)
233 return matrix_ == rhs.matrix_
234 && impl_ == rhs.impl_;
239 return !(*
this == rhs);
250 NonZeroIterator nonZerosBegin() {
return NonZeroIterator(
this); }
251 NonZeroIterator nonZerosEnd() {
return NonZeroIterator(); }
264 virtual void print(std::ostream& o)
const
266 o << static_cast<const EigenBase&>(*this);
270 template <
typename T>
277 template <
typename T>
283 template <
typename T>
286 for (
int k = 0; k < m.outerSize(); ++k)
290 double tmp = it.value();
298 template <
typename T>
301 if (m.rows() != m.cols())
304 for (
int k = 0; k < m.outerSize(); ++k)
308 if (m.coeff(it.col(), it.row()) != it.value())
315 template <
typename T>
324 for (
int k = 0; k < m.outerSize(); ++k)
325 if ( m.coeff(k, k) <= 0 )
328 for (
int k = 0; k < m.outerSize(); ++k)
330 double tmp1 = 0.0, tmp2 = 0.0;
333 if (it.col() != it.row())
335 tmp1 += std::fabs(it.value());
336 tmp2 += std::fabs(m.coeff(it.col(),it.row()));
340 if ( !((tmp1 < m.coeff(k, k)) && (tmp2 < m.coeff(k, k))) )
bool isSymmetricMatrix(const SCIRun::Core::Datatypes::SparseRowMatrixGeneric< T > &m)
Definition: SparseRowMatrix.h:299
static PersistentTypeID type_id
Definition: SparseRowMatrix.h:259
bool isPositiveDefiniteMatrix(const SCIRun::Core::Datatypes::SparseRowMatrixGeneric< T > &m)
Definition: SparseRowMatrix.h:316
bool IsNan(double val)
Definition: MiscMath.h:538
Definition: Persistent.h:89
bool ContainsValidValues(const SCIRun::Core::Datatypes::SparseRowMatrixGeneric< T > &m)
Definition: SparseRowMatrix.h:284
Definition: Persistent.h:187
bool operator!=(const DenseMatrixGeneric< T > &lhs, const DenseMatrixGeneric< T > &rhs)
Definition: MatrixComparison.h:64
const index_type * get_cols() const
Definition: SparseRowMatrix.h:106
virtual void accept(MatrixVisitorGeneric< T > &visitor)
Definition: SparseRowMatrix.h:108
virtual size_t ncols() const
Definition: SparseRowMatrix.h:103
static Persistent * SparseRowMatrixGenericMaker()
Definition: SparseRowMatrix.h:271
#define THROW_INVALID_ARGUMENT(message)
Definition: Exception.h:71
std::vector< CellIndex< T > > operator*(const std::vector< CellIndex< T > > &r, double &)
Definition: FieldIndex.h:110
SparseRowMatrixGeneric(int nrows, int ncols)
Definition: SparseRowMatrix.h:56
virtual void put(int i, int j, const T &val) override
Definition: SparseRowMatrix.h:128
SparseRowMatrixGeneric< T > this_type
Definition: SparseRowMatrix.h:48
const index_type * get_rows() const
Definition: SparseRowMatrix.h:105
std::string type
Definition: Persistent.h:72
SparseRowMatrixGeneric(int nrows, int ncols, const index_type *rowCounter, const index_type *columnCounter, size_t nnz)
Legacy construction compatibility. Useful for converting old code, but should be avoided in new code...
Definition: SparseRowMatrix.h:59
virtual void io(Piostream &)
Definition: MatrixIO.h:214
virtual std::string dynamic_type_name() const
Persistent representation...
Definition: SparseRowMatrix.h:257
Eigen::SparseMatrix< T, Eigen::RowMajor, index_type > EigenBase
Definition: SparseRowMatrix.h:49
SparseRowMatrixGeneric(const Eigen::SparseMatrixBase< OtherDerived > &other)
This constructor allows you to construct SparseRowMatrixGeneric from Eigen expressions.
Definition: SparseRowMatrix.h:85
virtual SparseRowMatrixGeneric * clone() const
Definition: SparseRowMatrix.h:97
virtual size_t nrows() const
Definition: SparseRowMatrix.h:102
const MatrixBase< T > & castForPrinting() const
Definition: SparseRowMatrix.h:254
bool isSymmetric() const
Definition: SparseRowMatrix.h:113
long long index_type
Definition: Types.h:39
bool IsFinite(double val)
Definition: MiscMath.h:543
virtual void visit(DenseMatrixGeneric< T > &)=0
SparseRowMatrixGeneric & operator=(const Eigen::SparseMatrixBase< OtherDerived > &other)
This method allows you to assign Eigen expressions to SparseRowMatrixGeneric.
Definition: SparseRowMatrix.h:91
Definition: Persistent.h:64
bool operator==(const DenseMatrixGeneric< T > &lhs, const DenseMatrixGeneric< T > &rhs)
Definition: MatrixComparison.h:44
Definition: MatrixFwd.h:64
Eigen::Triplet< T > Triplet
Definition: SparseRowMatrix.h:50
T value_type
Definition: SparseRowMatrix.h:47
SparseRowMatrixGeneric()
Definition: SparseRowMatrix.h:55