11 #ifndef EIGEN_GENERAL_PRODUCT_H
12 #define EIGEN_GENERAL_PRODUCT_H
23 template<
int Rows,
int Cols,
int Depth>
struct product_type_selector;
25 template<
int Size,
int MaxSize>
struct product_size_category
27 enum { is_large = MaxSize == Dynamic ||
28 Size >= EIGEN_CACHEFRIENDLY_PRODUCT_THRESHOLD,
29 value = is_large ? Large
35 template<
typename Lhs,
typename Rhs>
struct product_type
37 typedef typename remove_all<Lhs>::type _Lhs;
38 typedef typename remove_all<Rhs>::type _Rhs;
40 MaxRows = traits<_Lhs>::MaxRowsAtCompileTime,
41 Rows = traits<_Lhs>::RowsAtCompileTime,
42 MaxCols = traits<_Rhs>::MaxColsAtCompileTime,
43 Cols = traits<_Rhs>::ColsAtCompileTime,
44 MaxDepth = EIGEN_SIZE_MIN_PREFER_FIXED(traits<_Lhs>::MaxColsAtCompileTime,
45 traits<_Rhs>::MaxRowsAtCompileTime),
46 Depth = EIGEN_SIZE_MIN_PREFER_FIXED(traits<_Lhs>::ColsAtCompileTime,
47 traits<_Rhs>::RowsAtCompileTime)
54 rows_select = product_size_category<Rows,MaxRows>::value,
55 cols_select = product_size_category<Cols,MaxCols>::value,
56 depth_select = product_size_category<Depth,MaxDepth>::value
58 typedef product_type_selector<rows_select, cols_select, depth_select> selector;
62 value = selector::ret,
65 #ifdef EIGEN_DEBUG_PRODUCT
68 EIGEN_DEBUG_VAR(Rows);
69 EIGEN_DEBUG_VAR(Cols);
70 EIGEN_DEBUG_VAR(Depth);
71 EIGEN_DEBUG_VAR(rows_select);
72 EIGEN_DEBUG_VAR(cols_select);
73 EIGEN_DEBUG_VAR(depth_select);
74 EIGEN_DEBUG_VAR(value);
109 template<
int M,
int N>
struct product_type_selector<M,N,1> {
enum { ret = OuterProduct }; };
110 template<
int Depth>
struct product_type_selector<1, 1, Depth> {
enum { ret = InnerProduct }; };
111 template<>
struct product_type_selector<1, 1, 1> {
enum { ret = InnerProduct }; };
112 template<>
struct product_type_selector<Small,1, Small> {
enum { ret = CoeffBasedProductMode }; };
113 template<>
struct product_type_selector<1, Small,Small> {
enum { ret = CoeffBasedProductMode }; };
114 template<>
struct product_type_selector<Small,Small,Small> {
enum { ret = CoeffBasedProductMode }; };
115 template<>
struct product_type_selector<Small, Small, 1> {
enum { ret = LazyCoeffBasedProductMode }; };
116 template<>
struct product_type_selector<Small, Large, 1> {
enum { ret = LazyCoeffBasedProductMode }; };
117 template<>
struct product_type_selector<Large, Small, 1> {
enum { ret = LazyCoeffBasedProductMode }; };
118 template<>
struct product_type_selector<1, Large,Small> {
enum { ret = CoeffBasedProductMode }; };
119 template<>
struct product_type_selector<1, Large,Large> {
enum { ret = GemvProduct }; };
120 template<>
struct product_type_selector<1, Small,Large> {
enum { ret = CoeffBasedProductMode }; };
121 template<>
struct product_type_selector<Large,1, Small> {
enum { ret = CoeffBasedProductMode }; };
122 template<>
struct product_type_selector<Large,1, Large> {
enum { ret = GemvProduct }; };
123 template<>
struct product_type_selector<Small,1, Large> {
enum { ret = CoeffBasedProductMode }; };
124 template<>
struct product_type_selector<Small,Small,Large> {
enum { ret = GemmProduct }; };
125 template<>
struct product_type_selector<Large,Small,Large> {
enum { ret = GemmProduct }; };
126 template<>
struct product_type_selector<Small,Large,Large> {
enum { ret = GemmProduct }; };
127 template<>
struct product_type_selector<Large,Large,Large> {
enum { ret = GemmProduct }; };
128 template<>
struct product_type_selector<Large,Small,Small> {
enum { ret = GemmProduct }; };
129 template<>
struct product_type_selector<Small,Large,Small> {
enum { ret = GemmProduct }; };
130 template<>
struct product_type_selector<Large,Large,Small> {
enum { ret = GemmProduct }; };
162 template<
int S
ide,
int StorageOrder,
bool BlasCompatible>
163 struct gemv_dense_selector;
169 template<
typename Scalar,
int Size,
int MaxSize,
bool Cond>
struct gemv_static_vector_if;
171 template<
typename Scalar,
int Size,
int MaxSize>
172 struct gemv_static_vector_if<Scalar,Size,MaxSize,false>
174 EIGEN_STRONG_INLINE Scalar* data() { eigen_internal_assert(
false &&
"should never be called");
return 0; }
177 template<
typename Scalar,
int Size>
178 struct gemv_static_vector_if<Scalar,Size,Dynamic,true>
180 EIGEN_STRONG_INLINE Scalar* data() {
return 0; }
183 template<
typename Scalar,
int Size,
int MaxSize>
184 struct gemv_static_vector_if<Scalar,Size,MaxSize,true>
186 #if EIGEN_MAX_STATIC_ALIGN_BYTES!=0
187 internal::plain_array<Scalar,EIGEN_SIZE_MIN_PREFER_FIXED(Size,MaxSize),0> m_data;
188 EIGEN_STRONG_INLINE Scalar* data() {
return m_data.array; }
193 ForceAlignment = internal::packet_traits<Scalar>::Vectorizable,
194 PacketSize = internal::packet_traits<Scalar>::size
196 internal::plain_array<Scalar,EIGEN_SIZE_MIN_PREFER_FIXED(Size,MaxSize)+(ForceAlignment?PacketSize:0),0> m_data;
197 EIGEN_STRONG_INLINE Scalar* data() {
198 return ForceAlignment
199 ?
reinterpret_cast<Scalar*
>((
reinterpret_cast<size_t>(m_data.array) & ~(
size_t(EIGEN_MAX_ALIGN_BYTES-1))) + EIGEN_MAX_ALIGN_BYTES)
206 template<
int StorageOrder,
bool BlasCompatible>
207 struct gemv_dense_selector<
OnTheLeft,StorageOrder,BlasCompatible>
209 template<
typename Lhs,
typename Rhs,
typename Dest>
210 static void run(
const Lhs &lhs,
const Rhs &rhs, Dest& dest,
const typename Dest::Scalar& alpha)
212 Transpose<Dest> destT(dest);
214 gemv_dense_selector<OnTheRight,OtherStorageOrder,BlasCompatible>
215 ::run(rhs.transpose(), lhs.transpose(), destT, alpha);
221 template<
typename Lhs,
typename Rhs,
typename Dest>
222 static inline void run(
const Lhs &lhs,
const Rhs &rhs, Dest& dest,
const typename Dest::Scalar& alpha)
224 typedef typename Lhs::Scalar LhsScalar;
225 typedef typename Rhs::Scalar RhsScalar;
226 typedef typename Dest::Scalar ResScalar;
227 typedef typename Dest::RealScalar RealScalar;
229 typedef internal::blas_traits<Lhs> LhsBlasTraits;
230 typedef typename LhsBlasTraits::DirectLinearAccessType ActualLhsType;
231 typedef internal::blas_traits<Rhs> RhsBlasTraits;
232 typedef typename RhsBlasTraits::DirectLinearAccessType ActualRhsType;
234 typedef Map<Matrix<ResScalar,Dynamic,1>,
Aligned> MappedDest;
236 ActualLhsType actualLhs = LhsBlasTraits::extract(lhs);
237 ActualRhsType actualRhs = RhsBlasTraits::extract(rhs);
239 ResScalar actualAlpha = alpha * LhsBlasTraits::extractScalarFactor(lhs)
240 * RhsBlasTraits::extractScalarFactor(rhs);
245 EvalToDestAtCompileTime = Dest::InnerStrideAtCompileTime==1,
246 ComplexByReal = (NumTraits<LhsScalar>::IsComplex) && (!NumTraits<RhsScalar>::IsComplex),
247 MightCannotUseDest = (Dest::InnerStrideAtCompileTime!=1) || ComplexByReal
250 gemv_static_vector_if<ResScalar,Dest::SizeAtCompileTime,Dest::MaxSizeAtCompileTime,MightCannotUseDest> static_dest;
252 const bool alphaIsCompatible = (!ComplexByReal) || (numext::imag(actualAlpha)==RealScalar(0));
253 const bool evalToDest = EvalToDestAtCompileTime && alphaIsCompatible;
255 RhsScalar compatibleAlpha = get_factor<ResScalar,RhsScalar>::run(actualAlpha);
257 ei_declare_aligned_stack_constructed_variable(ResScalar,actualDestPtr,dest.size(),
258 evalToDest ? dest.data() : static_dest.data());
262 #ifdef EIGEN_DENSE_STORAGE_CTOR_PLUGIN
263 Index size = dest.size();
264 EIGEN_DENSE_STORAGE_CTOR_PLUGIN
266 if(!alphaIsCompatible)
268 MappedDest(actualDestPtr, dest.size()).setZero();
269 compatibleAlpha = RhsScalar(1);
272 MappedDest(actualDestPtr, dest.size()) = dest;
275 typedef const_blas_data_mapper<LhsScalar,Index,ColMajor> LhsMapper;
276 typedef const_blas_data_mapper<RhsScalar,Index,RowMajor> RhsMapper;
277 general_matrix_vector_product
278 <Index,LhsScalar,LhsMapper,
ColMajor,LhsBlasTraits::NeedToConjugate,RhsScalar,RhsMapper,RhsBlasTraits::NeedToConjugate>::run(
279 actualLhs.rows(), actualLhs.cols(),
280 LhsMapper(actualLhs.data(), actualLhs.outerStride()),
281 RhsMapper(actualRhs.data(), actualRhs.innerStride()),
287 if(!alphaIsCompatible)
288 dest += actualAlpha * MappedDest(actualDestPtr, dest.size());
290 dest = MappedDest(actualDestPtr, dest.size());
297 template<
typename Lhs,
typename Rhs,
typename Dest>
298 static void run(
const Lhs &lhs,
const Rhs &rhs, Dest& dest,
const typename Dest::Scalar& alpha)
300 typedef typename Lhs::Scalar LhsScalar;
301 typedef typename Rhs::Scalar RhsScalar;
302 typedef typename Dest::Scalar ResScalar;
304 typedef internal::blas_traits<Lhs> LhsBlasTraits;
305 typedef typename LhsBlasTraits::DirectLinearAccessType ActualLhsType;
306 typedef internal::blas_traits<Rhs> RhsBlasTraits;
307 typedef typename RhsBlasTraits::DirectLinearAccessType ActualRhsType;
308 typedef typename internal::remove_all<ActualRhsType>::type ActualRhsTypeCleaned;
310 typename add_const<ActualLhsType>::type actualLhs = LhsBlasTraits::extract(lhs);
311 typename add_const<ActualRhsType>::type actualRhs = RhsBlasTraits::extract(rhs);
313 ResScalar actualAlpha = alpha * LhsBlasTraits::extractScalarFactor(lhs)
314 * RhsBlasTraits::extractScalarFactor(rhs);
319 DirectlyUseRhs = ActualRhsTypeCleaned::InnerStrideAtCompileTime==1
322 gemv_static_vector_if<RhsScalar,ActualRhsTypeCleaned::SizeAtCompileTime,ActualRhsTypeCleaned::MaxSizeAtCompileTime,!DirectlyUseRhs> static_rhs;
324 ei_declare_aligned_stack_constructed_variable(RhsScalar,actualRhsPtr,actualRhs.size(),
325 DirectlyUseRhs ?
const_cast<RhsScalar*
>(actualRhs.data()) : static_rhs.data());
329 #ifdef EIGEN_DENSE_STORAGE_CTOR_PLUGIN
330 Index size = actualRhs.size();
331 EIGEN_DENSE_STORAGE_CTOR_PLUGIN
333 Map<typename ActualRhsTypeCleaned::PlainObject>(actualRhsPtr, actualRhs.size()) = actualRhs;
336 typedef const_blas_data_mapper<LhsScalar,Index,RowMajor> LhsMapper;
337 typedef const_blas_data_mapper<RhsScalar,Index,ColMajor> RhsMapper;
338 general_matrix_vector_product
339 <Index,LhsScalar,LhsMapper,
RowMajor,LhsBlasTraits::NeedToConjugate,RhsScalar,RhsMapper,RhsBlasTraits::NeedToConjugate>::run(
340 actualLhs.rows(), actualLhs.cols(),
341 LhsMapper(actualLhs.data(), actualLhs.outerStride()),
342 RhsMapper(actualRhsPtr, 1),
343 dest.data(), dest.innerStride(),
348 template<>
struct gemv_dense_selector<
OnTheRight,ColMajor,false>
350 template<
typename Lhs,
typename Rhs,
typename Dest>
351 static void run(
const Lhs &lhs,
const Rhs &rhs, Dest& dest,
const typename Dest::Scalar& alpha)
354 typename nested_eval<Rhs,1>::type actual_rhs(rhs);
355 const Index size = rhs.rows();
356 for(Index k=0; k<size; ++k)
357 dest += (alpha*actual_rhs.coeff(k)) * lhs.col(k);
361 template<>
struct gemv_dense_selector<
OnTheRight,RowMajor,false>
363 template<
typename Lhs,
typename Rhs,
typename Dest>
364 static void run(
const Lhs &lhs,
const Rhs &rhs, Dest& dest,
const typename Dest::Scalar& alpha)
366 typename nested_eval<Rhs,Lhs::RowsAtCompileTime>::type actual_rhs(rhs);
367 const Index rows = dest.rows();
368 for(Index i=0; i<rows; ++i)
369 dest.coeffRef(i) += alpha * (lhs.row(i).cwiseProduct(actual_rhs.transpose())).sum();
387 template<
typename Derived>
388 template<
typename OtherDerived>
389 inline const Product<Derived, OtherDerived>
397 ProductIsValid = Derived::ColsAtCompileTime==Dynamic
398 || OtherDerived::RowsAtCompileTime==Dynamic
399 || int(Derived::ColsAtCompileTime)==int(OtherDerived::RowsAtCompileTime),
400 AreVectors = Derived::IsVectorAtCompileTime && OtherDerived::IsVectorAtCompileTime,
401 SameSizes = EIGEN_PREDICATE_SAME_MATRIX_SIZE(Derived,OtherDerived)
406 EIGEN_STATIC_ASSERT(ProductIsValid || !(AreVectors && SameSizes),
407 INVALID_VECTOR_VECTOR_PRODUCT__IF_YOU_WANTED_A_DOT_OR_COEFF_WISE_PRODUCT_YOU_MUST_USE_THE_EXPLICIT_FUNCTIONS)
408 EIGEN_STATIC_ASSERT(ProductIsValid || !(SameSizes && !AreVectors),
409 INVALID_MATRIX_PRODUCT__IF_YOU_WANTED_A_COEFF_WISE_PRODUCT_YOU_MUST_USE_THE_EXPLICIT_FUNCTION)
410 EIGEN_STATIC_ASSERT(ProductIsValid || SameSizes, INVALID_MATRIX_PRODUCT)
411 #ifdef EIGEN_DEBUG_PRODUCT
412 internal::product_type<Derived,OtherDerived>::debug();
431 template<
typename Derived>
432 template<
typename OtherDerived>
437 ProductIsValid = Derived::ColsAtCompileTime==Dynamic
438 || OtherDerived::RowsAtCompileTime==Dynamic
439 || int(Derived::ColsAtCompileTime)==int(OtherDerived::RowsAtCompileTime),
440 AreVectors = Derived::IsVectorAtCompileTime && OtherDerived::IsVectorAtCompileTime,
441 SameSizes = EIGEN_PREDICATE_SAME_MATRIX_SIZE(Derived,OtherDerived)
446 EIGEN_STATIC_ASSERT(ProductIsValid || !(AreVectors && SameSizes),
447 INVALID_VECTOR_VECTOR_PRODUCT__IF_YOU_WANTED_A_DOT_OR_COEFF_WISE_PRODUCT_YOU_MUST_USE_THE_EXPLICIT_FUNCTIONS)
448 EIGEN_STATIC_ASSERT(ProductIsValid || !(SameSizes && !AreVectors),
449 INVALID_MATRIX_PRODUCT__IF_YOU_WANTED_A_COEFF_WISE_PRODUCT_YOU_MUST_USE_THE_EXPLICIT_FUNCTION)
450 EIGEN_STATIC_ASSERT(ProductIsValid || SameSizes, INVALID_MATRIX_PRODUCT)
457 #endif // EIGEN_PRODUCT_H
Expression of the product of two arbitrary matrices or vectors.
Definition: Product.h:107
Definition: Constants.h:333
const Product< Derived, OtherDerived, LazyProduct > lazyProduct(const MatrixBase< OtherDerived > &other) const
Definition: GeneralProduct.h:434
Definition: Constants.h:320
Definition: Constants.h:335
Definition: Constants.h:322
const ScalarMultipleReturnType operator*(const Scalar &scalar) const
Definition: MatrixBase.h:57
Definition: Eigen_Colamd.h:54
Definition: Constants.h:235
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48