CwiseUnaryOp.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) 2008-2010 Gael Guennebaud <gael.guennebaud@inria.fr>
5 // Copyright (C) 2006-2008 Benoit Jacob <jacob.benoit.1@gmail.com>
6 //
7 // Eigen is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU Lesser General Public
9 // License as published by the Free Software Foundation; either
10 // version 3 of the License, or (at your option) any later version.
11 //
12 // Alternatively, you can redistribute it and/or
13 // modify it under the terms of the GNU General Public License as
14 // published by the Free Software Foundation; either version 2 of
15 // the License, or (at your option) any later version.
16 //
17 // Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 // FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
20 // GNU General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public
23 // License and a copy of the GNU General Public License along with
24 // Eigen. If not, see <http://www.gnu.org/licenses/>.
25 
26 #ifndef EIGEN_CWISE_UNARY_OP_H
27 #define EIGEN_CWISE_UNARY_OP_H
28 
29 namespace Eigen {
30 
51 namespace internal {
52 template<typename UnaryOp, typename XprType>
53 struct traits<CwiseUnaryOp<UnaryOp, XprType> >
54  : traits<XprType>
55 {
56  typedef typename result_of<
57  UnaryOp(typename XprType::Scalar)
58  >::type Scalar;
59  typedef typename XprType::Nested XprTypeNested;
60  typedef typename remove_reference<XprTypeNested>::type _XprTypeNested;
61  enum {
62  Flags = _XprTypeNested::Flags & (
64  | (functor_traits<UnaryOp>::PacketAccess ? PacketAccessBit : 0)),
65  CoeffReadCost = _XprTypeNested::CoeffReadCost + functor_traits<UnaryOp>::Cost
66  };
67 };
68 }
69 
70 template<typename UnaryOp, typename XprType, typename StorageKind>
71 class CwiseUnaryOpImpl;
72 
73 template<typename UnaryOp, typename XprType>
74 class CwiseUnaryOp : internal::no_assignment_operator,
75  public CwiseUnaryOpImpl<UnaryOp, XprType, typename internal::traits<XprType>::StorageKind>
76 {
77  public:
78 
81 
82  inline CwiseUnaryOp(const XprType& xpr, const UnaryOp& func = UnaryOp())
83  : m_xpr(xpr), m_functor(func) {}
84 
85  EIGEN_STRONG_INLINE Index rows() const { return m_xpr.rows(); }
86  EIGEN_STRONG_INLINE Index cols() const { return m_xpr.cols(); }
87 
89  const UnaryOp& functor() const { return m_functor; }
90 
92  const typename internal::remove_all<typename XprType::Nested>::type&
93  nestedExpression() const { return m_xpr; }
94 
96  typename internal::remove_all<typename XprType::Nested>::type&
97  nestedExpression() { return m_xpr.const_cast_derived(); }
98 
99  protected:
100  typename XprType::Nested m_xpr;
101  const UnaryOp m_functor;
102 };
103 
104 // This is the generic implementation for dense storage.
105 // It can be used for any expression types implementing the dense concept.
106 template<typename UnaryOp, typename XprType>
107 class CwiseUnaryOpImpl<UnaryOp,XprType,Dense>
108  : public internal::dense_xpr_base<CwiseUnaryOp<UnaryOp, XprType> >::type
109 {
110  public:
111 
113  typedef typename internal::dense_xpr_base<CwiseUnaryOp<UnaryOp, XprType> >::type Base;
115 
116  EIGEN_STRONG_INLINE const Scalar coeff(Index row, Index col) const
117  {
118  return derived().functor()(derived().nestedExpression().coeff(row, col));
119  }
120 
121  template<int LoadMode>
122  EIGEN_STRONG_INLINE PacketScalar packet(Index row, Index col) const
123  {
124  return derived().functor().packetOp(derived().nestedExpression().template packet<LoadMode>(row, col));
125  }
126 
127  EIGEN_STRONG_INLINE const Scalar coeff(Index index) const
128  {
129  return derived().functor()(derived().nestedExpression().coeff(index));
130  }
131 
132  template<int LoadMode>
133  EIGEN_STRONG_INLINE PacketScalar packet(Index index) const
134  {
135  return derived().functor().packetOp(derived().nestedExpression().template packet<LoadMode>(index));
136  }
137 };
138 
139 } // end namespace Eigen
140 
141 #endif // EIGEN_CWISE_UNARY_OP_H