Matrix.h
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2006-2010 Benoit Jacob <jacob.benoit.1@gmail.com>
00005 // Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
00006 //
00007 // Eigen is free software; you can redistribute it and/or
00008 // modify it under the terms of the GNU Lesser General Public
00009 // License as published by the Free Software Foundation; either
00010 // version 3 of the License, or (at your option) any later version.
00011 //
00012 // Alternatively, you can redistribute it and/or
00013 // modify it under the terms of the GNU General Public License as
00014 // published by the Free Software Foundation; either version 2 of
00015 // the License, or (at your option) any later version.
00016 //
00017 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00018 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00019 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00020 // GNU General Public License for more details.
00021 //
00022 // You should have received a copy of the GNU Lesser General Public
00023 // License and a copy of the GNU General Public License along with
00024 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00025 
00026 #ifndef EIGEN_MATRIX_H
00027 #define EIGEN_MATRIX_H
00028 
00029 namespace Eigen {
00030 
00119 namespace internal {
00120 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
00121 struct traits<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
00122 {
00123   typedef _Scalar Scalar;
00124   typedef Dense StorageKind;
00125   typedef DenseIndex Index;
00126   typedef MatrixXpr XprKind;
00127   enum {
00128     RowsAtCompileTime = _Rows,
00129     ColsAtCompileTime = _Cols,
00130     MaxRowsAtCompileTime = _MaxRows,
00131     MaxColsAtCompileTime = _MaxCols,
00132     Flags = compute_matrix_flags<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols>::ret,
00133     CoeffReadCost = NumTraits<Scalar>::ReadCost,
00134     Options = _Options,
00135     InnerStrideAtCompileTime = 1,
00136     OuterStrideAtCompileTime = (Options&RowMajor) ? ColsAtCompileTime : RowsAtCompileTime
00137   };
00138 };
00139 }
00140 
00141 template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
00142 class Matrix
00143   : public PlainObjectBase<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
00144 {
00145   public:
00146 
00150     typedef PlainObjectBase<Matrix> Base;
00151 
00152     enum { Options = _Options };
00153 
00154     EIGEN_DENSE_PUBLIC_INTERFACE(Matrix)
00155 
00156     typedef typename Base::PlainObject PlainObject;
00157 
00158     using Base::base;
00159     using Base::coeffRef;
00160 
00169     EIGEN_STRONG_INLINE Matrix& operator=(const Matrix& other)
00170     {
00171       return Base::_set(other);
00172     }
00173 
00184     template<typename OtherDerived>
00185     EIGEN_STRONG_INLINE Matrix& operator=(const MatrixBase<OtherDerived>& other)
00186     {
00187       return Base::_set(other);
00188     }
00189 
00190     /* Here, doxygen failed to copy the brief information when using \copydoc */
00191 
00196     template<typename OtherDerived>
00197     EIGEN_STRONG_INLINE Matrix& operator=(const EigenBase<OtherDerived> &other)
00198     {
00199       return Base::operator=(other);
00200     }
00201 
00202     template<typename OtherDerived>
00203     EIGEN_STRONG_INLINE Matrix& operator=(const ReturnByValue<OtherDerived>& func)
00204     {
00205       return Base::operator=(func);
00206     }
00207 
00218     EIGEN_STRONG_INLINE explicit Matrix() : Base()
00219     {
00220       Base::_check_template_params();
00221       EIGEN_INITIALIZE_BY_ZERO_IF_THAT_OPTION_IS_ENABLED
00222     }
00223 
00224     // FIXME is it still needed
00225     Matrix(internal::constructor_without_unaligned_array_assert)
00226       : Base(internal::constructor_without_unaligned_array_assert())
00227     { Base::_check_template_params(); EIGEN_INITIALIZE_BY_ZERO_IF_THAT_OPTION_IS_ENABLED }
00228 
00235     EIGEN_STRONG_INLINE explicit Matrix(Index dim)
00236       : Base(dim, RowsAtCompileTime == 1 ? 1 : dim, ColsAtCompileTime == 1 ? 1 : dim)
00237     {
00238       Base::_check_template_params();
00239       EIGEN_STATIC_ASSERT_VECTOR_ONLY(Matrix)
00240       eigen_assert(dim >= 0);
00241       eigen_assert(SizeAtCompileTime == Dynamic || SizeAtCompileTime == dim);
00242       EIGEN_INITIALIZE_BY_ZERO_IF_THAT_OPTION_IS_ENABLED
00243     }
00244 
00245     #ifndef EIGEN_PARSED_BY_DOXYGEN
00246     template<typename T0, typename T1>
00247     EIGEN_STRONG_INLINE Matrix(const T0& x, const T1& y)
00248     {
00249       Base::_check_template_params();
00250       Base::template _init2<T0,T1>(x, y);
00251     }
00252     #else
00253 
00258     Matrix(Index rows, Index cols);
00260     Matrix(const Scalar& x, const Scalar& y);
00261     #endif
00262 
00264     EIGEN_STRONG_INLINE Matrix(const Scalar& x, const Scalar& y, const Scalar& z)
00265     {
00266       Base::_check_template_params();
00267       EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Matrix, 3)
00268       m_storage.data()[0] = x;
00269       m_storage.data()[1] = y;
00270       m_storage.data()[2] = z;
00271     }
00273     EIGEN_STRONG_INLINE Matrix(const Scalar& x, const Scalar& y, const Scalar& z, const Scalar& w)
00274     {
00275       Base::_check_template_params();
00276       EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(Matrix, 4)
00277       m_storage.data()[0] = x;
00278       m_storage.data()[1] = y;
00279       m_storage.data()[2] = z;
00280       m_storage.data()[3] = w;
00281     }
00282 
00283     explicit Matrix(const Scalar *data);
00284 
00286     template<typename OtherDerived>
00287     EIGEN_STRONG_INLINE Matrix(const MatrixBase<OtherDerived>& other)
00288              : Base(other.rows() * other.cols(), other.rows(), other.cols())
00289     {
00290       // This test resides here, to bring the error messages closer to the user. Normally, these checks
00291       // are performed deeply within the library, thus causing long and scary error traces.
00292       EIGEN_STATIC_ASSERT((internal::is_same<Scalar, typename OtherDerived::Scalar>::value),
00293         YOU_MIXED_DIFFERENT_NUMERIC_TYPES__YOU_NEED_TO_USE_THE_CAST_METHOD_OF_MATRIXBASE_TO_CAST_NUMERIC_TYPES_EXPLICITLY)
00294 
00295       Base::_check_template_params();
00296       Base::_set_noalias(other);
00297     }
00299     EIGEN_STRONG_INLINE Matrix(const Matrix& other)
00300             : Base(other.rows() * other.cols(), other.rows(), other.cols())
00301     {
00302       Base::_check_template_params();
00303       Base::_set_noalias(other);
00304     }
00306     template<typename OtherDerived>
00307     EIGEN_STRONG_INLINE Matrix(const ReturnByValue<OtherDerived>& other)
00308     {
00309       Base::_check_template_params();
00310       Base::resize(other.rows(), other.cols());
00311       other.evalTo(*this);
00312     }
00313 
00317     template<typename OtherDerived>
00318     EIGEN_STRONG_INLINE Matrix(const EigenBase<OtherDerived> &other)
00319       : Base(other.derived().rows() * other.derived().cols(), other.derived().rows(), other.derived().cols())
00320     {
00321       Base::_check_template_params();
00322       Base::resize(other.rows(), other.cols());
00323       // FIXME/CHECK: isn't *this = other.derived() more efficient. it allows to
00324       //              go for pure _set() implementations, right?
00325       *this = other;
00326     }
00327 
00332     template<typename OtherDerived>
00333     void swap(MatrixBase<OtherDerived> const & other)
00334     { this->_swap(other.derived()); }
00335 
00336     inline Index innerStride() const { return 1; }
00337     inline Index outerStride() const { return this->innerSize(); }
00338 
00340 
00341     template<typename OtherDerived>
00342     explicit Matrix(const RotationBase<OtherDerived,ColsAtCompileTime>& r);
00343     template<typename OtherDerived>
00344     Matrix& operator=(const RotationBase<OtherDerived,ColsAtCompileTime>& r);
00345 
00346     #ifdef EIGEN2_SUPPORT
00347     template<typename OtherDerived>
00348     explicit Matrix(const eigen2_RotationBase<OtherDerived,ColsAtCompileTime>& r);
00349     template<typename OtherDerived>
00350     Matrix& operator=(const eigen2_RotationBase<OtherDerived,ColsAtCompileTime>& r);
00351     #endif
00352 
00353     // allow to extend Matrix outside Eigen
00354     #ifdef EIGEN_MATRIX_PLUGIN
00355     #include EIGEN_MATRIX_PLUGIN
00356     #endif
00357 
00358   protected:
00359     template <typename Derived, typename OtherDerived, bool IsVector>
00360     friend struct internal::conservative_resize_like_impl;
00361 
00362     using Base::m_storage;
00363 };
00364 
00385 #define EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Size, SizeSuffix)   \
00386                                     \
00387 typedef Matrix<Type, Size, Size> Matrix##SizeSuffix##TypeSuffix;  \
00388                                     \
00389 typedef Matrix<Type, Size, 1>    Vector##SizeSuffix##TypeSuffix;  \
00390                                     \
00391 typedef Matrix<Type, 1, Size>    RowVector##SizeSuffix##TypeSuffix;
00392 
00393 #define EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, Size)         \
00394                                     \
00395 typedef Matrix<Type, Size, Dynamic> Matrix##Size##X##TypeSuffix;  \
00396                                     \
00397 typedef Matrix<Type, Dynamic, Size> Matrix##X##Size##TypeSuffix;
00398 
00399 #define EIGEN_MAKE_TYPEDEFS_ALL_SIZES(Type, TypeSuffix) \
00400 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 2, 2) \
00401 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 3, 3) \
00402 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, 4, 4) \
00403 EIGEN_MAKE_TYPEDEFS(Type, TypeSuffix, Dynamic, X) \
00404 EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, 2) \
00405 EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, 3) \
00406 EIGEN_MAKE_FIXED_TYPEDEFS(Type, TypeSuffix, 4)
00407 
00408 EIGEN_MAKE_TYPEDEFS_ALL_SIZES(int,                  i)
00409 EIGEN_MAKE_TYPEDEFS_ALL_SIZES(float,                f)
00410 EIGEN_MAKE_TYPEDEFS_ALL_SIZES(double,               d)
00411 EIGEN_MAKE_TYPEDEFS_ALL_SIZES(std::complex<float>,  cf)
00412 EIGEN_MAKE_TYPEDEFS_ALL_SIZES(std::complex<double>, cd)
00413 
00414 #undef EIGEN_MAKE_TYPEDEFS_ALL_SIZES
00415 #undef EIGEN_MAKE_TYPEDEFS
00416 #undef EIGEN_MAKE_FIXED_TYPEDEFS
00417 
00418 } // end namespace Eigen
00419 
00420 #endif // EIGEN_MATRIX_H