ArrayBase.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2009 Gael Guennebaud <gael.guennebaud@inria.fr>
  5. //
  6. // This Source Code Form is subject to the terms of the Mozilla
  7. // Public License v. 2.0. If a copy of the MPL was not distributed
  8. // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. #ifndef EIGEN_ARRAYBASE_H
  10. #define EIGEN_ARRAYBASE_H
  11. namespace Eigen {
  12. template<typename ExpressionType> class MatrixWrapper;
  13. /** \class ArrayBase
  14. * \ingroup Core_Module
  15. *
  16. * \brief Base class for all 1D and 2D array, and related expressions
  17. *
  18. * An array is similar to a dense vector or matrix. While matrices are mathematical
  19. * objects with well defined linear algebra operators, an array is just a collection
  20. * of scalar values arranged in a one or two dimensionnal fashion. As the main consequence,
  21. * all operations applied to an array are performed coefficient wise. Furthermore,
  22. * arrays support scalar math functions of the c++ standard library (e.g., std::sin(x)), and convenient
  23. * constructors allowing to easily write generic code working for both scalar values
  24. * and arrays.
  25. *
  26. * This class is the base that is inherited by all array expression types.
  27. *
  28. * \tparam Derived is the derived type, e.g., an array or an expression type.
  29. *
  30. * This class can be extended with the help of the plugin mechanism described on the page
  31. * \ref TopicCustomizing_Plugins by defining the preprocessor symbol \c EIGEN_ARRAYBASE_PLUGIN.
  32. *
  33. * \sa class MatrixBase, \ref TopicClassHierarchy
  34. */
  35. template<typename Derived> class ArrayBase
  36. : public DenseBase<Derived>
  37. {
  38. public:
  39. #ifndef EIGEN_PARSED_BY_DOXYGEN
  40. /** The base class for a given storage type. */
  41. typedef ArrayBase StorageBaseType;
  42. typedef ArrayBase Eigen_BaseClassForSpecializationOfGlobalMathFuncImpl;
  43. typedef typename internal::traits<Derived>::StorageKind StorageKind;
  44. typedef typename internal::traits<Derived>::Scalar Scalar;
  45. typedef typename internal::packet_traits<Scalar>::type PacketScalar;
  46. typedef typename NumTraits<Scalar>::Real RealScalar;
  47. typedef DenseBase<Derived> Base;
  48. using Base::RowsAtCompileTime;
  49. using Base::ColsAtCompileTime;
  50. using Base::SizeAtCompileTime;
  51. using Base::MaxRowsAtCompileTime;
  52. using Base::MaxColsAtCompileTime;
  53. using Base::MaxSizeAtCompileTime;
  54. using Base::IsVectorAtCompileTime;
  55. using Base::Flags;
  56. using Base::derived;
  57. using Base::const_cast_derived;
  58. using Base::rows;
  59. using Base::cols;
  60. using Base::size;
  61. using Base::coeff;
  62. using Base::coeffRef;
  63. using Base::lazyAssign;
  64. using Base::operator=;
  65. using Base::operator+=;
  66. using Base::operator-=;
  67. using Base::operator*=;
  68. using Base::operator/=;
  69. typedef typename Base::CoeffReturnType CoeffReturnType;
  70. #endif // not EIGEN_PARSED_BY_DOXYGEN
  71. #ifndef EIGEN_PARSED_BY_DOXYGEN
  72. typedef typename Base::PlainObject PlainObject;
  73. /** \internal Represents a matrix with all coefficients equal to one another*/
  74. typedef CwiseNullaryOp<internal::scalar_constant_op<Scalar>,PlainObject> ConstantReturnType;
  75. #endif // not EIGEN_PARSED_BY_DOXYGEN
  76. #define EIGEN_CURRENT_STORAGE_BASE_CLASS Eigen::ArrayBase
  77. #define EIGEN_DOC_UNARY_ADDONS(X,Y)
  78. # include "../plugins/CommonCwiseUnaryOps.h"
  79. # include "../plugins/MatrixCwiseUnaryOps.h"
  80. # include "../plugins/ArrayCwiseUnaryOps.h"
  81. # include "../plugins/CommonCwiseBinaryOps.h"
  82. # include "../plugins/MatrixCwiseBinaryOps.h"
  83. # include "../plugins/ArrayCwiseBinaryOps.h"
  84. # ifdef EIGEN_ARRAYBASE_PLUGIN
  85. # include EIGEN_ARRAYBASE_PLUGIN
  86. # endif
  87. #undef EIGEN_CURRENT_STORAGE_BASE_CLASS
  88. #undef EIGEN_DOC_UNARY_ADDONS
  89. /** Special case of the template operator=, in order to prevent the compiler
  90. * from generating a default operator= (issue hit with g++ 4.1)
  91. */
  92. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  93. Derived& operator=(const ArrayBase& other)
  94. {
  95. internal::call_assignment(derived(), other.derived());
  96. return derived();
  97. }
  98. /** Set all the entries to \a value.
  99. * \sa DenseBase::setConstant(), DenseBase::fill() */
  100. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  101. Derived& operator=(const Scalar &value)
  102. { Base::setConstant(value); return derived(); }
  103. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  104. Derived& operator+=(const Scalar& scalar);
  105. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  106. Derived& operator-=(const Scalar& scalar);
  107. template<typename OtherDerived>
  108. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  109. Derived& operator+=(const ArrayBase<OtherDerived>& other);
  110. template<typename OtherDerived>
  111. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  112. Derived& operator-=(const ArrayBase<OtherDerived>& other);
  113. template<typename OtherDerived>
  114. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  115. Derived& operator*=(const ArrayBase<OtherDerived>& other);
  116. template<typename OtherDerived>
  117. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
  118. Derived& operator/=(const ArrayBase<OtherDerived>& other);
  119. public:
  120. EIGEN_DEVICE_FUNC
  121. ArrayBase<Derived>& array() { return *this; }
  122. EIGEN_DEVICE_FUNC
  123. const ArrayBase<Derived>& array() const { return *this; }
  124. /** \returns an \link Eigen::MatrixBase Matrix \endlink expression of this array
  125. * \sa MatrixBase::array() */
  126. EIGEN_DEVICE_FUNC
  127. MatrixWrapper<Derived> matrix() { return MatrixWrapper<Derived>(derived()); }
  128. EIGEN_DEVICE_FUNC
  129. const MatrixWrapper<const Derived> matrix() const { return MatrixWrapper<const Derived>(derived()); }
  130. // template<typename Dest>
  131. // inline void evalTo(Dest& dst) const { dst = matrix(); }
  132. protected:
  133. EIGEN_DEFAULT_COPY_CONSTRUCTOR(ArrayBase)
  134. EIGEN_DEFAULT_EMPTY_CONSTRUCTOR_AND_DESTRUCTOR(ArrayBase)
  135. private:
  136. explicit ArrayBase(Index);
  137. ArrayBase(Index,Index);
  138. template<typename OtherDerived> explicit ArrayBase(const ArrayBase<OtherDerived>&);
  139. protected:
  140. // mixing arrays and matrices is not legal
  141. template<typename OtherDerived> Derived& operator+=(const MatrixBase<OtherDerived>& )
  142. {EIGEN_STATIC_ASSERT(std::ptrdiff_t(sizeof(typename OtherDerived::Scalar))==-1,YOU_CANNOT_MIX_ARRAYS_AND_MATRICES); return *this;}
  143. // mixing arrays and matrices is not legal
  144. template<typename OtherDerived> Derived& operator-=(const MatrixBase<OtherDerived>& )
  145. {EIGEN_STATIC_ASSERT(std::ptrdiff_t(sizeof(typename OtherDerived::Scalar))==-1,YOU_CANNOT_MIX_ARRAYS_AND_MATRICES); return *this;}
  146. };
  147. /** replaces \c *this by \c *this - \a other.
  148. *
  149. * \returns a reference to \c *this
  150. */
  151. template<typename Derived>
  152. template<typename OtherDerived>
  153. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived &
  154. ArrayBase<Derived>::operator-=(const ArrayBase<OtherDerived> &other)
  155. {
  156. call_assignment(derived(), other.derived(), internal::sub_assign_op<Scalar,typename OtherDerived::Scalar>());
  157. return derived();
  158. }
  159. /** replaces \c *this by \c *this + \a other.
  160. *
  161. * \returns a reference to \c *this
  162. */
  163. template<typename Derived>
  164. template<typename OtherDerived>
  165. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived &
  166. ArrayBase<Derived>::operator+=(const ArrayBase<OtherDerived>& other)
  167. {
  168. call_assignment(derived(), other.derived(), internal::add_assign_op<Scalar,typename OtherDerived::Scalar>());
  169. return derived();
  170. }
  171. /** replaces \c *this by \c *this * \a other coefficient wise.
  172. *
  173. * \returns a reference to \c *this
  174. */
  175. template<typename Derived>
  176. template<typename OtherDerived>
  177. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived &
  178. ArrayBase<Derived>::operator*=(const ArrayBase<OtherDerived>& other)
  179. {
  180. call_assignment(derived(), other.derived(), internal::mul_assign_op<Scalar,typename OtherDerived::Scalar>());
  181. return derived();
  182. }
  183. /** replaces \c *this by \c *this / \a other coefficient wise.
  184. *
  185. * \returns a reference to \c *this
  186. */
  187. template<typename Derived>
  188. template<typename OtherDerived>
  189. EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Derived &
  190. ArrayBase<Derived>::operator/=(const ArrayBase<OtherDerived>& other)
  191. {
  192. call_assignment(derived(), other.derived(), internal::div_assign_op<Scalar,typename OtherDerived::Scalar>());
  193. return derived();
  194. }
  195. } // end namespace Eigen
  196. #endif // EIGEN_ARRAYBASE_H