1c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// This file is part of Eigen, a lightweight C++ template library
2c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// for linear algebra.
3c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//
4c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
5c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
6c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//
7c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// This Source Code Form is subject to the terms of the Mozilla
8c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// Public License v. 2.0. If a copy of the MPL was not distributed
9c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
11c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#ifndef EIGEN_DENSESTORAGEBASE_H
12c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#define EIGEN_DENSESTORAGEBASE_H
13c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
147faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez#if defined(EIGEN_INITIALIZE_MATRICES_BY_ZERO)
157faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez# define EIGEN_INITIALIZE_COEFFS
167faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez# define EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED for(int i=0;i<base().size();++i) coeffRef(i)=Scalar(0);
177faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez#elif defined(EIGEN_INITIALIZE_MATRICES_BY_NAN)
187faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez# define EIGEN_INITIALIZE_COEFFS
197faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez# define EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED for(int i=0;i<base().size();++i) coeffRef(i)=std::numeric_limits<Scalar>::quiet_NaN();
20c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#else
217faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez# undef EIGEN_INITIALIZE_COEFFS
227faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez# define EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
23c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#endif
24c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
25c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathnamespace Eigen {
26c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
27c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathnamespace internal {
28c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
297faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandeztemplate<int MaxSizeAtCompileTime> struct check_rows_cols_for_overflow {
307faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  template<typename Index>
317faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  static EIGEN_ALWAYS_INLINE void run(Index, Index)
327faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  {
337faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  }
347faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez};
357faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
367faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandeztemplate<> struct check_rows_cols_for_overflow<Dynamic> {
377faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  template<typename Index>
387faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  static EIGEN_ALWAYS_INLINE void run(Index rows, Index cols)
397faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  {
407faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    // http://hg.mozilla.org/mozilla-central/file/6c8a909977d3/xpcom/ds/CheckedInt.h#l242
417faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    // we assume Index is signed
427faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    Index max_index = (size_t(1) << (8 * sizeof(Index) - 1)) - 1; // assume Index is signed
437faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    bool error = (rows == 0 || cols == 0) ? false
447faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez               : (rows > max_index / cols);
457faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    if (error)
467faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      throw_std_bad_alloc();
477faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  }
487faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez};
497faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
507faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandeztemplate <typename Derived,
517faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez          typename OtherDerived = Derived,
527faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez          bool IsVector = bool(Derived::IsVectorAtCompileTime) && bool(OtherDerived::IsVectorAtCompileTime)>
537faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandezstruct conservative_resize_like_impl;
54c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
55c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename MatrixTypeA, typename MatrixTypeB, bool SwapPointers> struct matrix_swap_impl;
56c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
57c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath} // end namespace internal
58c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
59c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/** \class PlainObjectBase
60c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * \brief %Dense storage base class for matrices and arrays.
61c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *
62c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * This class can be extended with the help of the plugin mechanism described on the page
63c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * \ref TopicCustomizingEigen by defining the preprocessor symbol \c EIGEN_PLAINOBJECTBASE_PLUGIN.
64c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  *
65c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  * \sa \ref TopicClassHierarchy
66c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  */
67c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#ifdef EIGEN_PARSED_BY_DOXYGEN
68c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathnamespace internal {
69c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
70c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// this is a warkaround to doxygen not being able to understand the inheritence logic
71c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath// when it is hidden by the dense_xpr_base helper struct.
72c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Derived> struct dense_xpr_base_dispatcher_for_doxygen;// : public MatrixBase<Derived> {};
73c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/** This class is just a workaround for Doxygen and it does not not actually exist. */
74c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
75c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct dense_xpr_base_dispatcher_for_doxygen<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
76c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    : public MatrixBase<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> > {};
77c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath/** This class is just a workaround for Doxygen and it does not not actually exist. */
78c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
79c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct dense_xpr_base_dispatcher_for_doxygen<Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
80c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    : public ArrayBase<Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> > {};
81c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
82c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath} // namespace internal
83c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
84c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Derived>
85c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathclass PlainObjectBase : public internal::dense_xpr_base_dispatcher_for_doxygen<Derived>
86c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#else
87c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename Derived>
88c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathclass PlainObjectBase : public internal::dense_xpr_base<Derived>::type
89c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#endif
90c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
91c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  public:
92c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    enum { Options = internal::traits<Derived>::Options };
93c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    typedef typename internal::dense_xpr_base<Derived>::type Base;
94c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
95c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    typedef typename internal::traits<Derived>::StorageKind StorageKind;
96c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    typedef typename internal::traits<Derived>::Index Index;
97c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    typedef typename internal::traits<Derived>::Scalar Scalar;
98c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    typedef typename internal::packet_traits<Scalar>::type PacketScalar;
99c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    typedef typename NumTraits<Scalar>::Real RealScalar;
100c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    typedef Derived DenseType;
101c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
102c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    using Base::RowsAtCompileTime;
103c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    using Base::ColsAtCompileTime;
104c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    using Base::SizeAtCompileTime;
105c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    using Base::MaxRowsAtCompileTime;
106c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    using Base::MaxColsAtCompileTime;
107c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    using Base::MaxSizeAtCompileTime;
108c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    using Base::IsVectorAtCompileTime;
109c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    using Base::Flags;
110c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
111c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<typename PlainObjectType, int MapOptions, typename StrideType> friend class Eigen::Map;
112c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    friend  class Eigen::Map<Derived, Unaligned>;
113c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    typedef Eigen::Map<Derived, Unaligned>  MapType;
114c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    friend  class Eigen::Map<const Derived, Unaligned>;
115c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    typedef const Eigen::Map<const Derived, Unaligned> ConstMapType;
116c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    friend  class Eigen::Map<Derived, Aligned>;
117c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    typedef Eigen::Map<Derived, Aligned> AlignedMapType;
118c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    friend  class Eigen::Map<const Derived, Aligned>;
119c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    typedef const Eigen::Map<const Derived, Aligned> ConstAlignedMapType;
120c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<typename StrideType> struct StridedMapType { typedef Eigen::Map<Derived, Unaligned, StrideType> type; };
121c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<typename StrideType> struct StridedConstMapType { typedef Eigen::Map<const Derived, Unaligned, StrideType> type; };
122c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<typename StrideType> struct StridedAlignedMapType { typedef Eigen::Map<Derived, Aligned, StrideType> type; };
123c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<typename StrideType> struct StridedConstAlignedMapType { typedef Eigen::Map<const Derived, Aligned, StrideType> type; };
124c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
125c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  protected:
126c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    DenseStorage<Scalar, Base::MaxSizeAtCompileTime, Base::RowsAtCompileTime, Base::ColsAtCompileTime, Options> m_storage;
127c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
128c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  public:
129c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    enum { NeedsToAlign = SizeAtCompileTime != Dynamic && (internal::traits<Derived>::Flags & AlignedBit) != 0 };
130c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)
131c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
132c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    Base& base() { return *static_cast<Base*>(this); }
133c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    const Base& base() const { return *static_cast<const Base*>(this); }
134c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
135c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_STRONG_INLINE Index rows() const { return m_storage.rows(); }
136c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_STRONG_INLINE Index cols() const { return m_storage.cols(); }
137c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
1387faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    EIGEN_STRONG_INLINE const Scalar& coeff(Index rowId, Index colId) const
139c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
140c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      if(Flags & RowMajorBit)
1417faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        return m_storage.data()[colId + rowId * m_storage.cols()];
142c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      else // column-major
1437faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        return m_storage.data()[rowId + colId * m_storage.rows()];
144c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
145c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
146c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_STRONG_INLINE const Scalar& coeff(Index index) const
147c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
148c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      return m_storage.data()[index];
149c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
150c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
1517faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    EIGEN_STRONG_INLINE Scalar& coeffRef(Index rowId, Index colId)
152c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
153c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      if(Flags & RowMajorBit)
1547faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        return m_storage.data()[colId + rowId * m_storage.cols()];
155c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      else // column-major
1567faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        return m_storage.data()[rowId + colId * m_storage.rows()];
157c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
158c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
159c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_STRONG_INLINE Scalar& coeffRef(Index index)
160c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
161c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      return m_storage.data()[index];
162c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
163c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
1647faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    EIGEN_STRONG_INLINE const Scalar& coeffRef(Index rowId, Index colId) const
165c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
166c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      if(Flags & RowMajorBit)
1677faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        return m_storage.data()[colId + rowId * m_storage.cols()];
168c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      else // column-major
1697faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        return m_storage.data()[rowId + colId * m_storage.rows()];
170c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
171c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
172c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_STRONG_INLINE const Scalar& coeffRef(Index index) const
173c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
174c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      return m_storage.data()[index];
175c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
176c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
177c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    /** \internal */
178c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<int LoadMode>
1797faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    EIGEN_STRONG_INLINE PacketScalar packet(Index rowId, Index colId) const
180c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
181c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      return internal::ploadt<PacketScalar, LoadMode>
182c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath               (m_storage.data() + (Flags & RowMajorBit
1837faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez                                   ? colId + rowId * m_storage.cols()
1847faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez                                   : rowId + colId * m_storage.rows()));
185c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
186c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
187c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    /** \internal */
188c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<int LoadMode>
189c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_STRONG_INLINE PacketScalar packet(Index index) const
190c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
191c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      return internal::ploadt<PacketScalar, LoadMode>(m_storage.data() + index);
192c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
193c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
194c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    /** \internal */
195c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<int StoreMode>
1967faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    EIGEN_STRONG_INLINE void writePacket(Index rowId, Index colId, const PacketScalar& val)
197c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
198c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      internal::pstoret<Scalar, PacketScalar, StoreMode>
199c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath              (m_storage.data() + (Flags & RowMajorBit
2007faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez                                   ? colId + rowId * m_storage.cols()
2017faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez                                   : rowId + colId * m_storage.rows()), val);
202c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
203c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
204c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    /** \internal */
205c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<int StoreMode>
2067faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    EIGEN_STRONG_INLINE void writePacket(Index index, const PacketScalar& val)
207c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
2087faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      internal::pstoret<Scalar, PacketScalar, StoreMode>(m_storage.data() + index, val);
209c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
210c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
211c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    /** \returns a const pointer to the data array of this matrix */
212c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_STRONG_INLINE const Scalar *data() const
213c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    { return m_storage.data(); }
214c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
215c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    /** \returns a pointer to the data array of this matrix */
216c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_STRONG_INLINE Scalar *data()
217c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    { return m_storage.data(); }
218c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
219c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    /** Resizes \c *this to a \a rows x \a cols matrix.
220c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      *
221c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * This method is intended for dynamic-size matrices, although it is legal to call it on any
222c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * matrix as long as fixed dimensions are left unchanged. If you only want to change the number
223c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * of rows and/or of columns, you can use resize(NoChange_t, Index), resize(Index, NoChange_t).
224c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      *
225c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * If the current number of coefficients of \c *this exactly matches the
226c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * product \a rows * \a cols, then no memory allocation is performed and
227c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * the current values are left unchanged. In all other cases, including
228c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * shrinking, the data is reallocated and all previous values are lost.
229c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      *
230c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * Example: \include Matrix_resize_int_int.cpp
231c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * Output: \verbinclude Matrix_resize_int_int.out
232c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      *
233c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * \sa resize(Index) for vectors, resize(NoChange_t, Index), resize(Index, NoChange_t)
234c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      */
2357faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    EIGEN_STRONG_INLINE void resize(Index nbRows, Index nbCols)
2367faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    {
2377faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      eigen_assert(   EIGEN_IMPLIES(RowsAtCompileTime!=Dynamic,nbRows==RowsAtCompileTime)
2387faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez                   && EIGEN_IMPLIES(ColsAtCompileTime!=Dynamic,nbCols==ColsAtCompileTime)
2397faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez                   && EIGEN_IMPLIES(RowsAtCompileTime==Dynamic && MaxRowsAtCompileTime!=Dynamic,nbRows<=MaxRowsAtCompileTime)
2407faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez                   && EIGEN_IMPLIES(ColsAtCompileTime==Dynamic && MaxColsAtCompileTime!=Dynamic,nbCols<=MaxColsAtCompileTime)
2417faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez                   && nbRows>=0 && nbCols>=0 && "Invalid sizes when resizing a matrix or array.");
2427faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      internal::check_rows_cols_for_overflow<MaxSizeAtCompileTime>::run(nbRows, nbCols);
2437faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      #ifdef EIGEN_INITIALIZE_COEFFS
2447faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        Index size = nbRows*nbCols;
245c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        bool size_changed = size != this->size();
2467faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        m_storage.resize(size, nbRows, nbCols);
2477faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        if(size_changed) EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
248c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      #else
2497faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        internal::check_rows_cols_for_overflow<MaxSizeAtCompileTime>::run(nbRows, nbCols);
2507faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        m_storage.resize(nbRows*nbCols, nbRows, nbCols);
251c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      #endif
252c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
253c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
254c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    /** Resizes \c *this to a vector of length \a size
255c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      *
256c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * \only_for_vectors. This method does not work for
257c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * partially dynamic matrices when the static dimension is anything other
258c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * than 1. For example it will not work with Matrix<double, 2, Dynamic>.
259c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      *
260c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * Example: \include Matrix_resize_int.cpp
261c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * Output: \verbinclude Matrix_resize_int.out
262c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      *
263c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * \sa resize(Index,Index), resize(NoChange_t, Index), resize(Index, NoChange_t)
264c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      */
265c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    inline void resize(Index size)
266c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
267c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      EIGEN_STATIC_ASSERT_VECTOR_ONLY(PlainObjectBase)
2687faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      eigen_assert(((SizeAtCompileTime == Dynamic && (MaxSizeAtCompileTime==Dynamic || size<=MaxSizeAtCompileTime)) || SizeAtCompileTime == size) && size>=0);
2697faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      #ifdef EIGEN_INITIALIZE_COEFFS
270c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        bool size_changed = size != this->size();
271c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      #endif
272c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      if(RowsAtCompileTime == 1)
273c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        m_storage.resize(size, 1, size);
274c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      else
275c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        m_storage.resize(size, size, 1);
2767faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      #ifdef EIGEN_INITIALIZE_COEFFS
2777faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez        if(size_changed) EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
278c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      #endif
279c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
280c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
281c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    /** Resizes the matrix, changing only the number of columns. For the parameter of type NoChange_t, just pass the special value \c NoChange
282c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * as in the example below.
283c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      *
284c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * Example: \include Matrix_resize_NoChange_int.cpp
285c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * Output: \verbinclude Matrix_resize_NoChange_int.out
286c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      *
287c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * \sa resize(Index,Index)
288c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      */
2897faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    inline void resize(NoChange_t, Index nbCols)
290c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
2917faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      resize(rows(), nbCols);
292c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
293c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
294c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    /** Resizes the matrix, changing only the number of rows. For the parameter of type NoChange_t, just pass the special value \c NoChange
295c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * as in the example below.
296c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      *
297c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * Example: \include Matrix_resize_int_NoChange.cpp
298c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * Output: \verbinclude Matrix_resize_int_NoChange.out
299c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      *
300c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * \sa resize(Index,Index)
301c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      */
3027faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    inline void resize(Index nbRows, NoChange_t)
303c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
3047faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      resize(nbRows, cols());
305c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
306c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
307c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    /** Resizes \c *this to have the same dimensions as \a other.
308c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * Takes care of doing all the checking that's needed.
309c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      *
310c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * Note that copying a row-vector into a vector (and conversely) is allowed.
311c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * The resizing, if any, is then done in the appropriate way so that row-vectors
312c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * remain row-vectors and vectors remain vectors.
313c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      */
314c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<typename OtherDerived>
315c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_STRONG_INLINE void resizeLike(const EigenBase<OtherDerived>& _other)
316c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
317c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      const OtherDerived& other = _other.derived();
3187faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      internal::check_rows_cols_for_overflow<MaxSizeAtCompileTime>::run(other.rows(), other.cols());
319c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      const Index othersize = other.rows()*other.cols();
320c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      if(RowsAtCompileTime == 1)
321c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      {
322c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        eigen_assert(other.rows() == 1 || other.cols() == 1);
323c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        resize(1, othersize);
324c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      }
325c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      else if(ColsAtCompileTime == 1)
326c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      {
327c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        eigen_assert(other.rows() == 1 || other.cols() == 1);
328c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        resize(othersize, 1);
329c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      }
330c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      else resize(other.rows(), other.cols());
331c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
332c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
333c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    /** Resizes the matrix to \a rows x \a cols while leaving old values untouched.
334c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      *
335c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * The method is intended for matrices of dynamic size. If you only want to change the number
336c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * of rows and/or of columns, you can use conservativeResize(NoChange_t, Index) or
337c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * conservativeResize(Index, NoChange_t).
338c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      *
339c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * Matrices are resized relative to the top-left element. In case values need to be
340c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * appended to the matrix they will be uninitialized.
341c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      */
3427faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    EIGEN_STRONG_INLINE void conservativeResize(Index nbRows, Index nbCols)
343c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
3447faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      internal::conservative_resize_like_impl<Derived>::run(*this, nbRows, nbCols);
345c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
346c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
347c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    /** Resizes the matrix to \a rows x \a cols while leaving old values untouched.
348c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      *
349c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * As opposed to conservativeResize(Index rows, Index cols), this version leaves
350c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * the number of columns unchanged.
351c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      *
352c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * In case the matrix is growing, new rows will be uninitialized.
353c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      */
3547faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    EIGEN_STRONG_INLINE void conservativeResize(Index nbRows, NoChange_t)
355c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
356c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      // Note: see the comment in conservativeResize(Index,Index)
3577faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      conservativeResize(nbRows, cols());
358c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
359c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
360c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    /** Resizes the matrix to \a rows x \a cols while leaving old values untouched.
361c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      *
362c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * As opposed to conservativeResize(Index rows, Index cols), this version leaves
363c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * the number of rows unchanged.
364c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      *
365c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * In case the matrix is growing, new columns will be uninitialized.
366c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      */
3677faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    EIGEN_STRONG_INLINE void conservativeResize(NoChange_t, Index nbCols)
368c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
369c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      // Note: see the comment in conservativeResize(Index,Index)
3707faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      conservativeResize(rows(), nbCols);
371c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
372c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
373c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    /** Resizes the vector to \a size while retaining old values.
374c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      *
375c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * \only_for_vectors. This method does not work for
376c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * partially dynamic matrices when the static dimension is anything other
377c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * than 1. For example it will not work with Matrix<double, 2, Dynamic>.
378c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      *
379c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * When values are appended, they will be uninitialized.
380c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      */
381c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_STRONG_INLINE void conservativeResize(Index size)
382c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
383c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      internal::conservative_resize_like_impl<Derived>::run(*this, size);
384c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
385c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
386c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    /** Resizes the matrix to \a rows x \a cols of \c other, while leaving old values untouched.
387c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      *
388c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * The method is intended for matrices of dynamic size. If you only want to change the number
389c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * of rows and/or of columns, you can use conservativeResize(NoChange_t, Index) or
390c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * conservativeResize(Index, NoChange_t).
391c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      *
392c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * Matrices are resized relative to the top-left element. In case values need to be
393c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * appended to the matrix they will copied from \c other.
394c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      */
395c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<typename OtherDerived>
396c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_STRONG_INLINE void conservativeResizeLike(const DenseBase<OtherDerived>& other)
397c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
398c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      internal::conservative_resize_like_impl<Derived,OtherDerived>::run(*this, other);
399c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
400c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
401c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    /** This is a special case of the templated operator=. Its purpose is to
402c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * prevent a default operator= from hiding the templated operator=.
403c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      */
404c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_STRONG_INLINE Derived& operator=(const PlainObjectBase& other)
405c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
406c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      return _set(other);
407c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
408c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
409c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    /** \sa MatrixBase::lazyAssign() */
410c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<typename OtherDerived>
411c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_STRONG_INLINE Derived& lazyAssign(const DenseBase<OtherDerived>& other)
412c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
413c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      _resize_to_match(other);
414c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      return Base::lazyAssign(other.derived());
415c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
416c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
417c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<typename OtherDerived>
418c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_STRONG_INLINE Derived& operator=(const ReturnByValue<OtherDerived>& func)
419c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
420c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      resize(func.rows(), func.cols());
421c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      return Base::operator=(func);
422c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
423c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
4247faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    EIGEN_STRONG_INLINE PlainObjectBase() : m_storage()
425c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
426c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//       _check_template_params();
4277faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez//       EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
428c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
429c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
430c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#ifndef EIGEN_PARSED_BY_DOXYGEN
431c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    // FIXME is it still needed ?
432c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    /** \internal */
433c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    PlainObjectBase(internal::constructor_without_unaligned_array_assert)
434c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      : m_storage(internal::constructor_without_unaligned_array_assert())
435c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
4367faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez//       _check_template_params(); EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
437c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
438c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#endif
439c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
4407faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    EIGEN_STRONG_INLINE PlainObjectBase(Index a_size, Index nbRows, Index nbCols)
4417faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      : m_storage(a_size, nbRows, nbCols)
442c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
443c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath//       _check_template_params();
4447faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez//       EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
445c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
446c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
447c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    /** \copydoc MatrixBase::operator=(const EigenBase<OtherDerived>&)
448c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      */
449c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<typename OtherDerived>
450c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_STRONG_INLINE Derived& operator=(const EigenBase<OtherDerived> &other)
451c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
452c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      _resize_to_match(other);
453c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      Base::operator=(other.derived());
454c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      return this->derived();
455c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
456c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
457c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    /** \sa MatrixBase::operator=(const EigenBase<OtherDerived>&) */
458c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<typename OtherDerived>
459c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_STRONG_INLINE PlainObjectBase(const EigenBase<OtherDerived> &other)
460c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      : m_storage(other.derived().rows() * other.derived().cols(), other.derived().rows(), other.derived().cols())
461c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
462c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      _check_template_params();
4637faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      internal::check_rows_cols_for_overflow<MaxSizeAtCompileTime>::run(other.derived().rows(), other.derived().cols());
464c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      Base::operator=(other.derived());
465c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
466c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
467c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    /** \name Map
468c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * These are convenience functions returning Map objects. The Map() static functions return unaligned Map objects,
469c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * while the AlignedMap() functions return aligned Map objects and thus should be called only with 16-byte-aligned
470c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * \a data pointers.
471c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      *
472c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * \see class Map
473c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      */
474c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    //@{
475c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    static inline ConstMapType Map(const Scalar* data)
476c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    { return ConstMapType(data); }
477c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    static inline MapType Map(Scalar* data)
478c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    { return MapType(data); }
479c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    static inline ConstMapType Map(const Scalar* data, Index size)
480c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    { return ConstMapType(data, size); }
481c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    static inline MapType Map(Scalar* data, Index size)
482c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    { return MapType(data, size); }
483c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    static inline ConstMapType Map(const Scalar* data, Index rows, Index cols)
484c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    { return ConstMapType(data, rows, cols); }
485c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    static inline MapType Map(Scalar* data, Index rows, Index cols)
486c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    { return MapType(data, rows, cols); }
487c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
488c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    static inline ConstAlignedMapType MapAligned(const Scalar* data)
489c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    { return ConstAlignedMapType(data); }
490c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    static inline AlignedMapType MapAligned(Scalar* data)
491c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    { return AlignedMapType(data); }
492c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    static inline ConstAlignedMapType MapAligned(const Scalar* data, Index size)
493c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    { return ConstAlignedMapType(data, size); }
494c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    static inline AlignedMapType MapAligned(Scalar* data, Index size)
495c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    { return AlignedMapType(data, size); }
496c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    static inline ConstAlignedMapType MapAligned(const Scalar* data, Index rows, Index cols)
497c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    { return ConstAlignedMapType(data, rows, cols); }
498c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    static inline AlignedMapType MapAligned(Scalar* data, Index rows, Index cols)
499c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    { return AlignedMapType(data, rows, cols); }
500c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
501c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<int Outer, int Inner>
502c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    static inline typename StridedConstMapType<Stride<Outer, Inner> >::type Map(const Scalar* data, const Stride<Outer, Inner>& stride)
503c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    { return typename StridedConstMapType<Stride<Outer, Inner> >::type(data, stride); }
504c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<int Outer, int Inner>
505c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    static inline typename StridedMapType<Stride<Outer, Inner> >::type Map(Scalar* data, const Stride<Outer, Inner>& stride)
506c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    { return typename StridedMapType<Stride<Outer, Inner> >::type(data, stride); }
507c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<int Outer, int Inner>
508c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    static inline typename StridedConstMapType<Stride<Outer, Inner> >::type Map(const Scalar* data, Index size, const Stride<Outer, Inner>& stride)
509c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    { return typename StridedConstMapType<Stride<Outer, Inner> >::type(data, size, stride); }
510c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<int Outer, int Inner>
511c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    static inline typename StridedMapType<Stride<Outer, Inner> >::type Map(Scalar* data, Index size, const Stride<Outer, Inner>& stride)
512c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    { return typename StridedMapType<Stride<Outer, Inner> >::type(data, size, stride); }
513c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<int Outer, int Inner>
514c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    static inline typename StridedConstMapType<Stride<Outer, Inner> >::type Map(const Scalar* data, Index rows, Index cols, const Stride<Outer, Inner>& stride)
515c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    { return typename StridedConstMapType<Stride<Outer, Inner> >::type(data, rows, cols, stride); }
516c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<int Outer, int Inner>
517c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    static inline typename StridedMapType<Stride<Outer, Inner> >::type Map(Scalar* data, Index rows, Index cols, const Stride<Outer, Inner>& stride)
518c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    { return typename StridedMapType<Stride<Outer, Inner> >::type(data, rows, cols, stride); }
519c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
520c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<int Outer, int Inner>
521c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    static inline typename StridedConstAlignedMapType<Stride<Outer, Inner> >::type MapAligned(const Scalar* data, const Stride<Outer, Inner>& stride)
522c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    { return typename StridedConstAlignedMapType<Stride<Outer, Inner> >::type(data, stride); }
523c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<int Outer, int Inner>
524c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    static inline typename StridedAlignedMapType<Stride<Outer, Inner> >::type MapAligned(Scalar* data, const Stride<Outer, Inner>& stride)
525c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    { return typename StridedAlignedMapType<Stride<Outer, Inner> >::type(data, stride); }
526c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<int Outer, int Inner>
527c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    static inline typename StridedConstAlignedMapType<Stride<Outer, Inner> >::type MapAligned(const Scalar* data, Index size, const Stride<Outer, Inner>& stride)
528c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    { return typename StridedConstAlignedMapType<Stride<Outer, Inner> >::type(data, size, stride); }
529c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<int Outer, int Inner>
530c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    static inline typename StridedAlignedMapType<Stride<Outer, Inner> >::type MapAligned(Scalar* data, Index size, const Stride<Outer, Inner>& stride)
531c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    { return typename StridedAlignedMapType<Stride<Outer, Inner> >::type(data, size, stride); }
532c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<int Outer, int Inner>
533c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    static inline typename StridedConstAlignedMapType<Stride<Outer, Inner> >::type MapAligned(const Scalar* data, Index rows, Index cols, const Stride<Outer, Inner>& stride)
534c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    { return typename StridedConstAlignedMapType<Stride<Outer, Inner> >::type(data, rows, cols, stride); }
535c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<int Outer, int Inner>
536c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    static inline typename StridedAlignedMapType<Stride<Outer, Inner> >::type MapAligned(Scalar* data, Index rows, Index cols, const Stride<Outer, Inner>& stride)
537c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    { return typename StridedAlignedMapType<Stride<Outer, Inner> >::type(data, rows, cols, stride); }
538c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    //@}
539c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
540c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    using Base::setConstant;
541c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    Derived& setConstant(Index size, const Scalar& value);
542c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    Derived& setConstant(Index rows, Index cols, const Scalar& value);
543c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
544c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    using Base::setZero;
545c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    Derived& setZero(Index size);
546c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    Derived& setZero(Index rows, Index cols);
547c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
548c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    using Base::setOnes;
549c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    Derived& setOnes(Index size);
550c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    Derived& setOnes(Index rows, Index cols);
551c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
552c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    using Base::setRandom;
553c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    Derived& setRandom(Index size);
554c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    Derived& setRandom(Index rows, Index cols);
555c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
556c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    #ifdef EIGEN_PLAINOBJECTBASE_PLUGIN
557c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    #include EIGEN_PLAINOBJECTBASE_PLUGIN
558c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    #endif
559c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
560c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  protected:
561c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    /** \internal Resizes *this in preparation for assigning \a other to it.
562c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * Takes care of doing all the checking that's needed.
563c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      *
564c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * Note that copying a row-vector into a vector (and conversely) is allowed.
565c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * The resizing, if any, is then done in the appropriate way so that row-vectors
566c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * remain row-vectors and vectors remain vectors.
567c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      */
568c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<typename OtherDerived>
569c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_STRONG_INLINE void _resize_to_match(const EigenBase<OtherDerived>& other)
570c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
571c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      #ifdef EIGEN_NO_AUTOMATIC_RESIZING
572c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      eigen_assert((this->size()==0 || (IsVectorAtCompileTime ? (this->size() == other.size())
573c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath                 : (rows() == other.rows() && cols() == other.cols())))
574c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        && "Size mismatch. Automatic resizing is disabled because EIGEN_NO_AUTOMATIC_RESIZING is defined");
5757faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      EIGEN_ONLY_USED_FOR_DEBUG(other);
576c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      #else
577c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      resizeLike(other);
578c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      #endif
579c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
580c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
581c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    /**
582c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * \brief Copies the value of the expression \a other into \c *this with automatic resizing.
583c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      *
584c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * *this might be resized to match the dimensions of \a other. If *this was a null matrix (not already initialized),
585c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * it will be initialized.
586c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      *
587c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * Note that copying a row-vector into a vector (and conversely) is allowed.
588c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * The resizing, if any, is then done in the appropriate way so that row-vectors
589c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * remain row-vectors and vectors remain vectors.
590c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      *
591c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * \sa operator=(const MatrixBase<OtherDerived>&), _set_noalias()
592c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      *
593c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * \internal
594c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      */
595c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<typename OtherDerived>
596c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_STRONG_INLINE Derived& _set(const DenseBase<OtherDerived>& other)
597c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
598c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      _set_selector(other.derived(), typename internal::conditional<static_cast<bool>(int(OtherDerived::Flags) & EvalBeforeAssigningBit), internal::true_type, internal::false_type>::type());
599c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      return this->derived();
600c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
601c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
602c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<typename OtherDerived>
603c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_STRONG_INLINE void _set_selector(const OtherDerived& other, const internal::true_type&) { _set_noalias(other.eval()); }
604c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
605c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<typename OtherDerived>
606c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_STRONG_INLINE void _set_selector(const OtherDerived& other, const internal::false_type&) { _set_noalias(other); }
607c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
608c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    /** \internal Like _set() but additionally makes the assumption that no aliasing effect can happen (which
609c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * is the case when creating a new matrix) so one can enforce lazy evaluation.
610c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      *
611c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * \sa operator=(const MatrixBase<OtherDerived>&), _set()
612c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      */
613c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<typename OtherDerived>
614c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_STRONG_INLINE Derived& _set_noalias(const DenseBase<OtherDerived>& other)
615c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
616c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      // I don't think we need this resize call since the lazyAssign will anyways resize
617c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      // and lazyAssign will be called by the assign selector.
618c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      //_resize_to_match(other);
619c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      // the 'false' below means to enforce lazy evaluation. We don't use lazyAssign() because
620c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      // it wouldn't allow to copy a row-vector into a column-vector.
621c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      return internal::assign_selector<Derived,OtherDerived,false>::run(this->derived(), other.derived());
622c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
623c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
624c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<typename T0, typename T1>
6257faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    EIGEN_STRONG_INLINE void _init2(Index nbRows, Index nbCols, typename internal::enable_if<Base::SizeAtCompileTime!=2,T0>::type* = 0)
626c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
627c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      EIGEN_STATIC_ASSERT(bool(NumTraits<T0>::IsInteger) &&
628c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath                          bool(NumTraits<T1>::IsInteger),
629c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath                          FLOATING_POINT_ARGUMENT_PASSED__INTEGER_WAS_EXPECTED)
6307faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      resize(nbRows,nbCols);
631c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
632c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<typename T0, typename T1>
6337faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez    EIGEN_STRONG_INLINE void _init2(const Scalar& val0, const Scalar& val1, typename internal::enable_if<Base::SizeAtCompileTime==2,T0>::type* = 0)
634c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
635c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(PlainObjectBase, 2)
6367faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      m_storage.data()[0] = val0;
6377faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      m_storage.data()[1] = val1;
638c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
639c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
640c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<typename MatrixTypeA, typename MatrixTypeB, bool SwapPointers>
641c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    friend struct internal::matrix_swap_impl;
642c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
643c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    /** \internal generic implementation of swap for dense storage since for dynamic-sized matrices of same type it is enough to swap the
644c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      * data pointers.
645c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      */
646c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    template<typename OtherDerived>
647c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    void _swap(DenseBase<OtherDerived> const & other)
648c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
649c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      enum { SwapPointers = internal::is_same<Derived, OtherDerived>::value && Base::SizeAtCompileTime==Dynamic };
650c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      internal::matrix_swap_impl<Derived, OtherDerived, bool(SwapPointers)>::run(this->derived(), other.const_cast_derived());
651c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
652c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
653c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  public:
654c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#ifndef EIGEN_PARSED_BY_DOXYGEN
655c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    static EIGEN_STRONG_INLINE void _check_template_params()
656c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
657c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      EIGEN_STATIC_ASSERT((EIGEN_IMPLIES(MaxRowsAtCompileTime==1 && MaxColsAtCompileTime!=1, (Options&RowMajor)==RowMajor)
658c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath                        && EIGEN_IMPLIES(MaxColsAtCompileTime==1 && MaxRowsAtCompileTime!=1, (Options&RowMajor)==0)
659c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath                        && ((RowsAtCompileTime == Dynamic) || (RowsAtCompileTime >= 0))
660c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath                        && ((ColsAtCompileTime == Dynamic) || (ColsAtCompileTime >= 0))
661c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath                        && ((MaxRowsAtCompileTime == Dynamic) || (MaxRowsAtCompileTime >= 0))
662c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath                        && ((MaxColsAtCompileTime == Dynamic) || (MaxColsAtCompileTime >= 0))
663c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath                        && (MaxRowsAtCompileTime == RowsAtCompileTime || RowsAtCompileTime==Dynamic)
664c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath                        && (MaxColsAtCompileTime == ColsAtCompileTime || ColsAtCompileTime==Dynamic)
665c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath                        && (Options & (DontAlign|RowMajor)) == Options),
666c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        INVALID_MATRIX_TEMPLATE_PARAMETERS)
667c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
668c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#endif
669c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
670c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathprivate:
671c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    enum { ThisConstantIsPrivateInPlainObjectBase };
672c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
673c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
6747faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandeznamespace internal {
6757faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
676c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate <typename Derived, typename OtherDerived, bool IsVector>
6777faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandezstruct conservative_resize_like_impl
678c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
679c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename Derived::Index Index;
680c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static void run(DenseBase<Derived>& _this, Index rows, Index cols)
681c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
682c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    if (_this.rows() == rows && _this.cols() == cols) return;
683c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_STATIC_ASSERT_DYNAMIC_SIZE(Derived)
684c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
685c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    if ( ( Derived::IsRowMajor && _this.cols() == cols) || // row-major and we change only the number of rows
686c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath         (!Derived::IsRowMajor && _this.rows() == rows) )  // column-major and we change only the number of columns
687c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
6887faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez      internal::check_rows_cols_for_overflow<Derived::MaxSizeAtCompileTime>::run(rows, cols);
689c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      _this.derived().m_storage.conservativeResize(rows*cols,rows,cols);
690c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
691c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    else
692c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
693c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      // The storage order does not allow us to use reallocation.
694c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      typename Derived::PlainObject tmp(rows,cols);
695c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      const Index common_rows = (std::min)(rows, _this.rows());
696c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      const Index common_cols = (std::min)(cols, _this.cols());
697c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      tmp.block(0,0,common_rows,common_cols) = _this.block(0,0,common_rows,common_cols);
698c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      _this.derived().swap(tmp);
699c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
700c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
701c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
702c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static void run(DenseBase<Derived>& _this, const DenseBase<OtherDerived>& other)
703c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
704c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    if (_this.rows() == other.rows() && _this.cols() == other.cols()) return;
705c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
706c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    // Note: Here is space for improvement. Basically, for conservativeResize(Index,Index),
707c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    // neither RowsAtCompileTime or ColsAtCompileTime must be Dynamic. If only one of the
708c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    // dimensions is dynamic, one could use either conservativeResize(Index rows, NoChange_t) or
709c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    // conservativeResize(NoChange_t, Index cols). For these methods new static asserts like
710c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    // EIGEN_STATIC_ASSERT_DYNAMIC_ROWS and EIGEN_STATIC_ASSERT_DYNAMIC_COLS would be good.
711c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_STATIC_ASSERT_DYNAMIC_SIZE(Derived)
712c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    EIGEN_STATIC_ASSERT_DYNAMIC_SIZE(OtherDerived)
713c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
714c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    if ( ( Derived::IsRowMajor && _this.cols() == other.cols()) || // row-major and we change only the number of rows
715c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath         (!Derived::IsRowMajor && _this.rows() == other.rows()) )  // column-major and we change only the number of columns
716c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
717c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      const Index new_rows = other.rows() - _this.rows();
718c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      const Index new_cols = other.cols() - _this.cols();
719c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      _this.derived().m_storage.conservativeResize(other.size(),other.rows(),other.cols());
720c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      if (new_rows>0)
721c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        _this.bottomRightCorner(new_rows, other.cols()) = other.bottomRows(new_rows);
722c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      else if (new_cols>0)
723c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath        _this.bottomRightCorner(other.rows(), new_cols) = other.rightCols(new_cols);
724c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
725c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    else
726c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    {
727c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      // The storage order does not allow us to use reallocation.
728c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      typename Derived::PlainObject tmp(other);
729c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      const Index common_rows = (std::min)(tmp.rows(), _this.rows());
730c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      const Index common_cols = (std::min)(tmp.cols(), _this.cols());
731c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      tmp.block(0,0,common_rows,common_cols) = _this.block(0,0,common_rows,common_cols);
732c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      _this.derived().swap(tmp);
733c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    }
734c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
735c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
736c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
7377faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez// Here, the specialization for vectors inherits from the general matrix case
7387faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez// to allow calling .conservativeResize(rows,cols) on vectors.
739c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate <typename Derived, typename OtherDerived>
740c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct conservative_resize_like_impl<Derived,OtherDerived,true>
7417faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  : conservative_resize_like_impl<Derived,OtherDerived,false>
742c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
7437faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez  using conservative_resize_like_impl<Derived,OtherDerived,false>::run;
7447faaa9f3f0df9d23790277834d426c3d992ac3baCarlos Hernandez
745c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  typedef typename Derived::Index Index;
746c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static void run(DenseBase<Derived>& _this, Index size)
747c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
748c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    const Index new_rows = Derived::RowsAtCompileTime==1 ? 1 : size;
749c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    const Index new_cols = Derived::RowsAtCompileTime==1 ? size : 1;
750c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    _this.derived().m_storage.conservativeResize(size,new_rows,new_cols);
751c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
752c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
753c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static void run(DenseBase<Derived>& _this, const DenseBase<OtherDerived>& other)
754c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
755c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    if (_this.rows() == other.rows() && _this.cols() == other.cols()) return;
756c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
757c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    const Index num_new_elements = other.size() - _this.size();
758c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
759c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    const Index new_rows = Derived::RowsAtCompileTime==1 ? 1 : other.rows();
760c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    const Index new_cols = Derived::RowsAtCompileTime==1 ? other.cols() : 1;
761c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    _this.derived().m_storage.conservativeResize(other.size(),new_rows,new_cols);
762c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
763c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    if (num_new_elements > 0)
764c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath      _this.tail(num_new_elements) = other.tail(num_new_elements);
765c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
766c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
767c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
768c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename MatrixTypeA, typename MatrixTypeB, bool SwapPointers>
769c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct matrix_swap_impl
770c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
771c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline void run(MatrixTypeA& a, MatrixTypeB& b)
772c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
773c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    a.base().swap(b);
774c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
775c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
776c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
777c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathtemplate<typename MatrixTypeA, typename MatrixTypeB>
778c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamathstruct matrix_swap_impl<MatrixTypeA, MatrixTypeB, true>
779c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath{
780c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  static inline void run(MatrixTypeA& a, MatrixTypeB& b)
781c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  {
782c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath    static_cast<typename MatrixTypeA::Base&>(a).m_storage.swap(static_cast<typename MatrixTypeB::Base&>(b).m_storage);
783c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath  }
784c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath};
785c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
786c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath} // end namespace internal
787c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
788c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath} // end namespace Eigen
789c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath
790c981c48f5bc9aefeffc0bcb0cc3934c2fae179ddNarayan Kamath#endif // EIGEN_DENSESTORAGEBASE_H
791