CwiseUnaryView.h
00001 // This file is part of Eigen, a lightweight C++ template library
00002 // for linear algebra.
00003 //
00004 // Copyright (C) 2009-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
00005 //
00006 // This Source Code Form is subject to the terms of the Mozilla
00007 // Public License v. 2.0. If a copy of the MPL was not distributed
00008 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
00009 
00010 #ifndef EIGEN_CWISE_UNARY_VIEW_H
00011 #define EIGEN_CWISE_UNARY_VIEW_H
00012 
00013 namespace Eigen {
00014 
00029 namespace internal {
00030 template<typename ViewOp, typename MatrixType>
00031 struct traits<CwiseUnaryView<ViewOp, MatrixType> >
00032  : traits<MatrixType>
00033 {
00034   typedef typename result_of<
00035                      ViewOp(typename traits<MatrixType>::Scalar)
00036                    >::type Scalar;
00037   typedef typename MatrixType::Nested MatrixTypeNested;
00038   typedef typename remove_all<MatrixTypeNested>::type _MatrixTypeNested;
00039   enum {
00040     Flags = (traits<_MatrixTypeNested>::Flags & (HereditaryBits | LvalueBit | LinearAccessBit | DirectAccessBit)),
00041     CoeffReadCost = traits<_MatrixTypeNested>::CoeffReadCost + functor_traits<ViewOp>::Cost,
00042     MatrixTypeInnerStride =  inner_stride_at_compile_time<MatrixType>::ret,
00043     // need to cast the sizeof's from size_t to int explicitly, otherwise:
00044     // "error: no integral type can represent all of the enumerator values
00045     InnerStrideAtCompileTime = MatrixTypeInnerStride == Dynamic
00046                              ? int(Dynamic)
00047                              : int(MatrixTypeInnerStride) * int(sizeof(typename traits<MatrixType>::Scalar) / sizeof(Scalar)),
00048     OuterStrideAtCompileTime = outer_stride_at_compile_time<MatrixType>::ret == Dynamic
00049                              ? int(Dynamic)
00050                              : outer_stride_at_compile_time<MatrixType>::ret * int(sizeof(typename traits<MatrixType>::Scalar) / sizeof(Scalar))
00051   };
00052 };
00053 }
00054 
00055 template<typename ViewOp, typename MatrixType, typename StorageKind>
00056 class CwiseUnaryViewImpl;
00057 
00058 template<typename ViewOp, typename MatrixType>
00059 class CwiseUnaryView : internal::no_assignment_operator,
00060   public CwiseUnaryViewImpl<ViewOp, MatrixType, typename internal::traits<MatrixType>::StorageKind>
00061 {
00062   public:
00063 
00064     typedef typename CwiseUnaryViewImpl<ViewOp, MatrixType,typename internal::traits<MatrixType>::StorageKind>::Base Base;
00065     EIGEN_GENERIC_PUBLIC_INTERFACE(CwiseUnaryView)
00066 
00067     inline CwiseUnaryView(const MatrixType& mat, const ViewOp& func = ViewOp())
00068       : m_matrix(mat), m_functor(func) {}
00069 
00070     EIGEN_INHERIT_ASSIGNMENT_OPERATORS(CwiseUnaryView)
00071 
00072     EIGEN_STRONG_INLINE Index rows() const { return m_matrix.rows(); }
00073     EIGEN_STRONG_INLINE Index cols() const { return m_matrix.cols(); }
00074 
00076     const ViewOp& functor() const { return m_functor; }
00077 
00079     const typename internal::remove_all<typename MatrixType::Nested>::type&
00080     nestedExpression() const { return m_matrix; }
00081 
00083     typename internal::remove_all<typename MatrixType::Nested>::type&
00084     nestedExpression() { return m_matrix.const_cast_derived(); }
00085 
00086   protected:
00087     // FIXME changed from MatrixType::Nested because of a weird compilation error with sun CC
00088     typename internal::nested<MatrixType>::type m_matrix;
00089     ViewOp m_functor;
00090 };
00091 
00092 template<typename ViewOp, typename MatrixType>
00093 class CwiseUnaryViewImpl<ViewOp,MatrixType,Dense>
00094   : public internal::dense_xpr_base< CwiseUnaryView<ViewOp, MatrixType> >::type
00095 {
00096   public:
00097 
00098     typedef CwiseUnaryView<ViewOp, MatrixType> Derived;
00099     typedef typename internal::dense_xpr_base< CwiseUnaryView<ViewOp, MatrixType> >::type Base;
00100 
00101     EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
00102 
00103     inline Index innerStride() const
00104     {
00105       return derived().nestedExpression().innerStride() * sizeof(typename internal::traits<MatrixType>::Scalar) / sizeof(Scalar);
00106     }
00107 
00108     inline Index outerStride() const
00109     {
00110       return derived().nestedExpression().outerStride() * sizeof(typename internal::traits<MatrixType>::Scalar) / sizeof(Scalar);
00111     }
00112 
00113     EIGEN_STRONG_INLINE CoeffReturnType coeff(Index row, Index col) const
00114     {
00115       return derived().functor()(derived().nestedExpression().coeff(row, col));
00116     }
00117 
00118     EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
00119     {
00120       return derived().functor()(derived().nestedExpression().coeff(index));
00121     }
00122 
00123     EIGEN_STRONG_INLINE Scalar& coeffRef(Index row, Index col)
00124     {
00125       return derived().functor()(const_cast_derived().nestedExpression().coeffRef(row, col));
00126     }
00127 
00128     EIGEN_STRONG_INLINE Scalar& coeffRef(Index index)
00129     {
00130       return derived().functor()(const_cast_derived().nestedExpression().coeffRef(index));
00131     }
00132 };
00133 
00134 } // end namespace Eigen
00135 
00136 #endif // EIGEN_CWISE_UNARY_VIEW_H