1
// This file is part of Eigen, a lightweight C++ template library
4
// Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
5
// Copyright (C) 2008-2011 Gael Guennebaud <gael.guennebaud@inria.fr>
7
// This Source Code Form is subject to the terms of the Mozilla
8
// Public License v. 2.0. If a copy of the MPL was not distributed
9
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
11
#ifndef EIGEN_GENERAL_PRODUCT_H
12
#define EIGEN_GENERAL_PRODUCT_H
16
/** \class GeneralProduct
17
* \ingroup Core_Module
19
* \brief Expression of the product of two general matrices or vectors
21
* \param LhsNested the type used to store the left-hand side
22
* \param RhsNested the type used to store the right-hand side
23
* \param ProductMode the type of the product
25
* This class represents an expression of the product of two general matrices.
26
* We call a general matrix, a dense matrix with full storage. For instance,
27
* This excludes triangular, selfadjoint, and sparse matrices.
28
* It is the return type of the operator* between general matrices. Its template
29
* arguments are determined automatically by ProductReturnType. Therefore,
30
* GeneralProduct should never be used direclty. To determine the result type of a
31
* function which involves a matrix product, use ProductReturnType::Type.
33
* \sa ProductReturnType, MatrixBase::operator*(const MatrixBase<OtherDerived>&)
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
73
// the splitting into different lines of code here, introducing the _select enums and the typedef below,
74
// is to work around an internal compiler error with gcc 4.1 and 4.2.
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);
102
/* The following allows to select the kind of product at compile time
103
* based on the three dimensions of the product.
104
* This is a compile time mapping from {1,Small,Large}^3 -> {product types} */
105
// FIXME I'm not sure the current mapping is the ideal one.
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 }; };
129
} // end namespace internal
131
/** \class ProductReturnType
132
* \ingroup Core_Module
134
* \brief Helper class to get the correct and optimized returned type of operator*
136
* \param Lhs the type of the left-hand side
137
* \param Rhs the type of the right-hand side
138
* \param ProductMode the type of the product (determined automatically by internal::product_mode)
140
* This class defines the typename Type representing the optimized product expression
141
* between two matrix expressions. In practice, using ProductReturnType<Lhs,Rhs>::Type
142
* is the recommended way to define the result type of a function returning an expression
143
* which involve a matrix product. The class Product should never be
146
* \sa class Product, MatrixBase::operator*(const MatrixBase<OtherDerived>&)
148
template<typename Lhs, typename Rhs, int ProductType>
149
struct ProductReturnType
151
// TODO use the nested type to reduce instanciations ????
152
// typedef typename internal::nested<Lhs,Rhs::ColsAtCompileTime>::type LhsNested;
153
// typedef typename internal::nested<Rhs,Lhs::RowsAtCompileTime>::type RhsNested;
155
typedef GeneralProduct<Lhs/*Nested*/, Rhs/*Nested*/, ProductType> Type;
158
template<typename Lhs, typename Rhs>
159
struct ProductReturnType<Lhs,Rhs,CoeffBasedProductMode>
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>
167
struct ProductReturnType<Lhs,Rhs,LazyCoeffBasedProductMode>
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;
174
// this is a workaround for sun CC
175
template<typename Lhs, typename Rhs>
176
struct LazyProductReturnType : public ProductReturnType<Lhs,Rhs,LazyCoeffBasedProductMode>
179
/***********************************************************************
180
* Implementation of Inner Vector Vector Product
181
***********************************************************************/
183
// FIXME : maybe the "inner product" could return a Scalar
184
// instead of a 1x1 matrix ??
185
// Pro: more natural for the user
186
// Cons: this could be a problem if in a meta unrolled algorithm a matrix-matrix
187
// product ends up to a row-vector times col-vector product... To tackle this use
188
// case, we could have a specialization for Block<MatrixType,1,1> with: operator=(Scalar x);
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>
200
class GeneralProduct<Lhs, Rhs, InnerProduct>
201
: internal::no_assignment_operator,
202
public Matrix<typename internal::scalar_product_traits<typename Lhs::Scalar, typename Rhs::Scalar>::ReturnType,1,1>
204
typedef Matrix<typename internal::scalar_product_traits<typename Lhs::Scalar, typename Rhs::Scalar>::ReturnType,1,1> Base;
206
GeneralProduct(const Lhs& lhs, const Rhs& rhs)
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();
214
/** Convertion to scalar */
215
operator const typename Base::Scalar() const {
216
return Base::coeff(0,0);
220
/***********************************************************************
221
* Implementation of Outer Vector Vector Product
222
***********************************************************************/
225
template<int StorageOrder> struct outer_product_selector;
227
template<typename Lhs, typename Rhs>
228
struct traits<GeneralProduct<Lhs,Rhs,OuterProduct> >
229
: traits<ProductBase<GeneralProduct<Lhs,Rhs,OuterProduct>, Lhs, Rhs> >
234
template<typename Lhs, typename Rhs>
235
class GeneralProduct<Lhs, Rhs, OuterProduct>
236
: public ProductBase<GeneralProduct<Lhs,Rhs,OuterProduct>, Lhs, Rhs>
239
EIGEN_PRODUCT_PUBLIC_INTERFACE(GeneralProduct)
241
GeneralProduct(const Lhs& lhs, const Rhs& rhs) : Base(lhs,rhs)
243
EIGEN_STATIC_ASSERT((internal::is_same<typename Lhs::RealScalar, typename Rhs::RealScalar>::value),
244
YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
247
template<typename Dest> void scaleAndAddTo(Dest& dest, Scalar alpha) const
249
internal::outer_product_selector<(int(Dest::Flags)&RowMajorBit) ? RowMajor : ColMajor>::run(*this, dest, alpha);
255
template<> struct outer_product_selector<ColMajor> {
256
template<typename ProductType, typename Dest>
257
static EIGEN_DONT_INLINE void run(const ProductType& prod, Dest& dest, typename ProductType::Scalar alpha) {
258
typedef typename Dest::Index Index;
259
// FIXME make sure lhs is sequentially stored
260
// FIXME not very good if rhs is real and lhs complex while alpha is real too
261
const Index cols = dest.cols();
262
for (Index j=0; j<cols; ++j)
263
dest.col(j) += (alpha * prod.rhs().coeff(j)) * prod.lhs();
267
template<> struct outer_product_selector<RowMajor> {
268
template<typename ProductType, typename Dest>
269
static EIGEN_DONT_INLINE void run(const ProductType& prod, Dest& dest, typename ProductType::Scalar alpha) {
270
typedef typename Dest::Index Index;
271
// FIXME make sure rhs is sequentially stored
272
// FIXME not very good if lhs is real and rhs complex while alpha is real too
273
const Index rows = dest.rows();
274
for (Index i=0; i<rows; ++i)
275
dest.row(i) += (alpha * prod.lhs().coeff(i)) * prod.rhs();
279
} // end namespace internal
281
/***********************************************************************
282
* Implementation of General Matrix Vector Product
283
***********************************************************************/
285
/* According to the shape/flags of the matrix we have to distinghish 3 different cases:
286
* 1 - the matrix is col-major, BLAS compatible and M is large => call fast BLAS-like colmajor routine
287
* 2 - the matrix is row-major, BLAS compatible and N is large => call fast BLAS-like rowmajor routine
288
* 3 - all other cases are handled using a simple loop along the outer-storage direction.
289
* Therefore we need a lower level meta selector.
290
* Furthermore, if the matrix is the rhs, then the product has to be transposed.
294
template<typename Lhs, typename Rhs>
295
struct traits<GeneralProduct<Lhs,Rhs,GemvProduct> >
296
: traits<ProductBase<GeneralProduct<Lhs,Rhs,GemvProduct>, Lhs, Rhs> >
299
template<int Side, int StorageOrder, bool BlasCompatible>
300
struct gemv_selector;
302
} // end namespace internal
304
template<typename Lhs, typename Rhs>
305
class GeneralProduct<Lhs, Rhs, GemvProduct>
306
: public ProductBase<GeneralProduct<Lhs,Rhs,GemvProduct>, Lhs, Rhs>
309
EIGEN_PRODUCT_PUBLIC_INTERFACE(GeneralProduct)
311
typedef typename Lhs::Scalar LhsScalar;
312
typedef typename Rhs::Scalar RhsScalar;
314
GeneralProduct(const Lhs& lhs, const Rhs& rhs) : Base(lhs,rhs)
316
// EIGEN_STATIC_ASSERT((internal::is_same<typename Lhs::Scalar, typename Rhs::Scalar>::value),
317
// YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
320
enum { Side = Lhs::IsVectorAtCompileTime ? OnTheLeft : OnTheRight };
321
typedef typename internal::conditional<int(Side)==OnTheRight,_LhsNested,_RhsNested>::type MatrixType;
323
template<typename Dest> void scaleAndAddTo(Dest& dst, Scalar alpha) const
325
eigen_assert(m_lhs.rows() == dst.rows() && m_rhs.cols() == dst.cols());
326
internal::gemv_selector<Side,(int(MatrixType::Flags)&RowMajorBit) ? RowMajor : ColMajor,
327
bool(internal::blas_traits<MatrixType>::HasUsableDirectAccess)>::run(*this, dst, alpha);
333
// The vector is on the left => transposition
334
template<int StorageOrder, bool BlasCompatible>
335
struct gemv_selector<OnTheLeft,StorageOrder,BlasCompatible>
337
template<typename ProductType, typename Dest>
338
static void run(const ProductType& prod, Dest& dest, typename ProductType::Scalar alpha)
340
Transpose<Dest> destT(dest);
341
enum { OtherStorageOrder = StorageOrder == RowMajor ? ColMajor : RowMajor };
342
gemv_selector<OnTheRight,OtherStorageOrder,BlasCompatible>
343
::run(GeneralProduct<Transpose<const typename ProductType::_RhsNested>,Transpose<const typename ProductType::_LhsNested>, GemvProduct>
344
(prod.rhs().transpose(), prod.lhs().transpose()), destT, alpha);
348
template<typename Scalar,int Size,int MaxSize,bool Cond> struct gemv_static_vector_if;
350
template<typename Scalar,int Size,int MaxSize>
351
struct gemv_static_vector_if<Scalar,Size,MaxSize,false>
353
EIGEN_STRONG_INLINE Scalar* data() { eigen_internal_assert(false && "should never be called"); return 0; }
356
template<typename Scalar,int Size>
357
struct gemv_static_vector_if<Scalar,Size,Dynamic,true>
359
EIGEN_STRONG_INLINE Scalar* data() { return 0; }
362
template<typename Scalar,int Size,int MaxSize>
363
struct gemv_static_vector_if<Scalar,Size,MaxSize,true>
365
#if EIGEN_ALIGN_STATICALLY
366
internal::plain_array<Scalar,EIGEN_SIZE_MIN_PREFER_FIXED(Size,MaxSize),0> m_data;
367
EIGEN_STRONG_INLINE Scalar* data() { return m_data.array; }
369
// Some architectures cannot align on the stack,
370
// => let's manually enforce alignment by allocating more data and return the address of the first aligned element.
372
ForceAlignment = internal::packet_traits<Scalar>::Vectorizable,
373
PacketSize = internal::packet_traits<Scalar>::size
375
internal::plain_array<Scalar,EIGEN_SIZE_MIN_PREFER_FIXED(Size,MaxSize)+(ForceAlignment?PacketSize:0),0> m_data;
376
EIGEN_STRONG_INLINE Scalar* data() {
377
return ForceAlignment
378
? reinterpret_cast<Scalar*>((reinterpret_cast<size_t>(m_data.array) & ~(size_t(15))) + 16)
384
template<> struct gemv_selector<OnTheRight,ColMajor,true>
386
template<typename ProductType, typename Dest>
387
static inline void run(const ProductType& prod, Dest& dest, typename ProductType::Scalar alpha)
389
typedef typename ProductType::Index Index;
390
typedef typename ProductType::LhsScalar LhsScalar;
391
typedef typename ProductType::RhsScalar RhsScalar;
392
typedef typename ProductType::Scalar ResScalar;
393
typedef typename ProductType::RealScalar RealScalar;
394
typedef typename ProductType::ActualLhsType ActualLhsType;
395
typedef typename ProductType::ActualRhsType ActualRhsType;
396
typedef typename ProductType::LhsBlasTraits LhsBlasTraits;
397
typedef typename ProductType::RhsBlasTraits RhsBlasTraits;
398
typedef Map<Matrix<ResScalar,Dynamic,1>, Aligned> MappedDest;
400
ActualLhsType actualLhs = LhsBlasTraits::extract(prod.lhs());
401
ActualRhsType actualRhs = RhsBlasTraits::extract(prod.rhs());
403
ResScalar actualAlpha = alpha * LhsBlasTraits::extractScalarFactor(prod.lhs())
404
* RhsBlasTraits::extractScalarFactor(prod.rhs());
407
// FIXME find a way to allow an inner stride on the result if packet_traits<Scalar>::size==1
408
// on, the other hand it is good for the cache to pack the vector anyways...
409
EvalToDestAtCompileTime = Dest::InnerStrideAtCompileTime==1,
410
ComplexByReal = (NumTraits<LhsScalar>::IsComplex) && (!NumTraits<RhsScalar>::IsComplex),
411
MightCannotUseDest = (Dest::InnerStrideAtCompileTime!=1) || ComplexByReal
414
gemv_static_vector_if<ResScalar,Dest::SizeAtCompileTime,Dest::MaxSizeAtCompileTime,MightCannotUseDest> static_dest;
416
bool alphaIsCompatible = (!ComplexByReal) || (imag(actualAlpha)==RealScalar(0));
417
bool evalToDest = EvalToDestAtCompileTime && alphaIsCompatible;
419
RhsScalar compatibleAlpha = get_factor<ResScalar,RhsScalar>::run(actualAlpha);
421
ei_declare_aligned_stack_constructed_variable(ResScalar,actualDestPtr,dest.size(),
422
evalToDest ? dest.data() : static_dest.data());
426
#ifdef EIGEN_DENSE_STORAGE_CTOR_PLUGIN
427
int size = dest.size();
428
EIGEN_DENSE_STORAGE_CTOR_PLUGIN
430
if(!alphaIsCompatible)
432
MappedDest(actualDestPtr, dest.size()).setZero();
433
compatibleAlpha = RhsScalar(1);
436
MappedDest(actualDestPtr, dest.size()) = dest;
439
general_matrix_vector_product
440
<Index,LhsScalar,ColMajor,LhsBlasTraits::NeedToConjugate,RhsScalar,RhsBlasTraits::NeedToConjugate>::run(
441
actualLhs.rows(), actualLhs.cols(),
442
actualLhs.data(), actualLhs.outerStride(),
443
actualRhs.data(), actualRhs.innerStride(),
449
if(!alphaIsCompatible)
450
dest += actualAlpha * MappedDest(actualDestPtr, dest.size());
452
dest = MappedDest(actualDestPtr, dest.size());
457
template<> struct gemv_selector<OnTheRight,RowMajor,true>
459
template<typename ProductType, typename Dest>
460
static void run(const ProductType& prod, Dest& dest, typename ProductType::Scalar alpha)
462
typedef typename ProductType::LhsScalar LhsScalar;
463
typedef typename ProductType::RhsScalar RhsScalar;
464
typedef typename ProductType::Scalar ResScalar;
465
typedef typename ProductType::Index Index;
466
typedef typename ProductType::ActualLhsType ActualLhsType;
467
typedef typename ProductType::ActualRhsType ActualRhsType;
468
typedef typename ProductType::_ActualRhsType _ActualRhsType;
469
typedef typename ProductType::LhsBlasTraits LhsBlasTraits;
470
typedef typename ProductType::RhsBlasTraits RhsBlasTraits;
472
typename add_const<ActualLhsType>::type actualLhs = LhsBlasTraits::extract(prod.lhs());
473
typename add_const<ActualRhsType>::type actualRhs = RhsBlasTraits::extract(prod.rhs());
475
ResScalar actualAlpha = alpha * LhsBlasTraits::extractScalarFactor(prod.lhs())
476
* RhsBlasTraits::extractScalarFactor(prod.rhs());
479
// FIXME find a way to allow an inner stride on the result if packet_traits<Scalar>::size==1
480
// on, the other hand it is good for the cache to pack the vector anyways...
481
DirectlyUseRhs = _ActualRhsType::InnerStrideAtCompileTime==1
484
gemv_static_vector_if<RhsScalar,_ActualRhsType::SizeAtCompileTime,_ActualRhsType::MaxSizeAtCompileTime,!DirectlyUseRhs> static_rhs;
486
ei_declare_aligned_stack_constructed_variable(RhsScalar,actualRhsPtr,actualRhs.size(),
487
DirectlyUseRhs ? const_cast<RhsScalar*>(actualRhs.data()) : static_rhs.data());
491
#ifdef EIGEN_DENSE_STORAGE_CTOR_PLUGIN
492
int size = actualRhs.size();
493
EIGEN_DENSE_STORAGE_CTOR_PLUGIN
495
Map<typename _ActualRhsType::PlainObject>(actualRhsPtr, actualRhs.size()) = actualRhs;
498
general_matrix_vector_product
499
<Index,LhsScalar,RowMajor,LhsBlasTraits::NeedToConjugate,RhsScalar,RhsBlasTraits::NeedToConjugate>::run(
500
actualLhs.rows(), actualLhs.cols(),
501
actualLhs.data(), actualLhs.outerStride(),
503
dest.data(), dest.innerStride(),
508
template<> struct gemv_selector<OnTheRight,ColMajor,false>
510
template<typename ProductType, typename Dest>
511
static void run(const ProductType& prod, Dest& dest, typename ProductType::Scalar alpha)
513
typedef typename Dest::Index Index;
514
// TODO makes sure dest is sequentially stored in memory, otherwise use a temp
515
const Index size = prod.rhs().rows();
516
for(Index k=0; k<size; ++k)
517
dest += (alpha*prod.rhs().coeff(k)) * prod.lhs().col(k);
521
template<> struct gemv_selector<OnTheRight,RowMajor,false>
523
template<typename ProductType, typename Dest>
524
static void run(const ProductType& prod, Dest& dest, typename ProductType::Scalar alpha)
526
typedef typename Dest::Index Index;
527
// TODO makes sure rhs is sequentially stored in memory, otherwise use a temp
528
const Index rows = prod.rows();
529
for(Index i=0; i<rows; ++i)
530
dest.coeffRef(i) += alpha * (prod.lhs().row(i).cwiseProduct(prod.rhs().transpose())).sum();
534
} // end namespace internal
536
/***************************************************************************
537
* Implementation of matrix base methods
538
***************************************************************************/
540
/** \returns the matrix product of \c *this and \a other.
542
* \note If instead of the matrix product you want the coefficient-wise product, see Cwise::operator*().
544
* \sa lazyProduct(), operator*=(const MatrixBase&), Cwise::operator*()
546
template<typename Derived>
547
template<typename OtherDerived>
548
inline const typename ProductReturnType<Derived, OtherDerived>::Type
549
MatrixBase<Derived>::operator*(const MatrixBase<OtherDerived> &other) const
551
// A note regarding the function declaration: In MSVC, this function will sometimes
552
// not be inlined since DenseStorage is an unwindable object for dynamic
553
// matrices and product types are holding a member to store the result.
554
// Thus it does not help tagging this function with EIGEN_STRONG_INLINE.
556
ProductIsValid = Derived::ColsAtCompileTime==Dynamic
557
|| OtherDerived::RowsAtCompileTime==Dynamic
558
|| int(Derived::ColsAtCompileTime)==int(OtherDerived::RowsAtCompileTime),
559
AreVectors = Derived::IsVectorAtCompileTime && OtherDerived::IsVectorAtCompileTime,
560
SameSizes = EIGEN_PREDICATE_SAME_MATRIX_SIZE(Derived,OtherDerived)
562
// note to the lost user:
563
// * for a dot product use: v1.dot(v2)
564
// * for a coeff-wise product use: v1.cwiseProduct(v2)
565
EIGEN_STATIC_ASSERT(ProductIsValid || !(AreVectors && SameSizes),
566
INVALID_VECTOR_VECTOR_PRODUCT__IF_YOU_WANTED_A_DOT_OR_COEFF_WISE_PRODUCT_YOU_MUST_USE_THE_EXPLICIT_FUNCTIONS)
567
EIGEN_STATIC_ASSERT(ProductIsValid || !(SameSizes && !AreVectors),
568
INVALID_MATRIX_PRODUCT__IF_YOU_WANTED_A_COEFF_WISE_PRODUCT_YOU_MUST_USE_THE_EXPLICIT_FUNCTION)
569
EIGEN_STATIC_ASSERT(ProductIsValid || SameSizes, INVALID_MATRIX_PRODUCT)
570
#ifdef EIGEN_DEBUG_PRODUCT
571
internal::product_type<Derived,OtherDerived>::debug();
573
return typename ProductReturnType<Derived,OtherDerived>::Type(derived(), other.derived());
576
/** \returns an expression of the matrix product of \c *this and \a other without implicit evaluation.
578
* The returned product will behave like any other expressions: the coefficients of the product will be
579
* computed once at a time as requested. This might be useful in some extremely rare cases when only
580
* a small and no coherent fraction of the result's coefficients have to be computed.
582
* \warning This version of the matrix product can be much much slower. So use it only if you know
583
* what you are doing and that you measured a true speed improvement.
585
* \sa operator*(const MatrixBase&)
587
template<typename Derived>
588
template<typename OtherDerived>
589
const typename LazyProductReturnType<Derived,OtherDerived>::Type
590
MatrixBase<Derived>::lazyProduct(const MatrixBase<OtherDerived> &other) const
593
ProductIsValid = Derived::ColsAtCompileTime==Dynamic
594
|| OtherDerived::RowsAtCompileTime==Dynamic
595
|| int(Derived::ColsAtCompileTime)==int(OtherDerived::RowsAtCompileTime),
596
AreVectors = Derived::IsVectorAtCompileTime && OtherDerived::IsVectorAtCompileTime,
597
SameSizes = EIGEN_PREDICATE_SAME_MATRIX_SIZE(Derived,OtherDerived)
599
// note to the lost user:
600
// * for a dot product use: v1.dot(v2)
601
// * for a coeff-wise product use: v1.cwiseProduct(v2)
602
EIGEN_STATIC_ASSERT(ProductIsValid || !(AreVectors && SameSizes),
603
INVALID_VECTOR_VECTOR_PRODUCT__IF_YOU_WANTED_A_DOT_OR_COEFF_WISE_PRODUCT_YOU_MUST_USE_THE_EXPLICIT_FUNCTIONS)
604
EIGEN_STATIC_ASSERT(ProductIsValid || !(SameSizes && !AreVectors),
605
INVALID_MATRIX_PRODUCT__IF_YOU_WANTED_A_COEFF_WISE_PRODUCT_YOU_MUST_USE_THE_EXPLICIT_FUNCTION)
606
EIGEN_STATIC_ASSERT(ProductIsValid || SameSizes, INVALID_MATRIX_PRODUCT)
608
return typename LazyProductReturnType<Derived,OtherDerived>::Type(derived(), other.derived());
611
} // end namespace Eigen
613
#endif // EIGEN_PRODUCT_H