CwiseUnaryView.h
Go to the documentation of this file.
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2009-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // Eigen is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 3 of the License, or (at your option) any later version.
10 //
11 // Alternatively, you can redistribute it and/or
12 // modify it under the terms of the GNU General Public License as
13 // published by the Free Software Foundation; either version 2 of
14 // the License, or (at your option) any later version.
15 //
16 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU Lesser General Public
22 // License and a copy of the GNU General Public License along with
23 // Eigen. If not, see <http://www.gnu.org/licenses/>.
24 
25 #ifndef EIGEN_CWISE_UNARY_VIEW_H
26 #define EIGEN_CWISE_UNARY_VIEW_H
27 
28 namespace Eigen {
29 
44 namespace internal {
45 template<typename ViewOp, typename MatrixType>
46 struct traits<CwiseUnaryView<ViewOp, MatrixType> >
47  : traits<MatrixType>
48 {
49  typedef typename result_of<
50  ViewOp(typename traits<MatrixType>::Scalar)
51  >::type Scalar;
52  typedef typename MatrixType::Nested MatrixTypeNested;
53  typedef typename remove_all<MatrixTypeNested>::type _MatrixTypeNested;
54  enum {
56  CoeffReadCost = traits<_MatrixTypeNested>::CoeffReadCost + functor_traits<ViewOp>::Cost,
57  MatrixTypeInnerStride = inner_stride_at_compile_time<MatrixType>::ret,
58  // need to cast the sizeof's from size_t to int explicitly, otherwise:
59  // "error: no integral type can represent all of the enumerator values
60  InnerStrideAtCompileTime = MatrixTypeInnerStride == Dynamic
61  ? int(Dynamic)
62  : int(MatrixTypeInnerStride)
63  * int(sizeof(typename traits<MatrixType>::Scalar) / sizeof(Scalar)),
64  OuterStrideAtCompileTime = outer_stride_at_compile_time<MatrixType>::ret
65  };
66 };
67 }
68 
69 template<typename ViewOp, typename MatrixType, typename StorageKind>
70 class CwiseUnaryViewImpl;
71 
72 template<typename ViewOp, typename MatrixType>
73 class CwiseUnaryView : internal::no_assignment_operator,
74  public CwiseUnaryViewImpl<ViewOp, MatrixType, typename internal::traits<MatrixType>::StorageKind>
75 {
76  public:
77 
80 
81  inline CwiseUnaryView(const MatrixType& mat, const ViewOp& func = ViewOp())
82  : m_matrix(mat), m_functor(func) {}
83 
85 
86  EIGEN_STRONG_INLINE Index rows() const { return m_matrix.rows(); }
87  EIGEN_STRONG_INLINE Index cols() const { return m_matrix.cols(); }
88 
90  const ViewOp& functor() const { return m_functor; }
91 
93  const typename internal::remove_all<typename MatrixType::Nested>::type&
94  nestedExpression() const { return m_matrix; }
95 
97  typename internal::remove_all<typename MatrixType::Nested>::type&
98  nestedExpression() { return m_matrix.const_cast_derived(); }
99 
100  protected:
101  // FIXME changed from MatrixType::Nested because of a weird compilation error with sun CC
102  typename internal::nested<MatrixType>::type m_matrix;
103  ViewOp m_functor;
104 };
105 
106 template<typename ViewOp, typename MatrixType>
107 class CwiseUnaryViewImpl<ViewOp,MatrixType,Dense>
108  : public internal::dense_xpr_base< CwiseUnaryView<ViewOp, MatrixType> >::type
109 {
110  public:
111 
113  typedef typename internal::dense_xpr_base< CwiseUnaryView<ViewOp, MatrixType> >::type Base;
114 
116 
117  inline Index innerStride() const
118  {
119  return derived().nestedExpression().innerStride() * sizeof(typename internal::traits<MatrixType>::Scalar) / sizeof(Scalar);
120  }
121 
122  inline Index outerStride() const
123  {
124  return derived().nestedExpression().outerStride();
125  }
126 
127  EIGEN_STRONG_INLINE CoeffReturnType coeff(Index row, Index col) const
128  {
129  return derived().functor()(derived().nestedExpression().coeff(row, col));
130  }
131 
132  EIGEN_STRONG_INLINE CoeffReturnType coeff(Index index) const
133  {
134  return derived().functor()(derived().nestedExpression().coeff(index));
135  }
136 
137  EIGEN_STRONG_INLINE Scalar& coeffRef(Index row, Index col)
138  {
139  return derived().functor()(const_cast_derived().nestedExpression().coeffRef(row, col));
140  }
141 
142  EIGEN_STRONG_INLINE Scalar& coeffRef(Index index)
143  {
144  return derived().functor()(const_cast_derived().nestedExpression().coeffRef(index));
145  }
146 };
147 
148 } // end namespace Eigen
149 
150 #endif // EIGEN_CWISE_UNARY_VIEW_H