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 // IWYU pragma: private
14 #include "../InternalHeaderCheck.h"
15 
16 namespace Eigen {
17 
18 namespace internal {
19 
20 template <typename Scalar>
24  template <typename PacketType>
26  return internal::pset1<PacketType>(m_other);
27  }
28  const Scalar m_other;
29 };
30 template <typename Scalar>
32  enum {
33  Cost = 0 /* as the constant value should be loaded in register only once for the whole expression */,
35  IsRepeatable = true
36  };
37 };
38 
39 template <typename Scalar>
43  template <typename PacketType>
45  return internal::pzero<PacketType>(PacketType());
46  }
47 };
48 template <typename Scalar>
49 struct functor_traits<scalar_zero_op<Scalar>> : functor_traits<scalar_constant_op<Scalar>> {};
50 
51 template <typename Scalar>
53  template <typename IndexType>
54  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator()(IndexType row, IndexType col) const {
55  return row == col ? Scalar(1) : Scalar(0);
56  }
57 };
58 template <typename Scalar>
61 };
62 
63 template <typename Scalar, bool IsInteger>
65 
66 template <typename Scalar>
67 struct linspaced_op_impl<Scalar, /*IsInteger*/ false> {
69 
70  EIGEN_DEVICE_FUNC linspaced_op_impl(const Scalar& low, const Scalar& high, Index num_steps)
71  : m_low(low),
72  m_high(high),
73  m_size1(num_steps == 1 ? 1 : num_steps - 1),
74  m_step(num_steps == 1 ? Scalar() : Scalar((high - low) / RealScalar(num_steps - 1))),
75  m_flip(numext::abs(high) < numext::abs(low)) {}
76 
77  template <typename IndexType>
79  if (m_flip)
80  return (i == 0) ? m_low : Scalar(m_high - RealScalar(m_size1 - i) * m_step);
81  else
82  return (i == m_size1) ? m_high : Scalar(m_low + RealScalar(i) * m_step);
83  }
84 
85  template <typename Packet, typename IndexType>
87  // Principle:
88  // [low, ..., low] + ( [step, ..., step] * ( [i, ..., i] + [0, ..., size] ) )
89  if (m_flip) {
90  Packet pi = plset<Packet>(Scalar(i - m_size1));
91  Packet res = padd(pset1<Packet>(m_high), pmul(pset1<Packet>(m_step), pi));
92  if (EIGEN_PREDICT_TRUE(i != 0)) return res;
93  Packet mask = pcmp_lt(pset1<Packet>(0), plset<Packet>(0));
94  return pselect<Packet>(mask, res, pset1<Packet>(m_low));
95  } else {
96  Packet pi = plset<Packet>(Scalar(i));
97  Packet res = padd(pset1<Packet>(m_low), pmul(pset1<Packet>(m_step), pi));
98  if (EIGEN_PREDICT_TRUE(i != m_size1 - unpacket_traits<Packet>::size + 1)) return res;
99  Packet mask = pcmp_lt(plset<Packet>(0), pset1<Packet>(unpacket_traits<Packet>::size - 1));
100  return pselect<Packet>(mask, res, pset1<Packet>(m_high));
101  }
102  }
103 
104  const Scalar m_low;
105  const Scalar m_high;
106  const Index m_size1;
107  const Scalar m_step;
108  const bool m_flip;
109 };
110 
111 template <typename Scalar>
112 struct linspaced_op_impl<Scalar, /*IsInteger*/ true> {
113  EIGEN_DEVICE_FUNC linspaced_op_impl(const Scalar& low, const Scalar& high, Index num_steps)
114  : m_low(low),
115  m_multiplier((high - low) / convert_index<Scalar>(num_steps <= 1 ? 1 : num_steps - 1)),
116  m_divisor(convert_index<Scalar>((high >= low ? num_steps : -num_steps) + (high - low)) /
117  ((numext::abs(high - low) + 1) == 0 ? 1 : (numext::abs(high - low) + 1))),
118  m_use_divisor(num_steps > 1 && (numext::abs(high - low) + 1) < num_steps) {}
119 
120  template <typename IndexType>
122  if (m_use_divisor)
123  return m_low + convert_index<Scalar>(i) / m_divisor;
124  else
125  return m_low + convert_index<Scalar>(i) * m_multiplier;
126  }
127 
128  const Scalar m_low;
131  const bool m_use_divisor;
132 };
133 
134 // ----- Linspace functor ----------------------------------------------------------------
135 
136 // Forward declaration (we default to random access which does not really give
137 // us a speed gain when using packet access but it allows to use the functor in
138 // nested expressions).
139 template <typename Scalar>
140 struct linspaced_op;
141 template <typename Scalar>
143  enum {
144  Cost = 1,
146  /*&& ((!NumTraits<Scalar>::IsInteger) || packet_traits<Scalar>::HasDiv),*/ // <- vectorization for integer is
147  // currently disabled
148  IsRepeatable = true
149  };
150 };
151 template <typename Scalar>
152 struct linspaced_op {
153  EIGEN_DEVICE_FUNC linspaced_op(const Scalar& low, const Scalar& high, Index num_steps)
154  : impl((num_steps == 1 ? high : low), high, num_steps) {}
155 
156  template <typename IndexType>
158  return impl(i);
159  }
160 
161  template <typename Packet, typename IndexType>
163  return impl.template packetOp<Packet>(i);
164  }
165 
166  // This proxy object handles the actual required temporaries and the different
167  // implementations (integer vs. floating point).
169 };
170 
171 template <typename Scalar>
174 
176  template <typename IndexType>
178  return m_start + m_step * static_cast<Scalar>(i);
179  }
180  template <typename Packet, typename IndexType>
182  const Packet cst_start = pset1<Packet>(m_start);
183  const Packet cst_step = pset1<Packet>(m_step);
184  const Packet cst_lin0 = plset<Packet>(Scalar(0));
185  const Packet cst_offset = pmadd(cst_lin0, cst_step, cst_start);
186 
187  Packet i_packet = pset1<Packet>(static_cast<Scalar>(i));
188  return pmadd(i_packet, cst_step, cst_offset);
189  }
191  const Scalar m_step;
192 };
193 
194 template <typename Scalar>
196  enum {
198  PacketAccess =
200  IsRepeatable = true
201  };
202 };
203 
204 // Linear access is automatically determined from the operator() prototypes available for the given functor.
205 // If it exposes an operator()(i,j), then we assume the i and j coefficients are required independently
206 // and linear access is not possible. In all other cases, linear access is enabled.
207 // Users should not have to deal with this structure.
208 template <typename Functor>
211 };
212 
213 // For unreliable compilers, let's specialize the has_*ary_operator
214 // helpers so that at least built-in nullary functors work fine.
215 #if !(EIGEN_COMP_MSVC || EIGEN_COMP_GNUC || (EIGEN_COMP_ICC >= 1600))
216 template <typename Scalar, typename IndexType>
218  enum { value = 1 };
219 };
220 template <typename Scalar, typename IndexType>
222  enum { value = 0 };
223 };
224 template <typename Scalar, typename IndexType>
226  enum { value = 0 };
227 };
228 
229 template <typename Scalar, typename IndexType>
231  enum { value = 0 };
232 };
233 template <typename Scalar, typename IndexType>
235  enum { value = 0 };
236 };
237 template <typename Scalar, typename IndexType>
239  enum { value = 1 };
240 };
241 
242 template <typename Scalar, typename IndexType>
244  enum { value = 0 };
245 };
246 template <typename Scalar, typename IndexType>
248  enum { value = 1 };
249 };
250 template <typename Scalar, typename IndexType>
252  enum { value = 0 };
253 };
254 
255 template <typename Scalar, typename IndexType>
257  enum { value = 1 };
258 };
259 template <typename Scalar, typename IndexType>
261  enum { value = 0 };
262 };
263 template <typename Scalar, typename IndexType>
265  enum { value = 0 };
266 };
267 #endif
268 
269 } // end namespace internal
270 
271 } // end namespace Eigen
272 
273 #endif // EIGEN_NULLARY_FUNCTORS_H
AnnoyingScalar abs(const AnnoyingScalar &x)
Definition: AnnoyingScalar.h:135
int i
Definition: BiCGSTAB_step_by_step.cpp:9
#define EIGEN_PREDICT_TRUE(x)
Definition: Macros.h:1180
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:892
#define EIGEN_STRONG_INLINE
Definition: Macros.h:834
m col(1)
m row(1)
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
Definition: PartialRedux_count.cpp:3
SCALAR Scalar
Definition: bench_gemm.cpp:45
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:46
EIGEN_DEVICE_FUNC Packet padd(const Packet &a, const Packet &b)
Definition: GenericPacketMath.h:318
EIGEN_DEVICE_FUNC IndexDest convert_index(const IndexSrc &idx)
Definition: XprHelper.h:63
EIGEN_STRONG_INLINE Packet4i pcmp_lt(const Packet4i &a, const Packet4i &b)
Definition: AltiVec/PacketMath.h:1341
EIGEN_STRONG_INLINE Packet4f pmadd(const Packet4f &a, const Packet4f &b, const Packet4f &c)
Definition: AltiVec/PacketMath.h:1218
EIGEN_STRONG_INLINE Packet4cf pmul(const Packet4cf &a, const Packet4cf &b)
Definition: AVX/Complex.h:88
Namespace containing all symbols from the Eigen library.
Definition: bench_norm.cpp:70
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:83
const Mdouble pi
Definition: ExtendedMath.h:23
Definition: Eigen_Colamd.h:49
void start(const unsigned &i)
(Re-)start i-th timer
Definition: oomph_utilities.cc:243
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:217
Definition: TensorMeta.h:47
Definition: NullaryFunctors.h:172
NumTraits< Scalar >::Real RealScalar
Definition: NullaryFunctors.h:173
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(IndexType i) const
Definition: NullaryFunctors.h:181
const Scalar m_step
Definition: NullaryFunctors.h:191
const Scalar m_start
Definition: NullaryFunctors.h:190
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator()(IndexType i) const
Definition: NullaryFunctors.h:177
EIGEN_DEVICE_FUNC equalspaced_op(const Scalar &start, const Scalar &step)
Definition: NullaryFunctors.h:175
Definition: NullaryFunctors.h:209
@ ret
Definition: NullaryFunctors.h:210
Definition: XprHelper.h:205
@ PacketAccess
Definition: XprHelper.h:206
@ Cost
Definition: XprHelper.h:206
@ IsRepeatable
Definition: XprHelper.h:206
@ value
Definition: Meta.h:456
@ value
Definition: Meta.h:446
NumTraits< Scalar >::Real RealScalar
Definition: NullaryFunctors.h:68
const Scalar m_step
Definition: NullaryFunctors.h:107
const Scalar m_low
Definition: NullaryFunctors.h:104
EIGEN_DEVICE_FUNC linspaced_op_impl(const Scalar &low, const Scalar &high, Index num_steps)
Definition: NullaryFunctors.h:70
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(IndexType i) const
Definition: NullaryFunctors.h:86
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator()(IndexType i) const
Definition: NullaryFunctors.h:78
const bool m_flip
Definition: NullaryFunctors.h:108
const Index m_size1
Definition: NullaryFunctors.h:106
const Scalar m_high
Definition: NullaryFunctors.h:105
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator()(IndexType i) const
Definition: NullaryFunctors.h:121
const Scalar m_multiplier
Definition: NullaryFunctors.h:129
const Scalar m_divisor
Definition: NullaryFunctors.h:130
EIGEN_DEVICE_FUNC linspaced_op_impl(const Scalar &low, const Scalar &high, Index num_steps)
Definition: NullaryFunctors.h:113
const bool m_use_divisor
Definition: NullaryFunctors.h:131
const Scalar m_low
Definition: NullaryFunctors.h:128
Definition: NullaryFunctors.h:64
Definition: NullaryFunctors.h:152
const linspaced_op_impl< Scalar, NumTraits< Scalar >::IsInteger > impl
Definition: NullaryFunctors.h:168
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(IndexType i) const
Definition: NullaryFunctors.h:162
EIGEN_DEVICE_FUNC linspaced_op(const Scalar &low, const Scalar &high, Index num_steps)
Definition: NullaryFunctors.h:153
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator()(IndexType i) const
Definition: NullaryFunctors.h:157
Definition: GenericPacketMath.h:108
Definition: NullaryFunctors.h:21
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator()() const
Definition: NullaryFunctors.h:23
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const PacketType packetOp() const
Definition: NullaryFunctors.h:25
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE scalar_constant_op(const Scalar &other)
Definition: NullaryFunctors.h:22
const Scalar m_other
Definition: NullaryFunctors.h:28
Definition: NullaryFunctors.h:52
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator()(IndexType row, IndexType col) const
Definition: NullaryFunctors.h:54
Definition: Random.h:21
Definition: NullaryFunctors.h:40
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE scalar_zero_op()=default
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator()() const
Definition: NullaryFunctors.h:42
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const PacketType packetOp() const
Definition: NullaryFunctors.h:44
Definition: GenericPacketMath.h:134
Definition: ZVector/PacketMath.h:50