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
|
|
|
//
|
2010-06-25 05:21:58 +08:00
|
|
|
// Copyright (C) 2009 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
|
|
|
template<typename Lhs, typename Rhs>
|
2010-10-25 22:15:22 +08:00
|
|
|
struct traits<SparseDiagonalProduct<Lhs, Rhs> >
|
2009-02-09 17:59:30 +08:00
|
|
|
{
|
2010-10-26 22:47:01 +08:00
|
|
|
typedef typename remove_all<Lhs>::type _Lhs;
|
|
|
|
|
typedef typename remove_all<Rhs>::type _Rhs;
|
2009-07-04 17:16:27 +08:00
|
|
|
typedef typename _Lhs::Scalar Scalar;
|
2010-10-25 22:15:22 +08:00
|
|
|
typedef typename promote_index_type<typename traits<Lhs>::Index,
|
|
|
|
|
typename traits<Rhs>::Index>::type Index;
|
2010-04-16 22:13:32 +08:00
|
|
|
typedef Sparse StorageKind;
|
|
|
|
|
typedef MatrixXpr XprKind;
|
2009-02-09 17:59:30 +08:00
|
|
|
enum {
|
2009-07-04 17:16:27 +08:00
|
|
|
RowsAtCompileTime = _Lhs::RowsAtCompileTime,
|
|
|
|
|
ColsAtCompileTime = _Rhs::ColsAtCompileTime,
|
|
|
|
|
|
|
|
|
|
MaxRowsAtCompileTime = _Lhs::MaxRowsAtCompileTime,
|
|
|
|
|
MaxColsAtCompileTime = _Rhs::MaxColsAtCompileTime,
|
2009-11-17 23:04:19 +08:00
|
|
|
|
2010-10-25 22:15:22 +08:00
|
|
|
SparseFlags = is_diagonal<_Lhs>::ret ? int(_Rhs::Flags) : int(_Lhs::Flags),
|
2009-11-17 23:04:19 +08:00
|
|
|
Flags = (SparseFlags&RowMajorBit),
|
2009-07-04 17:16:27 +08:00
|
|
|
CoeffReadCost = Dynamic
|
2009-02-09 17:59:30 +08:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum {SDP_IsDiagonal, SDP_IsSparseRowMajor, SDP_IsSparseColMajor};
|
2009-11-17 23:04:19 +08:00
|
|
|
template<typename Lhs, typename Rhs, typename SparseDiagonalProductType, int RhsMode, int LhsMode>
|
2010-10-25 22:15:22 +08:00
|
|
|
class sparse_diagonal_product_inner_iterator_selector;
|
|
|
|
|
|
|
|
|
|
} // end namespace internal
|
2009-02-09 17:59:30 +08:00
|
|
|
|
2009-07-04 17:16:27 +08:00
|
|
|
template<typename Lhs, typename Rhs>
|
|
|
|
|
class SparseDiagonalProduct
|
|
|
|
|
: public SparseMatrixBase<SparseDiagonalProduct<Lhs,Rhs> >,
|
2010-10-25 22:15:22 +08:00
|
|
|
internal::no_assignment_operator
|
2009-02-09 17:59:30 +08:00
|
|
|
{
|
2009-07-04 17:16:27 +08:00
|
|
|
typedef typename Lhs::Nested LhsNested;
|
|
|
|
|
typedef typename Rhs::Nested RhsNested;
|
|
|
|
|
|
2010-10-26 22:47:01 +08:00
|
|
|
typedef typename internal::remove_all<LhsNested>::type _LhsNested;
|
|
|
|
|
typedef typename internal::remove_all<RhsNested>::type _RhsNested;
|
2009-11-17 23:04:19 +08:00
|
|
|
|
2009-02-09 17:59:30 +08:00
|
|
|
enum {
|
2010-10-25 22:15:22 +08:00
|
|
|
LhsMode = internal::is_diagonal<_LhsNested>::ret ? internal::SDP_IsDiagonal
|
|
|
|
|
: (_LhsNested::Flags&RowMajorBit) ? internal::SDP_IsSparseRowMajor : internal::SDP_IsSparseColMajor,
|
|
|
|
|
RhsMode = internal::is_diagonal<_RhsNested>::ret ? internal::SDP_IsDiagonal
|
|
|
|
|
: (_RhsNested::Flags&RowMajorBit) ? internal::SDP_IsSparseRowMajor : internal::SDP_IsSparseColMajor
|
2009-02-09 17:59:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
2010-06-03 14:41:11 +08:00
|
|
|
EIGEN_SPARSE_PUBLIC_INTERFACE(SparseDiagonalProduct)
|
2009-11-17 23:04:19 +08:00
|
|
|
|
2010-10-25 22:15:22 +08:00
|
|
|
typedef internal::sparse_diagonal_product_inner_iterator_selector
|
2009-02-09 17:59:30 +08:00
|
|
|
<_LhsNested,_RhsNested,SparseDiagonalProduct,LhsMode,RhsMode> InnerIterator;
|
|
|
|
|
|
2009-12-01 13:21:29 +08:00
|
|
|
EIGEN_STRONG_INLINE SparseDiagonalProduct(const Lhs& lhs, const Rhs& rhs)
|
2009-02-09 17:59:30 +08:00
|
|
|
: m_lhs(lhs), m_rhs(rhs)
|
|
|
|
|
{
|
2010-10-25 22:15:22 +08:00
|
|
|
eigen_assert(lhs.cols() == rhs.rows() && "invalid sparse matrix * diagonal matrix product");
|
2009-02-09 17:59:30 +08:00
|
|
|
}
|
|
|
|
|
|
2010-05-31 04:00:58 +08:00
|
|
|
EIGEN_STRONG_INLINE Index rows() const { return m_lhs.rows(); }
|
|
|
|
|
EIGEN_STRONG_INLINE Index cols() const { return m_rhs.cols(); }
|
2009-02-09 17:59:30 +08:00
|
|
|
|
|
|
|
|
EIGEN_STRONG_INLINE const _LhsNested& lhs() const { return m_lhs; }
|
|
|
|
|
EIGEN_STRONG_INLINE const _RhsNested& rhs() const { return m_rhs; }
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
LhsNested m_lhs;
|
|
|
|
|
RhsNested m_rhs;
|
|
|
|
|
};
|
|
|
|
|
|
2010-10-25 22:15:22 +08:00
|
|
|
namespace internal {
|
2009-02-09 17:59:30 +08:00
|
|
|
|
|
|
|
|
template<typename Lhs, typename Rhs, typename SparseDiagonalProductType>
|
2010-10-25 22:15:22 +08:00
|
|
|
class sparse_diagonal_product_inner_iterator_selector
|
2009-02-09 17:59:30 +08:00
|
|
|
<Lhs,Rhs,SparseDiagonalProductType,SDP_IsDiagonal,SDP_IsSparseRowMajor>
|
2011-01-25 23:33:02 +08:00
|
|
|
: public CwiseUnaryOp<scalar_multiple_op<typename Lhs::Scalar>,const Rhs>::InnerIterator
|
2009-02-09 17:59:30 +08:00
|
|
|
{
|
2011-01-25 23:33:02 +08:00
|
|
|
typedef typename CwiseUnaryOp<scalar_multiple_op<typename Lhs::Scalar>,const Rhs>::InnerIterator Base;
|
2010-05-31 04:00:58 +08:00
|
|
|
typedef typename Lhs::Index Index;
|
2009-02-09 17:59:30 +08:00
|
|
|
public:
|
2010-10-25 22:15:22 +08:00
|
|
|
inline sparse_diagonal_product_inner_iterator_selector(
|
2010-05-31 04:00:58 +08:00
|
|
|
const SparseDiagonalProductType& expr, Index outer)
|
2009-02-09 17:59:30 +08:00
|
|
|
: Base(expr.rhs()*(expr.lhs().diagonal().coeff(outer)), outer)
|
|
|
|
|
{}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<typename Lhs, typename Rhs, typename SparseDiagonalProductType>
|
2010-10-25 22:15:22 +08:00
|
|
|
class sparse_diagonal_product_inner_iterator_selector
|
2009-02-09 17:59:30 +08:00
|
|
|
<Lhs,Rhs,SparseDiagonalProductType,SDP_IsDiagonal,SDP_IsSparseColMajor>
|
2009-11-17 17:11:27 +08:00
|
|
|
: public CwiseBinaryOp<
|
2010-10-25 22:15:22 +08:00
|
|
|
scalar_product_op<typename Lhs::Scalar>,
|
2009-02-09 17:59:30 +08:00
|
|
|
SparseInnerVectorSet<Rhs,1>,
|
2009-07-04 17:16:27 +08:00
|
|
|
typename Lhs::DiagonalVectorType>::InnerIterator
|
2009-02-09 17:59:30 +08:00
|
|
|
{
|
2009-11-17 17:11:27 +08:00
|
|
|
typedef typename CwiseBinaryOp<
|
2010-10-25 22:15:22 +08:00
|
|
|
scalar_product_op<typename Lhs::Scalar>,
|
2009-02-09 17:59:30 +08:00
|
|
|
SparseInnerVectorSet<Rhs,1>,
|
2009-07-04 17:16:27 +08:00
|
|
|
typename Lhs::DiagonalVectorType>::InnerIterator Base;
|
2010-05-31 04:00:58 +08:00
|
|
|
typedef typename Lhs::Index Index;
|
2012-10-03 15:06:19 +08:00
|
|
|
Index m_outer;
|
2009-02-09 17:59:30 +08:00
|
|
|
public:
|
2010-10-25 22:15:22 +08:00
|
|
|
inline sparse_diagonal_product_inner_iterator_selector(
|
2010-05-31 04:00:58 +08:00
|
|
|
const SparseDiagonalProductType& expr, Index outer)
|
2012-10-03 15:06:19 +08:00
|
|
|
: Base(expr.rhs().innerVector(outer) .cwiseProduct(expr.lhs().diagonal()), 0), m_outer(outer)
|
2009-02-09 17:59:30 +08:00
|
|
|
{}
|
2012-10-03 15:06:19 +08:00
|
|
|
|
|
|
|
|
inline Index outer() const { return m_outer; }
|
|
|
|
|
inline Index col() const { return m_outer; }
|
2009-02-09 17:59:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<typename Lhs, typename Rhs, typename SparseDiagonalProductType>
|
2010-10-25 22:15:22 +08:00
|
|
|
class sparse_diagonal_product_inner_iterator_selector
|
2009-02-09 17:59:30 +08:00
|
|
|
<Lhs,Rhs,SparseDiagonalProductType,SDP_IsSparseColMajor,SDP_IsDiagonal>
|
2011-01-25 23:33:02 +08:00
|
|
|
: public CwiseUnaryOp<scalar_multiple_op<typename Rhs::Scalar>,const Lhs>::InnerIterator
|
2009-02-09 17:59:30 +08:00
|
|
|
{
|
2011-01-25 23:33:02 +08:00
|
|
|
typedef typename CwiseUnaryOp<scalar_multiple_op<typename Rhs::Scalar>,const Lhs>::InnerIterator Base;
|
2010-05-31 04:00:58 +08:00
|
|
|
typedef typename Lhs::Index Index;
|
2009-02-09 17:59:30 +08:00
|
|
|
public:
|
2010-10-25 22:15:22 +08:00
|
|
|
inline sparse_diagonal_product_inner_iterator_selector(
|
2010-05-31 04:00:58 +08:00
|
|
|
const SparseDiagonalProductType& expr, Index outer)
|
2009-02-09 17:59:30 +08:00
|
|
|
: Base(expr.lhs()*expr.rhs().diagonal().coeff(outer), outer)
|
|
|
|
|
{}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<typename Lhs, typename Rhs, typename SparseDiagonalProductType>
|
2010-10-25 22:15:22 +08:00
|
|
|
class sparse_diagonal_product_inner_iterator_selector
|
2009-02-09 17:59:30 +08:00
|
|
|
<Lhs,Rhs,SparseDiagonalProductType,SDP_IsSparseRowMajor,SDP_IsDiagonal>
|
2009-11-17 17:11:27 +08:00
|
|
|
: public CwiseBinaryOp<
|
2010-10-25 22:15:22 +08:00
|
|
|
scalar_product_op<typename Rhs::Scalar>,
|
2009-02-09 17:59:30 +08:00
|
|
|
SparseInnerVectorSet<Lhs,1>,
|
2011-01-15 03:29:55 +08:00
|
|
|
Transpose<const typename Rhs::DiagonalVectorType> >::InnerIterator
|
2009-02-09 17:59:30 +08:00
|
|
|
{
|
2009-11-17 17:11:27 +08:00
|
|
|
typedef typename CwiseBinaryOp<
|
2010-10-25 22:15:22 +08:00
|
|
|
scalar_product_op<typename Rhs::Scalar>,
|
2009-02-09 17:59:30 +08:00
|
|
|
SparseInnerVectorSet<Lhs,1>,
|
2011-01-15 03:29:55 +08:00
|
|
|
Transpose<const typename Rhs::DiagonalVectorType> >::InnerIterator Base;
|
2010-05-31 04:00:58 +08:00
|
|
|
typedef typename Lhs::Index Index;
|
2012-10-03 15:06:19 +08:00
|
|
|
Index m_outer;
|
2009-02-09 17:59:30 +08:00
|
|
|
public:
|
2010-10-25 22:15:22 +08:00
|
|
|
inline sparse_diagonal_product_inner_iterator_selector(
|
2010-05-31 04:00:58 +08:00
|
|
|
const SparseDiagonalProductType& expr, Index outer)
|
2012-10-03 15:06:19 +08:00
|
|
|
: Base(expr.lhs().innerVector(outer) .cwiseProduct(expr.rhs().diagonal().transpose()), 0), m_outer(outer)
|
2009-02-09 17:59:30 +08:00
|
|
|
{}
|
2012-10-03 15:06:19 +08:00
|
|
|
|
|
|
|
|
inline Index outer() const { return m_outer; }
|
|
|
|
|
inline Index row() const { return m_outer; }
|
2009-02-09 17:59:30 +08:00
|
|
|
};
|
|
|
|
|
|
2010-10-25 22:15:22 +08:00
|
|
|
} // end namespace internal
|
|
|
|
|
|
2009-07-04 17:16:27 +08:00
|
|
|
// SparseMatrixBase functions
|
|
|
|
|
|
|
|
|
|
template<typename Derived>
|
|
|
|
|
template<typename OtherDerived>
|
|
|
|
|
const SparseDiagonalProduct<Derived,OtherDerived>
|
|
|
|
|
SparseMatrixBase<Derived>::operator*(const DiagonalBase<OtherDerived> &other) const
|
|
|
|
|
{
|
|
|
|
|
return SparseDiagonalProduct<Derived,OtherDerived>(this->derived(), other.derived());
|
|
|
|
|
}
|
|
|
|
|
|
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
|