10 #ifndef EIGEN_SPARSEDENSEPRODUCT_H
11 #define EIGEN_SPARSEDENSEPRODUCT_H
15 template<
typename Lhs,
typename Rhs,
int InnerSize>
struct SparseDenseProductReturnType
17 typedef SparseTimeDenseProduct<Lhs,Rhs> Type;
20 template<
typename Lhs,
typename Rhs>
struct SparseDenseProductReturnType<Lhs,Rhs,1>
22 typedef SparseDenseOuterProduct<Lhs,Rhs,false> Type;
25 template<
typename Lhs,
typename Rhs,
int InnerSize>
struct DenseSparseProductReturnType
27 typedef DenseTimeSparseProduct<Lhs,Rhs> Type;
30 template<
typename Lhs,
typename Rhs>
struct DenseSparseProductReturnType<Lhs,Rhs,1>
32 typedef SparseDenseOuterProduct<Rhs,Lhs,true> Type;
37 template<
typename Lhs,
typename Rhs,
bool Tr>
38 struct traits<SparseDenseOuterProduct<Lhs,Rhs,Tr> >
40 typedef Sparse StorageKind;
41 typedef typename scalar_product_traits<typename traits<Lhs>::Scalar,
42 typename traits<Rhs>::Scalar>::ReturnType Scalar;
43 typedef typename Lhs::Index Index;
44 typedef typename Lhs::Nested LhsNested;
45 typedef typename Rhs::Nested RhsNested;
46 typedef typename remove_all<LhsNested>::type _LhsNested;
47 typedef typename remove_all<RhsNested>::type _RhsNested;
50 LhsCoeffReadCost = traits<_LhsNested>::CoeffReadCost,
51 RhsCoeffReadCost = traits<_RhsNested>::CoeffReadCost,
53 RowsAtCompileTime = Tr ? int(traits<Rhs>::RowsAtCompileTime) : int(traits<Lhs>::RowsAtCompileTime),
54 ColsAtCompileTime = Tr ? int(traits<Lhs>::ColsAtCompileTime) : int(traits<Rhs>::ColsAtCompileTime),
55 MaxRowsAtCompileTime = Tr ? int(traits<Rhs>::MaxRowsAtCompileTime) : int(traits<Lhs>::MaxRowsAtCompileTime),
56 MaxColsAtCompileTime = Tr ? int(traits<Lhs>::MaxColsAtCompileTime) : int(traits<Rhs>::MaxColsAtCompileTime),
60 CoeffReadCost = LhsCoeffReadCost + RhsCoeffReadCost + NumTraits<Scalar>::MulCost
66 template<
typename Lhs,
typename Rhs,
bool Tr>
67 class SparseDenseOuterProduct
68 :
public SparseMatrixBase<SparseDenseOuterProduct<Lhs,Rhs,Tr> >
72 typedef SparseMatrixBase<SparseDenseOuterProduct> Base;
73 EIGEN_DENSE_PUBLIC_INTERFACE(SparseDenseOuterProduct)
74 typedef internal::traits<SparseDenseOuterProduct> Traits;
78 typedef typename Traits::LhsNested LhsNested;
79 typedef typename Traits::RhsNested RhsNested;
80 typedef typename Traits::_LhsNested _LhsNested;
81 typedef typename Traits::_RhsNested _RhsNested;
87 EIGEN_STRONG_INLINE SparseDenseOuterProduct(const Lhs& lhs, const Rhs& rhs)
88 : m_lhs(lhs), m_rhs(rhs)
90 EIGEN_STATIC_ASSERT(!Tr,YOU_MADE_A_PROGRAMMING_MISTAKE);
93 EIGEN_STRONG_INLINE SparseDenseOuterProduct(
const Rhs& rhs,
const Lhs& lhs)
94 : m_lhs(lhs), m_rhs(rhs)
96 EIGEN_STATIC_ASSERT(Tr,YOU_MADE_A_PROGRAMMING_MISTAKE);
99 EIGEN_STRONG_INLINE Index rows()
const {
return Tr ? m_rhs.rows() : m_lhs.rows(); }
100 EIGEN_STRONG_INLINE Index cols()
const {
return Tr ? m_lhs.cols() : m_rhs.cols(); }
102 EIGEN_STRONG_INLINE
const _LhsNested& lhs()
const {
return m_lhs; }
103 EIGEN_STRONG_INLINE
const _RhsNested& rhs()
const {
return m_rhs; }
110 template<
typename Lhs,
typename Rhs,
bool Transpose>
111 class SparseDenseOuterProduct<Lhs,Rhs,Transpose>::InnerIterator :
public _LhsNested::InnerIterator
113 typedef typename _LhsNested::InnerIterator Base;
114 typedef typename SparseDenseOuterProduct::Index Index;
116 EIGEN_STRONG_INLINE InnerIterator(
const SparseDenseOuterProduct& prod, Index outer)
117 : Base(prod.lhs(), 0), m_outer(outer), m_factor(prod.rhs().coeff(outer))
121 inline Index outer()
const {
return m_outer; }
122 inline Index row()
const {
return Transpose ? Base::row() : m_outer; }
123 inline Index col()
const {
return Transpose ? m_outer : Base::row(); }
125 inline Scalar value()
const {
return Base::value() * m_factor; }
133 template<
typename Lhs,
typename Rhs>
134 struct traits<SparseTimeDenseProduct<Lhs,Rhs> >
135 : traits<ProductBase<SparseTimeDenseProduct<Lhs,Rhs>, Lhs, Rhs> >
137 typedef Dense StorageKind;
138 typedef MatrixXpr XprKind;
141 template<
typename SparseLhsType,
typename DenseRhsType,
typename DenseResType,
143 bool ColPerCol = ((DenseRhsType::Flags&
RowMajorBit)==0) || DenseRhsType::ColsAtCompileTime==1>
144 struct sparse_time_dense_product_impl;
146 template<
typename SparseLhsType,
typename DenseRhsType,
typename DenseResType>
147 struct sparse_time_dense_product_impl<SparseLhsType,DenseRhsType,DenseResType,
RowMajor, true>
149 typedef typename internal::remove_all<SparseLhsType>::type Lhs;
150 typedef typename internal::remove_all<DenseRhsType>::type Rhs;
151 typedef typename internal::remove_all<DenseResType>::type Res;
152 typedef typename Lhs::Index Index;
153 typedef typename Lhs::InnerIterator LhsInnerIterator;
154 static void run(
const SparseLhsType& lhs,
const DenseRhsType& rhs, DenseResType& res,
const typename Res::Scalar& alpha)
156 for(Index c=0; c<rhs.cols(); ++c)
158 int n = lhs.outerSize();
159 for(Index j=0; j<n; ++j)
161 typename Res::Scalar tmp(0);
162 for(LhsInnerIterator it(lhs,j); it ;++it)
163 tmp += it.value() * rhs.coeff(it.index(),c);
164 res.coeffRef(j,c) = alpha * tmp;
170 template<
typename SparseLhsType,
typename DenseRhsType,
typename DenseResType>
171 struct sparse_time_dense_product_impl<SparseLhsType,DenseRhsType,DenseResType,
ColMajor, true>
173 typedef typename internal::remove_all<SparseLhsType>::type Lhs;
174 typedef typename internal::remove_all<DenseRhsType>::type Rhs;
175 typedef typename internal::remove_all<DenseResType>::type Res;
176 typedef typename Lhs::InnerIterator LhsInnerIterator;
177 typedef typename Lhs::Index Index;
178 static void run(
const SparseLhsType& lhs,
const DenseRhsType& rhs, DenseResType& res,
const typename Res::Scalar& alpha)
180 for(Index c=0; c<rhs.cols(); ++c)
182 for(Index j=0; j<lhs.outerSize(); ++j)
184 typename Res::Scalar rhs_j = alpha * rhs.coeff(j,c);
185 for(LhsInnerIterator it(lhs,j); it ;++it)
186 res.coeffRef(it.index(),c) += it.value() * rhs_j;
192 template<
typename SparseLhsType,
typename DenseRhsType,
typename DenseResType>
193 struct sparse_time_dense_product_impl<SparseLhsType,DenseRhsType,DenseResType,
RowMajor, false>
195 typedef typename internal::remove_all<SparseLhsType>::type Lhs;
196 typedef typename internal::remove_all<DenseRhsType>::type Rhs;
197 typedef typename internal::remove_all<DenseResType>::type Res;
198 typedef typename Lhs::InnerIterator LhsInnerIterator;
199 typedef typename Lhs::Index Index;
200 static void run(
const SparseLhsType& lhs,
const DenseRhsType& rhs, DenseResType& res,
const typename Res::Scalar& alpha)
202 for(Index j=0; j<lhs.outerSize(); ++j)
204 typename Res::RowXpr res_j(res.row(j));
205 for(LhsInnerIterator it(lhs,j); it ;++it)
206 res_j += (alpha*it.value()) * rhs.row(it.index());
211 template<
typename SparseLhsType,
typename DenseRhsType,
typename DenseResType>
212 struct sparse_time_dense_product_impl<SparseLhsType,DenseRhsType,DenseResType,
ColMajor, false>
214 typedef typename internal::remove_all<SparseLhsType>::type Lhs;
215 typedef typename internal::remove_all<DenseRhsType>::type Rhs;
216 typedef typename internal::remove_all<DenseResType>::type Res;
217 typedef typename Lhs::InnerIterator LhsInnerIterator;
218 typedef typename Lhs::Index Index;
219 static void run(
const SparseLhsType& lhs,
const DenseRhsType& rhs, DenseResType& res,
const typename Res::Scalar& alpha)
221 for(Index j=0; j<lhs.outerSize(); ++j)
223 typename Rhs::ConstRowXpr rhs_j(rhs.row(j));
224 for(LhsInnerIterator it(lhs,j); it ;++it)
225 res.row(it.index()) += (alpha*it.value()) * rhs_j;
230 template<
typename SparseLhsType,
typename DenseRhsType,
typename DenseResType,
typename AlphaType>
231 inline void sparse_time_dense_product(
const SparseLhsType& lhs,
const DenseRhsType& rhs, DenseResType& res,
const AlphaType& alpha)
233 sparse_time_dense_product_impl<SparseLhsType,DenseRhsType,DenseResType>::run(lhs, rhs, res, alpha);
238 template<
typename Lhs,
typename Rhs>
239 class SparseTimeDenseProduct
240 :
public ProductBase<SparseTimeDenseProduct<Lhs,Rhs>, Lhs, Rhs>
243 EIGEN_PRODUCT_PUBLIC_INTERFACE(SparseTimeDenseProduct)
245 SparseTimeDenseProduct(const Lhs& lhs, const Rhs& rhs) : Base(lhs,rhs)
248 template<
typename Dest>
void scaleAndAddTo(Dest& dest,
const Scalar& alpha)
const
250 internal::sparse_time_dense_product(m_lhs, m_rhs, dest, alpha);
254 SparseTimeDenseProduct& operator=(
const SparseTimeDenseProduct&);
260 template<
typename Lhs,
typename Rhs>
261 struct traits<DenseTimeSparseProduct<Lhs,Rhs> >
262 : traits<ProductBase<DenseTimeSparseProduct<Lhs,Rhs>, Lhs, Rhs> >
264 typedef Dense StorageKind;
268 template<
typename Lhs,
typename Rhs>
269 class DenseTimeSparseProduct
270 :
public ProductBase<DenseTimeSparseProduct<Lhs,Rhs>, Lhs, Rhs>
273 EIGEN_PRODUCT_PUBLIC_INTERFACE(DenseTimeSparseProduct)
275 DenseTimeSparseProduct(const Lhs& lhs, const Rhs& rhs) : Base(lhs,rhs)
278 template<
typename Dest>
void scaleAndAddTo(Dest& dest,
const Scalar& alpha)
const
280 Transpose<const _LhsNested> lhs_t(m_lhs);
281 Transpose<const _RhsNested> rhs_t(m_rhs);
282 Transpose<Dest> dest_t(dest);
283 internal::sparse_time_dense_product(rhs_t, lhs_t, dest_t, alpha);
287 DenseTimeSparseProduct& operator=(
const DenseTimeSparseProduct&);
291 template<
typename Derived>
292 template<
typename OtherDerived>
293 inline const typename SparseDenseProductReturnType<Derived,OtherDerived>::Type
296 return typename SparseDenseProductReturnType<Derived,OtherDerived>::Type(derived(), other.derived());
301 #endif // EIGEN_SPARSEDENSEPRODUCT_H
Definition: Constants.h:266
Definition: Constants.h:264
const ScalarMultipleReturnType operator*(const Scalar &scalar) const
Definition: SparseMatrixBase.h:50
const unsigned int RowMajorBit
Definition: Constants.h:53
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48