CwiseUnaryView.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-2010 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_CWISE_UNARY_VIEW_H
00026 #define EIGEN_CWISE_UNARY_VIEW_H
00027 
00028 namespace Eigen {
00029 
00044 namespace internal {
00045 template<typename ViewOp, typename MatrixType>
00046 struct traits<CwiseUnaryView<ViewOp, MatrixType> >
00047  : traits<MatrixType>
00048 {
00049   typedef typename result_of<
00050                      ViewOp(typename traits<MatrixType>::Scalar)
00051                    >::type Scalar;
00052   typedef typename MatrixType::Nested MatrixTypeNested;
00053   typedef typename remove_all<MatrixTypeNested>::type _MatrixTypeNested;
00054   enum {
00055     Flags = (traits<_MatrixTypeNested>::Flags & (HereditaryBits | LvalueBit | LinearAccessBit | DirectAccessBit)),
00056     CoeffReadCost = traits<_MatrixTypeNested>::CoeffReadCost + functor_traits<ViewOp>::Cost,
00057     MatrixTypeInnerStride =  inner_stride_at_compile_time<MatrixType>::ret,
00058     // need to cast the sizeof's from size_t to int explicitly, otherwise:
00059     // "error: no integral type can represent all of the enumerator values
00060     InnerStrideAtCompileTime = MatrixTypeInnerStride == Dynamic
00061                              ? int(Dynamic)
00062                              : int(MatrixTypeInnerStride)
00063                                * int(sizeof(typename traits<MatrixType>::Scalar) / sizeof(Scalar)),
00064     OuterStrideAtCompileTime = outer_stride_at_compile_time<MatrixType>::ret
00065   };
00066 };
00067 }
00068 
00069 template<typename ViewOp, typename MatrixType, typename StorageKind>
00070 class CwiseUnaryViewImpl;
00071 
00072 template<typename ViewOp, typename MatrixType>
00073 class CwiseUnaryView : internal::no_assignment_operator,
00074   public CwiseUnaryViewImpl<ViewOp, MatrixType, typename internal::traits<MatrixType>::StorageKind>
00075 {
00076   public:
00077 
00078     typedef typename CwiseUnaryViewImpl<ViewOp, MatrixType,typename internal::traits<MatrixType>::StorageKind>::Base Base;
00079     EIGEN_GENERIC_PUBLIC_INTERFACE(CwiseUnaryView)
00080 
00081     inline CwiseUnaryView(const MatrixType& mat, const ViewOp& func = ViewOp())
00082       : m_matrix(mat), m_functor(func) {}
00083 
00084     EIGEN_INHERIT_ASSIGNMENT_OPERATORS(CwiseUnaryView)
00085 
00086     EIGEN_STRONG_INLINE Index rows() const { return m_matrix.rows(); }
00087     EIGEN_STRONG_INLINE Index cols() const { return m_matrix.cols(); }
00088 
00090     const ViewOp& functor() const { return m_functor; }
00091 
00093     const typename internal::remove_all<typename MatrixType::Nested>::type&
00094     nestedExpression() const { return m_matrix; }
00095 
00097     typename internal::remove_all<typename MatrixType::Nested>::type&
00098     nestedExpression() { return m_matrix.const_cast_derived(); }
00099 
00100   protected:
00101     // FIXME changed from MatrixType::Nested because of a weird compilation error with sun CC
00102     typename internal::nested<MatrixType>::type m_matrix;
00103     ViewOp m_functor;
00104 };
00105 
00106 template<typename ViewOp, typename MatrixType>
00107 class CwiseUnaryViewImpl<ViewOp,MatrixType,Dense>
00108   : public internal::dense_xpr_base< CwiseUnaryView<ViewOp, MatrixType> >::type
00109 {
00110   public:
00111 
00112     typedef CwiseUnaryView<ViewOp, MatrixType> Derived;
00113     typedef typename internal::dense_xpr_base< CwiseUnaryView<ViewOp, MatrixType> >::type Base;
00114 
00115     EIGEN_DENSE_PUBLIC_INTERFACE(Derived)
00116 
00117     inline Index innerStride() const
00118     {
00119       return derived().nestedExpression().innerStride() * sizeof(typename internal::traits<MatrixType>::Scalar) / sizeof(Scalar);
00120     }
00121 
00122     inline Index outerStride() const
00123     {
00124       return derived().nestedExpression().outerStride();
00125     }
00126 
00127     EIGEN_STRONG_INLINE CoeffReturnType coeff(Index row, Index col) const
00128     {
00129       return derived().functor()(derived().nestedExpression().coeff(row, col));
00130     }
00131 
00132     EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
00133     {
00134       return derived().functor()(derived().nestedExpression().coeff(index));
00135     }
00136 
00137     EIGEN_STRONG_INLINE Scalar& coeffRef(Index row, Index col)
00138     {
00139       return derived().functor()(const_cast_derived().nestedExpression().coeffRef(row, col));
00140     }
00141 
00142     EIGEN_STRONG_INLINE Scalar& coeffRef(Index index)
00143     {
00144       return derived().functor()(const_cast_derived().nestedExpression().coeffRef(index));
00145     }
00146 };
00147 
00148 } // end namespace Eigen
00149 
00150 #endif // EIGEN_CWISE_UNARY_VIEW_H