2009-02-09 17:59:30 +08:00
|
|
|
// This file is part of Eigen, a lightweight C++ template library
|
2009-05-23 02:25:33 +08:00
|
|
|
// for linear algebra.
|
2009-02-09 17:59:30 +08:00
|
|
|
//
|
2014-07-01 19:27:35 +08:00
|
|
|
// Copyright (C) 2009-2014 Gael Guennebaud <gael.guennebaud@inria.fr>
|
2009-02-09 17:59:30 +08:00
|
|
|
//
|
2012-07-14 02:42:47 +08:00
|
|
|
// 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/.
|
2009-02-09 17:59:30 +08:00
|
|
|
|
|
|
|
|
#ifndef EIGEN_SPARSE_DIAGONAL_PRODUCT_H
|
|
|
|
|
#define EIGEN_SPARSE_DIAGONAL_PRODUCT_H
|
|
|
|
|
|
2012-04-15 18:06:28 +08:00
|
|
|
namespace Eigen {
|
|
|
|
|
|
2009-07-04 17:16:27 +08:00
|
|
|
// The product of a diagonal matrix with a sparse matrix can be easily
|
|
|
|
|
// implemented using expression template.
|
|
|
|
|
// We have two consider very different cases:
|
2009-02-09 17:59:30 +08:00
|
|
|
// 1 - diag * row-major sparse
|
|
|
|
|
// => each inner vector <=> scalar * sparse vector product
|
|
|
|
|
// => so we can reuse CwiseUnaryOp::InnerIterator
|
|
|
|
|
// 2 - diag * col-major sparse
|
|
|
|
|
// => each inner vector <=> densevector * sparse vector cwise product
|
|
|
|
|
// => again, we can reuse specialization of CwiseBinaryOp::InnerIterator
|
|
|
|
|
// for that particular case
|
|
|
|
|
// The two other cases are symmetric.
|
|
|
|
|
|
2010-10-25 22:15:22 +08:00
|
|
|
namespace internal {
|
2009-02-09 17:59:30 +08:00
|
|
|
|
2014-06-27 21:54:44 +08:00
|
|
|
enum {
|
|
|
|
|
SDP_AsScalarProduct,
|
|
|
|
|
SDP_AsCwiseProduct
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<typename SparseXprType, typename DiagonalCoeffType, int SDP_Tag>
|
|
|
|
|
struct sparse_diagonal_product_evaluator;
|
|
|
|
|
|
2014-07-31 19:35:49 +08:00
|
|
|
template<typename Lhs, typename Rhs, int ProductTag>
|
|
|
|
|
struct product_evaluator<Product<Lhs, Rhs, DefaultProduct>, ProductTag, DiagonalShape, SparseShape, typename traits<Lhs>::Scalar, typename traits<Rhs>::Scalar>
|
2014-06-27 21:54:44 +08:00
|
|
|
: public sparse_diagonal_product_evaluator<Rhs, typename Lhs::DiagonalVectorType, Rhs::Flags&RowMajorBit?SDP_AsScalarProduct:SDP_AsCwiseProduct>
|
|
|
|
|
{
|
2014-07-31 19:35:49 +08:00
|
|
|
typedef Product<Lhs, Rhs, DefaultProduct> XprType;
|
First part of a big refactoring of alignment control to enable the handling of arbitrarily aligned buffers. It includes:
- AlignedBit flag is deprecated. Alignment is now specified by the evaluator through the 'Alignment' enum, e.g., evaluator<Xpr>::Alignment. Its value is in Bytes.
- Add several enums to specify alignment: Aligned8, Aligned16, Aligned32, Aligned64, Aligned128. AlignedMax corresponds to EIGEN_MAX_ALIGN_BYTES. Such enums are used to define the above Alignment value, and as the 'Options' template parameter of Map<> and Ref<>.
- The Aligned enum is now deprecated. It is now an alias for Aligned16.
- Currently, traits<Matrix<>>, traits<Array<>>, traits<Ref<>>, traits<Map<>>, and traits<Block<>> also expose the Alignment enum.
2015-08-06 21:31:07 +08:00
|
|
|
enum { CoeffReadCost = Dynamic, Flags = Rhs::Flags&RowMajorBit, Alignment = 0 }; // FIXME CoeffReadCost & Flags
|
2014-06-27 21:54:44 +08:00
|
|
|
|
|
|
|
|
typedef sparse_diagonal_product_evaluator<Rhs, typename Lhs::DiagonalVectorType, Rhs::Flags&RowMajorBit?SDP_AsScalarProduct:SDP_AsCwiseProduct> Base;
|
2014-09-23 20:28:23 +08:00
|
|
|
explicit product_evaluator(const XprType& xpr) : Base(xpr.rhs(), xpr.lhs().diagonal()) {}
|
2014-06-27 21:54:44 +08:00
|
|
|
};
|
|
|
|
|
|
2014-07-31 19:35:49 +08:00
|
|
|
template<typename Lhs, typename Rhs, int ProductTag>
|
|
|
|
|
struct product_evaluator<Product<Lhs, Rhs, DefaultProduct>, ProductTag, SparseShape, DiagonalShape, typename traits<Lhs>::Scalar, typename traits<Rhs>::Scalar>
|
2014-06-27 21:54:44 +08:00
|
|
|
: public sparse_diagonal_product_evaluator<Lhs, Transpose<const typename Rhs::DiagonalVectorType>, Lhs::Flags&RowMajorBit?SDP_AsCwiseProduct:SDP_AsScalarProduct>
|
|
|
|
|
{
|
2014-07-31 19:35:49 +08:00
|
|
|
typedef Product<Lhs, Rhs, DefaultProduct> XprType;
|
First part of a big refactoring of alignment control to enable the handling of arbitrarily aligned buffers. It includes:
- AlignedBit flag is deprecated. Alignment is now specified by the evaluator through the 'Alignment' enum, e.g., evaluator<Xpr>::Alignment. Its value is in Bytes.
- Add several enums to specify alignment: Aligned8, Aligned16, Aligned32, Aligned64, Aligned128. AlignedMax corresponds to EIGEN_MAX_ALIGN_BYTES. Such enums are used to define the above Alignment value, and as the 'Options' template parameter of Map<> and Ref<>.
- The Aligned enum is now deprecated. It is now an alias for Aligned16.
- Currently, traits<Matrix<>>, traits<Array<>>, traits<Ref<>>, traits<Map<>>, and traits<Block<>> also expose the Alignment enum.
2015-08-06 21:31:07 +08:00
|
|
|
enum { CoeffReadCost = Dynamic, Flags = Lhs::Flags&RowMajorBit, Alignment = 0 }; // FIXME CoeffReadCost & Flags
|
2014-06-27 21:54:44 +08:00
|
|
|
|
|
|
|
|
typedef sparse_diagonal_product_evaluator<Lhs, Transpose<const typename Rhs::DiagonalVectorType>, Lhs::Flags&RowMajorBit?SDP_AsCwiseProduct:SDP_AsScalarProduct> Base;
|
2014-09-23 20:28:23 +08:00
|
|
|
explicit product_evaluator(const XprType& xpr) : Base(xpr.lhs(), xpr.rhs().diagonal().transpose()) {}
|
2014-06-27 21:54:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<typename SparseXprType, typename DiagonalCoeffType>
|
First part of a big refactoring of alignment control to enable the handling of arbitrarily aligned buffers. It includes:
- AlignedBit flag is deprecated. Alignment is now specified by the evaluator through the 'Alignment' enum, e.g., evaluator<Xpr>::Alignment. Its value is in Bytes.
- Add several enums to specify alignment: Aligned8, Aligned16, Aligned32, Aligned64, Aligned128. AlignedMax corresponds to EIGEN_MAX_ALIGN_BYTES. Such enums are used to define the above Alignment value, and as the 'Options' template parameter of Map<> and Ref<>.
- The Aligned enum is now deprecated. It is now an alias for Aligned16.
- Currently, traits<Matrix<>>, traits<Array<>>, traits<Ref<>>, traits<Map<>>, and traits<Block<>> also expose the Alignment enum.
2015-08-06 21:31:07 +08:00
|
|
|
struct sparse_diagonal_product_evaluator<SparseXprType, DiagonalCoeffType, SDP_AsScalarProduct>
|
2014-06-27 21:54:44 +08:00
|
|
|
{
|
|
|
|
|
protected:
|
|
|
|
|
typedef typename evaluator<SparseXprType>::InnerIterator SparseXprInnerIterator;
|
|
|
|
|
typedef typename SparseXprType::Scalar Scalar;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
class InnerIterator : public SparseXprInnerIterator
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
InnerIterator(const sparse_diagonal_product_evaluator &xprEval, Index outer)
|
|
|
|
|
: SparseXprInnerIterator(xprEval.m_sparseXprImpl, outer),
|
|
|
|
|
m_coeff(xprEval.m_diagCoeffImpl.coeff(outer))
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
EIGEN_STRONG_INLINE Scalar value() const { return m_coeff * SparseXprInnerIterator::value(); }
|
|
|
|
|
protected:
|
|
|
|
|
typename DiagonalCoeffType::Scalar m_coeff;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
sparse_diagonal_product_evaluator(const SparseXprType &sparseXpr, const DiagonalCoeffType &diagCoeff)
|
|
|
|
|
: m_sparseXprImpl(sparseXpr), m_diagCoeffImpl(diagCoeff)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
protected:
|
2015-09-03 04:10:39 +08:00
|
|
|
evaluator<SparseXprType> m_sparseXprImpl;
|
|
|
|
|
evaluator<DiagonalCoeffType> m_diagCoeffImpl;
|
2014-06-27 21:54:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<typename SparseXprType, typename DiagCoeffType>
|
First part of a big refactoring of alignment control to enable the handling of arbitrarily aligned buffers. It includes:
- AlignedBit flag is deprecated. Alignment is now specified by the evaluator through the 'Alignment' enum, e.g., evaluator<Xpr>::Alignment. Its value is in Bytes.
- Add several enums to specify alignment: Aligned8, Aligned16, Aligned32, Aligned64, Aligned128. AlignedMax corresponds to EIGEN_MAX_ALIGN_BYTES. Such enums are used to define the above Alignment value, and as the 'Options' template parameter of Map<> and Ref<>.
- The Aligned enum is now deprecated. It is now an alias for Aligned16.
- Currently, traits<Matrix<>>, traits<Array<>>, traits<Ref<>>, traits<Map<>>, and traits<Block<>> also expose the Alignment enum.
2015-08-06 21:31:07 +08:00
|
|
|
struct sparse_diagonal_product_evaluator<SparseXprType, DiagCoeffType, SDP_AsCwiseProduct>
|
2014-06-27 21:54:44 +08:00
|
|
|
{
|
|
|
|
|
typedef typename SparseXprType::Scalar Scalar;
|
2015-09-03 20:53:51 +08:00
|
|
|
typedef typename SparseXprType::StorageIndex StorageIndex;
|
2014-06-27 21:54:44 +08:00
|
|
|
|
2015-09-02 04:34:30 +08:00
|
|
|
typedef typename nested_eval<DiagCoeffType,SparseXprType::IsRowMajor ? SparseXprType::RowsAtCompileTime
|
|
|
|
|
: SparseXprType::ColsAtCompileTime>::type DiagCoeffNested;
|
2014-06-27 21:54:44 +08:00
|
|
|
|
2014-06-27 22:41:45 +08:00
|
|
|
class InnerIterator
|
2014-06-27 21:54:44 +08:00
|
|
|
{
|
2015-09-02 04:34:30 +08:00
|
|
|
typedef typename evaluator<SparseXprType>::InnerIterator SparseXprIter;
|
2014-06-27 21:54:44 +08:00
|
|
|
public:
|
|
|
|
|
InnerIterator(const sparse_diagonal_product_evaluator &xprEval, Index outer)
|
2015-09-02 04:34:30 +08:00
|
|
|
: m_sparseIter(xprEval.m_sparseXprEval, outer), m_diagCoeffNested(xprEval.m_diagCoeffNested)
|
2014-06-27 22:41:45 +08:00
|
|
|
{}
|
2014-06-27 21:54:44 +08:00
|
|
|
|
2015-09-02 04:34:30 +08:00
|
|
|
inline Scalar value() const { return m_sparseIter.value() * m_diagCoeffNested.coeff(index()); }
|
2015-09-03 20:53:51 +08:00
|
|
|
inline StorageIndex index() const { return m_sparseIter.index(); }
|
2015-09-02 04:34:30 +08:00
|
|
|
inline Index outer() const { return m_sparseIter.outer(); }
|
|
|
|
|
inline Index col() const { return SparseXprType::IsRowMajor ? m_sparseIter.index() : m_sparseIter.outer(); }
|
|
|
|
|
inline Index row() const { return SparseXprType::IsRowMajor ? m_sparseIter.outer() : m_sparseIter.index(); }
|
2014-06-27 22:41:45 +08:00
|
|
|
|
2015-09-02 04:34:30 +08:00
|
|
|
EIGEN_STRONG_INLINE InnerIterator& operator++() { ++m_sparseIter; return *this; }
|
|
|
|
|
inline operator bool() const { return m_sparseIter; }
|
2014-06-27 21:54:44 +08:00
|
|
|
|
|
|
|
|
protected:
|
2015-09-02 04:34:30 +08:00
|
|
|
SparseXprIter m_sparseIter;
|
|
|
|
|
DiagCoeffNested m_diagCoeffNested;
|
2014-06-27 21:54:44 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
sparse_diagonal_product_evaluator(const SparseXprType &sparseXpr, const DiagCoeffType &diagCoeff)
|
2015-09-02 04:34:30 +08:00
|
|
|
: m_sparseXprEval(sparseXpr), m_diagCoeffNested(diagCoeff)
|
2014-06-27 21:54:44 +08:00
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
protected:
|
2015-09-03 03:38:40 +08:00
|
|
|
evaluator<SparseXprType> m_sparseXprEval;
|
2015-09-02 04:34:30 +08:00
|
|
|
DiagCoeffNested m_diagCoeffNested;
|
2014-06-27 21:54:44 +08:00
|
|
|
};
|
|
|
|
|
|
2010-10-25 22:15:22 +08:00
|
|
|
} // end namespace internal
|
|
|
|
|
|
2012-04-15 18:06:28 +08:00
|
|
|
} // end namespace Eigen
|
|
|
|
|
|
2009-02-09 17:59:30 +08:00
|
|
|
#endif // EIGEN_SPARSE_DIAGONAL_PRODUCT_H
|