ArrayBase.h
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
13namespace Eigen {
14
15template<typename ExpressionType> class MatrixWrapper;
16
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 */
39template<typename Derived> class ArrayBase
40 : public DenseBase<Derived>
41{
42 public:
43#ifndef EIGEN_PARSED_BY_DOXYGEN
45 typedef ArrayBase StorageBaseType;
46
47 typedef ArrayBase Eigen_BaseClassForSpecializationOfGlobalMathFuncImpl;
48
49 using internal::special_scalar_op_base<Derived,typename internal::traits<Derived>::Scalar,
52 typedef typename internal::traits<Derived>::StorageKind StorageKind;
53 typedef typename internal::traits<Derived>::Index Index;
54 typedef typename internal::traits<Derived>::Scalar Scalar;
55 typedef typename internal::packet_traits<Scalar>::type PacketScalar;
56 typedef typename NumTraits<Scalar>::Real RealScalar;
57
58 typedef DenseBase<Derived> Base;
59 using Base::RowsAtCompileTime;
60 using Base::ColsAtCompileTime;
61 using Base::SizeAtCompileTime;
62 using Base::MaxRowsAtCompileTime;
63 using Base::MaxColsAtCompileTime;
64 using Base::MaxSizeAtCompileTime;
65 using Base::IsVectorAtCompileTime;
66 using Base::Flags;
67 using Base::CoeffReadCost;
68
69 using Base::derived;
70 using Base::const_cast_derived;
71 using Base::rows;
72 using Base::cols;
73 using Base::size;
74 using Base::coeff;
75 using Base::coeffRef;
76 using Base::lazyAssign;
77 using Base::operator=;
78 using Base::operator+=;
79 using Base::operator-=;
80 using Base::operator*=;
81 using Base::operator/=;
82
83 typedef typename Base::CoeffReturnType CoeffReturnType;
84
85#endif // not EIGEN_PARSED_BY_DOXYGEN
86
87#ifndef EIGEN_PARSED_BY_DOXYGEN
91 * PlainObject or const PlainObject&.
92 */
94 internal::traits<Derived>::RowsAtCompileTime,
95 internal::traits<Derived>::ColsAtCompileTime,
96 AutoAlign | (internal::traits<Derived>::Flags&RowMajorBit ? RowMajor : ColMajor),
97 internal::traits<Derived>::MaxRowsAtCompileTime,
98 internal::traits<Derived>::MaxColsAtCompileTime
99 > PlainObject;
100
101
102 /** \internal Represents a matrix with all coefficients equal to one another*/
103 typedef CwiseNullaryOp<internal::scalar_constant_op<Scalar>,Derived> ConstantReturnType;
104#endif // not EIGEN_PARSED_BY_DOXYGEN
105
106#define EIGEN_CURRENT_STORAGE_BASE_CLASS Eigen::ArrayBase
107# include "../plugins/CommonCwiseUnaryOps.h"
108# include "../plugins/MatrixCwiseUnaryOps.h"
109# include "../plugins/ArrayCwiseUnaryOps.h"
110# include "../plugins/CommonCwiseBinaryOps.h"
111# include "../plugins/MatrixCwiseBinaryOps.h"
112# include "../plugins/ArrayCwiseBinaryOps.h"
113# ifdef EIGEN_ARRAYBASE_PLUGIN
114# include EIGEN_ARRAYBASE_PLUGIN
115# endif
116#undef EIGEN_CURRENT_STORAGE_BASE_CLASS
121 Derived& operator=(const ArrayBase& other)
122 {
123 return internal::assign_selector<Derived,Derived>::run(derived(), other.derived());
124 }
125
126 Derived& operator+=(const Scalar& scalar)
127 { return *this = derived() + scalar; }
128 Derived& operator-=(const Scalar& scalar)
129 { return *this = derived() - scalar; }
131 template<typename OtherDerived>
132 Derived& operator+=(const ArrayBase<OtherDerived>& other);
133 template<typename OtherDerived>
134 Derived& operator-=(const ArrayBase<OtherDerived>& other);
135
136 template<typename OtherDerived>
137 Derived& operator*=(const ArrayBase<OtherDerived>& other);
138
139 template<typename OtherDerived>
140 Derived& operator/=(const ArrayBase<OtherDerived>& other);
141
142 public:
143 ArrayBase<Derived>& array() { return *this; }
144 const ArrayBase<Derived>& array() const { return *this; }
145
148 MatrixWrapper<Derived> matrix() { return derived(); }
149 const MatrixWrapper<const Derived> matrix() const { return derived(); }
150
151// template<typename Dest>
152// inline void evalTo(Dest& dst) const { dst = matrix(); }
153
154 protected:
155 ArrayBase() : Base() {}
156
157 private:
158 explicit ArrayBase(Index);
159 ArrayBase(Index,Index);
160 template<typename OtherDerived> explicit ArrayBase(const ArrayBase<OtherDerived>&);
161 protected:
162 // mixing arrays and matrices is not legal
163 template<typename OtherDerived> Derived& operator+=(const MatrixBase<OtherDerived>& )
164 {EIGEN_STATIC_ASSERT(std::ptrdiff_t(sizeof(typename OtherDerived::Scalar))==-1,YOU_CANNOT_MIX_ARRAYS_AND_MATRICES); return *this;}
165 // mixing arrays and matrices is not legal
166 template<typename OtherDerived> Derived& operator-=(const MatrixBase<OtherDerived>& )
167 {EIGEN_STATIC_ASSERT(std::ptrdiff_t(sizeof(typename OtherDerived::Scalar))==-1,YOU_CANNOT_MIX_ARRAYS_AND_MATRICES); return *this;}
168};
169
172 * \returns a reference to \c *this
173 */
174template<typename Derived>
175template<typename OtherDerived>
176EIGEN_STRONG_INLINE Derived &
177ArrayBase<Derived>::operator-=(const ArrayBase<OtherDerived> &other)
178{
179 SelfCwiseBinaryOp<internal::scalar_difference_op<Scalar>, Derived, OtherDerived> tmp(derived());
180 tmp = other.derived();
181 return derived();
182}
183
186 * \returns a reference to \c *this
187 */
188template<typename Derived>
189template<typename OtherDerived>
190EIGEN_STRONG_INLINE Derived &
191ArrayBase<Derived>::operator+=(const ArrayBase<OtherDerived>& other)
192{
193 SelfCwiseBinaryOp<internal::scalar_sum_op<Scalar>, Derived, OtherDerived> tmp(derived());
194 tmp = other.derived();
195 return derived();
196}
197
202template<typename Derived>
203template<typename OtherDerived>
204EIGEN_STRONG_INLINE Derived &
205ArrayBase<Derived>::operator*=(const ArrayBase<OtherDerived>& other)
206{
207 SelfCwiseBinaryOp<internal::scalar_product_op<Scalar>, Derived, OtherDerived> tmp(derived());
208 tmp = other.derived();
209 return derived();
210}
211
216template<typename Derived>
217template<typename OtherDerived>
218EIGEN_STRONG_INLINE Derived &
219ArrayBase<Derived>::operator/=(const ArrayBase<OtherDerived>& other)
220{
221 SelfCwiseBinaryOp<internal::scalar_quotient_op<Scalar>, Derived, OtherDerived> tmp(derived());
222 tmp = other.derived();
223 return derived();
224}
225
226} // end namespace Eigen
227
228#endif // EIGEN_ARRAYBASE_H
Derived & operator*=(const ArrayBase< OtherDerived > &other)
Definition ArrayBase.h:205
Derived & operator-=(const ArrayBase< OtherDerived > &other)
Definition ArrayBase.h:177
Derived & operator=(const ArrayBase &other)
Definition ArrayBase.h:121
MatrixWrapper< Derived > matrix()
Definition ArrayBase.h:148
Derived & operator+=(const ArrayBase< OtherDerived > &other)
Definition ArrayBase.h:191
Derived & operator/=(const ArrayBase< OtherDerived > &other)
Definition ArrayBase.h:219
General-purpose arrays with easy API for coefficient-wise operations.
Definition Array.h:44
Generic expression of a matrix where all coefficients are defined by a functor.
Definition CwiseNullaryOp.h:51
DenseBase()
Definition DenseBase.h:513
internal::traits< Array< _Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols > >::Index Index
Definition DenseBase.h:51
Base class for all dense matrices, vectors, and expressions.
Definition MatrixBase.h:50
Expression of an array as a mathematical vector or matrix.
Definition ArrayWrapper.h:157
@ RowMajor
Definition Constants.h:259
@ ColMajor
Definition Constants.h:257
const unsigned int RowMajorBit
Definition Constants.h:48
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition NumTraits.h:89