PlainObjectBase.h 44.1 KB
Newer Older
LM's avatar
LM committed
1 2 3 4 5 6
// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2008-2009 Gael Guennebaud <gael.guennebaud@inria.fr>
// Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
//
Don Gagne's avatar
Don Gagne committed
7 8 9
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
LM's avatar
LM committed
10 11 12 13

#ifndef EIGEN_DENSESTORAGEBASE_H
#define EIGEN_DENSESTORAGEBASE_H

Don Gagne's avatar
Don Gagne committed
14 15 16 17 18 19
#if defined(EIGEN_INITIALIZE_MATRICES_BY_ZERO)
# define EIGEN_INITIALIZE_COEFFS
# define EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED for(int i=0;i<base().size();++i) coeffRef(i)=Scalar(0);
#elif defined(EIGEN_INITIALIZE_MATRICES_BY_NAN)
# define EIGEN_INITIALIZE_COEFFS
# 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();
LM's avatar
LM committed
20
#else
Don Gagne's avatar
Don Gagne committed
21 22
# undef EIGEN_INITIALIZE_COEFFS
# define EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
LM's avatar
LM committed
23 24
#endif

Don Gagne's avatar
Don Gagne committed
25 26
namespace Eigen {

LM's avatar
LM committed
27 28
namespace internal {

Don Gagne's avatar
Don Gagne committed
29 30
template<int MaxSizeAtCompileTime> struct check_rows_cols_for_overflow {
  template<typename Index>
31
  EIGEN_DEVICE_FUNC
Don Gagne's avatar
Don Gagne committed
32 33 34 35 36 37 38
  static EIGEN_ALWAYS_INLINE void run(Index, Index)
  {
  }
};

template<> struct check_rows_cols_for_overflow<Dynamic> {
  template<typename Index>
39
  EIGEN_DEVICE_FUNC
Don Gagne's avatar
Don Gagne committed
40 41 42 43
  static EIGEN_ALWAYS_INLINE void run(Index rows, Index cols)
  {
    // http://hg.mozilla.org/mozilla-central/file/6c8a909977d3/xpcom/ds/CheckedInt.h#l242
    // we assume Index is signed
44
    Index max_index = (std::size_t(1) << (8 * sizeof(Index) - 1)) - 1; // assume Index is signed
Don Gagne's avatar
Don Gagne committed
45 46 47 48 49 50 51
    bool error = (rows == 0 || cols == 0) ? false
               : (rows > max_index / cols);
    if (error)
      throw_std_bad_alloc();
  }
};

52 53 54 55
template <typename Derived,
          typename OtherDerived = Derived,
          bool IsVector = bool(Derived::IsVectorAtCompileTime) && bool(OtherDerived::IsVectorAtCompileTime)>
struct conservative_resize_like_impl;
LM's avatar
LM committed
56 57 58 59 60

template<typename MatrixTypeA, typename MatrixTypeB, bool SwapPointers> struct matrix_swap_impl;

} // end namespace internal

Don Gagne's avatar
Don Gagne committed
61
#ifdef EIGEN_PARSED_BY_DOXYGEN
62
namespace doxygen {
Don Gagne's avatar
Don Gagne committed
63

64
// This is a workaround to doxygen not being able to understand the inheritance logic
Don Gagne's avatar
Don Gagne committed
65
// when it is hidden by the dense_xpr_base helper struct.
66 67 68 69 70 71
// Moreover, doxygen fails to include members that are not documented in the declaration body of
// MatrixBase if we inherits MatrixBase<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >,
// this is why we simply inherits MatrixBase, though this does not make sense.

/** This class is just a workaround for Doxygen and it does not not actually exist. */
template<typename Derived> struct dense_xpr_base_dispatcher;
Don Gagne's avatar
Don Gagne committed
72 73
/** This class is just a workaround for Doxygen and it does not not actually exist. */
template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
74 75
struct dense_xpr_base_dispatcher<Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
    : public MatrixBase {};
Don Gagne's avatar
Don Gagne committed
76 77
/** This class is just a workaround for Doxygen and it does not not actually exist. */
template<typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
78 79
struct dense_xpr_base_dispatcher<Array<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> >
    : public ArrayBase {};
Don Gagne's avatar
Don Gagne committed
80

81
} // namespace doxygen
Don Gagne's avatar
Don Gagne committed
82

83 84 85 86 87 88 89 90 91 92 93
/** \class PlainObjectBase
  * \ingroup Core_Module
  * \brief %Dense storage base class for matrices and arrays.
  *
  * This class can be extended with the help of the plugin mechanism described on the page
  * \ref TopicCustomizing_Plugins by defining the preprocessor symbol \c EIGEN_PLAINOBJECTBASE_PLUGIN.
  *
  * \tparam Derived is the derived type, e.g., a Matrix or Array
  *
  * \sa \ref TopicClassHierarchy
  */
Don Gagne's avatar
Don Gagne committed
94
template<typename Derived>
95
class PlainObjectBase : public doxygen::dense_xpr_base_dispatcher<Derived>
Don Gagne's avatar
Don Gagne committed
96
#else
LM's avatar
LM committed
97 98
template<typename Derived>
class PlainObjectBase : public internal::dense_xpr_base<Derived>::type
Don Gagne's avatar
Don Gagne committed
99
#endif
LM's avatar
LM committed
100 101 102 103 104 105 106
{
  public:
    enum { Options = internal::traits<Derived>::Options };
    typedef typename internal::dense_xpr_base<Derived>::type Base;

    typedef typename internal::traits<Derived>::StorageKind StorageKind;
    typedef typename internal::traits<Derived>::Scalar Scalar;
107
    
LM's avatar
LM committed
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    typedef typename internal::packet_traits<Scalar>::type PacketScalar;
    typedef typename NumTraits<Scalar>::Real RealScalar;
    typedef Derived DenseType;

    using Base::RowsAtCompileTime;
    using Base::ColsAtCompileTime;
    using Base::SizeAtCompileTime;
    using Base::MaxRowsAtCompileTime;
    using Base::MaxColsAtCompileTime;
    using Base::MaxSizeAtCompileTime;
    using Base::IsVectorAtCompileTime;
    using Base::Flags;

    template<typename PlainObjectType, int MapOptions, typename StrideType> friend class Eigen::Map;
    friend  class Eigen::Map<Derived, Unaligned>;
    typedef Eigen::Map<Derived, Unaligned>  MapType;
    friend  class Eigen::Map<const Derived, Unaligned>;
    typedef const Eigen::Map<const Derived, Unaligned> ConstMapType;
126 127 128 129 130 131 132
#if EIGEN_MAX_ALIGN_BYTES>0
    // for EIGEN_MAX_ALIGN_BYTES==0, AlignedMax==Unaligned, and many compilers generate warnings for friend-ing a class twice.
    friend  class Eigen::Map<Derived, AlignedMax>;
    friend  class Eigen::Map<const Derived, AlignedMax>;
#endif
    typedef Eigen::Map<Derived, AlignedMax> AlignedMapType;
    typedef const Eigen::Map<const Derived, AlignedMax> ConstAlignedMapType;
LM's avatar
LM committed
133 134
    template<typename StrideType> struct StridedMapType { typedef Eigen::Map<Derived, Unaligned, StrideType> type; };
    template<typename StrideType> struct StridedConstMapType { typedef Eigen::Map<const Derived, Unaligned, StrideType> type; };
135 136
    template<typename StrideType> struct StridedAlignedMapType { typedef Eigen::Map<Derived, AlignedMax, StrideType> type; };
    template<typename StrideType> struct StridedConstAlignedMapType { typedef Eigen::Map<const Derived, AlignedMax, StrideType> type; };
LM's avatar
LM committed
137 138 139 140 141

  protected:
    DenseStorage<Scalar, Base::MaxSizeAtCompileTime, Base::RowsAtCompileTime, Base::ColsAtCompileTime, Options> m_storage;

  public:
142
    enum { NeedsToAlign = (SizeAtCompileTime != Dynamic) && (internal::traits<Derived>::Alignment>0) };
LM's avatar
LM committed
143 144
    EIGEN_MAKE_ALIGNED_OPERATOR_NEW_IF(NeedsToAlign)

145
    EIGEN_DEVICE_FUNC
LM's avatar
LM committed
146
    Base& base() { return *static_cast<Base*>(this); }
147
    EIGEN_DEVICE_FUNC
LM's avatar
LM committed
148 149
    const Base& base() const { return *static_cast<const Base*>(this); }

150
    EIGEN_DEVICE_FUNC
LM's avatar
LM committed
151
    EIGEN_STRONG_INLINE Index rows() const { return m_storage.rows(); }
152
    EIGEN_DEVICE_FUNC
LM's avatar
LM committed
153 154
    EIGEN_STRONG_INLINE Index cols() const { return m_storage.cols(); }

155 156 157 158 159
    /** This is an overloaded version of DenseCoeffsBase<Derived,ReadOnlyAccessors>::coeff(Index,Index) const
      * provided to by-pass the creation of an evaluator of the expression, thus saving compilation efforts.
      *
      * See DenseCoeffsBase<Derived,ReadOnlyAccessors>::coeff(Index) const for details. */
    EIGEN_DEVICE_FUNC
Don Gagne's avatar
Don Gagne committed
160
    EIGEN_STRONG_INLINE const Scalar& coeff(Index rowId, Index colId) const
LM's avatar
LM committed
161 162
    {
      if(Flags & RowMajorBit)
Don Gagne's avatar
Don Gagne committed
163
        return m_storage.data()[colId + rowId * m_storage.cols()];
LM's avatar
LM committed
164
      else // column-major
Don Gagne's avatar
Don Gagne committed
165
        return m_storage.data()[rowId + colId * m_storage.rows()];
LM's avatar
LM committed
166 167
    }

168 169 170 171 172
    /** This is an overloaded version of DenseCoeffsBase<Derived,ReadOnlyAccessors>::coeff(Index) const
      * provided to by-pass the creation of an evaluator of the expression, thus saving compilation efforts.
      *
      * See DenseCoeffsBase<Derived,ReadOnlyAccessors>::coeff(Index) const for details. */
    EIGEN_DEVICE_FUNC
LM's avatar
LM committed
173 174 175 176 177
    EIGEN_STRONG_INLINE const Scalar& coeff(Index index) const
    {
      return m_storage.data()[index];
    }

178 179 180 181 182
    /** This is an overloaded version of DenseCoeffsBase<Derived,WriteAccessors>::coeffRef(Index,Index) const
      * provided to by-pass the creation of an evaluator of the expression, thus saving compilation efforts.
      *
      * See DenseCoeffsBase<Derived,WriteAccessors>::coeffRef(Index,Index) const for details. */
    EIGEN_DEVICE_FUNC
Don Gagne's avatar
Don Gagne committed
183
    EIGEN_STRONG_INLINE Scalar& coeffRef(Index rowId, Index colId)
LM's avatar
LM committed
184 185
    {
      if(Flags & RowMajorBit)
Don Gagne's avatar
Don Gagne committed
186
        return m_storage.data()[colId + rowId * m_storage.cols()];
LM's avatar
LM committed
187
      else // column-major
Don Gagne's avatar
Don Gagne committed
188
        return m_storage.data()[rowId + colId * m_storage.rows()];
LM's avatar
LM committed
189 190
    }

191 192 193 194 195
    /** This is an overloaded version of DenseCoeffsBase<Derived,WriteAccessors>::coeffRef(Index) const
      * provided to by-pass the creation of an evaluator of the expression, thus saving compilation efforts.
      *
      * See DenseCoeffsBase<Derived,WriteAccessors>::coeffRef(Index) const for details. */
    EIGEN_DEVICE_FUNC
LM's avatar
LM committed
196 197 198 199 200
    EIGEN_STRONG_INLINE Scalar& coeffRef(Index index)
    {
      return m_storage.data()[index];
    }

201 202 203
    /** This is the const version of coeffRef(Index,Index) which is thus synonym of coeff(Index,Index).
      * It is provided for convenience. */
    EIGEN_DEVICE_FUNC
Don Gagne's avatar
Don Gagne committed
204
    EIGEN_STRONG_INLINE const Scalar& coeffRef(Index rowId, Index colId) const
LM's avatar
LM committed
205 206
    {
      if(Flags & RowMajorBit)
Don Gagne's avatar
Don Gagne committed
207
        return m_storage.data()[colId + rowId * m_storage.cols()];
LM's avatar
LM committed
208
      else // column-major
Don Gagne's avatar
Don Gagne committed
209
        return m_storage.data()[rowId + colId * m_storage.rows()];
LM's avatar
LM committed
210 211
    }

212 213 214
    /** This is the const version of coeffRef(Index) which is thus synonym of coeff(Index).
      * It is provided for convenience. */
    EIGEN_DEVICE_FUNC
LM's avatar
LM committed
215 216 217 218 219 220 221
    EIGEN_STRONG_INLINE const Scalar& coeffRef(Index index) const
    {
      return m_storage.data()[index];
    }

    /** \internal */
    template<int LoadMode>
Don Gagne's avatar
Don Gagne committed
222
    EIGEN_STRONG_INLINE PacketScalar packet(Index rowId, Index colId) const
LM's avatar
LM committed
223 224 225
    {
      return internal::ploadt<PacketScalar, LoadMode>
               (m_storage.data() + (Flags & RowMajorBit
Don Gagne's avatar
Don Gagne committed
226 227
                                   ? colId + rowId * m_storage.cols()
                                   : rowId + colId * m_storage.rows()));
LM's avatar
LM committed
228 229 230 231 232 233 234 235 236 237 238
    }

    /** \internal */
    template<int LoadMode>
    EIGEN_STRONG_INLINE PacketScalar packet(Index index) const
    {
      return internal::ploadt<PacketScalar, LoadMode>(m_storage.data() + index);
    }

    /** \internal */
    template<int StoreMode>
Don Gagne's avatar
Don Gagne committed
239
    EIGEN_STRONG_INLINE void writePacket(Index rowId, Index colId, const PacketScalar& val)
LM's avatar
LM committed
240 241 242
    {
      internal::pstoret<Scalar, PacketScalar, StoreMode>
              (m_storage.data() + (Flags & RowMajorBit
Don Gagne's avatar
Don Gagne committed
243 244
                                   ? colId + rowId * m_storage.cols()
                                   : rowId + colId * m_storage.rows()), val);
LM's avatar
LM committed
245 246 247 248
    }

    /** \internal */
    template<int StoreMode>
Don Gagne's avatar
Don Gagne committed
249
    EIGEN_STRONG_INLINE void writePacket(Index index, const PacketScalar& val)
LM's avatar
LM committed
250
    {
Don Gagne's avatar
Don Gagne committed
251
      internal::pstoret<Scalar, PacketScalar, StoreMode>(m_storage.data() + index, val);
LM's avatar
LM committed
252 253 254
    }

    /** \returns a const pointer to the data array of this matrix */
255
    EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar *data() const
LM's avatar
LM committed
256 257 258
    { return m_storage.data(); }

    /** \returns a pointer to the data array of this matrix */
259
    EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Scalar *data()
LM's avatar
LM committed
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
    { return m_storage.data(); }

    /** Resizes \c *this to a \a rows x \a cols matrix.
      *
      * This method is intended for dynamic-size matrices, although it is legal to call it on any
      * matrix as long as fixed dimensions are left unchanged. If you only want to change the number
      * of rows and/or of columns, you can use resize(NoChange_t, Index), resize(Index, NoChange_t).
      *
      * If the current number of coefficients of \c *this exactly matches the
      * product \a rows * \a cols, then no memory allocation is performed and
      * the current values are left unchanged. In all other cases, including
      * shrinking, the data is reallocated and all previous values are lost.
      *
      * Example: \include Matrix_resize_int_int.cpp
      * Output: \verbinclude Matrix_resize_int_int.out
      *
      * \sa resize(Index) for vectors, resize(NoChange_t, Index), resize(Index, NoChange_t)
      */
278 279 280 281 282 283 284 285 286
    EIGEN_DEVICE_FUNC
    EIGEN_STRONG_INLINE void resize(Index rows, Index cols)
    {
      eigen_assert(   EIGEN_IMPLIES(RowsAtCompileTime!=Dynamic,rows==RowsAtCompileTime)
                   && EIGEN_IMPLIES(ColsAtCompileTime!=Dynamic,cols==ColsAtCompileTime)
                   && EIGEN_IMPLIES(RowsAtCompileTime==Dynamic && MaxRowsAtCompileTime!=Dynamic,rows<=MaxRowsAtCompileTime)
                   && EIGEN_IMPLIES(ColsAtCompileTime==Dynamic && MaxColsAtCompileTime!=Dynamic,cols<=MaxColsAtCompileTime)
                   && rows>=0 && cols>=0 && "Invalid sizes when resizing a matrix or array.");
      internal::check_rows_cols_for_overflow<MaxSizeAtCompileTime>::run(rows, cols);
Don Gagne's avatar
Don Gagne committed
287
      #ifdef EIGEN_INITIALIZE_COEFFS
288
        Index size = rows*cols;
LM's avatar
LM committed
289
        bool size_changed = size != this->size();
290
        m_storage.resize(size, rows, cols);
Don Gagne's avatar
Don Gagne committed
291
        if(size_changed) EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
LM's avatar
LM committed
292
      #else
293
        m_storage.resize(rows*cols, rows, cols);
LM's avatar
LM committed
294 295 296 297 298 299 300 301 302 303 304 305 306 307
      #endif
    }

    /** Resizes \c *this to a vector of length \a size
      *
      * \only_for_vectors. This method does not work for
      * partially dynamic matrices when the static dimension is anything other
      * than 1. For example it will not work with Matrix<double, 2, Dynamic>.
      *
      * Example: \include Matrix_resize_int.cpp
      * Output: \verbinclude Matrix_resize_int.out
      *
      * \sa resize(Index,Index), resize(NoChange_t, Index), resize(Index, NoChange_t)
      */
308
    EIGEN_DEVICE_FUNC
LM's avatar
LM committed
309 310 311
    inline void resize(Index size)
    {
      EIGEN_STATIC_ASSERT_VECTOR_ONLY(PlainObjectBase)
Don Gagne's avatar
Don Gagne committed
312 313
      eigen_assert(((SizeAtCompileTime == Dynamic && (MaxSizeAtCompileTime==Dynamic || size<=MaxSizeAtCompileTime)) || SizeAtCompileTime == size) && size>=0);
      #ifdef EIGEN_INITIALIZE_COEFFS
LM's avatar
LM committed
314 315 316 317 318 319
        bool size_changed = size != this->size();
      #endif
      if(RowsAtCompileTime == 1)
        m_storage.resize(size, 1, size);
      else
        m_storage.resize(size, size, 1);
Don Gagne's avatar
Don Gagne committed
320 321
      #ifdef EIGEN_INITIALIZE_COEFFS
        if(size_changed) EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
LM's avatar
LM committed
322 323 324 325 326 327 328 329 330 331 332
      #endif
    }

    /** Resizes the matrix, changing only the number of columns. For the parameter of type NoChange_t, just pass the special value \c NoChange
      * as in the example below.
      *
      * Example: \include Matrix_resize_NoChange_int.cpp
      * Output: \verbinclude Matrix_resize_NoChange_int.out
      *
      * \sa resize(Index,Index)
      */
333 334
    EIGEN_DEVICE_FUNC
    inline void resize(NoChange_t, Index cols)
LM's avatar
LM committed
335
    {
336
      resize(rows(), cols);
LM's avatar
LM committed
337 338 339 340 341 342 343 344 345 346
    }

    /** Resizes the matrix, changing only the number of rows. For the parameter of type NoChange_t, just pass the special value \c NoChange
      * as in the example below.
      *
      * Example: \include Matrix_resize_int_NoChange.cpp
      * Output: \verbinclude Matrix_resize_int_NoChange.out
      *
      * \sa resize(Index,Index)
      */
347 348
    EIGEN_DEVICE_FUNC
    inline void resize(Index rows, NoChange_t)
LM's avatar
LM committed
349
    {
350
      resize(rows, cols());
LM's avatar
LM committed
351 352 353 354 355 356 357 358 359 360
    }

    /** Resizes \c *this to have the same dimensions as \a other.
      * Takes care of doing all the checking that's needed.
      *
      * Note that copying a row-vector into a vector (and conversely) is allowed.
      * The resizing, if any, is then done in the appropriate way so that row-vectors
      * remain row-vectors and vectors remain vectors.
      */
    template<typename OtherDerived>
361
    EIGEN_DEVICE_FUNC 
LM's avatar
LM committed
362 363 364
    EIGEN_STRONG_INLINE void resizeLike(const EigenBase<OtherDerived>& _other)
    {
      const OtherDerived& other = _other.derived();
Don Gagne's avatar
Don Gagne committed
365
      internal::check_rows_cols_for_overflow<MaxSizeAtCompileTime>::run(other.rows(), other.cols());
LM's avatar
LM committed
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
      const Index othersize = other.rows()*other.cols();
      if(RowsAtCompileTime == 1)
      {
        eigen_assert(other.rows() == 1 || other.cols() == 1);
        resize(1, othersize);
      }
      else if(ColsAtCompileTime == 1)
      {
        eigen_assert(other.rows() == 1 || other.cols() == 1);
        resize(othersize, 1);
      }
      else resize(other.rows(), other.cols());
    }

    /** Resizes the matrix to \a rows x \a cols while leaving old values untouched.
      *
      * The method is intended for matrices of dynamic size. If you only want to change the number
      * of rows and/or of columns, you can use conservativeResize(NoChange_t, Index) or
      * conservativeResize(Index, NoChange_t).
      *
      * Matrices are resized relative to the top-left element. In case values need to be 
      * appended to the matrix they will be uninitialized.
      */
389 390
    EIGEN_DEVICE_FUNC
    EIGEN_STRONG_INLINE void conservativeResize(Index rows, Index cols)
LM's avatar
LM committed
391
    {
392
      internal::conservative_resize_like_impl<Derived>::run(*this, rows, cols);
LM's avatar
LM committed
393 394 395 396 397 398 399 400 401
    }

    /** Resizes the matrix to \a rows x \a cols while leaving old values untouched.
      *
      * As opposed to conservativeResize(Index rows, Index cols), this version leaves
      * the number of columns unchanged.
      *
      * In case the matrix is growing, new rows will be uninitialized.
      */
402 403
    EIGEN_DEVICE_FUNC
    EIGEN_STRONG_INLINE void conservativeResize(Index rows, NoChange_t)
LM's avatar
LM committed
404 405
    {
      // Note: see the comment in conservativeResize(Index,Index)
406
      conservativeResize(rows, cols());
LM's avatar
LM committed
407 408 409 410 411 412 413 414 415
    }

    /** Resizes the matrix to \a rows x \a cols while leaving old values untouched.
      *
      * As opposed to conservativeResize(Index rows, Index cols), this version leaves
      * the number of rows unchanged.
      *
      * In case the matrix is growing, new columns will be uninitialized.
      */
416 417
    EIGEN_DEVICE_FUNC
    EIGEN_STRONG_INLINE void conservativeResize(NoChange_t, Index cols)
LM's avatar
LM committed
418 419
    {
      // Note: see the comment in conservativeResize(Index,Index)
420
      conservativeResize(rows(), cols);
LM's avatar
LM committed
421 422 423 424 425 426 427 428 429 430
    }

    /** Resizes the vector to \a size while retaining old values.
      *
      * \only_for_vectors. This method does not work for
      * partially dynamic matrices when the static dimension is anything other
      * than 1. For example it will not work with Matrix<double, 2, Dynamic>.
      *
      * When values are appended, they will be uninitialized.
      */
431
    EIGEN_DEVICE_FUNC
LM's avatar
LM committed
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
    EIGEN_STRONG_INLINE void conservativeResize(Index size)
    {
      internal::conservative_resize_like_impl<Derived>::run(*this, size);
    }

    /** Resizes the matrix to \a rows x \a cols of \c other, while leaving old values untouched.
      *
      * The method is intended for matrices of dynamic size. If you only want to change the number
      * of rows and/or of columns, you can use conservativeResize(NoChange_t, Index) or
      * conservativeResize(Index, NoChange_t).
      *
      * Matrices are resized relative to the top-left element. In case values need to be 
      * appended to the matrix they will copied from \c other.
      */
    template<typename OtherDerived>
447
    EIGEN_DEVICE_FUNC
LM's avatar
LM committed
448 449 450 451 452 453 454 455
    EIGEN_STRONG_INLINE void conservativeResizeLike(const DenseBase<OtherDerived>& other)
    {
      internal::conservative_resize_like_impl<Derived,OtherDerived>::run(*this, other);
    }

    /** This is a special case of the templated operator=. Its purpose is to
      * prevent a default operator= from hiding the templated operator=.
      */
456
    EIGEN_DEVICE_FUNC
LM's avatar
LM committed
457 458 459 460 461 462 463
    EIGEN_STRONG_INLINE Derived& operator=(const PlainObjectBase& other)
    {
      return _set(other);
    }

    /** \sa MatrixBase::lazyAssign() */
    template<typename OtherDerived>
464
    EIGEN_DEVICE_FUNC
LM's avatar
LM committed
465 466 467 468 469 470 471
    EIGEN_STRONG_INLINE Derived& lazyAssign(const DenseBase<OtherDerived>& other)
    {
      _resize_to_match(other);
      return Base::lazyAssign(other.derived());
    }

    template<typename OtherDerived>
472
    EIGEN_DEVICE_FUNC
LM's avatar
LM committed
473 474 475 476 477 478
    EIGEN_STRONG_INLINE Derived& operator=(const ReturnByValue<OtherDerived>& func)
    {
      resize(func.rows(), func.cols());
      return Base::operator=(func);
    }

479 480 481 482 483
    // Prevent user from trying to instantiate PlainObjectBase objects
    // by making all its constructor protected. See bug 1074.
  protected:

    EIGEN_DEVICE_FUNC
Don Gagne's avatar
Don Gagne committed
484
    EIGEN_STRONG_INLINE PlainObjectBase() : m_storage()
LM's avatar
LM committed
485 486
    {
//       _check_template_params();
Don Gagne's avatar
Don Gagne committed
487
//       EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
LM's avatar
LM committed
488 489 490 491 492
    }

#ifndef EIGEN_PARSED_BY_DOXYGEN
    // FIXME is it still needed ?
    /** \internal */
493 494
    EIGEN_DEVICE_FUNC
    explicit PlainObjectBase(internal::constructor_without_unaligned_array_assert)
LM's avatar
LM committed
495 496
      : m_storage(internal::constructor_without_unaligned_array_assert())
    {
Don Gagne's avatar
Don Gagne committed
497
//       _check_template_params(); EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
LM's avatar
LM committed
498 499 500
    }
#endif

501 502 503
#if EIGEN_HAS_RVALUE_REFERENCES
    EIGEN_DEVICE_FUNC
    PlainObjectBase(PlainObjectBase&& other) EIGEN_NOEXCEPT
504 505 506 507
      : m_storage( std::move(other.m_storage) )
    {
    }

508 509
    EIGEN_DEVICE_FUNC
    PlainObjectBase& operator=(PlainObjectBase&& other) EIGEN_NOEXCEPT
510 511 512 513 514 515 516 517
    {
      using std::swap;
      swap(m_storage, other.m_storage);
      return *this;
    }
#endif

    /** Copy constructor */
518
    EIGEN_DEVICE_FUNC
519
    EIGEN_STRONG_INLINE PlainObjectBase(const PlainObjectBase& other)
520 521 522 523
      : Base(), m_storage(other.m_storage) { }
    EIGEN_DEVICE_FUNC
    EIGEN_STRONG_INLINE PlainObjectBase(Index size, Index rows, Index cols)
      : m_storage(size, rows, cols)
524
    {
525 526
//       _check_template_params();
//       EIGEN_INITIALIZE_COEFFS_IF_THAT_OPTION_IS_ENABLED
527 528
    }

529
    /** \sa PlainObjectBase::operator=(const EigenBase<OtherDerived>&) */
530
    template<typename OtherDerived>
531
    EIGEN_DEVICE_FUNC
532 533 534 535
    EIGEN_STRONG_INLINE PlainObjectBase(const DenseBase<OtherDerived> &other)
      : m_storage()
    {
      _check_template_params();
536 537
      resizeLike(other);
      _set_noalias(other);
538 539
    }

540 541 542 543 544
    /** \sa PlainObjectBase::operator=(const EigenBase<OtherDerived>&) */
    template<typename OtherDerived>
    EIGEN_DEVICE_FUNC
    EIGEN_STRONG_INLINE PlainObjectBase(const EigenBase<OtherDerived> &other)
      : m_storage()
LM's avatar
LM committed
545
    {
546 547 548 549 550 551 552 553 554 555 556 557 558
      _check_template_params();
      resizeLike(other);
      *this = other.derived();
    }
    /** \brief Copy constructor with in-place evaluation */
    template<typename OtherDerived>
    EIGEN_DEVICE_FUNC
    EIGEN_STRONG_INLINE PlainObjectBase(const ReturnByValue<OtherDerived>& other)
    {
      _check_template_params();
      // FIXME this does not automatically transpose vectors if necessary
      resize(other.rows(), other.cols());
      other.evalTo(this->derived());
LM's avatar
LM committed
559 560
    }

561 562 563 564
  public:

    /** \brief Copies the generic expression \a other into *this.
      * \copydetails DenseBase::operator=(const EigenBase<OtherDerived> &other)
LM's avatar
LM committed
565 566
      */
    template<typename OtherDerived>
567
    EIGEN_DEVICE_FUNC 
LM's avatar
LM committed
568 569 570 571 572 573 574 575 576 577 578 579
    EIGEN_STRONG_INLINE Derived& operator=(const EigenBase<OtherDerived> &other)
    {
      _resize_to_match(other);
      Base::operator=(other.derived());
      return this->derived();
    }

    /** \name Map
      * These are convenience functions returning Map objects. The Map() static functions return unaligned Map objects,
      * while the AlignedMap() functions return aligned Map objects and thus should be called only with 16-byte-aligned
      * \a data pointers.
      *
580 581 582 583
      * Here is an example using strides:
      * \include Matrix_Map_stride.cpp
      * Output: \verbinclude Matrix_Map_stride.out
      *
LM's avatar
LM committed
584 585 586
      * \see class Map
      */
    //@{
Don Gagne's avatar
Don Gagne committed
587
    static inline ConstMapType Map(const Scalar* data)
LM's avatar
LM committed
588
    { return ConstMapType(data); }
Don Gagne's avatar
Don Gagne committed
589
    static inline MapType Map(Scalar* data)
LM's avatar
LM committed
590
    { return MapType(data); }
Don Gagne's avatar
Don Gagne committed
591
    static inline ConstMapType Map(const Scalar* data, Index size)
LM's avatar
LM committed
592
    { return ConstMapType(data, size); }
Don Gagne's avatar
Don Gagne committed
593
    static inline MapType Map(Scalar* data, Index size)
LM's avatar
LM committed
594
    { return MapType(data, size); }
Don Gagne's avatar
Don Gagne committed
595
    static inline ConstMapType Map(const Scalar* data, Index rows, Index cols)
LM's avatar
LM committed
596
    { return ConstMapType(data, rows, cols); }
Don Gagne's avatar
Don Gagne committed
597
    static inline MapType Map(Scalar* data, Index rows, Index cols)
LM's avatar
LM committed
598 599
    { return MapType(data, rows, cols); }

Don Gagne's avatar
Don Gagne committed
600
    static inline ConstAlignedMapType MapAligned(const Scalar* data)
LM's avatar
LM committed
601
    { return ConstAlignedMapType(data); }
Don Gagne's avatar
Don Gagne committed
602
    static inline AlignedMapType MapAligned(Scalar* data)
LM's avatar
LM committed
603
    { return AlignedMapType(data); }
Don Gagne's avatar
Don Gagne committed
604
    static inline ConstAlignedMapType MapAligned(const Scalar* data, Index size)
LM's avatar
LM committed
605
    { return ConstAlignedMapType(data, size); }
Don Gagne's avatar
Don Gagne committed
606
    static inline AlignedMapType MapAligned(Scalar* data, Index size)
LM's avatar
LM committed
607
    { return AlignedMapType(data, size); }
Don Gagne's avatar
Don Gagne committed
608
    static inline ConstAlignedMapType MapAligned(const Scalar* data, Index rows, Index cols)
LM's avatar
LM committed
609
    { return ConstAlignedMapType(data, rows, cols); }
Don Gagne's avatar
Don Gagne committed
610
    static inline AlignedMapType MapAligned(Scalar* data, Index rows, Index cols)
LM's avatar
LM committed
611 612 613
    { return AlignedMapType(data, rows, cols); }

    template<int Outer, int Inner>
Don Gagne's avatar
Don Gagne committed
614
    static inline typename StridedConstMapType<Stride<Outer, Inner> >::type Map(const Scalar* data, const Stride<Outer, Inner>& stride)
LM's avatar
LM committed
615 616
    { return typename StridedConstMapType<Stride<Outer, Inner> >::type(data, stride); }
    template<int Outer, int Inner>
Don Gagne's avatar
Don Gagne committed
617
    static inline typename StridedMapType<Stride<Outer, Inner> >::type Map(Scalar* data, const Stride<Outer, Inner>& stride)
LM's avatar
LM committed
618 619
    { return typename StridedMapType<Stride<Outer, Inner> >::type(data, stride); }
    template<int Outer, int Inner>
Don Gagne's avatar
Don Gagne committed
620
    static inline typename StridedConstMapType<Stride<Outer, Inner> >::type Map(const Scalar* data, Index size, const Stride<Outer, Inner>& stride)
LM's avatar
LM committed
621 622
    { return typename StridedConstMapType<Stride<Outer, Inner> >::type(data, size, stride); }
    template<int Outer, int Inner>
Don Gagne's avatar
Don Gagne committed
623
    static inline typename StridedMapType<Stride<Outer, Inner> >::type Map(Scalar* data, Index size, const Stride<Outer, Inner>& stride)
LM's avatar
LM committed
624 625
    { return typename StridedMapType<Stride<Outer, Inner> >::type(data, size, stride); }
    template<int Outer, int Inner>
Don Gagne's avatar
Don Gagne committed
626
    static inline typename StridedConstMapType<Stride<Outer, Inner> >::type Map(const Scalar* data, Index rows, Index cols, const Stride<Outer, Inner>& stride)
LM's avatar
LM committed
627 628
    { return typename StridedConstMapType<Stride<Outer, Inner> >::type(data, rows, cols, stride); }
    template<int Outer, int Inner>
Don Gagne's avatar
Don Gagne committed
629
    static inline typename StridedMapType<Stride<Outer, Inner> >::type Map(Scalar* data, Index rows, Index cols, const Stride<Outer, Inner>& stride)
LM's avatar
LM committed
630 631 632
    { return typename StridedMapType<Stride<Outer, Inner> >::type(data, rows, cols, stride); }

    template<int Outer, int Inner>
Don Gagne's avatar
Don Gagne committed
633
    static inline typename StridedConstAlignedMapType<Stride<Outer, Inner> >::type MapAligned(const Scalar* data, const Stride<Outer, Inner>& stride)
LM's avatar
LM committed
634 635
    { return typename StridedConstAlignedMapType<Stride<Outer, Inner> >::type(data, stride); }
    template<int Outer, int Inner>
Don Gagne's avatar
Don Gagne committed
636
    static inline typename StridedAlignedMapType<Stride<Outer, Inner> >::type MapAligned(Scalar* data, const Stride<Outer, Inner>& stride)
LM's avatar
LM committed
637 638
    { return typename StridedAlignedMapType<Stride<Outer, Inner> >::type(data, stride); }
    template<int Outer, int Inner>
Don Gagne's avatar
Don Gagne committed
639
    static inline typename StridedConstAlignedMapType<Stride<Outer, Inner> >::type MapAligned(const Scalar* data, Index size, const Stride<Outer, Inner>& stride)
LM's avatar
LM committed
640 641
    { return typename StridedConstAlignedMapType<Stride<Outer, Inner> >::type(data, size, stride); }
    template<int Outer, int Inner>
Don Gagne's avatar
Don Gagne committed
642
    static inline typename StridedAlignedMapType<Stride<Outer, Inner> >::type MapAligned(Scalar* data, Index size, const Stride<Outer, Inner>& stride)
LM's avatar
LM committed
643 644
    { return typename StridedAlignedMapType<Stride<Outer, Inner> >::type(data, size, stride); }
    template<int Outer, int Inner>
Don Gagne's avatar
Don Gagne committed
645
    static inline typename StridedConstAlignedMapType<Stride<Outer, Inner> >::type MapAligned(const Scalar* data, Index rows, Index cols, const Stride<Outer, Inner>& stride)
LM's avatar
LM committed
646 647
    { return typename StridedConstAlignedMapType<Stride<Outer, Inner> >::type(data, rows, cols, stride); }
    template<int Outer, int Inner>
Don Gagne's avatar
Don Gagne committed
648
    static inline typename StridedAlignedMapType<Stride<Outer, Inner> >::type MapAligned(Scalar* data, Index rows, Index cols, const Stride<Outer, Inner>& stride)
LM's avatar
LM committed
649 650 651 652
    { return typename StridedAlignedMapType<Stride<Outer, Inner> >::type(data, rows, cols, stride); }
    //@}

    using Base::setConstant;
653 654
    EIGEN_DEVICE_FUNC Derived& setConstant(Index size, const Scalar& val);
    EIGEN_DEVICE_FUNC Derived& setConstant(Index rows, Index cols, const Scalar& val);
LM's avatar
LM committed
655 656

    using Base::setZero;
657 658
    EIGEN_DEVICE_FUNC Derived& setZero(Index size);
    EIGEN_DEVICE_FUNC Derived& setZero(Index rows, Index cols);
LM's avatar
LM committed
659 660

    using Base::setOnes;
661 662
    EIGEN_DEVICE_FUNC Derived& setOnes(Index size);
    EIGEN_DEVICE_FUNC Derived& setOnes(Index rows, Index cols);
LM's avatar
LM committed
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680

    using Base::setRandom;
    Derived& setRandom(Index size);
    Derived& setRandom(Index rows, Index cols);

    #ifdef EIGEN_PLAINOBJECTBASE_PLUGIN
    #include EIGEN_PLAINOBJECTBASE_PLUGIN
    #endif

  protected:
    /** \internal Resizes *this in preparation for assigning \a other to it.
      * Takes care of doing all the checking that's needed.
      *
      * Note that copying a row-vector into a vector (and conversely) is allowed.
      * The resizing, if any, is then done in the appropriate way so that row-vectors
      * remain row-vectors and vectors remain vectors.
      */
    template<typename OtherDerived>
681
    EIGEN_DEVICE_FUNC 
LM's avatar
LM committed
682 683 684 685 686 687
    EIGEN_STRONG_INLINE void _resize_to_match(const EigenBase<OtherDerived>& other)
    {
      #ifdef EIGEN_NO_AUTOMATIC_RESIZING
      eigen_assert((this->size()==0 || (IsVectorAtCompileTime ? (this->size() == other.size())
                 : (rows() == other.rows() && cols() == other.cols())))
        && "Size mismatch. Automatic resizing is disabled because EIGEN_NO_AUTOMATIC_RESIZING is defined");
Don Gagne's avatar
Don Gagne committed
688
      EIGEN_ONLY_USED_FOR_DEBUG(other);
LM's avatar
LM committed
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
      #else
      resizeLike(other);
      #endif
    }

    /**
      * \brief Copies the value of the expression \a other into \c *this with automatic resizing.
      *
      * *this might be resized to match the dimensions of \a other. If *this was a null matrix (not already initialized),
      * it will be initialized.
      *
      * Note that copying a row-vector into a vector (and conversely) is allowed.
      * The resizing, if any, is then done in the appropriate way so that row-vectors
      * remain row-vectors and vectors remain vectors.
      *
      * \sa operator=(const MatrixBase<OtherDerived>&), _set_noalias()
      *
      * \internal
      */
708 709
    // aliasing is dealt once in internall::call_assignment
    // so at this stage we have to assume aliasing... and resising has to be done later.
LM's avatar
LM committed
710
    template<typename OtherDerived>
711
    EIGEN_DEVICE_FUNC 
LM's avatar
LM committed
712 713
    EIGEN_STRONG_INLINE Derived& _set(const DenseBase<OtherDerived>& other)
    {
714
      internal::call_assignment(this->derived(), other.derived());
LM's avatar
LM committed
715 716 717 718 719 720 721 722 723
      return this->derived();
    }

    /** \internal Like _set() but additionally makes the assumption that no aliasing effect can happen (which
      * is the case when creating a new matrix) so one can enforce lazy evaluation.
      *
      * \sa operator=(const MatrixBase<OtherDerived>&), _set()
      */
    template<typename OtherDerived>
724
    EIGEN_DEVICE_FUNC 
LM's avatar
LM committed
725 726 727 728 729 730 731
    EIGEN_STRONG_INLINE Derived& _set_noalias(const DenseBase<OtherDerived>& other)
    {
      // I don't think we need this resize call since the lazyAssign will anyways resize
      // and lazyAssign will be called by the assign selector.
      //_resize_to_match(other);
      // the 'false' below means to enforce lazy evaluation. We don't use lazyAssign() because
      // it wouldn't allow to copy a row-vector into a column-vector.
732 733
      internal::call_assignment_no_alias(this->derived(), other.derived(), internal::assign_op<Scalar,typename OtherDerived::Scalar>());
      return this->derived();
LM's avatar
LM committed
734 735 736
    }

    template<typename T0, typename T1>
737 738
    EIGEN_DEVICE_FUNC
    EIGEN_STRONG_INLINE void _init2(Index rows, Index cols, typename internal::enable_if<Base::SizeAtCompileTime!=2,T0>::type* = 0)
LM's avatar
LM committed
739
    {
Don Gagne's avatar
Don Gagne committed
740 741 742
      EIGEN_STATIC_ASSERT(bool(NumTraits<T0>::IsInteger) &&
                          bool(NumTraits<T1>::IsInteger),
                          FLOATING_POINT_ARGUMENT_PASSED__INTEGER_WAS_EXPECTED)
743 744 745 746 747 748 749 750 751 752
      resize(rows,cols);
    }
    
    template<typename T0, typename T1>
    EIGEN_DEVICE_FUNC 
    EIGEN_STRONG_INLINE void _init2(const T0& val0, const T1& val1, typename internal::enable_if<Base::SizeAtCompileTime==2,T0>::type* = 0)
    {
      EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(PlainObjectBase, 2)
      m_storage.data()[0] = Scalar(val0);
      m_storage.data()[1] = Scalar(val1);
LM's avatar
LM committed
753
    }
754
    
LM's avatar
LM committed
755
    template<typename T0, typename T1>
756 757 758 759 760 761
    EIGEN_DEVICE_FUNC 
    EIGEN_STRONG_INLINE void _init2(const Index& val0, const Index& val1,
                                    typename internal::enable_if<    (!internal::is_same<Index,Scalar>::value)
                                                                  && (internal::is_same<T0,Index>::value)
                                                                  && (internal::is_same<T1,Index>::value)
                                                                  && Base::SizeAtCompileTime==2,T1>::type* = 0)
LM's avatar
LM committed
762 763
    {
      EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(PlainObjectBase, 2)
764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
      m_storage.data()[0] = Scalar(val0);
      m_storage.data()[1] = Scalar(val1);
    }

    // The argument is convertible to the Index type and we either have a non 1x1 Matrix, or a dynamic-sized Array,
    // then the argument is meant to be the size of the object.
    template<typename T>
    EIGEN_DEVICE_FUNC
    EIGEN_STRONG_INLINE void _init1(Index size, typename internal::enable_if<    (Base::SizeAtCompileTime!=1 || !internal::is_convertible<T, Scalar>::value)
                                                                              && ((!internal::is_same<typename internal::traits<Derived>::XprKind,ArrayXpr>::value || Base::SizeAtCompileTime==Dynamic)),T>::type* = 0)
    {
      // NOTE MSVC 2008 complains if we directly put bool(NumTraits<T>::IsInteger) as the EIGEN_STATIC_ASSERT argument.
      const bool is_integer = NumTraits<T>::IsInteger;
      EIGEN_UNUSED_VARIABLE(is_integer);
      EIGEN_STATIC_ASSERT(is_integer,
                          FLOATING_POINT_ARGUMENT_PASSED__INTEGER_WAS_EXPECTED)
      resize(size);
    }
    
    // We have a 1x1 matrix/array => the argument is interpreted as the value of the unique coefficient (case where scalar type can be implicitely converted)
    template<typename T>
    EIGEN_DEVICE_FUNC
    EIGEN_STRONG_INLINE void _init1(const Scalar& val0, typename internal::enable_if<Base::SizeAtCompileTime==1 && internal::is_convertible<T, Scalar>::value,T>::type* = 0)
    {
      EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(PlainObjectBase, 1)
Don Gagne's avatar
Don Gagne committed
789
      m_storage.data()[0] = val0;
790 791 792 793 794 795 796 797 798 799 800 801 802
    }
    
    // We have a 1x1 matrix/array => the argument is interpreted as the value of the unique coefficient (case where scalar type match the index type)
    template<typename T>
    EIGEN_DEVICE_FUNC
    EIGEN_STRONG_INLINE void _init1(const Index& val0,
                                    typename internal::enable_if<    (!internal::is_same<Index,Scalar>::value)
                                                                  && (internal::is_same<Index,T>::value)
                                                                  && Base::SizeAtCompileTime==1
                                                                  && internal::is_convertible<T, Scalar>::value,T*>::type* = 0)
    {
      EIGEN_STATIC_ASSERT_VECTOR_SPECIFIC_SIZE(PlainObjectBase, 1)
      m_storage.data()[0] = Scalar(val0);
LM's avatar
LM committed
803 804
    }

805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873
    // Initialize a fixed size matrix from a pointer to raw data
    template<typename T>
    EIGEN_DEVICE_FUNC
    EIGEN_STRONG_INLINE void _init1(const Scalar* data){
      this->_set_noalias(ConstMapType(data));
    }

    // Initialize an arbitrary matrix from a dense expression
    template<typename T, typename OtherDerived>
    EIGEN_DEVICE_FUNC
    EIGEN_STRONG_INLINE void _init1(const DenseBase<OtherDerived>& other){
      this->_set_noalias(other);
    }

    // Initialize an arbitrary matrix from an object convertible to the Derived type.
    template<typename T>
    EIGEN_DEVICE_FUNC
    EIGEN_STRONG_INLINE void _init1(const Derived& other){
      this->_set_noalias(other);
    }

    // Initialize an arbitrary matrix from a generic Eigen expression
    template<typename T, typename OtherDerived>
    EIGEN_DEVICE_FUNC
    EIGEN_STRONG_INLINE void _init1(const EigenBase<OtherDerived>& other){
      this->derived() = other;
    }

    template<typename T, typename OtherDerived>
    EIGEN_DEVICE_FUNC
    EIGEN_STRONG_INLINE void _init1(const ReturnByValue<OtherDerived>& other)
    {
      resize(other.rows(), other.cols());
      other.evalTo(this->derived());
    }

    template<typename T, typename OtherDerived, int ColsAtCompileTime>
    EIGEN_DEVICE_FUNC
    EIGEN_STRONG_INLINE void _init1(const RotationBase<OtherDerived,ColsAtCompileTime>& r)
    {
      this->derived() = r;
    }
    
    // For fixed-size Array<Scalar,...>
    template<typename T>
    EIGEN_DEVICE_FUNC
    EIGEN_STRONG_INLINE void _init1(const Scalar& val0,
                                    typename internal::enable_if<    Base::SizeAtCompileTime!=Dynamic
                                                                  && Base::SizeAtCompileTime!=1
                                                                  && internal::is_convertible<T, Scalar>::value
                                                                  && internal::is_same<typename internal::traits<Derived>::XprKind,ArrayXpr>::value,T>::type* = 0)
    {
      Base::setConstant(val0);
    }
    
    // For fixed-size Array<Index,...>
    template<typename T>
    EIGEN_DEVICE_FUNC
    EIGEN_STRONG_INLINE void _init1(const Index& val0,
                                    typename internal::enable_if<    (!internal::is_same<Index,Scalar>::value)
                                                                  && (internal::is_same<Index,T>::value)
                                                                  && Base::SizeAtCompileTime!=Dynamic
                                                                  && Base::SizeAtCompileTime!=1
                                                                  && internal::is_convertible<T, Scalar>::value
                                                                  && internal::is_same<typename internal::traits<Derived>::XprKind,ArrayXpr>::value,T*>::type* = 0)
    {
      Base::setConstant(val0);
    }
    
LM's avatar
LM committed
874 875 876
    template<typename MatrixTypeA, typename MatrixTypeB, bool SwapPointers>
    friend struct internal::matrix_swap_impl;

877 878 879 880 881 882
  public:
    
#ifndef EIGEN_PARSED_BY_DOXYGEN
    /** \internal
      * \brief Override DenseBase::swap() since for dynamic-sized matrices
      * of same type it is enough to swap the data pointers.
LM's avatar
LM committed
883 884
      */
    template<typename OtherDerived>
885 886
    EIGEN_DEVICE_FUNC
    void swap(DenseBase<OtherDerived> & other)
LM's avatar
LM committed
887 888
    {
      enum { SwapPointers = internal::is_same<Derived, OtherDerived>::value && Base::SizeAtCompileTime==Dynamic };
889
      internal::matrix_swap_impl<Derived, OtherDerived, bool(SwapPointers)>::run(this->derived(), other.derived());
LM's avatar
LM committed
890
    }
891 892 893 894 895 896 897 898 899 900
    
    /** \internal
      * \brief const version forwarded to DenseBase::swap
      */
    template<typename OtherDerived>
    EIGEN_DEVICE_FUNC
    void swap(DenseBase<OtherDerived> const & other)
    { Base::swap(other.derived()); }
    
    EIGEN_DEVICE_FUNC 
Don Gagne's avatar
Don Gagne committed
901
    static EIGEN_STRONG_INLINE void _check_template_params()
LM's avatar
LM committed
902 903 904 905 906 907 908 909 910 911 912 913 914
    {
      EIGEN_STATIC_ASSERT((EIGEN_IMPLIES(MaxRowsAtCompileTime==1 && MaxColsAtCompileTime!=1, (Options&RowMajor)==RowMajor)
                        && EIGEN_IMPLIES(MaxColsAtCompileTime==1 && MaxRowsAtCompileTime!=1, (Options&RowMajor)==0)
                        && ((RowsAtCompileTime == Dynamic) || (RowsAtCompileTime >= 0))
                        && ((ColsAtCompileTime == Dynamic) || (ColsAtCompileTime >= 0))
                        && ((MaxRowsAtCompileTime == Dynamic) || (MaxRowsAtCompileTime >= 0))
                        && ((MaxColsAtCompileTime == Dynamic) || (MaxColsAtCompileTime >= 0))
                        && (MaxRowsAtCompileTime == RowsAtCompileTime || RowsAtCompileTime==Dynamic)
                        && (MaxColsAtCompileTime == ColsAtCompileTime || ColsAtCompileTime==Dynamic)
                        && (Options & (DontAlign|RowMajor)) == Options),
        INVALID_MATRIX_TEMPLATE_PARAMETERS)
    }

915 916
    enum { IsPlainObjectBase = 1 };
#endif
LM's avatar
LM committed
917 918
};

919 920
namespace internal {

LM's avatar
LM committed
921
template <typename Derived, typename OtherDerived, bool IsVector>
922
struct conservative_resize_like_impl
LM's avatar
LM committed
923 924 925 926 927 928 929 930 931
{
  static void run(DenseBase<Derived>& _this, Index rows, Index cols)
  {
    if (_this.rows() == rows && _this.cols() == cols) return;
    EIGEN_STATIC_ASSERT_DYNAMIC_SIZE(Derived)

    if ( ( Derived::IsRowMajor && _this.cols() == cols) || // row-major and we change only the number of rows
         (!Derived::IsRowMajor && _this.rows() == rows) )  // column-major and we change only the number of columns
    {
Don Gagne's avatar
Don Gagne committed
932
      internal::check_rows_cols_for_overflow<Derived::MaxSizeAtCompileTime>::run(rows, cols);
LM's avatar
LM committed
933 934 935 936 937 938
      _this.derived().m_storage.conservativeResize(rows*cols,rows,cols);
    }
    else
    {
      // The storage order does not allow us to use reallocation.
      typename Derived::PlainObject tmp(rows,cols);
939 940
      const Index common_rows = numext::mini(rows, _this.rows());
      const Index common_cols = numext::mini(cols, _this.cols());
LM's avatar
LM committed
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
      tmp.block(0,0,common_rows,common_cols) = _this.block(0,0,common_rows,common_cols);
      _this.derived().swap(tmp);
    }
  }

  static void run(DenseBase<Derived>& _this, const DenseBase<OtherDerived>& other)
  {
    if (_this.rows() == other.rows() && _this.cols() == other.cols()) return;

    // Note: Here is space for improvement. Basically, for conservativeResize(Index,Index),
    // neither RowsAtCompileTime or ColsAtCompileTime must be Dynamic. If only one of the
    // dimensions is dynamic, one could use either conservativeResize(Index rows, NoChange_t) or
    // conservativeResize(NoChange_t, Index cols). For these methods new static asserts like
    // EIGEN_STATIC_ASSERT_DYNAMIC_ROWS and EIGEN_STATIC_ASSERT_DYNAMIC_COLS would be good.
    EIGEN_STATIC_ASSERT_DYNAMIC_SIZE(Derived)
    EIGEN_STATIC_ASSERT_DYNAMIC_SIZE(OtherDerived)

    if ( ( Derived::IsRowMajor && _this.cols() == other.cols()) || // row-major and we change only the number of rows
         (!Derived::IsRowMajor && _this.rows() == other.rows()) )  // column-major and we change only the number of columns
    {
      const Index new_rows = other.rows() - _this.rows();
      const Index new_cols = other.cols() - _this.cols();
      _this.derived().m_storage.conservativeResize(other.size(),other.rows(),other.cols());
      if (new_rows>0)
        _this.bottomRightCorner(new_rows, other.cols()) = other.bottomRows(new_rows);
      else if (new_cols>0)
        _this.bottomRightCorner(other.rows(), new_cols) = other.rightCols(new_cols);
    }
    else
    {
      // The storage order does not allow us to use reallocation.
      typename Derived::PlainObject tmp(other);
973 974
      const Index common_rows = numext::mini(tmp.rows(), _this.rows());
      const Index common_cols = numext::mini(tmp.cols(), _this.cols());
LM's avatar
LM committed
975 976 977 978 979 980
      tmp.block(0,0,common_rows,common_cols) = _this.block(0,0,common_rows,common_cols);
      _this.derived().swap(tmp);
    }
  }
};

981 982
// Here, the specialization for vectors inherits from the general matrix case
// to allow calling .conservativeResize(rows,cols) on vectors.
LM's avatar
LM committed
983 984
template <typename Derived, typename OtherDerived>
struct conservative_resize_like_impl<Derived,OtherDerived,true>
985
  : conservative_resize_like_impl<Derived,OtherDerived,false>
LM's avatar
LM committed
986
{
987 988
  using conservative_resize_like_impl<Derived,OtherDerived,false>::run;
  
LM's avatar
LM committed
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
  static void run(DenseBase<Derived>& _this, Index size)
  {
    const Index new_rows = Derived::RowsAtCompileTime==1 ? 1 : size;
    const Index new_cols = Derived::RowsAtCompileTime==1 ? size : 1;
    _this.derived().m_storage.conservativeResize(size,new_rows,new_cols);
  }

  static void run(DenseBase<Derived>& _this, const DenseBase<OtherDerived>& other)
  {
    if (_this.rows() == other.rows() && _this.cols() == other.cols()) return;

    const Index num_new_elements = other.size() - _this.size();

    const Index new_rows = Derived::RowsAtCompileTime==1 ? 1 : other.rows();
    const Index new_cols = Derived::RowsAtCompileTime==1 ? other.cols() : 1;
    _this.derived().m_storage.conservativeResize(other.size(),new_rows,new_cols);

    if (num_new_elements > 0)
      _this.tail(num_new_elements) = other.tail(num_new_elements);
  }
};

template<typename MatrixTypeA, typename MatrixTypeB, bool SwapPointers>
struct matrix_swap_impl
{
1014
  EIGEN_DEVICE_FUNC
LM's avatar
LM committed
1015 1016 1017 1018 1019 1020 1021 1022 1023
  static inline void run(MatrixTypeA& a, MatrixTypeB& b)
  {
    a.base().swap(b);
  }
};

template<typename MatrixTypeA, typename MatrixTypeB>
struct matrix_swap_impl<MatrixTypeA, MatrixTypeB, true>
{
1024
  EIGEN_DEVICE_FUNC
LM's avatar
LM committed
1025 1026 1027 1028 1029 1030 1031 1032
  static inline void run(MatrixTypeA& a, MatrixTypeB& b)
  {
    static_cast<typename MatrixTypeA::Base&>(a).m_storage.swap(static_cast<typename MatrixTypeB::Base&>(b).m_storage);
  }
};

} // end namespace internal

Don Gagne's avatar
Don Gagne committed
1033 1034
} // end namespace Eigen

LM's avatar
LM committed
1035
#endif // EIGEN_DENSESTORAGEBASE_H