Medial Code Documentation
Loading...
Searching...
No Matches
Product.h
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2008-2011 Gael Guennebaud <gael.guennebaud@inria.fr>
5//
6// This Source Code Form is subject to the terms of the Mozilla
7// Public License v. 2.0. If a copy of the MPL was not distributed
8// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9
10#ifndef EIGEN_PRODUCT_H
11#define EIGEN_PRODUCT_H
12
13namespace Eigen {
14
15template<typename Lhs, typename Rhs, int Option, typename StorageKind> class ProductImpl;
16
33namespace internal {
34
35// Determine the scalar of Product<Lhs, Rhs>. This is normally the same as Lhs::Scalar times
36// Rhs::Scalar, but product with permutation matrices inherit the scalar of the other factor.
37template<typename Lhs, typename Rhs, typename LhsShape = typename evaluator_traits<Lhs>::Shape,
38 typename RhsShape = typename evaluator_traits<Rhs>::Shape >
43
44template<typename Lhs, typename Rhs, typename RhsShape>
46{
47 typedef typename Rhs::Scalar Scalar;
48};
49
50template<typename Lhs, typename Rhs, typename LhsShape>
52{
53 typedef typename Lhs::Scalar Scalar;
54};
55
56template<typename Lhs, typename Rhs, typename RhsShape>
58{
59 typedef typename Rhs::Scalar Scalar;
60};
61
62template<typename Lhs, typename Rhs, typename LhsShape>
64{
65 typedef typename Lhs::Scalar Scalar;
66};
67
68template<typename Lhs, typename Rhs, int Option>
69struct traits<Product<Lhs, Rhs, Option> >
70{
71 typedef typename remove_all<Lhs>::type LhsCleaned;
72 typedef typename remove_all<Rhs>::type RhsCleaned;
75
76 typedef MatrixXpr XprKind;
77
78 typedef typename product_result_scalar<LhsCleaned,RhsCleaned>::Scalar Scalar;
79 typedef typename product_promote_storage_type<typename LhsTraits::StorageKind,
80 typename RhsTraits::StorageKind,
82 typedef typename promote_index_type<typename LhsTraits::StorageIndex,
83 typename RhsTraits::StorageIndex>::type StorageIndex;
84
85 enum {
86 RowsAtCompileTime = LhsTraits::RowsAtCompileTime,
87 ColsAtCompileTime = RhsTraits::ColsAtCompileTime,
88 MaxRowsAtCompileTime = LhsTraits::MaxRowsAtCompileTime,
89 MaxColsAtCompileTime = RhsTraits::MaxColsAtCompileTime,
90
91 // FIXME: only needed by GeneralMatrixMatrixTriangular
92 InnerSize = EIGEN_SIZE_MIN_PREFER_FIXED(LhsTraits::ColsAtCompileTime, RhsTraits::RowsAtCompileTime),
93
94 // The storage order is somewhat arbitrary here. The correct one will be determined through the evaluator.
95 Flags = (MaxRowsAtCompileTime==1 && MaxColsAtCompileTime!=1) ? RowMajorBit
96 : (MaxColsAtCompileTime==1 && MaxRowsAtCompileTime!=1) ? 0
97 : ( ((LhsTraits::Flags&NoPreferredStorageOrderBit) && (RhsTraits::Flags&RowMajorBit))
98 || ((RhsTraits::Flags&NoPreferredStorageOrderBit) && (LhsTraits::Flags&RowMajorBit)) ) ? RowMajorBit
100 };
101};
102
103} // end namespace internal
104
105
106template<typename _Lhs, typename _Rhs, int Option>
107class Product : public ProductImpl<_Lhs,_Rhs,Option,
108 typename internal::product_promote_storage_type<typename internal::traits<_Lhs>::StorageKind,
109 typename internal::traits<_Rhs>::StorageKind,
110 internal::product_type<_Lhs,_Rhs>::ret>::ret>
111{
112 public:
113
114 typedef _Lhs Lhs;
115 typedef _Rhs Rhs;
116
117 typedef typename ProductImpl<
118 Lhs, Rhs, Option,
121 internal::product_type<Lhs,Rhs>::ret>::ret>::Base Base;
122 EIGEN_GENERIC_PUBLIC_INTERFACE(Product)
123
128
129 EIGEN_DEVICE_FUNC Product(const Lhs& lhs, const Rhs& rhs) : m_lhs(lhs), m_rhs(rhs)
130 {
131 eigen_assert(lhs.cols() == rhs.rows()
132 && "invalid matrix product"
133 && "if you wanted a coeff-wise or a dot product use the respective explicit functions");
134 }
135
136 EIGEN_DEVICE_FUNC inline Index rows() const { return m_lhs.rows(); }
137 EIGEN_DEVICE_FUNC inline Index cols() const { return m_rhs.cols(); }
138
139 EIGEN_DEVICE_FUNC const LhsNestedCleaned& lhs() const { return m_lhs; }
140 EIGEN_DEVICE_FUNC const RhsNestedCleaned& rhs() const { return m_rhs; }
141
142 protected:
143
144 LhsNested m_lhs;
145 RhsNested m_rhs;
146};
147
148namespace internal {
149
152 : public internal::dense_xpr_base<Product<Lhs,Rhs,Option> >::type
153{};
154
156template<typename Lhs, typename Rhs, int Option>
157class dense_product_base<Lhs, Rhs, Option, InnerProduct>
158 : public internal::dense_xpr_base<Product<Lhs,Rhs,Option> >::type
159{
161 typedef typename internal::dense_xpr_base<ProductXpr>::type Base;
162public:
163 using Base::derived;
164 typedef typename Base::Scalar Scalar;
165
166 operator const Scalar() const
167 {
168 return internal::evaluator<ProductXpr>(derived()).coeff(0,0);
169 }
170};
171
172} // namespace internal
173
174// Generic API dispatcher
175template<typename Lhs, typename Rhs, int Option, typename StorageKind>
176class ProductImpl : public internal::generic_xpr_base<Product<Lhs,Rhs,Option>, MatrixXpr, StorageKind>::type
177{
178 public:
179 typedef typename internal::generic_xpr_base<Product<Lhs,Rhs,Option>, MatrixXpr, StorageKind>::type Base;
180};
181
182template<typename Lhs, typename Rhs, int Option>
183class ProductImpl<Lhs,Rhs,Option,Dense>
184 : public internal::dense_product_base<Lhs,Rhs,Option>
185{
187
188 public:
189
191 EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
192 protected:
193 enum {
194 IsOneByOne = (RowsAtCompileTime == 1 || RowsAtCompileTime == Dynamic) &&
195 (ColsAtCompileTime == 1 || ColsAtCompileTime == Dynamic),
196 EnableCoeff = IsOneByOne || Option==LazyProduct
197 };
198
199 public:
200
201 EIGEN_DEVICE_FUNC Scalar coeff(Index row, Index col) const
202 {
203 EIGEN_STATIC_ASSERT(EnableCoeff, THIS_METHOD_IS_ONLY_FOR_INNER_OR_LAZY_PRODUCTS);
204 eigen_assert( (Option==LazyProduct) || (this->rows() == 1 && this->cols() == 1) );
205
206 return internal::evaluator<Derived>(derived()).coeff(row,col);
207 }
208
209 EIGEN_DEVICE_FUNC Scalar coeff(Index i) const
210 {
211 EIGEN_STATIC_ASSERT(EnableCoeff, THIS_METHOD_IS_ONLY_FOR_INNER_OR_LAZY_PRODUCTS);
212 eigen_assert( (Option==LazyProduct) || (this->rows() == 1 && this->cols() == 1) );
213
214 return internal::evaluator<Derived>(derived()).coeff(i);
215 }
216
217
218};
219
220} // end namespace Eigen
221
222#endif // EIGEN_PRODUCT_H
Definition Product.h:177
Expression of the product of two arbitrary matrices or vectors.
Definition Product.h:111
Pseudo expression representing a solving operation.
Definition Solve.h:63
Definition Product.h:153
const unsigned int NoPreferredStorageOrderBit
for an expression, this means that the storage order can be either row-major or column-major.
Definition Constants.h:172
const unsigned int RowMajorBit
for a matrix, this means that the storage order is row-major.
Definition Constants.h:61
The type used to identify a dense storage.
Definition Constants.h:490
The type used to identify a matrix expression.
Definition Constants.h:505
Definition Constants.h:518
Definition Constants.h:519
Definition XprHelper.h:428
Definition CoreEvaluators.h:82
Definition XprHelper.h:445
Definition GeneralProduct.h:36
Definition XprHelper.h:58
Definition ForwardDeclarations.h:17
Definition Meta.h:30