Files
eigen/Eigen/src/SparseCore/SparseView.h

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

226 lines
7.5 KiB
C
Raw Normal View History

2010-06-14 02:16:46 +03:00
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
2014-07-01 13:27:35 +02:00
// Copyright (C) 2011-2014 Gael Guennebaud <gael.guennebaud@inria.fr>
2010-06-14 02:16:46 +03:00
// Copyright (C) 2010 Daniel Lowengrub <lowdanie@gmail.com>
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
2010-06-14 02:16:46 +03:00
#ifndef EIGEN_SPARSEVIEW_H
#define EIGEN_SPARSEVIEW_H
// IWYU pragma: private
#include "./InternalHeaderCheck.h"
namespace Eigen {
namespace internal {
2010-06-14 02:16:46 +03:00
template <typename MatrixType>
struct traits<SparseView<MatrixType> > : traits<MatrixType> {
typedef typename MatrixType::StorageIndex StorageIndex;
typedef Sparse StorageKind;
enum { Flags = int(traits<MatrixType>::Flags) & (RowMajorBit) };
};
2010-06-14 02:16:46 +03:00
} // end namespace internal
2017-01-06 18:01:29 +01:00
/** \ingroup SparseCore_Module
* \class SparseView
*
* \brief Expression of a dense or sparse matrix with zero or too small values removed
*
* \tparam MatrixType the type of the object of which we are removing the small entries
*
* This class represents an expression of a given dense or sparse matrix with
* entries smaller than \c reference * \c epsilon are removed.
* It is the return type of MatrixBase::sparseView() and SparseMatrixBase::pruned()
* and most of the time this is the only way it is used.
*
* \sa MatrixBase::sparseView(), SparseMatrixBase::pruned()
*/
2010-06-14 02:16:46 +03:00
template <typename MatrixType>
class SparseView : public SparseMatrixBase<SparseView<MatrixType> > {
typedef typename MatrixType::Nested MatrixTypeNested;
typedef internal::remove_all_t<MatrixTypeNested> MatrixTypeNested_;
typedef SparseMatrixBase<SparseView> Base;
2023-11-29 11:12:48 +00:00
2010-06-14 02:16:46 +03:00
public:
2010-06-14 09:06:27 -04:00
EIGEN_SPARSE_PUBLIC_INTERFACE(SparseView)
typedef internal::remove_all_t<MatrixType> NestedExpression;
2010-06-14 02:16:46 +03:00
2015-06-09 09:11:12 +02:00
explicit SparseView(const MatrixType& mat, const Scalar& reference = Scalar(0),
const RealScalar& epsilon = NumTraits<Scalar>::dummy_precision())
2015-06-09 09:11:12 +02:00
: m_matrix(mat), m_reference(reference), m_epsilon(epsilon) {}
inline Index rows() const { return m_matrix.rows(); }
inline Index cols() const { return m_matrix.cols(); }
2010-06-14 02:16:46 +03:00
inline Index innerSize() const { return m_matrix.innerSize(); }
inline Index outerSize() const { return m_matrix.outerSize(); }
2023-11-29 11:12:48 +00:00
2014-06-26 13:52:19 +02:00
/** \returns the nested expression */
const internal::remove_all_t<MatrixTypeNested>& nestedExpression() const { return m_matrix; }
2023-11-29 11:12:48 +00:00
2014-06-26 13:52:19 +02:00
Scalar reference() const { return m_reference; }
RealScalar epsilon() const { return m_epsilon; }
2023-11-29 11:12:48 +00:00
2010-06-14 02:16:46 +03:00
protected:
MatrixTypeNested m_matrix;
2010-06-14 02:16:46 +03:00
Scalar m_reference;
2014-06-26 13:52:19 +02:00
RealScalar m_epsilon;
2010-06-14 02:16:46 +03:00
};
2014-06-26 13:52:19 +02:00
namespace internal {
// TODO: find a way to unify the two following variants
2014-06-26 13:52:19 +02:00
// This is tricky because implementing an inner iterator on top of an IndexBased evaluator is
// not easy because the evaluators do not expose the sizes of the underlying expression.
template <typename ArgType>
struct unary_evaluator<SparseView<ArgType>, IteratorBased> : public evaluator_base<SparseView<ArgType> > {
typedef typename evaluator<ArgType>::InnerIterator EvalIterator;
2023-11-29 11:12:48 +00:00
2014-06-26 13:52:19 +02:00
public:
typedef SparseView<ArgType> XprType;
2023-11-29 11:12:48 +00:00
2014-06-26 13:52:19 +02:00
class InnerIterator : public EvalIterator {
protected:
typedef typename XprType::Scalar Scalar;
2023-11-29 11:12:48 +00:00
2014-06-26 13:52:19 +02:00
public:
EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator& sve, Index outer)
2014-06-26 13:52:19 +02:00
: EvalIterator(sve.m_argImpl, outer), m_view(sve.m_view) {
incrementToNonZero();
}
2023-11-29 11:12:48 +00:00
2014-06-26 13:52:19 +02:00
EIGEN_STRONG_INLINE InnerIterator& operator++() {
EvalIterator::operator++();
incrementToNonZero();
return *this;
2023-11-29 11:12:48 +00:00
}
2014-06-26 13:52:19 +02:00
using EvalIterator::value;
2023-11-29 11:12:48 +00:00
2014-06-26 13:52:19 +02:00
protected:
const XprType& m_view;
2023-11-29 11:12:48 +00:00
private:
2014-06-26 13:52:19 +02:00
void incrementToNonZero() {
while ((bool(*this)) && internal::isMuchSmallerThan(value(), m_view.reference(), m_view.epsilon())) {
EvalIterator::operator++();
2023-11-29 11:12:48 +00:00
}
}
};
2014-06-26 13:52:19 +02:00
enum { CoeffReadCost = evaluator<ArgType>::CoeffReadCost, Flags = XprType::Flags };
2023-11-29 11:12:48 +00:00
2014-06-26 13:52:19 +02:00
explicit unary_evaluator(const XprType& xpr) : m_argImpl(xpr.nestedExpression()), m_view(xpr) {}
2023-11-29 11:12:48 +00:00
2014-06-26 13:52:19 +02:00
protected:
evaluator<ArgType> m_argImpl;
const XprType& m_view;
2023-11-29 11:12:48 +00:00
};
2014-06-26 13:52:19 +02:00
template <typename ArgType>
struct unary_evaluator<SparseView<ArgType>, IndexBased> : public evaluator_base<SparseView<ArgType> > {
2023-11-29 11:12:48 +00:00
public:
2014-06-26 13:52:19 +02:00
typedef SparseView<ArgType> XprType;
2023-11-29 11:12:48 +00:00
2014-06-26 13:52:19 +02:00
protected:
enum { IsRowMajor = (XprType::Flags & RowMajorBit) == RowMajorBit };
typedef typename XprType::Scalar Scalar;
typedef typename XprType::StorageIndex StorageIndex;
2023-11-29 11:12:48 +00:00
public:
class InnerIterator {
2023-11-29 11:12:48 +00:00
public:
EIGEN_STRONG_INLINE InnerIterator(const unary_evaluator& sve, Index outer)
: m_sve(sve), m_inner(0), m_outer(outer), m_end(sve.m_view.innerSize()) {
incrementToNonZero();
}
2023-11-29 11:12:48 +00:00
EIGEN_STRONG_INLINE InnerIterator& operator++() {
2023-11-29 11:12:48 +00:00
m_inner++;
incrementToNonZero();
return *this;
}
2023-11-29 11:12:48 +00:00
EIGEN_STRONG_INLINE Scalar value() const {
return (IsRowMajor) ? m_sve.m_argImpl.coeff(m_outer, m_inner) : m_sve.m_argImpl.coeff(m_inner, m_outer);
2023-11-29 11:12:48 +00:00
}
2014-06-26 13:52:19 +02:00
EIGEN_STRONG_INLINE StorageIndex index() const { return m_inner; }
inline Index row() const { return IsRowMajor ? m_outer : index(); }
inline Index col() const { return IsRowMajor ? index() : m_outer; }
2023-11-29 11:12:48 +00:00
EIGEN_STRONG_INLINE operator bool() const { return m_inner < m_end && m_inner >= 0; }
2023-11-29 11:12:48 +00:00
2014-06-26 13:52:19 +02:00
protected:
const unary_evaluator& m_sve;
Index m_inner;
const Index m_outer;
const Index m_end;
2023-11-29 11:12:48 +00:00
2014-06-26 13:52:19 +02:00
private:
void incrementToNonZero() {
while ((bool(*this)) && internal::isMuchSmallerThan(value(), m_sve.m_view.reference(), m_sve.m_view.epsilon())) {
m_inner++;
}
2023-11-29 11:12:48 +00:00
}
2014-06-26 13:52:19 +02:00
};
2023-11-29 11:12:48 +00:00
2014-06-26 13:52:19 +02:00
enum { CoeffReadCost = evaluator<ArgType>::CoeffReadCost, Flags = XprType::Flags };
2023-11-29 11:12:48 +00:00
explicit unary_evaluator(const XprType& xpr) : m_argImpl(xpr.nestedExpression()), m_view(xpr) {}
2023-11-29 11:12:48 +00:00
2014-06-26 13:52:19 +02:00
protected:
evaluator<ArgType> m_argImpl;
2014-06-26 13:52:19 +02:00
const XprType& m_view;
};
} // end namespace internal
2017-01-06 18:01:29 +01:00
/** \ingroup SparseCore_Module
*
* \returns a sparse expression of the dense expression \c *this with values smaller than
* \a reference * \a epsilon removed.
*
* This method is typically used when prototyping to convert a quickly assembled dense Matrix \c D to a SparseMatrix \c
* S: \code MatrixXd D(n,m); SparseMatrix<double> S; S = D.sparseView(); // suppress numerical zeros (exact)
* S = D.sparseView(reference);
* S = D.sparseView(reference,epsilon);
* \endcode
* where \a reference is a meaningful non zero reference value,
* and \a epsilon is a tolerance factor defaulting to NumTraits<Scalar>::dummy_precision().
*
* \sa SparseMatrixBase::pruned(), class SparseView */
2010-06-14 02:16:46 +03:00
template <typename Derived>
const SparseView<Derived> MatrixBase<Derived>::sparseView(const Scalar& reference,
const typename NumTraits<Scalar>::Real& epsilon) const {
return SparseView<Derived>(derived(), reference, epsilon);
}
/** \returns an expression of \c *this with values smaller than
2017-01-06 18:01:29 +01:00
* \a reference * \a epsilon removed.
*
* This method is typically used in conjunction with the product of two sparse matrices
* to automatically prune the smallest values as follows:
* \code
* C = (A*B).pruned(); // suppress numerical zeros (exact)
* C = (A*B).pruned(ref);
* C = (A*B).pruned(ref,epsilon);
* \endcode
* where \c ref is a meaningful non zero reference value.
* */
template <typename Derived>
const SparseView<Derived> SparseMatrixBase<Derived>::pruned(const Scalar& reference, const RealScalar& epsilon) const {
return SparseView<Derived>(derived(), reference, epsilon);
2010-06-14 02:16:46 +03:00
}
} // end namespace Eigen
2010-06-14 02:16:46 +03:00
#endif