10 #ifndef EIGEN_PRODUCTBASE_H
11 #define EIGEN_PRODUCTBASE_H
21 template<
typename Derived,
typename _Lhs,
typename _Rhs>
22 struct traits<ProductBase<Derived,_Lhs,_Rhs> >
24 typedef MatrixXpr XprKind;
25 typedef typename remove_all<_Lhs>::type Lhs;
26 typedef typename remove_all<_Rhs>::type Rhs;
27 typedef typename scalar_product_traits<typename Lhs::Scalar, typename Rhs::Scalar>::ReturnType Scalar;
28 typedef typename promote_storage_type<typename traits<Lhs>::StorageKind,
29 typename traits<Rhs>::StorageKind>::ret StorageKind;
30 typedef typename promote_index_type<typename traits<Lhs>::Index,
31 typename traits<Rhs>::Index>::type Index;
33 RowsAtCompileTime = traits<Lhs>::RowsAtCompileTime,
34 ColsAtCompileTime = traits<Rhs>::ColsAtCompileTime,
35 MaxRowsAtCompileTime = traits<Lhs>::MaxRowsAtCompileTime,
36 MaxColsAtCompileTime = traits<Rhs>::MaxColsAtCompileTime,
46 #define EIGEN_PRODUCT_PUBLIC_INTERFACE(Derived) \
47 typedef ProductBase<Derived, Lhs, Rhs > Base; \
48 EIGEN_DENSE_PUBLIC_INTERFACE(Derived) \
49 typedef typename Base::LhsNested LhsNested; \
50 typedef typename Base::_LhsNested _LhsNested; \
51 typedef typename Base::LhsBlasTraits LhsBlasTraits; \
52 typedef typename Base::ActualLhsType ActualLhsType; \
53 typedef typename Base::_ActualLhsType _ActualLhsType; \
54 typedef typename Base::RhsNested RhsNested; \
55 typedef typename Base::_RhsNested _RhsNested; \
56 typedef typename Base::RhsBlasTraits RhsBlasTraits; \
57 typedef typename Base::ActualRhsType ActualRhsType; \
58 typedef typename Base::_ActualRhsType _ActualRhsType; \
62 template<
typename Derived,
typename Lhs,
typename Rhs>
63 class ProductBase :
public MatrixBase<Derived>
66 typedef MatrixBase<Derived> Base;
67 EIGEN_DENSE_PUBLIC_INTERFACE(ProductBase)
69 typedef typename Lhs::Nested LhsNested;
70 typedef typename internal::remove_all<LhsNested>::type _LhsNested;
71 typedef internal::blas_traits<_LhsNested> LhsBlasTraits;
72 typedef typename LhsBlasTraits::DirectLinearAccessType ActualLhsType;
73 typedef typename internal::remove_all<ActualLhsType>::type _ActualLhsType;
74 typedef typename internal::traits<Lhs>::Scalar LhsScalar;
76 typedef typename Rhs::Nested RhsNested;
77 typedef typename internal::remove_all<RhsNested>::type _RhsNested;
78 typedef internal::blas_traits<_RhsNested> RhsBlasTraits;
79 typedef typename RhsBlasTraits::DirectLinearAccessType ActualRhsType;
80 typedef typename internal::remove_all<ActualRhsType>::type _ActualRhsType;
81 typedef typename internal::traits<Rhs>::Scalar RhsScalar;
84 typedef CoeffBasedProduct<LhsNested, RhsNested, 0> FullyLazyCoeffBaseProductType;
88 typedef typename Base::PlainObject PlainObject;
90 ProductBase(const Lhs& a_lhs, const Rhs& a_rhs)
91 : m_lhs(a_lhs), m_rhs(a_rhs)
93 eigen_assert(a_lhs.cols() == a_rhs.rows()
94 &&
"invalid matrix product"
95 &&
"if you wanted a coeff-wise or a dot product use the respective explicit functions");
98 inline Index rows()
const {
return m_lhs.rows(); }
99 inline Index cols()
const {
return m_rhs.cols(); }
101 template<
typename Dest>
102 inline void evalTo(Dest& dst)
const { dst.setZero(); scaleAndAddTo(dst,Scalar(1)); }
104 template<
typename Dest>
105 inline void addTo(Dest& dst)
const { scaleAndAddTo(dst,Scalar(1)); }
107 template<
typename Dest>
108 inline void subTo(Dest& dst)
const { scaleAndAddTo(dst,Scalar(-1)); }
110 template<
typename Dest>
111 inline void scaleAndAddTo(Dest& dst,
const Scalar& alpha)
const { derived().scaleAndAddTo(dst,alpha); }
113 const _LhsNested& lhs()
const {
return m_lhs; }
114 const _RhsNested& rhs()
const {
return m_rhs; }
117 operator const PlainObject& ()
const
119 m_result.resize(m_lhs.rows(), m_rhs.cols());
120 derived().evalTo(m_result);
124 const Diagonal<const FullyLazyCoeffBaseProductType,0> diagonal()
const
125 {
return FullyLazyCoeffBaseProductType(m_lhs, m_rhs); }
128 const Diagonal<FullyLazyCoeffBaseProductType,Index> diagonal()
const
129 {
return FullyLazyCoeffBaseProductType(m_lhs, m_rhs); }
131 const Diagonal<FullyLazyCoeffBaseProductType,Dynamic> diagonal(Index index)
const
132 {
return FullyLazyCoeffBaseProductType(m_lhs, m_rhs).diagonal(index); }
135 typename Base::CoeffReturnType coeff(Index row, Index col)
const
137 #ifdef EIGEN2_SUPPORT
138 return lhs().row(row).cwiseProduct(rhs().col(col).transpose()).sum();
140 EIGEN_STATIC_ASSERT_SIZE_1x1(Derived)
141 eigen_assert(this->rows() == 1 && this->cols() == 1);
142 Matrix<Scalar,1,1> result = *this;
143 return result.coeff(row,col);
147 typename Base::CoeffReturnType coeff(Index i)
const
149 EIGEN_STATIC_ASSERT_SIZE_1x1(Derived)
150 eigen_assert(this->rows() == 1 && this->cols() == 1);
151 Matrix<Scalar,1,1> result = *this;
152 return result.coeff(i);
155 const Scalar& coeffRef(Index row, Index col)
const
157 EIGEN_STATIC_ASSERT_SIZE_1x1(Derived)
158 eigen_assert(this->rows() == 1 && this->cols() == 1);
159 return derived().coeffRef(row,col);
162 const Scalar& coeffRef(Index i)
const
164 EIGEN_STATIC_ASSERT_SIZE_1x1(Derived)
165 eigen_assert(this->rows() == 1 && this->cols() == 1);
166 return derived().coeffRef(i);
174 mutable PlainObject m_result;
180 template<
typename Lhs,
typename Rhs,
int Mode,
int N,
typename PlainObject>
181 struct nested<GeneralProduct<Lhs,Rhs,Mode>, N, PlainObject>
183 typedef PlainObject
const& type;
187 template<
typename NestedProduct>
196 template<
typename Derived,
typename Lhs,
typename Rhs>
197 const ScaledProduct<Derived>
198 operator*(
const ProductBase<Derived,Lhs,Rhs>& prod,
const typename Derived::Scalar& x)
199 {
return ScaledProduct<Derived>(prod.derived(), x); }
201 template<
typename Derived,
typename Lhs,
typename Rhs>
202 typename internal::enable_if<!internal::is_same<typename Derived::Scalar,typename Derived::RealScalar>::value,
203 const ScaledProduct<Derived> >::type
204 operator*(
const ProductBase<Derived,Lhs,Rhs>& prod,
const typename Derived::RealScalar& x)
205 {
return ScaledProduct<Derived>(prod.derived(), x); }
208 template<
typename Derived,
typename Lhs,
typename Rhs>
209 const ScaledProduct<Derived>
210 operator*(
const typename Derived::Scalar& x,
const ProductBase<Derived,Lhs,Rhs>& prod)
211 {
return ScaledProduct<Derived>(prod.derived(), x); }
213 template<
typename Derived,
typename Lhs,
typename Rhs>
214 typename internal::enable_if<!internal::is_same<typename Derived::Scalar,typename Derived::RealScalar>::value,
215 const ScaledProduct<Derived> >::type
216 operator*(
const typename Derived::RealScalar& x,
const ProductBase<Derived,Lhs,Rhs>& prod)
217 {
return ScaledProduct<Derived>(prod.derived(), x); }
220 template<
typename NestedProduct>
221 struct traits<ScaledProduct<NestedProduct> >
222 : traits<ProductBase<ScaledProduct<NestedProduct>,
223 typename NestedProduct::_LhsNested,
224 typename NestedProduct::_RhsNested> >
226 typedef typename traits<NestedProduct>::StorageKind StorageKind;
230 template<
typename NestedProduct>
232 :
public ProductBase<ScaledProduct<NestedProduct>,
233 typename NestedProduct::_LhsNested,
234 typename NestedProduct::_RhsNested>
237 typedef ProductBase<ScaledProduct<NestedProduct>,
238 typename NestedProduct::_LhsNested,
239 typename NestedProduct::_RhsNested> Base;
240 typedef typename Base::Scalar Scalar;
241 typedef typename Base::PlainObject PlainObject;
244 ScaledProduct(
const NestedProduct& prod,
const Scalar& x)
245 : Base(prod.lhs(),prod.rhs()), m_prod(prod), m_alpha(x) {}
247 template<
typename Dest>
248 inline void evalTo(Dest& dst)
const { dst.setZero(); scaleAndAddTo(dst, Scalar(1)); }
250 template<
typename Dest>
251 inline void addTo(Dest& dst)
const { scaleAndAddTo(dst, Scalar(1)); }
253 template<
typename Dest>
254 inline void subTo(Dest& dst)
const { scaleAndAddTo(dst, Scalar(-1)); }
256 template<
typename Dest>
257 inline void scaleAndAddTo(Dest& dst,
const Scalar& a_alpha)
const { m_prod.derived().scaleAndAddTo(dst,a_alpha * m_alpha); }
259 const Scalar& alpha()
const {
return m_alpha; }
262 const NestedProduct& m_prod;
268 template<
typename Derived>
269 template<
typename ProductDerived,
typename Lhs,
typename Rhs>
270 Derived& MatrixBase<Derived>::lazyAssign(
const ProductBase<ProductDerived, Lhs,Rhs>& other)
272 other.derived().evalTo(derived());
278 #endif // EIGEN_PRODUCTBASE_H
const unsigned int EvalBeforeNestingBit
Definition: Constants.h:58
const internal::permut_matrix_product_retval< PermutationDerived, Derived, OnTheRight > operator*(const MatrixBase< Derived > &matrix, const PermutationBase< PermutationDerived > &permutation)
Definition: PermutationMatrix.h:510
const unsigned int EvalBeforeAssigningBit
Definition: Constants.h:63
const unsigned int RowMajorBit
Definition: Constants.h:53