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
28 #ifndef EIGEN_CUDA_ARCH 29 is_large = MaxSize ==
Dynamic ||
30 Size >= EIGEN_CACHEFRIENDLY_PRODUCT_THRESHOLD ||
31 (Size==
Dynamic && MaxSize>=EIGEN_CACHEFRIENDLY_PRODUCT_THRESHOLD),
35 value = is_large ? Large
41 template<
typename Lhs,
typename Rhs>
struct product_type
43 typedef typename remove_all<Lhs>::type _Lhs;
44 typedef typename remove_all<Rhs>::type _Rhs;
46 MaxRows = traits<_Lhs>::MaxRowsAtCompileTime,
47 Rows = traits<_Lhs>::RowsAtCompileTime,
48 MaxCols = traits<_Rhs>::MaxColsAtCompileTime,
49 Cols = traits<_Rhs>::ColsAtCompileTime,
50 MaxDepth = EIGEN_SIZE_MIN_PREFER_FIXED(traits<_Lhs>::MaxColsAtCompileTime,
51 traits<_Rhs>::MaxRowsAtCompileTime),
52 Depth = EIGEN_SIZE_MIN_PREFER_FIXED(traits<_Lhs>::ColsAtCompileTime,
53 traits<_Rhs>::RowsAtCompileTime)
60 rows_select = product_size_category<Rows,MaxRows>::value,
61 cols_select = product_size_category<Cols,MaxCols>::value,
62 depth_select = product_size_category<Depth,MaxDepth>::value
64 typedef product_type_selector<rows_select, cols_select, depth_select> selector;
68 value = selector::ret,
71 #ifdef EIGEN_DEBUG_PRODUCT 74 EIGEN_DEBUG_VAR(Rows);
75 EIGEN_DEBUG_VAR(Cols);
76 EIGEN_DEBUG_VAR(Depth);
77 EIGEN_DEBUG_VAR(rows_select);
78 EIGEN_DEBUG_VAR(cols_select);
79 EIGEN_DEBUG_VAR(depth_select);
80 EIGEN_DEBUG_VAR(value);
89 template<
int M,
int N>
struct product_type_selector<M,N,1> {
enum { ret = OuterProduct }; };
90 template<
int M>
struct product_type_selector<M, 1, 1> {
enum { ret = LazyCoeffBasedProductMode }; };
91 template<
int N>
struct product_type_selector<1, N, 1> {
enum { ret = LazyCoeffBasedProductMode }; };
92 template<
int Depth>
struct product_type_selector<1, 1, Depth> {
enum { ret = InnerProduct }; };
93 template<>
struct product_type_selector<1, 1, 1> {
enum { ret = InnerProduct }; };
94 template<>
struct product_type_selector<Small,1, Small> {
enum { ret = CoeffBasedProductMode }; };
95 template<>
struct product_type_selector<1, Small,Small> {
enum { ret = CoeffBasedProductMode }; };
96 template<>
struct product_type_selector<Small,Small,Small> {
enum { ret = CoeffBasedProductMode }; };
97 template<>
struct product_type_selector<Small, Small, 1> {
enum { ret = LazyCoeffBasedProductMode }; };
98 template<>
struct product_type_selector<Small, Large, 1> {
enum { ret = LazyCoeffBasedProductMode }; };
99 template<>
struct product_type_selector<Large, Small, 1> {
enum { ret = LazyCoeffBasedProductMode }; };
100 template<>
struct product_type_selector<1, Large,Small> {
enum { ret = CoeffBasedProductMode }; };
101 template<>
struct product_type_selector<1, Large,Large> {
enum { ret = GemvProduct }; };
102 template<>
struct product_type_selector<1, Small,Large> {
enum { ret = CoeffBasedProductMode }; };
103 template<>
struct product_type_selector<Large,1, Small> {
enum { ret = CoeffBasedProductMode }; };
104 template<>
struct product_type_selector<Large,1, Large> {
enum { ret = GemvProduct }; };
105 template<>
struct product_type_selector<Small,1, Large> {
enum { ret = CoeffBasedProductMode }; };
106 template<>
struct product_type_selector<Small,Small,Large> {
enum { ret = GemmProduct }; };
107 template<>
struct product_type_selector<Large,Small,Large> {
enum { ret = GemmProduct }; };
108 template<>
struct product_type_selector<Small,Large,Large> {
enum { ret = GemmProduct }; };
109 template<>
struct product_type_selector<Large,Large,Large> {
enum { ret = GemmProduct }; };
110 template<>
struct product_type_selector<Large,Small,Small> {
enum { ret = CoeffBasedProductMode }; };
111 template<>
struct product_type_selector<Small,Large,Small> {
enum { ret = CoeffBasedProductMode }; };
112 template<>
struct product_type_selector<Large,Large,Small> {
enum { ret = GemmProduct }; };
144 template<
int S
ide,
int StorageOrder,
bool BlasCompatible>
145 struct gemv_dense_selector;
151 template<
typename Scalar,
int Size,
int MaxSize,
bool Cond>
struct gemv_static_vector_if;
153 template<
typename Scalar,
int Size,
int MaxSize>
154 struct gemv_static_vector_if<Scalar,Size,MaxSize,false>
156 EIGEN_STRONG_INLINE Scalar* data() { eigen_internal_assert(
false &&
"should never be called");
return 0; }
159 template<
typename Scalar,
int Size>
160 struct gemv_static_vector_if<Scalar,Size,
Dynamic,true>
162 EIGEN_STRONG_INLINE Scalar* data() {
return 0; }
165 template<
typename Scalar,
int Size,
int MaxSize>
166 struct gemv_static_vector_if<Scalar,Size,MaxSize,true>
169 ForceAlignment = internal::packet_traits<Scalar>::Vectorizable,
170 PacketSize = internal::packet_traits<Scalar>::size
172 #if EIGEN_MAX_STATIC_ALIGN_BYTES!=0 173 internal::plain_array<Scalar,EIGEN_SIZE_MIN_PREFER_FIXED(Size,MaxSize),0,EIGEN_PLAIN_ENUM_MIN(AlignedMax,PacketSize)> m_data;
174 EIGEN_STRONG_INLINE Scalar* data() {
return m_data.array; }
178 internal::plain_array<Scalar,EIGEN_SIZE_MIN_PREFER_FIXED(Size,MaxSize)+(ForceAlignment?EIGEN_MAX_ALIGN_BYTES:0),0> m_data;
179 EIGEN_STRONG_INLINE Scalar* data() {
180 return ForceAlignment
181 ? reinterpret_cast<Scalar*>((internal::UIntPtr(m_data.array) & ~(std::size_t(EIGEN_MAX_ALIGN_BYTES-1))) + EIGEN_MAX_ALIGN_BYTES)
188 template<
int StorageOrder,
bool BlasCompatible>
189 struct gemv_dense_selector<
OnTheLeft,StorageOrder,BlasCompatible>
191 template<
typename Lhs,
typename Rhs,
typename Dest>
192 static void run(
const Lhs &lhs,
const Rhs &rhs, Dest& dest,
const typename Dest::Scalar& alpha)
194 Transpose<Dest> destT(dest);
196 gemv_dense_selector<OnTheRight,OtherStorageOrder,BlasCompatible>
197 ::run(rhs.transpose(), lhs.transpose(), destT, alpha);
203 template<
typename Lhs,
typename Rhs,
typename Dest>
204 static inline void run(
const Lhs &lhs,
const Rhs &rhs, Dest& dest,
const typename Dest::Scalar& alpha)
206 typedef typename Lhs::Scalar LhsScalar;
207 typedef typename Rhs::Scalar RhsScalar;
208 typedef typename Dest::Scalar ResScalar;
209 typedef typename Dest::RealScalar RealScalar;
211 typedef internal::blas_traits<Lhs> LhsBlasTraits;
212 typedef typename LhsBlasTraits::DirectLinearAccessType ActualLhsType;
213 typedef internal::blas_traits<Rhs> RhsBlasTraits;
214 typedef typename RhsBlasTraits::DirectLinearAccessType ActualRhsType;
216 typedef Map<Matrix<ResScalar,Dynamic,1>, EIGEN_PLAIN_ENUM_MIN(AlignedMax,internal::packet_traits<ResScalar>::size)> MappedDest;
218 ActualLhsType actualLhs = LhsBlasTraits::extract(lhs);
219 ActualRhsType actualRhs = RhsBlasTraits::extract(rhs);
221 ResScalar actualAlpha = alpha * LhsBlasTraits::extractScalarFactor(lhs)
222 * RhsBlasTraits::extractScalarFactor(rhs);
225 typedef typename conditional<Dest::IsVectorAtCompileTime, Dest, typename Dest::ColXpr>::type ActualDest;
230 EvalToDestAtCompileTime = (ActualDest::InnerStrideAtCompileTime==1),
231 ComplexByReal = (NumTraits<LhsScalar>::IsComplex) && (!NumTraits<RhsScalar>::IsComplex),
232 MightCannotUseDest = (!EvalToDestAtCompileTime) || ComplexByReal
235 typedef const_blas_data_mapper<LhsScalar,Index,ColMajor> LhsMapper;
236 typedef const_blas_data_mapper<RhsScalar,Index,RowMajor> RhsMapper;
237 RhsScalar compatibleAlpha = get_factor<ResScalar,RhsScalar>::run(actualAlpha);
239 if(!MightCannotUseDest)
243 general_matrix_vector_product
244 <
Index,LhsScalar,LhsMapper,
ColMajor,LhsBlasTraits::NeedToConjugate,RhsScalar,RhsMapper,RhsBlasTraits::NeedToConjugate>::run(
245 actualLhs.rows(), actualLhs.cols(),
246 LhsMapper(actualLhs.data(), actualLhs.outerStride()),
247 RhsMapper(actualRhs.data(), actualRhs.innerStride()),
253 gemv_static_vector_if<ResScalar,ActualDest::SizeAtCompileTime,ActualDest::MaxSizeAtCompileTime,MightCannotUseDest> static_dest;
255 const bool alphaIsCompatible = (!ComplexByReal) || (numext::imag(actualAlpha)==RealScalar(0));
256 const bool evalToDest = EvalToDestAtCompileTime && alphaIsCompatible;
258 ei_declare_aligned_stack_constructed_variable(ResScalar,actualDestPtr,dest.size(),
259 evalToDest ? dest.data() : static_dest.data());
263 #ifdef EIGEN_DENSE_STORAGE_CTOR_PLUGIN 264 Index size = dest.size();
265 EIGEN_DENSE_STORAGE_CTOR_PLUGIN
267 if(!alphaIsCompatible)
269 MappedDest(actualDestPtr, dest.size()).setZero();
270 compatibleAlpha = RhsScalar(1);
273 MappedDest(actualDestPtr, dest.size()) = dest;
276 general_matrix_vector_product
277 <
Index,LhsScalar,LhsMapper,
ColMajor,LhsBlasTraits::NeedToConjugate,RhsScalar,RhsMapper,RhsBlasTraits::NeedToConjugate>::run(
278 actualLhs.rows(), actualLhs.cols(),
279 LhsMapper(actualLhs.data(), actualLhs.outerStride()),
280 RhsMapper(actualRhs.data(), actualRhs.innerStride()),
286 if(!alphaIsCompatible)
287 dest.matrix() += actualAlpha * MappedDest(actualDestPtr, dest.size());
289 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.col(0).innerStride(),
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)
353 EIGEN_STATIC_ASSERT((!nested_eval<Lhs,1>::Evaluate),EIGEN_INTERNAL_COMPILATION_ERROR_OR_YOU_MADE_A_PROGRAMMING_MISTAKE);
355 typename nested_eval<Rhs,1>::type actual_rhs(rhs);
356 const Index size = rhs.rows();
357 for(
Index k=0; k<size; ++k)
358 dest += (alpha*actual_rhs.coeff(k)) * lhs.col(k);
364 template<
typename Lhs,
typename Rhs,
typename Dest>
365 static void run(
const Lhs &lhs,
const Rhs &rhs, Dest& dest,
const typename Dest::Scalar& alpha)
367 EIGEN_STATIC_ASSERT((!nested_eval<Lhs,1>::Evaluate),EIGEN_INTERNAL_COMPILATION_ERROR_OR_YOU_MADE_A_PROGRAMMING_MISTAKE);
368 typename nested_eval<Rhs,Lhs::RowsAtCompileTime>::type actual_rhs(rhs);
369 const Index rows = dest.rows();
370 for(
Index i=0; i<rows; ++i)
371 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();
429 template<
typename Derived>
430 template<
typename OtherDerived>
435 ProductIsValid = Derived::ColsAtCompileTime==
Dynamic 436 || OtherDerived::RowsAtCompileTime==
Dynamic 437 || int(Derived::ColsAtCompileTime)==int(OtherDerived::RowsAtCompileTime),
438 AreVectors = Derived::IsVectorAtCompileTime && OtherDerived::IsVectorAtCompileTime,
439 SameSizes = EIGEN_PREDICATE_SAME_MATRIX_SIZE(Derived,OtherDerived)
444 EIGEN_STATIC_ASSERT(ProductIsValid || !(AreVectors && SameSizes),
445 INVALID_VECTOR_VECTOR_PRODUCT__IF_YOU_WANTED_A_DOT_OR_COEFF_WISE_PRODUCT_YOU_MUST_USE_THE_EXPLICIT_FUNCTIONS)
446 EIGEN_STATIC_ASSERT(ProductIsValid || !(SameSizes && !AreVectors),
447 INVALID_MATRIX_PRODUCT__IF_YOU_WANTED_A_COEFF_WISE_PRODUCT_YOU_MUST_USE_THE_EXPLICIT_FUNCTION)
448 EIGEN_STATIC_ASSERT(ProductIsValid || SameSizes, INVALID_MATRIX_PRODUCT)
455 #endif // EIGEN_PRODUCT_H Definition: Constants.h:320
Expression of the product of two arbitrary matrices or vectors.
Definition: Product.h:71
Definition: Constants.h:335
Namespace containing all symbols from the Eigen library.
Definition: Core:306
Derived & derived()
Definition: EigenBase.h:45
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:33
Definition: Constants.h:333
Definition: Constants.h:322
const int Dynamic
Definition: Constants.h:21
Base class for all dense matrices, vectors, and expressions.
Definition: MatrixBase.h:48
const CwiseBinaryOp< internal::scalar_product_op< Scalar, T >, Derived, Constant< T > > operator*(const T &scalar) const
const Product< Derived, OtherDerived, LazyProduct > lazyProduct(const MatrixBase< OtherDerived > &other) const
Definition: GeneralProduct.h:432