NullaryFunctors.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-2016 Gael Guennebaud <gael.guennebaud@inria.fr>
5 //
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 
10 #ifndef EIGEN_NULLARY_FUNCTORS_H
11 #define EIGEN_NULLARY_FUNCTORS_H
12 
13 #include "../InternalHeaderCheck.h"
14 
15 namespace Eigen {
16 
17 namespace internal {
18 
19 template<typename Scalar>
20 struct scalar_constant_op {
21  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE scalar_constant_op(const scalar_constant_op& other) : m_other(other.m_other) { }
22  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE scalar_constant_op(const Scalar& other) : m_other(other) { }
23  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() () const { return m_other; }
24  template<typename PacketType>
25  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const PacketType packetOp() const { return internal::pset1<PacketType>(m_other); }
26  const Scalar m_other;
27 };
28 template<typename Scalar>
29 struct functor_traits<scalar_constant_op<Scalar> >
30 { enum { Cost = 0 /* as the constant value should be loaded in register only once for the whole expression */,
31  PacketAccess = packet_traits<Scalar>::Vectorizable, IsRepeatable = true }; };
32 
33 template<typename Scalar> struct scalar_identity_op {
34  template<typename IndexType>
35  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() (IndexType row, IndexType col) const { return row==col ? Scalar(1) : Scalar(0); }
36 };
37 template<typename Scalar>
38 struct functor_traits<scalar_identity_op<Scalar> >
39 { enum { Cost = NumTraits<Scalar>::AddCost, PacketAccess = false, IsRepeatable = true }; };
40 
41 template <typename Scalar, bool IsInteger> struct linspaced_op_impl;
42 
43 template <typename Scalar>
44 struct linspaced_op_impl<Scalar,/*IsInteger*/false>
45 {
46  typedef typename NumTraits<Scalar>::Real RealScalar;
47 
48  EIGEN_DEVICE_FUNC linspaced_op_impl(const Scalar& low, const Scalar& high, Index num_steps) :
49  m_low(low), m_high(high), m_size1(num_steps==1 ? 1 : num_steps-1), m_step(num_steps==1 ? Scalar() : Scalar((high-low)/RealScalar(num_steps-1))),
50  m_flip(numext::abs(high)<numext::abs(low))
51  {}
52 
53  template<typename IndexType>
54  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() (IndexType i) const {
55  if(m_flip)
56  return (i==0)? m_low : Scalar(m_high - RealScalar(m_size1-i)*m_step);
57  else
58  return (i==m_size1)? m_high : Scalar(m_low + RealScalar(i)*m_step);
59  }
60 
61  template<typename Packet, typename IndexType>
62  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(IndexType i) const
63  {
64  // Principle:
65  // [low, ..., low] + ( [step, ..., step] * ( [i, ..., i] + [0, ..., size] ) )
66  if(m_flip)
67  {
68  Packet pi = plset<Packet>(Scalar(i-m_size1));
69  Packet res = padd(pset1<Packet>(m_high), pmul(pset1<Packet>(m_step), pi));
70  if (EIGEN_PREDICT_TRUE(i != 0)) return res;
71  Packet mask = pcmp_lt(pset1<Packet>(0), plset<Packet>(0));
72  return pselect<Packet>(mask, res, pset1<Packet>(m_low));
73  }
74  else
75  {
76  Packet pi = plset<Packet>(Scalar(i));
77  Packet res = padd(pset1<Packet>(m_low), pmul(pset1<Packet>(m_step), pi));
78  if(EIGEN_PREDICT_TRUE(i != m_size1-unpacket_traits<Packet>::size+1)) return res;
79  Packet mask = pcmp_lt(plset<Packet>(0), pset1<Packet>(unpacket_traits<Packet>::size-1));
80  return pselect<Packet>(mask, res, pset1<Packet>(m_high));
81  }
82  }
83 
84  const Scalar m_low;
85  const Scalar m_high;
86  const Index m_size1;
87  const Scalar m_step;
88  const bool m_flip;
89 };
90 
91 template <typename Scalar>
92 struct linspaced_op_impl<Scalar,/*IsInteger*/true>
93 {
94  EIGEN_DEVICE_FUNC linspaced_op_impl(const Scalar& low, const Scalar& high, Index num_steps) :
95  m_low(low),
96  m_multiplier((high-low)/convert_index<Scalar>(num_steps<=1 ? 1 : num_steps-1)),
97  m_divisor(convert_index<Scalar>((high>=low?num_steps:-num_steps)+(high-low))/((numext::abs(high-low)+1)==0?1:(numext::abs(high-low)+1))),
98  m_use_divisor(num_steps>1 && (numext::abs(high-low)+1)<num_steps)
99  {}
100 
101  template<typename IndexType>
102  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
103  const Scalar operator() (IndexType i) const
104  {
105  if(m_use_divisor) return m_low + convert_index<Scalar>(i)/m_divisor;
106  else return m_low + convert_index<Scalar>(i)*m_multiplier;
107  }
108 
109  const Scalar m_low;
110  const Scalar m_multiplier;
111  const Scalar m_divisor;
112  const bool m_use_divisor;
113 };
114 
115 // ----- Linspace functor ----------------------------------------------------------------
116 
117 // Forward declaration (we default to random access which does not really give
118 // us a speed gain when using packet access but it allows to use the functor in
119 // nested expressions).
120 template <typename Scalar> struct linspaced_op;
121 template <typename Scalar> struct functor_traits< linspaced_op<Scalar> >
122 {
123  enum
124  {
125  Cost = 1,
126  PacketAccess = (!NumTraits<Scalar>::IsInteger) && packet_traits<Scalar>::HasSetLinear && packet_traits<Scalar>::HasBlend,
127  /*&& ((!NumTraits<Scalar>::IsInteger) || packet_traits<Scalar>::HasDiv),*/ // <- vectorization for integer is currently disabled
128  IsRepeatable = true
129  };
130 };
131 template <typename Scalar> struct linspaced_op
132 {
133  EIGEN_DEVICE_FUNC linspaced_op(const Scalar& low, const Scalar& high, Index num_steps)
134  : impl((num_steps==1 ? high : low),high,num_steps)
135  {}
136 
137  template<typename IndexType>
138  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() (IndexType i) const { return impl(i); }
139 
140  template<typename Packet,typename IndexType>
141  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(IndexType i) const { return impl.template packetOp<Packet>(i); }
142 
143  // This proxy object handles the actual required temporaries and the different
144  // implementations (integer vs. floating point).
145  const linspaced_op_impl<Scalar,NumTraits<Scalar>::IsInteger> impl;
146 };
147 
148 template <typename Scalar>
149 struct equalspaced_op {
150  typedef typename NumTraits<Scalar>::Real RealScalar;
151 
152  EIGEN_DEVICE_FUNC equalspaced_op(const Scalar& start, const Scalar& step) : m_start(start), m_step(step) {}
153  template <typename IndexType>
154  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator()(IndexType i) const {
155  return m_start + m_step * static_cast<Scalar>(i);
156  }
157  template <typename Packet, typename IndexType>
158  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(IndexType i) const {
159  const Packet cst_start = pset1<Packet>(m_start);
160  const Packet cst_step = pset1<Packet>(m_step);
161  const Packet cst_lin0 = plset<Packet>(Scalar(0));
162  const Packet cst_offset = pmadd(cst_lin0, cst_step, cst_start);
163 
164  Packet i_packet = pset1<Packet>(static_cast<Scalar>(i));
165  return pmadd(i_packet, cst_step, cst_offset);
166  }
167  const Scalar m_start;
168  const Scalar m_step;
169 };
170 
171 template <typename Scalar>
172 struct functor_traits<equalspaced_op<Scalar> > {
173  enum {
175  PacketAccess =
176  packet_traits<Scalar>::HasSetLinear && packet_traits<Scalar>::HasMul && packet_traits<Scalar>::HasAdd,
177  IsRepeatable = true
178  };
179 };
180 
181 // Linear access is automatically determined from the operator() prototypes available for the given functor.
182 // If it exposes an operator()(i,j), then we assume the i and j coefficients are required independently
183 // and linear access is not possible. In all other cases, linear access is enabled.
184 // Users should not have to deal with this structure.
185 template<typename Functor> struct functor_has_linear_access { enum { ret = !has_binary_operator<Functor>::value }; };
186 
187 // For unreliable compilers, let's specialize the has_*ary_operator
188 // helpers so that at least built-in nullary functors work fine.
189 #if !( EIGEN_COMP_MSVC || EIGEN_COMP_GNUC || (EIGEN_COMP_ICC>=1600))
190 template<typename Scalar,typename IndexType>
191 struct has_nullary_operator<scalar_constant_op<Scalar>,IndexType> { enum { value = 1}; };
192 template<typename Scalar,typename IndexType>
193 struct has_unary_operator<scalar_constant_op<Scalar>,IndexType> { enum { value = 0}; };
194 template<typename Scalar,typename IndexType>
195 struct has_binary_operator<scalar_constant_op<Scalar>,IndexType> { enum { value = 0}; };
196 
197 template<typename Scalar,typename IndexType>
198 struct has_nullary_operator<scalar_identity_op<Scalar>,IndexType> { enum { value = 0}; };
199 template<typename Scalar,typename IndexType>
200 struct has_unary_operator<scalar_identity_op<Scalar>,IndexType> { enum { value = 0}; };
201 template<typename Scalar,typename IndexType>
202 struct has_binary_operator<scalar_identity_op<Scalar>,IndexType> { enum { value = 1}; };
203 
204 template<typename Scalar,typename IndexType>
205 struct has_nullary_operator<linspaced_op<Scalar>,IndexType> { enum { value = 0}; };
206 template<typename Scalar,typename IndexType>
207 struct has_unary_operator<linspaced_op<Scalar>,IndexType> { enum { value = 1}; };
208 template<typename Scalar,typename IndexType>
209 struct has_binary_operator<linspaced_op<Scalar>,IndexType> { enum { value = 0}; };
210 
211 template<typename Scalar,typename IndexType>
212 struct has_nullary_operator<scalar_random_op<Scalar>,IndexType> { enum { value = 1}; };
213 template<typename Scalar,typename IndexType>
214 struct has_unary_operator<scalar_random_op<Scalar>,IndexType> { enum { value = 0}; };
215 template<typename Scalar,typename IndexType>
216 struct has_binary_operator<scalar_random_op<Scalar>,IndexType> { enum { value = 0}; };
217 #endif
218 
219 } // end namespace internal
220 
221 } // end namespace Eigen
222 
223 #endif // EIGEN_NULLARY_FUNCTORS_H
RowXpr row(Index i)
This is the const version of row(). *‍/.
ColXpr col(Index i)
This is the const version of col().
IndexedView_or_Block operator()(const RowIndices &rowIndices, const ColIndices &colIndices)
#define EIGEN_PREDICT_TRUE(x)
Definition: Macros.h:1178
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:883
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
Packet padd(const Packet &a, const Packet &b)
Packet4f pmadd(const Packet4f &a, const Packet4f &b, const Packet4f &c)
Packet pmul(const Packet &a, const Packet &b)
IndexDest convert_index(const IndexSrc &idx)
Definition: XprHelper.h:64
Packet4i pcmp_lt(const Packet4i &a, const Packet4i &b)
: InteropHeaders
Definition: Core:139
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:82
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_abs_op< typename Derived::Scalar >, const Derived > abs(const Eigen::ArrayBase< Derived > &x)