EigenBase.h 5.49 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) 2009 Benoit Jacob <jacob.benoit.1@gmail.com>
// Copyright (C) 2009 Gael Guennebaud <gael.guennebaud@inria.fr>
//
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_EIGENBASE_H
#define EIGEN_EIGENBASE_H

Don Gagne's avatar
Don Gagne committed
14
namespace Eigen {
LM's avatar
LM committed
15

16 17 18 19
/** \class EigenBase
  * \ingroup Core_Module
  * 
  * Common base class for all classes T such that MatrixBase has an operator=(T) and a constructor MatrixBase(T).
LM's avatar
LM committed
20 21 22 23 24 25 26
  *
  * In other words, an EigenBase object is an object that can be copied into a MatrixBase.
  *
  * Besides MatrixBase-derived classes, this also includes special matrix classes such as diagonal matrices, etc.
  *
  * Notice that this class is trivial, it is only used to disambiguate overloaded functions.
  *
27
  * \sa \blank \ref TopicClassHierarchy
LM's avatar
LM committed
28 29 30 31
  */
template<typename Derived> struct EigenBase
{
//   typedef typename internal::plain_matrix_type<Derived>::type PlainObject;
32 33 34 35 36 37 38 39 40
  
  /** \brief The interface type of indices
    * \details To change this, \c \#define the preprocessor symbol \c EIGEN_DEFAULT_DENSE_INDEX_TYPE.
    * \deprecated Since Eigen 3.3, its usage is deprecated. Use Eigen::Index instead.
    * \sa StorageIndex, \ref TopicPreprocessorDirectives.
    */
  typedef Eigen::Index Index;

  // FIXME is it needed?
LM's avatar
LM committed
41 42 43
  typedef typename internal::traits<Derived>::StorageKind StorageKind;

  /** \returns a reference to the derived object */
44
  EIGEN_DEVICE_FUNC
LM's avatar
LM committed
45 46
  Derived& derived() { return *static_cast<Derived*>(this); }
  /** \returns a const reference to the derived object */
47
  EIGEN_DEVICE_FUNC
LM's avatar
LM committed
48 49
  const Derived& derived() const { return *static_cast<const Derived*>(this); }

50
  EIGEN_DEVICE_FUNC
LM's avatar
LM committed
51 52
  inline Derived& const_cast_derived() const
  { return *static_cast<Derived*>(const_cast<EigenBase*>(this)); }
53
  EIGEN_DEVICE_FUNC
LM's avatar
LM committed
54 55 56 57
  inline const Derived& const_derived() const
  { return *static_cast<const Derived*>(this); }

  /** \returns the number of rows. \sa cols(), RowsAtCompileTime */
58
  EIGEN_DEVICE_FUNC
LM's avatar
LM committed
59 60
  inline Index rows() const { return derived().rows(); }
  /** \returns the number of columns. \sa rows(), ColsAtCompileTime*/
61
  EIGEN_DEVICE_FUNC
LM's avatar
LM committed
62 63 64
  inline Index cols() const { return derived().cols(); }
  /** \returns the number of coefficients, which is rows()*cols().
    * \sa rows(), cols(), SizeAtCompileTime. */
65
  EIGEN_DEVICE_FUNC
LM's avatar
LM committed
66 67 68
  inline Index size() const { return rows() * cols(); }

  /** \internal Don't use it, but do the equivalent: \code dst = *this; \endcode */
69 70 71
  template<typename Dest>
  EIGEN_DEVICE_FUNC
  inline void evalTo(Dest& dst) const
LM's avatar
LM committed
72 73 74
  { derived().evalTo(dst); }

  /** \internal Don't use it, but do the equivalent: \code dst += *this; \endcode */
75 76 77
  template<typename Dest>
  EIGEN_DEVICE_FUNC
  inline void addTo(Dest& dst) const
LM's avatar
LM committed
78 79 80 81 82 83 84 85 86
  {
    // This is the default implementation,
    // derived class can reimplement it in a more optimized way.
    typename Dest::PlainObject res(rows(),cols());
    evalTo(res);
    dst += res;
  }

  /** \internal Don't use it, but do the equivalent: \code dst -= *this; \endcode */
87 88 89
  template<typename Dest>
  EIGEN_DEVICE_FUNC
  inline void subTo(Dest& dst) const
LM's avatar
LM committed
90 91 92 93 94 95 96 97 98
  {
    // This is the default implementation,
    // derived class can reimplement it in a more optimized way.
    typename Dest::PlainObject res(rows(),cols());
    evalTo(res);
    dst -= res;
  }

  /** \internal Don't use it, but do the equivalent: \code dst.applyOnTheRight(*this); \endcode */
99 100
  template<typename Dest>
  EIGEN_DEVICE_FUNC inline void applyThisOnTheRight(Dest& dst) const
LM's avatar
LM committed
101 102 103 104 105 106 107
  {
    // This is the default implementation,
    // derived class can reimplement it in a more optimized way.
    dst = dst * this->derived();
  }

  /** \internal Don't use it, but do the equivalent: \code dst.applyOnTheLeft(*this); \endcode */
108 109
  template<typename Dest>
  EIGEN_DEVICE_FUNC inline void applyThisOnTheLeft(Dest& dst) const
LM's avatar
LM committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
  {
    // This is the default implementation,
    // derived class can reimplement it in a more optimized way.
    dst = this->derived() * dst;
  }

};

/***************************************************************************
* Implementation of matrix base methods
***************************************************************************/

/** \brief Copies the generic expression \a other into *this.
  *
  * \details The expression must provide a (templated) evalTo(Derived& dst) const
  * function which does the actual job. In practice, this allows any user to write
  * its own special matrix without having to modify MatrixBase
  *
  * \returns a reference to *this.
  */
template<typename Derived>
template<typename OtherDerived>
132
EIGEN_DEVICE_FUNC
LM's avatar
LM committed
133 134
Derived& DenseBase<Derived>::operator=(const EigenBase<OtherDerived> &other)
{
135
  call_assignment(derived(), other.derived());
LM's avatar
LM committed
136 137 138 139 140
  return derived();
}

template<typename Derived>
template<typename OtherDerived>
141
EIGEN_DEVICE_FUNC
LM's avatar
LM committed
142 143
Derived& DenseBase<Derived>::operator+=(const EigenBase<OtherDerived> &other)
{
144
  call_assignment(derived(), other.derived(), internal::add_assign_op<Scalar,typename OtherDerived::Scalar>());
LM's avatar
LM committed
145 146 147 148 149
  return derived();
}

template<typename Derived>
template<typename OtherDerived>
150
EIGEN_DEVICE_FUNC
LM's avatar
LM committed
151 152
Derived& DenseBase<Derived>::operator-=(const EigenBase<OtherDerived> &other)
{
153
  call_assignment(derived(), other.derived(), internal::sub_assign_op<Scalar,typename OtherDerived::Scalar>());
LM's avatar
LM committed
154 155 156
  return derived();
}

Don Gagne's avatar
Don Gagne committed
157 158
} // end namespace Eigen

LM's avatar
LM committed
159
#endif // EIGEN_EIGENBASE_H