11 #ifndef EIGEN_GENERAL_PRODUCT_H 12 #define EIGEN_GENERAL_PRODUCT_H 35 template<typename Lhs, typename Rhs, int ProductType = internal::product_type<Lhs,Rhs>::value>
45 template<
int Rows,
int Cols,
int Depth>
struct product_type_selector;
47 template<
int Size,
int MaxSize>
struct product_size_category
49 enum { is_large = MaxSize == Dynamic ||
50 Size >= EIGEN_CACHEFRIENDLY_PRODUCT_THRESHOLD,
51 value = is_large ? Large
57 template<
typename Lhs,
typename Rhs>
struct product_type
59 typedef typename remove_all<Lhs>::type _Lhs;
60 typedef typename remove_all<Rhs>::type _Rhs;
62 MaxRows = _Lhs::MaxRowsAtCompileTime,
63 Rows = _Lhs::RowsAtCompileTime,
64 MaxCols = _Rhs::MaxColsAtCompileTime,
65 Cols = _Rhs::ColsAtCompileTime,
66 MaxDepth = EIGEN_SIZE_MIN_PREFER_FIXED(_Lhs::MaxColsAtCompileTime,
67 _Rhs::MaxRowsAtCompileTime),
68 Depth = EIGEN_SIZE_MIN_PREFER_FIXED(_Lhs::ColsAtCompileTime,
69 _Rhs::RowsAtCompileTime),
70 LargeThreshold = EIGEN_CACHEFRIENDLY_PRODUCT_THRESHOLD
77 rows_select = product_size_category<Rows,MaxRows>::value,
78 cols_select = product_size_category<Cols,MaxCols>::value,
79 depth_select = product_size_category<Depth,MaxDepth>::value
81 typedef product_type_selector<rows_select, cols_select, depth_select> selector;
87 #ifdef EIGEN_DEBUG_PRODUCT 90 EIGEN_DEBUG_VAR(Rows);
91 EIGEN_DEBUG_VAR(Cols);
92 EIGEN_DEBUG_VAR(Depth);
93 EIGEN_DEBUG_VAR(rows_select);
94 EIGEN_DEBUG_VAR(cols_select);
95 EIGEN_DEBUG_VAR(depth_select);
96 EIGEN_DEBUG_VAR(value);
106 template<
int M,
int N>
struct product_type_selector<M,N,1> {
enum { ret = OuterProduct }; };
107 template<
int Depth>
struct product_type_selector<1, 1, Depth> {
enum { ret = InnerProduct }; };
108 template<>
struct product_type_selector<1, 1, 1> {
enum { ret = InnerProduct }; };
109 template<>
struct product_type_selector<Small,1, Small> {
enum { ret = CoeffBasedProductMode }; };
110 template<>
struct product_type_selector<1, Small,Small> {
enum { ret = CoeffBasedProductMode }; };
111 template<>
struct product_type_selector<Small,Small,Small> {
enum { ret = CoeffBasedProductMode }; };
112 template<>
struct product_type_selector<Small, Small, 1> {
enum { ret = LazyCoeffBasedProductMode }; };
113 template<>
struct product_type_selector<Small, Large, 1> {
enum { ret = LazyCoeffBasedProductMode }; };
114 template<>
struct product_type_selector<Large, Small, 1> {
enum { ret = LazyCoeffBasedProductMode }; };
115 template<>
struct product_type_selector<1, Large,Small> {
enum { ret = CoeffBasedProductMode }; };
116 template<>
struct product_type_selector<1, Large,Large> {
enum { ret = GemvProduct }; };
117 template<>
struct product_type_selector<1, Small,Large> {
enum { ret = CoeffBasedProductMode }; };
118 template<>
struct product_type_selector<Large,1, Small> {
enum { ret = CoeffBasedProductMode }; };
119 template<>
struct product_type_selector<Large,1, Large> {
enum { ret = GemvProduct }; };
120 template<>
struct product_type_selector<Small,1, Large> {
enum { ret = CoeffBasedProductMode }; };
121 template<>
struct product_type_selector<Small,Small,Large> {
enum { ret = GemmProduct }; };
122 template<>
struct product_type_selector<Large,Small,Large> {
enum { ret = GemmProduct }; };
123 template<>
struct product_type_selector<Small,Large,Large> {
enum { ret = GemmProduct }; };
124 template<>
struct product_type_selector<Large,Large,Large> {
enum { ret = GemmProduct }; };
125 template<>
struct product_type_selector<Large,Small,Small> {
enum { ret = GemmProduct }; };
126 template<>
struct product_type_selector<Small,Large,Small> {
enum { ret = GemmProduct }; };
127 template<>
struct product_type_selector<Large,Large,Small> {
enum { ret = GemmProduct }; };
148 template<
typename Lhs,
typename Rhs,
int ProductType>
158 template<
typename Lhs,
typename Rhs>
161 typedef typename internal::nested<Lhs, Rhs::ColsAtCompileTime, typename internal::plain_matrix_type<Lhs>::type >::type LhsNested;
162 typedef typename internal::nested<Rhs, Lhs::RowsAtCompileTime, typename internal::plain_matrix_type<Rhs>::type >::type RhsNested;
163 typedef CoeffBasedProduct<LhsNested, RhsNested, EvalBeforeAssigningBit | EvalBeforeNestingBit>
Type;
166 template<
typename Lhs,
typename Rhs>
169 typedef typename internal::nested<Lhs, Rhs::ColsAtCompileTime, typename internal::plain_matrix_type<Lhs>::type >::type LhsNested;
170 typedef typename internal::nested<Rhs, Lhs::RowsAtCompileTime, typename internal::plain_matrix_type<Rhs>::type >::type RhsNested;
171 typedef CoeffBasedProduct<LhsNested, RhsNested, NestByRefBit>
Type;
175 template<
typename Lhs,
typename Rhs>
176 struct LazyProductReturnType :
public ProductReturnType<Lhs,Rhs,LazyCoeffBasedProductMode>
192 template<
typename Lhs,
typename Rhs>
193 struct traits<GeneralProduct<Lhs,Rhs,InnerProduct> >
194 : traits<Matrix<typename scalar_product_traits<typename Lhs::Scalar, typename Rhs::Scalar>::ReturnType,1,1> >
199 template<
typename Lhs,
typename Rhs>
201 : internal::no_assignment_operator,
202 public Matrix<typename internal::scalar_product_traits<typename Lhs::Scalar, typename Rhs::Scalar>::ReturnType,1,1>
208 EIGEN_STATIC_ASSERT((internal::is_same<typename Lhs::RealScalar, typename Rhs::RealScalar>::value),
209 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
211 Base::coeffRef(0,0) = (lhs.transpose().cwiseProduct(rhs)).sum();
215 operator const typename Base::Scalar()
const {
216 return Base::coeff(0,0);
227 template<
typename ProductType,
typename Dest,
typename Func>
228 EIGEN_DONT_INLINE
void outer_product_selector_run(
const ProductType& prod, Dest& dest,
const Func& func,
const false_type&)
230 typedef typename Dest::Index Index;
233 const Index cols = dest.cols();
234 for (Index j=0; j<cols; ++j)
235 func(dest.col(j), prod.rhs().coeff(0,j) * prod.lhs());
239 template<
typename ProductType,
typename Dest,
typename Func>
240 EIGEN_DONT_INLINE
void outer_product_selector_run(
const ProductType& prod, Dest& dest,
const Func& func,
const true_type&) {
241 typedef typename Dest::Index Index;
244 const Index rows = dest.rows();
245 for (Index i=0; i<rows; ++i)
246 func(dest.row(i), prod.lhs().coeff(i,0) * prod.rhs());
249 template<
typename Lhs,
typename Rhs>
250 struct traits<GeneralProduct<Lhs,Rhs,OuterProduct> >
251 : traits<ProductBase<GeneralProduct<Lhs,Rhs,OuterProduct>, Lhs, Rhs> >
256 template<
typename Lhs,
typename Rhs>
258 :
public ProductBase<GeneralProduct<Lhs,Rhs,OuterProduct>, Lhs, Rhs>
260 template<
typename T>
struct is_row_major : internal::conditional<(int(T::Flags)&RowMajorBit), internal::true_type, internal::false_type>::type {};
267 EIGEN_STATIC_ASSERT((internal::is_same<typename Lhs::RealScalar, typename Rhs::RealScalar>::value),
268 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
271 struct set {
template<
typename Dst,
typename Src>
void operator()(
const Dst& dst,
const Src& src)
const { dst.const_cast_derived() = src; } };
272 struct add {
template<
typename Dst,
typename Src>
void operator()(
const Dst& dst,
const Src& src)
const { dst.const_cast_derived() += src; } };
273 struct sub {
template<
typename Dst,
typename Src>
void operator()(
const Dst& dst,
const Src& src)
const { dst.const_cast_derived() -= src; } };
276 adds(
const Scalar& s) : m_scale(s) {}
277 template<
typename Dst,
typename Src>
void operator()(
const Dst& dst,
const Src& src)
const {
278 dst.const_cast_derived() += m_scale * src;
282 template<
typename Dest>
283 inline void evalTo(Dest& dest)
const {
284 internal::outer_product_selector_run(*
this, dest,
set(), is_row_major<Dest>());
287 template<
typename Dest>
288 inline void addTo(Dest& dest)
const {
289 internal::outer_product_selector_run(*
this, dest, add(), is_row_major<Dest>());
292 template<
typename Dest>
293 inline void subTo(Dest& dest)
const {
294 internal::outer_product_selector_run(*
this, dest, sub(), is_row_major<Dest>());
297 template<
typename Dest>
void scaleAndAddTo(Dest& dest,
const Scalar& alpha)
const 299 internal::outer_product_selector_run(*
this, dest, adds(alpha), is_row_major<Dest>());
316 template<
typename Lhs,
typename Rhs>
317 struct traits<GeneralProduct<Lhs,Rhs,GemvProduct> >
318 : traits<ProductBase<GeneralProduct<Lhs,Rhs,GemvProduct>, Lhs, Rhs> >
321 template<
int S
ide,
int StorageOrder,
bool BlasCompatible>
322 struct gemv_selector;
326 template<
typename Lhs,
typename Rhs>
328 :
public ProductBase<GeneralProduct<Lhs,Rhs,GemvProduct>, Lhs, Rhs>
333 typedef typename Lhs::Scalar LhsScalar;
334 typedef typename Rhs::Scalar RhsScalar;
336 GeneralProduct(
const Lhs& a_lhs,
const Rhs& a_rhs) : Base(a_lhs,a_rhs)
343 typedef typename internal::conditional<int(Side)==OnTheRight,_LhsNested,_RhsNested>::type MatrixType;
345 template<
typename Dest>
void scaleAndAddTo(Dest& dst,
const Scalar& alpha)
const 347 eigen_assert(m_lhs.rows() == dst.rows() && m_rhs.cols() == dst.cols());
349 bool(internal::blas_traits<MatrixType>::HasUsableDirectAccess)>::run(*
this, dst, alpha);
356 template<
int StorageOrder,
bool BlasCompatible>
357 struct gemv_selector<OnTheLeft,StorageOrder,BlasCompatible>
359 template<
typename ProductType,
typename Dest>
360 static void run(
const ProductType& prod, Dest& dest,
const typename ProductType::Scalar& alpha)
364 gemv_selector<OnTheRight,OtherStorageOrder,BlasCompatible>
366 (prod.rhs().transpose(), prod.lhs().transpose()), destT, alpha);
370 template<
typename Scalar,
int Size,
int MaxSize,
bool Cond>
struct gemv_static_vector_if;
372 template<
typename Scalar,
int Size,
int MaxSize>
373 struct gemv_static_vector_if<Scalar,Size,MaxSize,false>
375 EIGEN_STRONG_INLINE Scalar* data() { eigen_internal_assert(
false &&
"should never be called");
return 0; }
378 template<
typename Scalar,
int Size>
379 struct gemv_static_vector_if<Scalar,Size,Dynamic,true>
381 EIGEN_STRONG_INLINE Scalar* data() {
return 0; }
384 template<
typename Scalar,
int Size,
int MaxSize>
385 struct gemv_static_vector_if<Scalar,Size,MaxSize,true>
387 #if EIGEN_ALIGN_STATICALLY 388 internal::plain_array<Scalar,EIGEN_SIZE_MIN_PREFER_FIXED(Size,MaxSize),0> m_data;
389 EIGEN_STRONG_INLINE Scalar* data() {
return m_data.array; }
394 ForceAlignment = internal::packet_traits<Scalar>::Vectorizable,
395 PacketSize = internal::packet_traits<Scalar>::size
397 internal::plain_array<Scalar,EIGEN_SIZE_MIN_PREFER_FIXED(Size,MaxSize)+(ForceAlignment?PacketSize:0),0> m_data;
398 EIGEN_STRONG_INLINE Scalar* data() {
399 return ForceAlignment
400 ?
reinterpret_cast<Scalar*
>((
reinterpret_cast<size_t>(m_data.array) & ~(
size_t(15))) + 16)
406 template<>
struct gemv_selector<OnTheRight,ColMajor,true>
408 template<
typename ProductType,
typename Dest>
409 static inline void run(
const ProductType& prod, Dest& dest,
const typename ProductType::Scalar& alpha)
411 typedef typename ProductType::Index Index;
412 typedef typename ProductType::LhsScalar LhsScalar;
413 typedef typename ProductType::RhsScalar RhsScalar;
414 typedef typename ProductType::Scalar ResScalar;
415 typedef typename ProductType::RealScalar RealScalar;
416 typedef typename ProductType::ActualLhsType ActualLhsType;
417 typedef typename ProductType::ActualRhsType ActualRhsType;
418 typedef typename ProductType::LhsBlasTraits LhsBlasTraits;
419 typedef typename ProductType::RhsBlasTraits RhsBlasTraits;
422 ActualLhsType actualLhs = LhsBlasTraits::extract(prod.lhs());
423 ActualRhsType actualRhs = RhsBlasTraits::extract(prod.rhs());
425 ResScalar actualAlpha = alpha * LhsBlasTraits::extractScalarFactor(prod.lhs())
426 * RhsBlasTraits::extractScalarFactor(prod.rhs());
429 typedef typename conditional<Dest::IsVectorAtCompileTime, Dest, typename Dest::ColXpr>::type ActualDest;
434 EvalToDestAtCompileTime = (ActualDest::InnerStrideAtCompileTime==1),
436 MightCannotUseDest = (ActualDest::InnerStrideAtCompileTime!=1) || ComplexByReal
439 gemv_static_vector_if<ResScalar,ActualDest::SizeAtCompileTime,ActualDest::MaxSizeAtCompileTime,MightCannotUseDest> static_dest;
441 bool alphaIsCompatible = (!ComplexByReal) || (numext::imag(actualAlpha)==RealScalar(0));
442 bool evalToDest = EvalToDestAtCompileTime && alphaIsCompatible;
444 RhsScalar compatibleAlpha = get_factor<ResScalar,RhsScalar>::run(actualAlpha);
446 ei_declare_aligned_stack_constructed_variable(ResScalar,actualDestPtr,dest.size(),
447 evalToDest ? dest.data() : static_dest.data());
451 #ifdef EIGEN_DENSE_STORAGE_CTOR_PLUGIN 452 int size = dest.size();
453 EIGEN_DENSE_STORAGE_CTOR_PLUGIN
455 if(!alphaIsCompatible)
457 MappedDest(actualDestPtr, dest.size()).setZero();
458 compatibleAlpha = RhsScalar(1);
461 MappedDest(actualDestPtr, dest.size()) = dest;
464 general_matrix_vector_product
465 <Index,LhsScalar,
ColMajor,LhsBlasTraits::NeedToConjugate,RhsScalar,RhsBlasTraits::NeedToConjugate>::run(
466 actualLhs.rows(), actualLhs.cols(),
467 actualLhs.data(), actualLhs.outerStride(),
468 actualRhs.data(), actualRhs.innerStride(),
474 if(!alphaIsCompatible)
475 dest += actualAlpha * MappedDest(actualDestPtr, dest.size());
477 dest = MappedDest(actualDestPtr, dest.size());
482 template<>
struct gemv_selector<OnTheRight,RowMajor,true>
484 template<
typename ProductType,
typename Dest>
485 static void run(
const ProductType& prod, Dest& dest,
const typename ProductType::Scalar& alpha)
487 typedef typename ProductType::LhsScalar LhsScalar;
488 typedef typename ProductType::RhsScalar RhsScalar;
489 typedef typename ProductType::Scalar ResScalar;
490 typedef typename ProductType::Index Index;
491 typedef typename ProductType::ActualLhsType ActualLhsType;
492 typedef typename ProductType::ActualRhsType ActualRhsType;
493 typedef typename ProductType::_ActualRhsType _ActualRhsType;
494 typedef typename ProductType::LhsBlasTraits LhsBlasTraits;
495 typedef typename ProductType::RhsBlasTraits RhsBlasTraits;
497 typename add_const<ActualLhsType>::type actualLhs = LhsBlasTraits::extract(prod.lhs());
498 typename add_const<ActualRhsType>::type actualRhs = RhsBlasTraits::extract(prod.rhs());
500 ResScalar actualAlpha = alpha * LhsBlasTraits::extractScalarFactor(prod.lhs())
501 * RhsBlasTraits::extractScalarFactor(prod.rhs());
506 DirectlyUseRhs = _ActualRhsType::InnerStrideAtCompileTime==1
509 gemv_static_vector_if<RhsScalar,_ActualRhsType::SizeAtCompileTime,_ActualRhsType::MaxSizeAtCompileTime,!DirectlyUseRhs> static_rhs;
511 ei_declare_aligned_stack_constructed_variable(RhsScalar,actualRhsPtr,actualRhs.size(),
512 DirectlyUseRhs ?
const_cast<RhsScalar*
>(actualRhs.data()) : static_rhs.data());
516 #ifdef EIGEN_DENSE_STORAGE_CTOR_PLUGIN 517 int size = actualRhs.size();
518 EIGEN_DENSE_STORAGE_CTOR_PLUGIN
523 general_matrix_vector_product
524 <Index,LhsScalar,
RowMajor,LhsBlasTraits::NeedToConjugate,RhsScalar,RhsBlasTraits::NeedToConjugate>::run(
525 actualLhs.rows(), actualLhs.cols(),
526 actualLhs.data(), actualLhs.outerStride(),
528 dest.data(), dest.col(0).innerStride(),
533 template<>
struct gemv_selector<OnTheRight,ColMajor,false>
535 template<
typename ProductType,
typename Dest>
536 static void run(
const ProductType& prod, Dest& dest,
const typename ProductType::Scalar& alpha)
538 typedef typename Dest::Index Index;
540 const Index size = prod.rhs().rows();
541 for(Index k=0; k<size; ++k)
542 dest += (alpha*prod.rhs().coeff(k)) * prod.lhs().col(k);
546 template<>
struct gemv_selector<OnTheRight,RowMajor,false>
548 template<
typename ProductType,
typename Dest>
549 static void run(
const ProductType& prod, Dest& dest,
const typename ProductType::Scalar& alpha)
551 typedef typename Dest::Index Index;
553 const Index rows = prod.rows();
554 for(Index i=0; i<rows; ++i)
555 dest.coeffRef(i) += alpha * (prod.lhs().row(i).cwiseProduct(prod.rhs().transpose())).sum();
571 template<
typename Derived>
572 template<
typename OtherDerived>
581 ProductIsValid = Derived::ColsAtCompileTime==Dynamic
582 || OtherDerived::RowsAtCompileTime==Dynamic
583 || int(Derived::ColsAtCompileTime)==int(OtherDerived::RowsAtCompileTime),
584 AreVectors = Derived::IsVectorAtCompileTime && OtherDerived::IsVectorAtCompileTime,
585 SameSizes = EIGEN_PREDICATE_SAME_MATRIX_SIZE(Derived,OtherDerived)
590 EIGEN_STATIC_ASSERT(ProductIsValid || !(AreVectors && SameSizes),
591 INVALID_VECTOR_VECTOR_PRODUCT__IF_YOU_WANTED_A_DOT_OR_COEFF_WISE_PRODUCT_YOU_MUST_USE_THE_EXPLICIT_FUNCTIONS)
592 EIGEN_STATIC_ASSERT(ProductIsValid || !(SameSizes && !AreVectors),
593 INVALID_MATRIX_PRODUCT__IF_YOU_WANTED_A_COEFF_WISE_PRODUCT_YOU_MUST_USE_THE_EXPLICIT_FUNCTION)
594 EIGEN_STATIC_ASSERT(ProductIsValid || SameSizes, INVALID_MATRIX_PRODUCT)
595 #ifdef EIGEN_DEBUG_PRODUCT 596 internal::product_type<Derived,OtherDerived>::debug();
612 template<
typename Derived>
613 template<
typename OtherDerived>
618 ProductIsValid = Derived::ColsAtCompileTime==Dynamic
619 || OtherDerived::RowsAtCompileTime==Dynamic
620 || int(Derived::ColsAtCompileTime)==int(OtherDerived::RowsAtCompileTime),
621 AreVectors = Derived::IsVectorAtCompileTime && OtherDerived::IsVectorAtCompileTime,
622 SameSizes = EIGEN_PREDICATE_SAME_MATRIX_SIZE(Derived,OtherDerived)
627 EIGEN_STATIC_ASSERT(ProductIsValid || !(AreVectors && SameSizes),
628 INVALID_VECTOR_VECTOR_PRODUCT__IF_YOU_WANTED_A_DOT_OR_COEFF_WISE_PRODUCT_YOU_MUST_USE_THE_EXPLICIT_FUNCTIONS)
629 EIGEN_STATIC_ASSERT(ProductIsValid || !(SameSizes && !AreVectors),
630 INVALID_MATRIX_PRODUCT__IF_YOU_WANTED_A_COEFF_WISE_PRODUCT_YOU_MUST_USE_THE_EXPLICIT_FUNCTION)
631 EIGEN_STATIC_ASSERT(ProductIsValid || SameSizes, INVALID_MATRIX_PRODUCT)
638 #endif // EIGEN_PRODUCT_H Expression of the product of two general matrices or vectors.
Definition: GeneralProduct.h:36
A matrix or vector expression mapping an existing array of data.
Definition: Map.h:104
Expression of the transpose of a matrix.
Definition: Transpose.h:57
Holds information about the various numeric (i.e. scalar) types allowed by Eigen. ...
Definition: NumTraits.h:88
Definition: Constants.h:264
Definition: Constants.h:194
Definition: Constants.h:279
const ScalarMultipleReturnType operator*(const Scalar &scalar) const
Definition: MatrixBase.h:50
const LazyProductReturnType< Derived, OtherDerived >::Type lazyProduct(const MatrixBase< OtherDerived > &other) const
Definition: GeneralProduct.h:615
Definition: Eigen_Colamd.h:50
Definition: Constants.h:266
const unsigned int RowMajorBit
Definition: Constants.h:53
Helper class to get the correct and optimized returned type of operator*.
Definition: GeneralProduct.h:149
The matrix class, also used for vectors and row-vectors.
Definition: Matrix.h:127
Definition: Constants.h:277
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48