source: pacpussensors/trunk/Vislab/lib3dv/eigen/Eigen/src/Core/Matrix.h@ 136

Last change on this file since 136 was 136, checked in by ldecherf, 7 years ago

Doc

File size: 17.0 KB
Line 
1// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2006-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
5// Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
6//
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/.
10
11#ifndef EIGEN_MATRIX_H
12#define EIGEN_MATRIX_H
13
14namespace Eigen {
15
16/** \class Matrix
17 * \ingroup Core_Module
18 *
19 * \brief The matrix class, also used for vectors and row-vectors
20 *
21 * The %Matrix class is the work-horse for all \em dense (\ref dense "note") matrices and vectors within Eigen.
22 * Vectors are matrices with one column, and row-vectors are matrices with one row.
23 *
24 * The %Matrix class encompasses \em both fixed-size and dynamic-size objects (\ref fixedsize "note").
25 *
26 * The first three template parameters are required:
27 * \tparam _Scalar \anchor matrix_tparam_scalar Numeric type, e.g. float, double, int or std::complex<float>.
28 * User defined sclar types are supported as well (see \ref user_defined_scalars "here").
29 * \tparam _Rows Number of rows, or \b Dynamic
30 * \tparam _Cols Number of columns, or \b Dynamic
31 *
32 * The remaining template parameters are optional -- in most cases you don't have to worry about them.
33 * \tparam _Options \anchor matrix_tparam_options A combination of either \b #RowMajor or \b #ColMajor, and of either
34 * \b #AutoAlign or \b #DontAlign.
35 * The former controls \ref TopicStorageOrders "storage order", and defaults to column-major. The latter controls alignment, which is required
36 * for vectorization. It defaults to aligning matrices except for fixed sizes that aren't a multiple of the packet size.
37 * \tparam _MaxRows Maximum number of rows. Defaults to \a _Rows (\ref maxrows "note").
38 * \tparam _MaxCols Maximum number of columns. Defaults to \a _Cols (\ref maxrows "note").
39 *
40 * Eigen provides a number of typedefs covering the usual cases. Here are some examples:
41 *
42 * \li \c Matrix2d is a 2x2 square matrix of doubles (\c Matrix<double, 2, 2>)
43 * \li \c Vector4f is a vector of 4 floats (\c Matrix<float, 4, 1>)
44 * \li \c RowVector3i is a row-vector of 3 ints (\c Matrix<int, 1, 3>)
45 *
46 * \li \c MatrixXf is a dynamic-size matrix of floats (\c Matrix<float, Dynamic, Dynamic>)
47 * \li \c VectorXf is a dynamic-size vector of floats (\c Matrix<float, Dynamic, 1>)
48 *
49 * \li \c Matrix2Xf is a partially fixed-size (dynamic-size) matrix of floats (\c Matrix<float, 2, Dynamic>)
50 * \li \c MatrixX3d is a partially dynamic-size (fixed-size) matrix of double (\c Matrix<double, Dynamic, 3>)
51 *
52 * See \link matrixtypedefs this page \endlink for a complete list of predefined \em %Matrix and \em Vector typedefs.
53 *
54 * You can access elements of vectors and matrices using normal subscripting:
55 *
56 * \code
57 * Eigen::VectorXd v(10);
58 * v[0] = 0.1;
59 * v[1] = 0.2;
60 * v(0) = 0.3;
61 * v(1) = 0.4;
62 *
63 * Eigen::MatrixXi m(10, 10);
64 * m(0, 1) = 1;
65 * m(0, 2) = 2;
66 * m(0, 3) = 3;
67 * \endcode
68 *
69 * This class can be extended with the help of the plugin mechanism described on the page
70 * \ref TopicCustomizingEigen by defining the preprocessor symbol \c EIGEN_MATRIX_PLUGIN.
71 *
72 * <i><b>Some notes:</b></i>
73 *
74 * <dl>
75 * <dt><b>\anchor dense Dense versus sparse:</b></dt>
76 * <dd>This %Matrix class handles dense, not sparse matrices and vectors. For sparse matrices and vectors, see the Sparse module.
77 *
78 * Dense matrices and vectors are plain usual arrays of coefficients. All the coefficients are stored, in an ordinary contiguous array.
79 * This is unlike Sparse matrices and vectors where the coefficients are stored as a list of nonzero coefficients.</dd>
80 *
81 * <dt><b>\anchor fixedsize Fixed-size versus dynamic-size:</b></dt>
82 * <dd>Fixed-size means that the numbers of rows and columns are known are compile-time. In this case, Eigen allocates the array
83 * of coefficients as a fixed-size array, as a class member. This makes sense for very small matrices, typically up to 4x4, sometimes up
84 * to 16x16. Larger matrices should be declared as dynamic-size even if one happens to know their size at compile-time.
85 *
86 * Dynamic-size means that the numbers of rows or columns are not necessarily known at compile-time. In this case they are runtime
87 * variables, and the array of coefficients is allocated dynamically on the heap.
88 *
89 * Note that \em dense matrices, be they Fixed-size or Dynamic-size, <em>do not</em> expand dynamically in the sense of a std::map.
90 * If you want this behavior, see the Sparse module.</dd>
91 *
92 * <dt><b>\anchor maxrows _MaxRows and _MaxCols:</b></dt>
93 * <dd>In most cases, one just leaves these parameters to the default values.
94 * These parameters mean the maximum size of rows and columns that the matrix may have. They are useful in cases
95 * when the exact numbers of rows and columns are not known are compile-time, but it is known at compile-time that they cannot
96 * exceed a certain value. This happens when taking dynamic-size blocks inside fixed-size matrices: in this case _MaxRows and _MaxCols
97 * are the dimensions of the original matrix, while _Rows and _Cols are Dynamic.</dd>
98 * </dl>
99 *
100 * \see MatrixBase for the majority of the API methods for matrices, \ref TopicClassHierarchy,
101 * \ref TopicStorageOrders
102 */
103
104namespace internal {
105template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
106struct traits<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
107{
108 typedef _Scalar Scalar;
109 typedef Dense StorageKind;
110 typedef DenseIndex Index;
111 typedef MatrixXpr XprKind;
112 enum {
113 RowsAtCompileTime = _Rows,
114 ColsAtCompileTime = _Cols,
115 MaxRowsAtCompileTime = _MaxRows,
116 MaxColsAtCompileTime = _MaxCols,
117 Flags = compute_matrix_flags<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::ret,
118 CoeffReadCost = NumTraits<Scalar>::ReadCost,
119 Options = _Options,
120 InnerStrideAtCompileTime = 1,
121 OuterStrideAtCompileTime = (Options&RowMajor) ? ColsAtCompileTime : RowsAtCompileTime
122 };
123};
124}
125
126template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
127class Matrix
128 : public PlainObjectBase<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
129{
130 public:
131
132 /** \brief Base class typedef.
133 * \sa PlainObjectBase
134 */
135 typedef PlainObjectBase<Matrix> Base;
136
137 enum { Options = _Options };
138
139 EIGEN_DENSE_PUBLIC_INTERFACE(Matrix)
140
141 typedef typename Base::PlainObject PlainObject;
142
143 using Base::base;
144 using Base::coeffRef;
145
146 /**
147 * \brief Assigns matrices to each other.
148 *
149 * \note This is a special case of the templated operator=. Its purpose is
150 * to prevent a default operator= from hiding the templated operator=.
151 *
152 * \callgraph
153 */
154 EIGEN_STRONG_INLINE Matrix& operator=(const Matrix& other)
155 {
156 return Base::_set(other);
157 }
158
159 /** \internal
160 * \brief Copies the value of the expression \a other into \c *this with automatic resizing.
161 *
162 * *this might be resized to match the dimensions of \a other. If *this was a null matrix (not already initialized),
163 * it will be initialized.
164 *
165 * Note that copying a row-vector into a vector (and conversely) is allowed.
166 * The resizing, if any, is then done in the appropriate way so that row-vectors
167 * remain row-vectors and vectors remain vectors.
168 */
169 template<typename OtherDerived>
170 EIGEN_STRONG_INLINE Matrix& operator=(const MatrixBase<OtherDerived>& other)
171 {
172 return Base::_set(other);
173 }
174
175 /* Here, doxygen failed to copy the brief information when using \copydoc */
176
177 /**
178 * \brief Copies the generic expression \a other into *this.
179 * \copydetails DenseBase::operator=(const EigenBase<OtherDerived> &other)
180 */
181 template<typename OtherDerived>
182 EIGEN_STRONG_INLINE Matrix& operator=(const EigenBase<OtherDerived> &other)
183 {
184 return Base::operator=(other);
185 }
186
187 template<typename OtherDerived>
188 EIGEN_STRONG_INLINE Matrix& operator=(const ReturnByValue<OtherDerived>& func)
189 {
190 return Base::operator=(func);
191 }
192
193 /** \brief Default constructor.
194 *
195 * For fixed-size matrices, does nothing.
196 *
197 * For dynamic-size matrices, creates an empty matrix of size 0. Does not allocate any array. Such a matrix
198 * is called a null matrix. This constructor is the unique way to create null matrices: resizing
199 * a matrix to 0 is not supported.
200 *
201 * \sa resize(Index,Index)
202 */
203 EIGEN_STRONG_INLINE Matrix() : Base()
204 {
205 Base::_check_template_params();
206 EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
207 }
208
209 // FIXME is it still needed
210 Matrix(internal::constructor_without_unaligned_array_assert)
211 : Base(internal::constructor_without_unaligned_array_assert())
212 { Base::_check_template_params(); EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED }
213
214#ifdef EIGEN_HAVE_RVALUE_REFERENCES
215 Matrix(Matrix&& other)
216 : Base(std::move(other))
217 {
218 Base::_check_template_params();
219 if (RowsAtCompileTime!=Dynamic && ColsAtCompileTime!=Dynamic)
220 Base::_set_noalias(other);
221 }
222 Matrix& operator=(Matrix&& other)
223 {
224 other.swap(*this);
225 return *this;
226 }
227#endif
228
229 /** \brief Constructs a vector or row-vector with given dimension. \only_for_vectors
230 *
231 * Note that this is only useful for dynamic-size vectors. For fixed-size vectors,
232 * it is redundant to pass the dimension here, so it makes more sense to use the default
233 * constructor Matrix() instead.
234 */
235 EIGEN_STRONG_INLINE explicit Matrix(Index dim)
236 : Base(dim, RowsAtCompileTime == 1 ? 1 : dim, ColsAtCompileTime == 1 ? 1 : dim)
237 {
238 Base::_check_template_params();
239 EIGEN_STATIC_ASSERT_VECTOR_ONLY(Matrix)
240 eigen_assert(dim >= 0);
241 eigen_assert(SizeAtCompileTime == Dynamic || SizeAtCompileTime == dim);
242 EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
243 }
244
245 #ifndef EIGEN_PARSED_BY_DOXYGEN
246 template<typename T0, typename T1>
247 EIGEN_STRONG_INLINE Matrix(const T0& x, const T1& y)
248 {
249 Base::_check_template_params();
250 Base::template _init2<T0,T1>(x, y);
251 }
252 #else
253 /** \brief Constructs an uninitialized matrix with \a rows rows and \a cols columns.
254 *
255 * This is useful for dynamic-size matrices. For fixed-size matrices,
256 * it is redundant to pass these parameters, so one should use the default constructor
257 * Matrix() instead. */
258 Matrix(Index rows, Index cols);
259 /** \brief Constructs an initialized 2D vector with given coefficients */
260 Matrix(const Scalar& x, const Scalar& y);
261 #endif
262
263 /** \brief Constructs an initialized 3D vector with given coefficients */
264 EIGEN_STRONG_INLINE Matrix(const Scalar& x, const Scalar& y, const Scalar& z)
265 {
266 Base::_check_template_params();
267 EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Matrix, 3)
268 m_storage.data()[0] = x;
269 m_storage.data()[1] = y;
270 m_storage.data()[2] = z;
271 }
272 /** \brief Constructs an initialized 4D vector with given coefficients */
273 EIGEN_STRONG_INLINE Matrix(const Scalar& x, const Scalar& y, const Scalar& z, const Scalar& w)
274 {
275 Base::_check_template_params();
276 EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Matrix, 4)
277 m_storage.data()[0] = x;
278 m_storage.data()[1] = y;
279 m_storage.data()[2] = z;
280 m_storage.data()[3] = w;
281 }
282
283 explicit Matrix(const Scalar *data);
284
285 /** \brief Constructor copying the value of the expression \a other */
286 template<typename OtherDerived>
287 EIGEN_STRONG_INLINE Matrix(const MatrixBase<OtherDerived>& other)
288 : Base(other.rows() * other.cols(), other.rows(), other.cols())
289 {
290 // This test resides here, to bring the error messages closer to the user. Normally, these checks
291 // are performed deeply within the library, thus causing long and scary error traces.
292 EIGEN_STATIC_ASSERT((internal::is_same<Scalar, typename OtherDerived::Scalar>::value),
293 YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
294
295 Base::_check_template_params();
296 Base::_set_noalias(other);
297 }
298 /** \brief Copy constructor */
299 EIGEN_STRONG_INLINE Matrix(const Matrix& other)
300 : Base(other.rows() * other.cols(), other.rows(), other.cols())
301 {
302 Base::_check_template_params();
303 Base::_set_noalias(other);
304 }
305 /** \brief Copy constructor with in-place evaluation */
306 template<typename OtherDerived>
307 EIGEN_STRONG_INLINE Matrix(const ReturnByValue<OtherDerived>& other)
308 {
309 Base::_check_template_params();
310 Base::resize(other.rows(), other.cols());
311 other.evalTo(*this);
312 }
313
314 /** \brief Copy constructor for generic expressions.
315 * \sa MatrixBase::operator=(const EigenBase<OtherDerived>&)
316 */
317 template<typename OtherDerived>
318 EIGEN_STRONG_INLINE Matrix(const EigenBase<OtherDerived> &other)
319 : Base(other.derived().rows() * other.derived().cols(), other.derived().rows(), other.derived().cols())
320 {
321 Base::_check_template_params();
322 Base::_resize_to_match(other);
323 // FIXME/CHECK: isn't *this = other.derived() more efficient. it allows to
324 // go for pure _set() implementations, right?
325 *this = other;
326 }
327
328 /** \internal
329 * \brief Override MatrixBase::swap() since for dynamic-sized matrices
330 * of same type it is enough to swap the data pointers.
331 */
332 template<typename OtherDerived>
333 void swap(MatrixBase<OtherDerived> const & other)
334 { this->_swap(other.derived()); }
335
336 inline Index innerStride() const { return 1; }
337 inline Index outerStride() const { return this->innerSize(); }
338
339 /////////// Geometry module ///////////
340
341 template<typename OtherDerived>
342 explicit Matrix(const RotationBase<OtherDerived,ColsAtCompileTime>& r);
343 template<typename OtherDerived>
344 Matrix& operator=(const RotationBase<OtherDerived,ColsAtCompileTime>& r);
345
346 #ifdef EIGEN2_SUPPORT
347 template<typename OtherDerived>
348 explicit Matrix(const eigen2_RotationBase<OtherDerived,ColsAtCompileTime>& r);
349 template<typename OtherDerived>
350 Matrix& operator=(const eigen2_RotationBase<OtherDerived,ColsAtCompileTime>& r);
351 #endif
352
353 // allow to extend Matrix outside Eigen
354 #ifdef EIGEN_MATRIX_PLUGIN
355 #include EIGEN_MATRIX_PLUGIN
356 #endif
357
358 protected:
359 template <typename Derived, typename OtherDerived, bool IsVector>
360 friend struct internal::conservative_resize_like_impl;
361
362 using Base::m_storage;
363};
364
365/** \defgroup matrixtypedefs Global matrix typedefs
366 *
367 * \ingroup Core_Module
368 *
369 * Eigen defines several typedef shortcuts for most common matrix and vector types.
370 *
371 * The general patterns are the following:
372 *
373 * \c MatrixSizeType where \c Size can be \c 2,\c 3,\c 4 for fixed size square matrices or \c X for dynamic size,
374 * and where \c Type can be \c i for integer, \c f for float, \c d for double, \c cf for complex float, \c cd
375 * for complex double.
376 *
377 * For example, \c Matrix3d is a fixed-size 3x3 matrix type of doubles, and \c MatrixXf is a dynamic-size matrix of floats.
378 *
379 * There are also \c VectorSizeType and \c RowVectorSizeType which are self-explanatory. For example, \c Vector4cf is
380 * a fixed-size vector of 4 complex floats.
381 *
382 * \sa class Matrix
383 */
384
385#define EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Size, SizeSuffix) \
386/** \ingroup matrixtypedefs */ \
387typedef Matrix<Type, Size, Size> Matrix##SizeSuffix##TypeSuffix; \
388/** \ingroup matrixtypedefs */ \
389typedef Matrix<Type, Size, 1> Vector##SizeSuffix##TypeSuffix; \
390/** \ingroup matrixtypedefs */ \
391typedef Matrix<Type, 1, Size> RowVector##SizeSuffix##TypeSuffix;
392
393#define EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, Size) \
394/** \ingroup matrixtypedefs */ \
395typedef Matrix<Type, Size, Dynamic> Matrix##Size##X##TypeSuffix; \
396/** \ingroup matrixtypedefs */ \
397typedef Matrix<Type, Dynamic, Size> Matrix##X##Size##TypeSuffix;
398
399#define EIGEN_MAKE_TYPEDEFS_ALL_SIZES(Type, TypeSuffix) \
400EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 2, 2) \
401EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 3, 3) \
402EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 4, 4) \
403EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Dynamic, X) \
404EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, 2) \
405EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, 3) \
406EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, 4)
407
408EIGEN_MAKE_TYPEDEFS_ALL_SIZES(int, i)
409EIGEN_MAKE_TYPEDEFS_ALL_SIZES(float, f)
410EIGEN_MAKE_TYPEDEFS_ALL_SIZES(double, d)
411EIGEN_MAKE_TYPEDEFS_ALL_SIZES(std::complex<float>, cf)
412EIGEN_MAKE_TYPEDEFS_ALL_SIZES(std::complex<double>, cd)
413
414#undef EIGEN_MAKE_TYPEDEFS_ALL_SIZES
415#undef EIGEN_MAKE_TYPEDEFS
416#undef EIGEN_MAKE_FIXED_TYPEDEFS
417
418} // end namespace Eigen
419
420#endif // EIGEN_MATRIX_H
Note: See TracBrowser for help on using the repository browser.