[136] | 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 |
|
---|
| 10 | #ifndef EIGEN_ARRAYBASE_H
|
---|
| 11 | #define EIGEN_ARRAYBASE_H
|
---|
| 12 |
|
---|
| 13 | namespace Eigen {
|
---|
| 14 |
|
---|
| 15 | template<typename ExpressionType> class MatrixWrapper;
|
---|
| 16 |
|
---|
| 17 | /** \class ArrayBase
|
---|
| 18 | * \ingroup Core_Module
|
---|
| 19 | *
|
---|
| 20 | * \brief Base class for all 1D and 2D array, and related expressions
|
---|
| 21 | *
|
---|
| 22 | * An array is similar to a dense vector or matrix. While matrices are mathematical
|
---|
| 23 | * objects with well defined linear algebra operators, an array is just a collection
|
---|
| 24 | * of scalar values arranged in a one or two dimensionnal fashion. As the main consequence,
|
---|
| 25 | * all operations applied to an array are performed coefficient wise. Furthermore,
|
---|
| 26 | * arrays support scalar math functions of the c++ standard library (e.g., std::sin(x)), and convenient
|
---|
| 27 | * constructors allowing to easily write generic code working for both scalar values
|
---|
| 28 | * and arrays.
|
---|
| 29 | *
|
---|
| 30 | * This class is the base that is inherited by all array expression types.
|
---|
| 31 | *
|
---|
| 32 | * \tparam Derived is the derived type, e.g., an array or an expression type.
|
---|
| 33 | *
|
---|
| 34 | * This class can be extended with the help of the plugin mechanism described on the page
|
---|
| 35 | * \ref TopicCustomizingEigen by defining the preprocessor symbol \c EIGEN_ARRAYBASE_PLUGIN.
|
---|
| 36 | *
|
---|
| 37 | * \sa class MatrixBase, \ref TopicClassHierarchy
|
---|
| 38 | */
|
---|
| 39 | template<typename Derived> class ArrayBase
|
---|
| 40 | : public DenseBase<Derived>
|
---|
| 41 | {
|
---|
| 42 | public:
|
---|
| 43 | #ifndef EIGEN_PARSED_BY_DOXYGEN
|
---|
| 44 | /** The base class for a given storage type. */
|
---|
| 45 | typedef ArrayBase StorageBaseType;
|
---|
| 46 |
|
---|
| 47 | typedef ArrayBase Eigen_BaseClassForSpecializationOfGlobalMathFuncImpl;
|
---|
| 48 |
|
---|
| 49 | typedef typename internal::traits<Derived>::StorageKind StorageKind;
|
---|
| 50 | typedef typename internal::traits<Derived>::Index Index;
|
---|
| 51 | typedef typename internal::traits<Derived>::Scalar Scalar;
|
---|
| 52 | typedef typename internal::packet_traits<Scalar>::type PacketScalar;
|
---|
| 53 | typedef typename NumTraits<Scalar>::Real RealScalar;
|
---|
| 54 |
|
---|
| 55 | typedef DenseBase<Derived> Base;
|
---|
| 56 | using Base::operator*;
|
---|
| 57 | using Base::RowsAtCompileTime;
|
---|
| 58 | using Base::ColsAtCompileTime;
|
---|
| 59 | using Base::SizeAtCompileTime;
|
---|
| 60 | using Base::MaxRowsAtCompileTime;
|
---|
| 61 | using Base::MaxColsAtCompileTime;
|
---|
| 62 | using Base::MaxSizeAtCompileTime;
|
---|
| 63 | using Base::IsVectorAtCompileTime;
|
---|
| 64 | using Base::Flags;
|
---|
| 65 | using Base::CoeffReadCost;
|
---|
| 66 |
|
---|
| 67 | using Base::derived;
|
---|
| 68 | using Base::const_cast_derived;
|
---|
| 69 | using Base::rows;
|
---|
| 70 | using Base::cols;
|
---|
| 71 | using Base::size;
|
---|
| 72 | using Base::coeff;
|
---|
| 73 | using Base::coeffRef;
|
---|
| 74 | using Base::lazyAssign;
|
---|
| 75 | using Base::operator=;
|
---|
| 76 | using Base::operator+=;
|
---|
| 77 | using Base::operator-=;
|
---|
| 78 | using Base::operator*=;
|
---|
| 79 | using Base::operator/=;
|
---|
| 80 |
|
---|
| 81 | typedef typename Base::CoeffReturnType CoeffReturnType;
|
---|
| 82 |
|
---|
| 83 | #endif // not EIGEN_PARSED_BY_DOXYGEN
|
---|
| 84 |
|
---|
| 85 | #ifndef EIGEN_PARSED_BY_DOXYGEN
|
---|
| 86 | /** \internal the plain matrix type corresponding to this expression. Note that is not necessarily
|
---|
| 87 | * exactly the return type of eval(): in the case of plain matrices, the return type of eval() is a const
|
---|
| 88 | * reference to a matrix, not a matrix! It is however guaranteed that the return type of eval() is either
|
---|
| 89 | * PlainObject or const PlainObject&.
|
---|
| 90 | */
|
---|
| 91 | typedef Array<typename internal::traits<Derived>::Scalar,
|
---|
| 92 | internal::traits<Derived>::RowsAtCompileTime,
|
---|
| 93 | internal::traits<Derived>::ColsAtCompileTime,
|
---|
| 94 | AutoAlign | (internal::traits<Derived>::Flags&RowMajorBit ? RowMajor : ColMajor),
|
---|
| 95 | internal::traits<Derived>::MaxRowsAtCompileTime,
|
---|
| 96 | internal::traits<Derived>::MaxColsAtCompileTime
|
---|
| 97 | > PlainObject;
|
---|
| 98 |
|
---|
| 99 |
|
---|
| 100 | /** \internal Represents a matrix with all coefficients equal to one another*/
|
---|
| 101 | typedef CwiseNullaryOp<internal::scalar_constant_op<Scalar>,Derived> ConstantReturnType;
|
---|
| 102 | #endif // not EIGEN_PARSED_BY_DOXYGEN
|
---|
| 103 |
|
---|
| 104 | #define EIGEN_CURRENT_STORAGE_BASE_CLASS Eigen::ArrayBase
|
---|
| 105 | # include "../plugins/CommonCwiseUnaryOps.h"
|
---|
| 106 | # include "../plugins/MatrixCwiseUnaryOps.h"
|
---|
| 107 | # include "../plugins/ArrayCwiseUnaryOps.h"
|
---|
| 108 | # include "../plugins/CommonCwiseBinaryOps.h"
|
---|
| 109 | # include "../plugins/MatrixCwiseBinaryOps.h"
|
---|
| 110 | # include "../plugins/ArrayCwiseBinaryOps.h"
|
---|
| 111 | # ifdef EIGEN_ARRAYBASE_PLUGIN
|
---|
| 112 | # include EIGEN_ARRAYBASE_PLUGIN
|
---|
| 113 | # endif
|
---|
| 114 | #undef EIGEN_CURRENT_STORAGE_BASE_CLASS
|
---|
| 115 |
|
---|
| 116 | /** Special case of the template operator=, in order to prevent the compiler
|
---|
| 117 | * from generating a default operator= (issue hit with g++ 4.1)
|
---|
| 118 | */
|
---|
| 119 | Derived& operator=(const ArrayBase& other)
|
---|
| 120 | {
|
---|
| 121 | return internal::assign_selector<Derived,Derived>::run(derived(), other.derived());
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | Derived& operator+=(const Scalar& scalar)
|
---|
| 125 | { return *this = derived() + scalar; }
|
---|
| 126 | Derived& operator-=(const Scalar& scalar)
|
---|
| 127 | { return *this = derived() - scalar; }
|
---|
| 128 |
|
---|
| 129 | template<typename OtherDerived>
|
---|
| 130 | Derived& operator+=(const ArrayBase<OtherDerived>& other);
|
---|
| 131 | template<typename OtherDerived>
|
---|
| 132 | Derived& operator-=(const ArrayBase<OtherDerived>& other);
|
---|
| 133 |
|
---|
| 134 | template<typename OtherDerived>
|
---|
| 135 | Derived& operator*=(const ArrayBase<OtherDerived>& other);
|
---|
| 136 |
|
---|
| 137 | template<typename OtherDerived>
|
---|
| 138 | Derived& operator/=(const ArrayBase<OtherDerived>& other);
|
---|
| 139 |
|
---|
| 140 | public:
|
---|
| 141 | ArrayBase<Derived>& array() { return *this; }
|
---|
| 142 | const ArrayBase<Derived>& array() const { return *this; }
|
---|
| 143 |
|
---|
| 144 | /** \returns an \link Eigen::MatrixBase Matrix \endlink expression of this array
|
---|
| 145 | * \sa MatrixBase::array() */
|
---|
| 146 | MatrixWrapper<Derived> matrix() { return derived(); }
|
---|
| 147 | const MatrixWrapper<const Derived> matrix() const { return derived(); }
|
---|
| 148 |
|
---|
| 149 | // template<typename Dest>
|
---|
| 150 | // inline void evalTo(Dest& dst) const { dst = matrix(); }
|
---|
| 151 |
|
---|
| 152 | protected:
|
---|
| 153 | ArrayBase() : Base() {}
|
---|
| 154 |
|
---|
| 155 | private:
|
---|
| 156 | explicit ArrayBase(Index);
|
---|
| 157 | ArrayBase(Index,Index);
|
---|
| 158 | template<typename OtherDerived> explicit ArrayBase(const ArrayBase<OtherDerived>&);
|
---|
| 159 | protected:
|
---|
| 160 | // mixing arrays and matrices is not legal
|
---|
| 161 | template<typename OtherDerived> Derived& operator+=(const MatrixBase<OtherDerived>& )
|
---|
| 162 | {EIGEN_STATIC_ASSERT(std::ptrdiff_t(sizeof(typename OtherDerived::Scalar))==-1,YOU_CANNOT_MIX_ARRAYS_AND_MATRICES); return *this;}
|
---|
| 163 | // mixing arrays and matrices is not legal
|
---|
| 164 | template<typename OtherDerived> Derived& operator-=(const MatrixBase<OtherDerived>& )
|
---|
| 165 | {EIGEN_STATIC_ASSERT(std::ptrdiff_t(sizeof(typename OtherDerived::Scalar))==-1,YOU_CANNOT_MIX_ARRAYS_AND_MATRICES); return *this;}
|
---|
| 166 | };
|
---|
| 167 |
|
---|
| 168 | /** replaces \c *this by \c *this - \a other.
|
---|
| 169 | *
|
---|
| 170 | * \returns a reference to \c *this
|
---|
| 171 | */
|
---|
| 172 | template<typename Derived>
|
---|
| 173 | template<typename OtherDerived>
|
---|
| 174 | EIGEN_STRONG_INLINE Derived &
|
---|
| 175 | ArrayBase<Derived>::operator-=(const ArrayBase<OtherDerived> &other)
|
---|
| 176 | {
|
---|
| 177 | SelfCwiseBinaryOp<internal::scalar_difference_op<Scalar>, Derived, OtherDerived> tmp(derived());
|
---|
| 178 | tmp = other.derived();
|
---|
| 179 | return derived();
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | /** replaces \c *this by \c *this + \a other.
|
---|
| 183 | *
|
---|
| 184 | * \returns a reference to \c *this
|
---|
| 185 | */
|
---|
| 186 | template<typename Derived>
|
---|
| 187 | template<typename OtherDerived>
|
---|
| 188 | EIGEN_STRONG_INLINE Derived &
|
---|
| 189 | ArrayBase<Derived>::operator+=(const ArrayBase<OtherDerived>& other)
|
---|
| 190 | {
|
---|
| 191 | SelfCwiseBinaryOp<internal::scalar_sum_op<Scalar>, Derived, OtherDerived> tmp(derived());
|
---|
| 192 | tmp = other.derived();
|
---|
| 193 | return derived();
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | /** replaces \c *this by \c *this * \a other coefficient wise.
|
---|
| 197 | *
|
---|
| 198 | * \returns a reference to \c *this
|
---|
| 199 | */
|
---|
| 200 | template<typename Derived>
|
---|
| 201 | template<typename OtherDerived>
|
---|
| 202 | EIGEN_STRONG_INLINE Derived &
|
---|
| 203 | ArrayBase<Derived>::operator*=(const ArrayBase<OtherDerived>& other)
|
---|
| 204 | {
|
---|
| 205 | SelfCwiseBinaryOp<internal::scalar_product_op<Scalar>, Derived, OtherDerived> tmp(derived());
|
---|
| 206 | tmp = other.derived();
|
---|
| 207 | return derived();
|
---|
| 208 | }
|
---|
| 209 |
|
---|
| 210 | /** replaces \c *this by \c *this / \a other coefficient wise.
|
---|
| 211 | *
|
---|
| 212 | * \returns a reference to \c *this
|
---|
| 213 | */
|
---|
| 214 | template<typename Derived>
|
---|
| 215 | template<typename OtherDerived>
|
---|
| 216 | EIGEN_STRONG_INLINE Derived &
|
---|
| 217 | ArrayBase<Derived>::operator/=(const ArrayBase<OtherDerived>& other)
|
---|
| 218 | {
|
---|
| 219 | SelfCwiseBinaryOp<internal::scalar_quotient_op<Scalar>, Derived, OtherDerived> tmp(derived());
|
---|
| 220 | tmp = other.derived();
|
---|
| 221 | return derived();
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | } // end namespace Eigen
|
---|
| 225 |
|
---|
| 226 | #endif // EIGEN_ARRAYBASE_H
|
---|