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 namespace Eigen {
14 
15 namespace internal {
16 
17 template<typename Scalar>
22  template<typename PacketType>
23  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const PacketType packetOp() const { return internal::pset1<PacketType>(m_other); }
24  const Scalar m_other;
25 };
26 template<typename Scalar>
28 { enum { Cost = 0 /* as the constant value should be loaded in register only once for the whole expression */,
30 
31 template<typename Scalar> struct scalar_identity_op {
33  template<typename IndexType>
34  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() (IndexType row, IndexType col) const { return row==col ? Scalar(1) : Scalar(0); }
35 };
36 template<typename Scalar>
38 { enum { Cost = NumTraits<Scalar>::AddCost, PacketAccess = false, IsRepeatable = true }; };
39 
40 template <typename Scalar, bool IsInteger> struct linspaced_op_impl;
41 
42 template <typename Scalar>
43 struct linspaced_op_impl<Scalar,/*IsInteger*/false>
44 {
46 
47  EIGEN_DEVICE_FUNC linspaced_op_impl(const Scalar& low, const Scalar& high, Index num_steps) :
48  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))),
49  m_flip(numext::abs(high)<numext::abs(low))
50  {}
51 
52  template<typename IndexType>
54  if(m_flip)
55  return (i==0)? m_low : Scalar(m_high - RealScalar(m_size1-i)*m_step);
56  else
57  return (i==m_size1)? m_high : Scalar(m_low + RealScalar(i)*m_step);
58  }
59 
60  template<typename Packet, typename IndexType>
62  {
63  // Principle:
64  // [low, ..., low] + ( [step, ..., step] * ( [i, ..., i] + [0, ..., size] ) )
65  if(m_flip)
66  {
67  Packet pi = plset<Packet>(Scalar(i-m_size1));
68  Packet res = padd(pset1<Packet>(m_high), pmul(pset1<Packet>(m_step), pi));
69  if (EIGEN_PREDICT_TRUE(i != 0)) return res;
70  Packet mask = pcmp_lt(pset1<Packet>(0), plset<Packet>(0));
71  return pselect<Packet>(mask, res, pset1<Packet>(m_low));
72  }
73  else
74  {
75  Packet pi = plset<Packet>(Scalar(i));
76  Packet res = padd(pset1<Packet>(m_low), pmul(pset1<Packet>(m_step), pi));
77  if(EIGEN_PREDICT_TRUE(i != m_size1-unpacket_traits<Packet>::size+1)) return res;
78  Packet mask = pcmp_lt(plset<Packet>(0), pset1<Packet>(unpacket_traits<Packet>::size-1));
79  return pselect<Packet>(mask, res, pset1<Packet>(m_high));
80  }
81  }
82 
83  const Scalar m_low;
84  const Scalar m_high;
85  const Index m_size1;
86  const Scalar m_step;
87  const bool m_flip;
88 };
89 
90 template <typename Scalar>
91 struct linspaced_op_impl<Scalar,/*IsInteger*/true>
92 {
93  EIGEN_DEVICE_FUNC linspaced_op_impl(const Scalar& low, const Scalar& high, Index num_steps) :
94  m_low(low),
95  m_multiplier((high-low)/convert_index<Scalar>(num_steps<=1 ? 1 : num_steps-1)),
96  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))),
97  m_use_divisor(num_steps>1 && (numext::abs(high-low)+1)<num_steps)
98  {}
99 
100  template<typename IndexType>
102  const Scalar operator() (IndexType i) const
103  {
104  if(m_use_divisor) return m_low + convert_index<Scalar>(i)/m_divisor;
105  else return m_low + convert_index<Scalar>(i)*m_multiplier;
106  }
107 
108  const Scalar m_low;
111  const bool m_use_divisor;
112 };
113 
114 // ----- Linspace functor ----------------------------------------------------------------
115 
116 // Forward declaration (we default to random access which does not really give
117 // us a speed gain when using packet access but it allows to use the functor in
118 // nested expressions).
119 template <typename Scalar> struct linspaced_op;
120 template <typename Scalar> struct functor_traits< linspaced_op<Scalar> >
121 {
122  enum
123  {
124  Cost = 1,
126  /*&& ((!NumTraits<Scalar>::IsInteger) || packet_traits<Scalar>::HasDiv),*/ // <- vectorization for integer is currently disabled
128  };
129 };
130 template <typename Scalar> struct linspaced_op
131 {
132  EIGEN_DEVICE_FUNC linspaced_op(const Scalar& low, const Scalar& high, Index num_steps)
133  : impl((num_steps==1 ? high : low),high,num_steps)
134  {}
135 
136  template<typename IndexType>
137  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() (IndexType i) const { return impl(i); }
138 
139  template<typename Packet,typename IndexType>
140  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(IndexType i) const { return impl.template packetOp<Packet>(i); }
141 
142  // This proxy object handles the actual required temporaries and the different
143  // implementations (integer vs. floating point).
145 };
146 
147 // Linear access is automatically determined from the operator() prototypes available for the given functor.
148 // If it exposes an operator()(i,j), then we assume the i and j coefficients are required independently
149 // and linear access is not possible. In all other cases, linear access is enabled.
150 // Users should not have to deal with this structure.
151 template<typename Functor> struct functor_has_linear_access { enum { ret = !has_binary_operator<Functor>::value }; };
152 
153 // For unreliable compilers, let's specialize the has_*ary_operator
154 // helpers so that at least built-in nullary functors work fine.
155 #if !( (EIGEN_COMP_MSVC>1600) || (EIGEN_GNUC_AT_LEAST(4,8)) || (EIGEN_COMP_ICC>=1600))
156 template<typename Scalar,typename IndexType>
157 struct has_nullary_operator<scalar_constant_op<Scalar>,IndexType> { enum { value = 1}; };
158 template<typename Scalar,typename IndexType>
159 struct has_unary_operator<scalar_constant_op<Scalar>,IndexType> { enum { value = 0}; };
160 template<typename Scalar,typename IndexType>
161 struct has_binary_operator<scalar_constant_op<Scalar>,IndexType> { enum { value = 0}; };
162 
163 template<typename Scalar,typename IndexType>
164 struct has_nullary_operator<scalar_identity_op<Scalar>,IndexType> { enum { value = 0}; };
165 template<typename Scalar,typename IndexType>
166 struct has_unary_operator<scalar_identity_op<Scalar>,IndexType> { enum { value = 0}; };
167 template<typename Scalar,typename IndexType>
168 struct has_binary_operator<scalar_identity_op<Scalar>,IndexType> { enum { value = 1}; };
169 
170 template<typename Scalar,typename IndexType>
171 struct has_nullary_operator<linspaced_op<Scalar>,IndexType> { enum { value = 0}; };
172 template<typename Scalar,typename IndexType>
173 struct has_unary_operator<linspaced_op<Scalar>,IndexType> { enum { value = 1}; };
174 template<typename Scalar,typename IndexType>
175 struct has_binary_operator<linspaced_op<Scalar>,IndexType> { enum { value = 0}; };
176 
177 template<typename Scalar,typename IndexType>
178 struct has_nullary_operator<scalar_random_op<Scalar>,IndexType> { enum { value = 1}; };
179 template<typename Scalar,typename IndexType>
180 struct has_unary_operator<scalar_random_op<Scalar>,IndexType> { enum { value = 0}; };
181 template<typename Scalar,typename IndexType>
182 struct has_binary_operator<scalar_random_op<Scalar>,IndexType> { enum { value = 0}; };
183 #endif
184 
185 } // end namespace internal
186 
187 } // end namespace Eigen
188 
189 #endif // EIGEN_NULLARY_FUNCTORS_H
Eigen::internal::linspaced_op::packetOp
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE Packet packetOp(IndexType i) const
Definition: NullaryFunctors.h:140
EIGEN_EMPTY_STRUCT_CTOR
#define EIGEN_EMPTY_STRUCT_CTOR(X)
Definition: XprHelper.h:22
EIGEN_DEVICE_FUNC
#define EIGEN_DEVICE_FUNC
Definition: Macros.h:976
Eigen::internal::scalar_identity_op
Definition: NullaryFunctors.h:31
Eigen
Namespace containing all symbols from the Eigen library.
Definition: jet.h:637
Eigen::internal::scalar_constant_op::packetOp
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE PacketType packetOp() const
Definition: NullaryFunctors.h:23
Eigen::internal::linspaced_op_impl< Scalar, true >::m_divisor
const Scalar m_divisor
Definition: NullaryFunctors.h:110
col
m col(1)
Eigen::internal::linspaced_op
Definition: NullaryFunctors.h:119
Eigen::internal::linspaced_op_impl< Scalar, false >::m_step
const Scalar m_step
Definition: NullaryFunctors.h:86
Eigen::internal::linspaced_op::impl
const linspaced_op_impl< Scalar, NumTraits< Scalar >::IsInteger > impl
Definition: NullaryFunctors.h:144
Eigen::internal::scalar_random_op
Definition: Random.h:17
Eigen::internal::pcmp_lt
EIGEN_STRONG_INLINE Packet4f pcmp_lt(const Packet4f &a, const Packet4f &b)
Definition: AltiVec/PacketMath.h:868
Eigen::internal::linspaced_op_impl< Scalar, false >::m_flip
const bool m_flip
Definition: NullaryFunctors.h:87
Eigen::internal::packet_traits
Definition: GenericPacketMath.h:106
res
cout<< "Here is the matrix m:"<< endl<< m<< endl;Matrix< ptrdiff_t, 3, 1 > res
Definition: PartialRedux_count.cpp:3
Eigen::internal::linspaced_op_impl
Definition: NullaryFunctors.h:40
Eigen::internal::linspaced_op_impl< Scalar, true >::m_low
const Scalar m_low
Definition: NullaryFunctors.h:108
Eigen::internal::linspaced_op::linspaced_op
EIGEN_DEVICE_FUNC linspaced_op(const Scalar &low, const Scalar &high, Index num_steps)
Definition: NullaryFunctors.h:132
Eigen::internal::has_binary_operator
Definition: Meta.h:685
Eigen::PacketType
Definition: TensorMeta.h:50
Eigen::internal::linspaced_op_impl< Scalar, false >::packetOp
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE Packet packetOp(IndexType i) const
Definition: NullaryFunctors.h:61
Eigen::internal::linspaced_op_impl< Scalar, false >::RealScalar
NumTraits< Scalar >::Real RealScalar
Definition: NullaryFunctors.h:45
Eigen::internal::scalar_constant_op::operator()
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE Scalar operator()() const
Definition: NullaryFunctors.h:21
Eigen::internal::scalar_constant_op::scalar_constant_op
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE scalar_constant_op(const Scalar &other)
Definition: NullaryFunctors.h:20
EIGEN_PREDICT_TRUE
#define EIGEN_PREDICT_TRUE(x)
Definition: Macros.h:1322
Eigen::internal::scalar_constant_op
Definition: NullaryFunctors.h:18
operator()
internal::enable_if< internal::valid_indexed_view_overload< RowIndices, ColIndices >::value &&internal::traits< typename EIGEN_INDEXED_VIEW_METHOD_TYPE< RowIndices, ColIndices >::type >::ReturnAsIndexedView, typename EIGEN_INDEXED_VIEW_METHOD_TYPE< RowIndices, ColIndices >::type >::type operator()(const RowIndices &rowIndices, const ColIndices &colIndices) EIGEN_INDEXED_VIEW_METHOD_CONST
Definition: IndexedViewMethods.h:73
Eigen::internal::scalar_constant_op::m_other
const Scalar m_other
Definition: NullaryFunctors.h:24
Eigen::internal::unpacket_traits
Definition: GenericPacketMath.h:132
EIGEN_STRONG_INLINE
#define EIGEN_STRONG_INLINE
Definition: Macros.h:917
Eigen::internal::linspaced_op_impl< Scalar, true >::linspaced_op_impl
EIGEN_DEVICE_FUNC linspaced_op_impl(const Scalar &low, const Scalar &high, Index num_steps)
Definition: NullaryFunctors.h:93
Eigen::internal::scalar_identity_op::operator()
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE Scalar operator()(IndexType row, IndexType col) const
Definition: NullaryFunctors.h:34
Eigen::internal::functor_traits::IsRepeatable
@ IsRepeatable
Definition: XprHelper.h:181
Eigen::internal::Packet
Definition: ZVector/PacketMath.h:47
Eigen::internal::functor_has_linear_access::ret
@ ret
Definition: NullaryFunctors.h:151
Eigen::internal::functor_has_linear_access
Definition: NullaryFunctors.h:151
Eigen::internal::linspaced_op::operator()
EIGEN_DEVICE_FUNC const EIGEN_STRONG_INLINE Scalar operator()(IndexType i) const
Definition: NullaryFunctors.h:137
Eigen::internal::functor_traits::PacketAccess
@ PacketAccess
Definition: XprHelper.h:180
Eigen::internal::convert_index
EIGEN_DEVICE_FUNC IndexDest convert_index(const IndexSrc &idx)
Definition: XprHelper.h:31
RealScalar
NumTraits< Scalar >::Real RealScalar
Definition: bench_gemm.cpp:47
Eigen::internal::scalar_constant_op::scalar_constant_op
EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE scalar_constant_op(const scalar_constant_op &other)
Definition: NullaryFunctors.h:19
Eigen::internal::linspaced_op_impl< Scalar, false >::linspaced_op_impl
EIGEN_DEVICE_FUNC linspaced_op_impl(const Scalar &low, const Scalar &high, Index num_steps)
Definition: NullaryFunctors.h:47
Eigen::internal::pmul
EIGEN_DEVICE_FUNC Packet pmul(const Packet &a, const Packet &b)
Definition: GenericPacketMath.h:237
Eigen::internal::functor_traits::Cost
@ Cost
Definition: XprHelper.h:179
Eigen::internal::has_nullary_operator::value
@ value
Definition: Meta.h:672
row
m row(1)
Eigen::internal::linspaced_op_impl< Scalar, false >::m_high
const Scalar m_high
Definition: NullaryFunctors.h:84
Eigen::internal::linspaced_op_impl< Scalar, false >::m_low
const Scalar m_low
Definition: NullaryFunctors.h:83
Eigen::internal::linspaced_op_impl< Scalar, true >::m_multiplier
const Scalar m_multiplier
Definition: NullaryFunctors.h:109
Eigen::internal::has_unary_operator
Definition: Meta.h:676
Eigen::internal::padd
EIGEN_DEVICE_FUNC Packet padd(const Packet &a, const Packet &b)
Definition: GenericPacketMath.h:215
Eigen::internal::linspaced_op_impl< Scalar, true >::m_use_divisor
const bool m_use_divisor
Definition: NullaryFunctors.h:111
abs
#define abs(x)
Definition: datatypes.h:17
Eigen::internal::functor_traits
Definition: XprHelper.h:175
internal
Definition: BandTriangularSolver.h:13
Eigen::NumTraits
Holds information about the various numeric (i.e. scalar) types allowed by Eigen.
Definition: NumTraits.h:232
Eigen::internal::has_nullary_operator
Definition: Meta.h:667
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Eigen::internal::has_binary_operator::value
@ value
Definition: Meta.h:690
pybind_wrapper_test_script.other
other
Definition: pybind_wrapper_test_script.py:42
Scalar
SCALAR Scalar
Definition: bench_gemm.cpp:46
Eigen::internal::linspaced_op_impl< Scalar, false >::m_size1
const Index m_size1
Definition: NullaryFunctors.h:85
Eigen::Index
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: Meta.h:74
Eigen::internal::has_unary_operator::value
@ value
Definition: Meta.h:681


gtsam
Author(s):
autogenerated on Fri Nov 1 2024 03:33:51