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);
83 template<
int M,
int N>
struct product_type_selector<M,N,1> {
enum { ret = OuterProduct }; };
84 template<
int M>
struct product_type_selector<M, 1, 1> {
enum { ret = LazyCoeffBasedProductMode }; };
85 template<
int N>
struct product_type_selector<1, N, 1> {
enum { ret = LazyCoeffBasedProductMode }; };
86 template<
int Depth>
struct product_type_selector<1, 1, Depth> {
enum { ret = InnerProduct }; };
87 template<>
struct product_type_selector<1, 1, 1> {
enum { ret = InnerProduct }; };
88 template<>
struct product_type_selector<Small,1, Small> {
enum { ret = CoeffBasedProductMode }; };
89 template<>
struct product_type_selector<1, Small,Small> {
enum { ret = CoeffBasedProductMode }; };
90 template<>
struct product_type_selector<Small,Small,Small> {
enum { ret = CoeffBasedProductMode }; };
91 template<>
struct product_type_selector<Small, Small, 1> {
enum { ret = LazyCoeffBasedProductMode }; };
92 template<>
struct product_type_selector<Small, Large, 1> {
enum { ret = LazyCoeffBasedProductMode }; };
93 template<>
struct product_type_selector<Large, Small, 1> {
enum { ret = LazyCoeffBasedProductMode }; };
94 template<>
struct product_type_selector<1, Large,Small> {
enum { ret = CoeffBasedProductMode }; };
95 template<>
struct product_type_selector<1, Large,Large> {
enum { ret = GemvProduct }; };
96 template<>
struct product_type_selector<1, Small,Large> {
enum { ret = CoeffBasedProductMode }; };
97 template<>
struct product_type_selector<Large,1, Small> {
enum { ret = CoeffBasedProductMode }; };
98 template<>
struct product_type_selector<Large,1, Large> {
enum { ret = GemvProduct }; };
99 template<>
struct product_type_selector<Small,1, Large> {
enum { ret = CoeffBasedProductMode }; };
100 template<>
struct product_type_selector<Small,Small,Large> {
enum { ret = GemmProduct }; };
101 template<>
struct product_type_selector<Large,Small,Large> {
enum { ret = GemmProduct }; };
102 template<>
struct product_type_selector<Small,Large,Large> {
enum { ret = GemmProduct }; };
103 template<>
struct product_type_selector<Large,Large,Large> {
enum { ret = GemmProduct }; };
104 template<>
struct product_type_selector<Large,Small,Small> {
enum { ret = CoeffBasedProductMode }; };
105 template<>
struct product_type_selector<Small,Large,Small> {
enum { ret = CoeffBasedProductMode }; };
106 template<>
struct product_type_selector<Large,Large,Small> {
enum { ret = GemmProduct }; };
138 template<
int S
ide,
int StorageOrder,
bool BlasCompatible>
139 struct gemv_dense_selector;
145 template<
typename Scalar,
int Size,
int MaxSize,
bool Cond>
struct gemv_static_vector_if;
147 template<
typename Scalar,
int Size,
int MaxSize>
148 struct gemv_static_vector_if<Scalar,Size,MaxSize,false>
150 EIGEN_STRONG_INLINE Scalar* data() { eigen_internal_assert(
false &&
"should never be called");
return 0; }
153 template<
typename Scalar,
int Size>
154 struct gemv_static_vector_if<Scalar,Size,
Dynamic,true>
156 EIGEN_STRONG_INLINE Scalar* data() {
return 0; }
159 template<
typename Scalar,
int Size,
int MaxSize>
160 struct gemv_static_vector_if<Scalar,Size,MaxSize,true>
162 #if EIGEN_MAX_STATIC_ALIGN_BYTES!=0 163 internal::plain_array<Scalar,EIGEN_SIZE_MIN_PREFER_FIXED(Size,MaxSize),0> m_data;
164 EIGEN_STRONG_INLINE Scalar* data() {
return m_data.array; }
169 ForceAlignment = internal::packet_traits<Scalar>::Vectorizable,
170 PacketSize = internal::packet_traits<Scalar>::size
172 internal::plain_array<Scalar,EIGEN_SIZE_MIN_PREFER_FIXED(Size,MaxSize)+(ForceAlignment?PacketSize:0),0> m_data;
173 EIGEN_STRONG_INLINE Scalar* data() {
174 return ForceAlignment
175 ?
reinterpret_cast<Scalar*
>((internal::UIntPtr(m_data.array) & ~(size_t(EIGEN_MAX_ALIGN_BYTES-1))) + EIGEN_MAX_ALIGN_BYTES)
182 template<
int StorageOrder,
bool BlasCompatible>
183 struct gemv_dense_selector<
OnTheLeft,StorageOrder,BlasCompatible>
185 template<
typename Lhs,
typename Rhs,
typename Dest>
186 static void run(
const Lhs &lhs,
const Rhs &rhs, Dest& dest,
const typename Dest::Scalar& alpha)
188 Transpose<Dest> destT(dest);
190 gemv_dense_selector<OnTheRight,OtherStorageOrder,BlasCompatible>
191 ::run(rhs.transpose(), lhs.transpose(), destT, alpha);
197 template<
typename Lhs,
typename Rhs,
typename Dest>
198 static inline void run(
const Lhs &lhs,
const Rhs &rhs, Dest& dest,
const typename Dest::Scalar& alpha)
200 typedef typename Lhs::Scalar LhsScalar;
201 typedef typename Rhs::Scalar RhsScalar;
202 typedef typename Dest::Scalar ResScalar;
203 typedef typename Dest::RealScalar RealScalar;
205 typedef internal::blas_traits<Lhs> LhsBlasTraits;
206 typedef typename LhsBlasTraits::DirectLinearAccessType ActualLhsType;
207 typedef internal::blas_traits<Rhs> RhsBlasTraits;
208 typedef typename RhsBlasTraits::DirectLinearAccessType ActualRhsType;
210 typedef Map<Matrix<ResScalar,Dynamic,1>,
Aligned> MappedDest;
212 ActualLhsType actualLhs = LhsBlasTraits::extract(lhs);
213 ActualRhsType actualRhs = RhsBlasTraits::extract(rhs);
215 ResScalar actualAlpha = alpha * LhsBlasTraits::extractScalarFactor(lhs)
216 * RhsBlasTraits::extractScalarFactor(rhs);
219 typedef typename conditional<Dest::IsVectorAtCompileTime, Dest, typename Dest::ColXpr>::type ActualDest;
224 EvalToDestAtCompileTime = (ActualDest::InnerStrideAtCompileTime==1),
225 ComplexByReal = (NumTraits<LhsScalar>::IsComplex) && (!NumTraits<RhsScalar>::IsComplex),
226 MightCannotUseDest = (ActualDest::InnerStrideAtCompileTime!=1) || ComplexByReal
229 gemv_static_vector_if<ResScalar,ActualDest::SizeAtCompileTime,ActualDest::MaxSizeAtCompileTime,MightCannotUseDest> static_dest;
231 const bool alphaIsCompatible = (!ComplexByReal) || (numext::imag(actualAlpha)==RealScalar(0));
232 const bool evalToDest = EvalToDestAtCompileTime && alphaIsCompatible;
234 RhsScalar compatibleAlpha = get_factor<ResScalar,RhsScalar>::run(actualAlpha);
236 ei_declare_aligned_stack_constructed_variable(ResScalar,actualDestPtr,dest.size(),
237 evalToDest ? dest.data() : static_dest.data());
241 #ifdef EIGEN_DENSE_STORAGE_CTOR_PLUGIN 242 Index size = dest.size();
243 EIGEN_DENSE_STORAGE_CTOR_PLUGIN
245 if(!alphaIsCompatible)
247 MappedDest(actualDestPtr, dest.size()).setZero();
248 compatibleAlpha = RhsScalar(1);
251 MappedDest(actualDestPtr, dest.size()) = dest;
254 typedef const_blas_data_mapper<LhsScalar,Index,ColMajor> LhsMapper;
255 typedef const_blas_data_mapper<RhsScalar,Index,RowMajor> RhsMapper;
256 general_matrix_vector_product
257 <
Index,LhsScalar,LhsMapper,
ColMajor,LhsBlasTraits::NeedToConjugate,RhsScalar,RhsMapper,RhsBlasTraits::NeedToConjugate>::run(
258 actualLhs.rows(), actualLhs.cols(),
259 LhsMapper(actualLhs.data(), actualLhs.outerStride()),
260 RhsMapper(actualRhs.data(), actualRhs.innerStride()),
266 if(!alphaIsCompatible)
267 dest += actualAlpha * MappedDest(actualDestPtr, dest.size());
269 dest = MappedDest(actualDestPtr, dest.size());
276 template<
typename Lhs,
typename Rhs,
typename Dest>
277 static void run(
const Lhs &lhs,
const Rhs &rhs, Dest& dest,
const typename Dest::Scalar& alpha)
279 typedef typename Lhs::Scalar LhsScalar;
280 typedef typename Rhs::Scalar RhsScalar;
281 typedef typename Dest::Scalar ResScalar;
283 typedef internal::blas_traits<Lhs> LhsBlasTraits;
284 typedef typename LhsBlasTraits::DirectLinearAccessType ActualLhsType;
285 typedef internal::blas_traits<Rhs> RhsBlasTraits;
286 typedef typename RhsBlasTraits::DirectLinearAccessType ActualRhsType;
287 typedef typename internal::remove_all<ActualRhsType>::type ActualRhsTypeCleaned;
289 typename add_const<ActualLhsType>::type actualLhs = LhsBlasTraits::extract(lhs);
290 typename add_const<ActualRhsType>::type actualRhs = RhsBlasTraits::extract(rhs);
292 ResScalar actualAlpha = alpha * LhsBlasTraits::extractScalarFactor(lhs)
293 * RhsBlasTraits::extractScalarFactor(rhs);
298 DirectlyUseRhs = ActualRhsTypeCleaned::InnerStrideAtCompileTime==1
301 gemv_static_vector_if<RhsScalar,ActualRhsTypeCleaned::SizeAtCompileTime,ActualRhsTypeCleaned::MaxSizeAtCompileTime,!DirectlyUseRhs> static_rhs;
303 ei_declare_aligned_stack_constructed_variable(RhsScalar,actualRhsPtr,actualRhs.size(),
304 DirectlyUseRhs ?
const_cast<RhsScalar*
>(actualRhs.data()) : static_rhs.data());
308 #ifdef EIGEN_DENSE_STORAGE_CTOR_PLUGIN 309 Index size = actualRhs.size();
310 EIGEN_DENSE_STORAGE_CTOR_PLUGIN
312 Map<typename ActualRhsTypeCleaned::PlainObject>(actualRhsPtr, actualRhs.size()) = actualRhs;
315 typedef const_blas_data_mapper<LhsScalar,Index,RowMajor> LhsMapper;
316 typedef const_blas_data_mapper<RhsScalar,Index,ColMajor> RhsMapper;
317 general_matrix_vector_product
318 <
Index,LhsScalar,LhsMapper,
RowMajor,LhsBlasTraits::NeedToConjugate,RhsScalar,RhsMapper,RhsBlasTraits::NeedToConjugate>::run(
319 actualLhs.rows(), actualLhs.cols(),
320 LhsMapper(actualLhs.data(), actualLhs.outerStride()),
321 RhsMapper(actualRhsPtr, 1),
322 dest.data(), dest.col(0).innerStride(),
327 template<>
struct gemv_dense_selector<
OnTheRight,ColMajor,false>
329 template<
typename Lhs,
typename Rhs,
typename Dest>
330 static void run(
const Lhs &lhs,
const Rhs &rhs, Dest& dest,
const typename Dest::Scalar& alpha)
333 typename nested_eval<Rhs,1>::type actual_rhs(rhs);
334 const Index size = rhs.rows();
335 for(Index k=0; k<size; ++k)
336 dest += (alpha*actual_rhs.coeff(k)) * lhs.col(k);
340 template<>
struct gemv_dense_selector<
OnTheRight,RowMajor,false>
342 template<
typename Lhs,
typename Rhs,
typename Dest>
343 static void run(
const Lhs &lhs,
const Rhs &rhs, Dest& dest,
const typename Dest::Scalar& alpha)
345 typename nested_eval<Rhs,Lhs::RowsAtCompileTime>::type actual_rhs(rhs);
346 const Index rows = dest.rows();
347 for(Index i=0; i<rows; ++i)
348 dest.coeffRef(i) += alpha * (lhs.row(i).cwiseProduct(actual_rhs.transpose())).sum();
366 template<
typename Derived>
367 template<
typename OtherDerived>
368 inline const Product<Derived, OtherDerived>
376 ProductIsValid = Derived::ColsAtCompileTime==
Dynamic 377 || OtherDerived::RowsAtCompileTime==
Dynamic 378 || int(Derived::ColsAtCompileTime)==int(OtherDerived::RowsAtCompileTime),
379 AreVectors = Derived::IsVectorAtCompileTime && OtherDerived::IsVectorAtCompileTime,
380 SameSizes = EIGEN_PREDICATE_SAME_MATRIX_SIZE(Derived,OtherDerived)
385 EIGEN_STATIC_ASSERT(ProductIsValid || !(AreVectors && SameSizes),
386 INVALID_VECTOR_VECTOR_PRODUCT__IF_YOU_WANTED_A_DOT_OR_COEFF_WISE_PRODUCT_YOU_MUST_USE_THE_EXPLICIT_FUNCTIONS)
387 EIGEN_STATIC_ASSERT(ProductIsValid || !(SameSizes && !AreVectors),
388 INVALID_MATRIX_PRODUCT__IF_YOU_WANTED_A_COEFF_WISE_PRODUCT_YOU_MUST_USE_THE_EXPLICIT_FUNCTION)
389 EIGEN_STATIC_ASSERT(ProductIsValid || SameSizes, INVALID_MATRIX_PRODUCT)
390 #ifdef EIGEN_DEBUG_PRODUCT 391 internal::product_type<Derived,OtherDerived>::debug();
410 template<
typename Derived>
411 template<
typename OtherDerived>
416 ProductIsValid = Derived::ColsAtCompileTime==
Dynamic 417 || OtherDerived::RowsAtCompileTime==
Dynamic 418 || int(Derived::ColsAtCompileTime)==int(OtherDerived::RowsAtCompileTime),
419 AreVectors = Derived::IsVectorAtCompileTime && OtherDerived::IsVectorAtCompileTime,
420 SameSizes = EIGEN_PREDICATE_SAME_MATRIX_SIZE(Derived,OtherDerived)
425 EIGEN_STATIC_ASSERT(ProductIsValid || !(AreVectors && SameSizes),
426 INVALID_VECTOR_VECTOR_PRODUCT__IF_YOU_WANTED_A_DOT_OR_COEFF_WISE_PRODUCT_YOU_MUST_USE_THE_EXPLICIT_FUNCTIONS)
427 EIGEN_STATIC_ASSERT(ProductIsValid || !(SameSizes && !AreVectors),
428 INVALID_MATRIX_PRODUCT__IF_YOU_WANTED_A_COEFF_WISE_PRODUCT_YOU_MUST_USE_THE_EXPLICIT_FUNCTION)
429 EIGEN_STATIC_ASSERT(ProductIsValid || SameSizes, INVALID_MATRIX_PRODUCT)
436 #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:271
Derived & derived()
Definition: EigenBase.h:44
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: XprHelper.h:35
Definition: Constants.h:235
Definition: Constants.h:333
Definition: Eigen_Colamd.h:50
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:413