ArrayBase.h
Go to the documentation of this file.
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2009 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 //
00006 // Eigen is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 3 of the License, or (at your option) any later version.
00010 //
00011 // Alternatively, you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License as
00013 // published by the Free Software Foundation; either version 2 of
00014 // the License, or (at your option) any later version.
00015 //
00016 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
00017 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00018 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
00019 // GNU General Public License for more details.
00020 //
00021 // You should have received a copy of the GNU Lesser General Public
00022 // License and a copy of the GNU General Public License along with
00023 // Eigen. If not, see <http://www.gnu.org/licenses/>.
00024 
00025 #ifndef EIGEN_ARRAYBASE_H
00026 #define EIGEN_ARRAYBASE_H
00027 
00028 namespace Eigen { 
00029 
00030 template<typename ExpressionType> class MatrixWrapper;
00031 
00054 template<typename Derived> class ArrayBase
00055   : public DenseBase<Derived>
00056 {
00057   public:
00058 #ifndef EIGEN_PARSED_BY_DOXYGEN
00059 
00060     typedef ArrayBase StorageBaseType;
00061 
00062     typedef ArrayBase Eigen_BaseClassForSpecializationOfGlobalMathFuncImpl;
00063 
00064     using internal::special_scalar_op_base<Derived,typename internal::traits<Derived>::Scalar,
00065                 typename NumTraits<typename internal::traits<Derived>::Scalar>::Real>::operator*;
00066 
00067     typedef typename internal::traits<Derived>::StorageKind StorageKind;
00068     typedef typename internal::traits<Derived>::Index Index;
00069     typedef typename internal::traits<Derived>::Scalar Scalar;
00070     typedef typename internal::packet_traits<Scalar>::type PacketScalar;
00071     typedef typename NumTraits<Scalar>::Real RealScalar;
00072 
00073     typedef DenseBase<Derived> Base;
00074     using Base::RowsAtCompileTime;
00075     using Base::ColsAtCompileTime;
00076     using Base::SizeAtCompileTime;
00077     using Base::MaxRowsAtCompileTime;
00078     using Base::MaxColsAtCompileTime;
00079     using Base::MaxSizeAtCompileTime;
00080     using Base::IsVectorAtCompileTime;
00081     using Base::Flags;
00082     using Base::CoeffReadCost;
00083 
00084     using Base::derived;
00085     using Base::const_cast_derived;
00086     using Base::rows;
00087     using Base::cols;
00088     using Base::size;
00089     using Base::coeff;
00090     using Base::coeffRef;
00091     using Base::lazyAssign;
00092     using Base::operator=;
00093     using Base::operator+=;
00094     using Base::operator-=;
00095     using Base::operator*=;
00096     using Base::operator/=;
00097 
00098     typedef typename Base::CoeffReturnType CoeffReturnType;
00099 
00100 #endif // not EIGEN_PARSED_BY_DOXYGEN
00101 
00102 #ifndef EIGEN_PARSED_BY_DOXYGEN
00103 
00108     typedef Array<typename internal::traits<Derived>::Scalar,
00109                 internal::traits<Derived>::RowsAtCompileTime,
00110                 internal::traits<Derived>::ColsAtCompileTime,
00111                 AutoAlign | (internal::traits<Derived>::Flags&RowMajorBit ? RowMajor : ColMajor),
00112                 internal::traits<Derived>::MaxRowsAtCompileTime,
00113                 internal::traits<Derived>::MaxColsAtCompileTime
00114           > PlainObject;
00115 
00116 
00118     typedef CwiseNullaryOp<internal::scalar_constant_op<Scalar>,Derived> ConstantReturnType;
00119 #endif // not EIGEN_PARSED_BY_DOXYGEN
00120 
00121 #define EIGEN_CURRENT_STORAGE_BASE_CLASS Eigen::ArrayBase
00122 #   include "../plugins/CommonCwiseUnaryOps.h"
00123 #   include "../plugins/MatrixCwiseUnaryOps.h"
00124 #   include "../plugins/ArrayCwiseUnaryOps.h"
00125 #   include "../plugins/CommonCwiseBinaryOps.h"
00126 #   include "../plugins/MatrixCwiseBinaryOps.h"
00127 #   include "../plugins/ArrayCwiseBinaryOps.h"
00128 #   ifdef EIGEN_ARRAYBASE_PLUGIN
00129 #     include EIGEN_ARRAYBASE_PLUGIN
00130 #   endif
00131 #undef EIGEN_CURRENT_STORAGE_BASE_CLASS
00132 
00136     Derived& operator=(const ArrayBase& other)
00137     {
00138       return internal::assign_selector<Derived,Derived>::run(derived(), other.derived());
00139     }
00140 
00141     Derived& operator+=(const Scalar& scalar)
00142     { return *this = derived() + scalar; }
00143     Derived& operator-=(const Scalar& scalar)
00144     { return *this = derived() - scalar; }
00145 
00146     template<typename OtherDerived>
00147     Derived& operator+=(const ArrayBase<OtherDerived>& other);
00148     template<typename OtherDerived>
00149     Derived& operator-=(const ArrayBase<OtherDerived>& other);
00150 
00151     template<typename OtherDerived>
00152     Derived& operator*=(const ArrayBase<OtherDerived>& other);
00153 
00154     template<typename OtherDerived>
00155     Derived& operator/=(const ArrayBase<OtherDerived>& other);
00156 
00157   public:
00158     ArrayBase<Derived>& array() { return *this; }
00159     const ArrayBase<Derived>& array() const { return *this; }
00160 
00163     MatrixWrapper<Derived> matrix() { return derived(); }
00164     const MatrixWrapper<const Derived> matrix() const { return derived(); }
00165 
00166 //     template<typename Dest>
00167 //     inline void evalTo(Dest& dst) const { dst = matrix(); }
00168 
00169   protected:
00170     ArrayBase() : Base() {}
00171 
00172   private:
00173     explicit ArrayBase(Index);
00174     ArrayBase(Index,Index);
00175     template<typename OtherDerived> explicit ArrayBase(const ArrayBase<OtherDerived>&);
00176   protected:
00177     // mixing arrays and matrices is not legal
00178     template<typename OtherDerived> Derived& operator+=(const MatrixBase<OtherDerived>& )
00179     {EIGEN_STATIC_ASSERT(std::ptrdiff_t(sizeof(typename OtherDerived::Scalar))==-1,YOU_CANNOT_MIX_ARRAYS_AND_MATRICES); return *this;}
00180     // mixing arrays and matrices is not legal
00181     template<typename OtherDerived> Derived& operator-=(const MatrixBase<OtherDerived>& )
00182     {EIGEN_STATIC_ASSERT(std::ptrdiff_t(sizeof(typename OtherDerived::Scalar))==-1,YOU_CANNOT_MIX_ARRAYS_AND_MATRICES); return *this;}
00183 };
00184 
00189 template<typename Derived>
00190 template<typename OtherDerived>
00191 EIGEN_STRONG_INLINE Derived &
00192 ArrayBase<Derived>::operator-=(const ArrayBase<OtherDerived> &other)
00193 {
00194   SelfCwiseBinaryOp<internal::scalar_difference_op<Scalar>, Derived, OtherDerived> tmp(derived());
00195   tmp = other.derived();
00196   return derived();
00197 }
00198 
00203 template<typename Derived>
00204 template<typename OtherDerived>
00205 EIGEN_STRONG_INLINE Derived &
00206 ArrayBase<Derived>::operator+=(const ArrayBase<OtherDerived>& other)
00207 {
00208   SelfCwiseBinaryOp<internal::scalar_sum_op<Scalar>, Derived, OtherDerived> tmp(derived());
00209   tmp = other.derived();
00210   return derived();
00211 }
00212 
00217 template<typename Derived>
00218 template<typename OtherDerived>
00219 EIGEN_STRONG_INLINE Derived &
00220 ArrayBase<Derived>::operator*=(const ArrayBase<OtherDerived>& other)
00221 {
00222   SelfCwiseBinaryOp<internal::scalar_product_op<Scalar>, Derived, OtherDerived> tmp(derived());
00223   tmp = other.derived();
00224   return derived();
00225 }
00226 
00231 template<typename Derived>
00232 template<typename OtherDerived>
00233 EIGEN_STRONG_INLINE Derived &
00234 ArrayBase<Derived>::operator/=(const ArrayBase<OtherDerived>& other)
00235 {
00236   SelfCwiseBinaryOp<internal::scalar_quotient_op<Scalar>, Derived, OtherDerived> tmp(derived());
00237   tmp = other.derived();
00238   return derived();
00239 }
00240 
00241 } // end namespace Eigen
00242 
00243 #endif // EIGEN_ARRAYBASE_H