Eigen  3.2.93
NullaryFunctors.h
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 //
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>
18 struct scalar_constant_op {
19  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE scalar_constant_op(const scalar_constant_op& other) : m_other(other.m_other) { }
20  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE scalar_constant_op(const Scalar& other) : m_other(other) { }
21  template<typename Index>
22  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() (Index, Index = 0) const { return m_other; }
23  template<typename Index, typename PacketType>
24  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const PacketType packetOp(Index, Index = 0) const { return internal::pset1<PacketType>(m_other); }
25  const Scalar m_other;
26 };
27 template<typename Scalar>
28 struct functor_traits<scalar_constant_op<Scalar> >
29 { enum { Cost = 0 /* as the constant value should be loaded in register only once for the whole expression */,
30  PacketAccess = packet_traits<Scalar>::Vectorizable, IsRepeatable = true }; };
31 
32 template<typename Scalar> struct scalar_identity_op {
33  EIGEN_EMPTY_STRUCT_CTOR(scalar_identity_op)
34  template<typename Index>
35  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() (Index row, Index 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, typename Packet, bool RandomAccess, bool IsInteger> struct linspaced_op_impl;
42 
43 // linear access for packet ops:
44 // 1) initialization
45 // base = [low, ..., low] + ([step, ..., step] * [-size, ..., 0])
46 // 2) each step (where size is 1 for coeff access or PacketSize for packet access)
47 // base += [size*step, ..., size*step]
48 //
49 // TODO: Perhaps it's better to initialize lazily (so not in the constructor but in packetOp)
50 // in order to avoid the padd() in operator() ?
51 template <typename Scalar, typename Packet>
52 struct linspaced_op_impl<Scalar,Packet,/*RandomAccess*/false,/*IsInteger*/false>
53 {
54  linspaced_op_impl(const Scalar& low, const Scalar& high, Index num_steps) :
55  m_low(low), m_step(num_steps==1 ? Scalar() : (high-low)/Scalar(num_steps-1)),
56  m_packetStep(pset1<Packet>(unpacket_traits<Packet>::size*m_step)),
57  m_base(padd(pset1<Packet>(low), pmul(pset1<Packet>(m_step),plset<Packet>(-unpacket_traits<Packet>::size)))) {}
58 
59  template<typename Index>
60  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() (Index i) const
61  {
62  m_base = padd(m_base, pset1<Packet>(m_step));
63  return m_low+Scalar(i)*m_step;
64  }
65 
66  template<typename Index>
67  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(Index) const { return m_base = padd(m_base,m_packetStep); }
68 
69  const Scalar m_low;
70  const Scalar m_step;
71  const Packet m_packetStep;
72  mutable Packet m_base;
73 };
74 
75 // random access for packet ops:
76 // 1) each step
77 // [low, ..., low] + ( [step, ..., step] * ( [i, ..., i] + [0, ..., size] ) )
78 template <typename Scalar, typename Packet>
79 struct linspaced_op_impl<Scalar,Packet,/*RandomAccess*/true,/*IsInteger*/false>
80 {
81  linspaced_op_impl(const Scalar& low, const Scalar& high, Index num_steps) :
82  m_low(low), m_step(num_steps==1 ? Scalar() : (high-low)/Scalar(num_steps-1)),
83  m_lowPacket(pset1<Packet>(m_low)), m_stepPacket(pset1<Packet>(m_step)), m_interPacket(plset<Packet>(0)) {}
84 
85  template<typename Index>
86  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() (Index i) const { return m_low+i*m_step; }
87 
88  template<typename Index>
89  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(Index i) const
90  { return internal::padd(m_lowPacket, pmul(m_stepPacket, padd(pset1<Packet>(Scalar(i)),m_interPacket))); }
91 
92  const Scalar m_low;
93  const Scalar m_step;
94  const Packet m_lowPacket;
95  const Packet m_stepPacket;
96  const Packet m_interPacket;
97 };
98 
99 template <typename Scalar, typename Packet>
100 struct linspaced_op_impl<Scalar,Packet,/*RandomAccess*/true,/*IsInteger*/true>
101 {
102  linspaced_op_impl(const Scalar& low, const Scalar& high, Index num_steps) :
103  m_low(low), m_length(high-low), m_divisor(convert_index<Scalar>(num_steps==1?1:num_steps-1)), m_interPacket(plset<Packet>(0))
104  {}
105 
106  template<typename Index>
107  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
108  const Scalar operator() (Index i) const {
109  return m_low + (m_length*Scalar(i))/m_divisor;
110  }
111 
112  template<typename Index>
113  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
114  const Packet packetOp(Index i) const {
115  return internal::padd(pset1<Packet>(m_low), pdiv(pmul(pset1<Packet>(m_length), padd(pset1<Packet>(Scalar(i)),m_interPacket)),
116  pset1<Packet>(m_divisor))); }
117 
118  const Scalar m_low;
119  const Scalar m_length;
120  const Scalar m_divisor;
121  const Packet m_interPacket;
122 };
123 
124 // ----- Linspace functor ----------------------------------------------------------------
125 
126 // Forward declaration (we default to random access which does not really give
127 // us a speed gain when using packet access but it allows to use the functor in
128 // nested expressions).
129 template <typename Scalar, typename PacketType, bool RandomAccess = true> struct linspaced_op;
130 template <typename Scalar, typename PacketType, bool RandomAccess> struct functor_traits< linspaced_op<Scalar,PacketType,RandomAccess> >
131 {
132  enum
133  {
134  Cost = 1,
135  PacketAccess = packet_traits<Scalar>::HasSetLinear
136  && ((!NumTraits<Scalar>::IsInteger) || packet_traits<Scalar>::HasDiv),
137  IsRepeatable = true
138  };
139 };
140 template <typename Scalar, typename PacketType, bool RandomAccess> struct linspaced_op
141 {
142  linspaced_op(const Scalar& low, const Scalar& high, Index num_steps)
143  : impl((num_steps==1 ? high : low),high,num_steps)
144  {}
145 
146  template<typename Index>
147  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() (Index i) const { return impl(i); }
148 
149  // We need this function when assigning e.g. a RowVectorXd to a MatrixXd since
150  // there row==0 and col is used for the actual iteration.
151  template<typename Index>
152  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() (Index row, Index col) const
153  {
154  eigen_assert(col==0 || row==0);
155  return impl(col + row);
156  }
157 
158  template<typename Index, typename Packet>
159  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(Index i) const { return impl.packetOp(i); }
160 
161  // We need this function when assigning e.g. a RowVectorXd to a MatrixXd since
162  // there row==0 and col is used for the actual iteration.
163  template<typename Index, typename Packet>
164  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(Index row, Index col) const
165  {
166  eigen_assert(col==0 || row==0);
167  return impl.packetOp(col + row);
168  }
169 
170  // This proxy object handles the actual required temporaries, the different
171  // implementations (random vs. sequential access) as well as the
172  // correct piping to size 2/4 packet operations.
173  // As long as we don't have a Bresenham-like implementation for linear-access and integer types,
174  // we have to by-pass RandomAccess for integer types. See bug 698.
175  const linspaced_op_impl<Scalar,PacketType,(NumTraits<Scalar>::IsInteger?true:RandomAccess),NumTraits<Scalar>::IsInteger> impl;
176 };
177 
178 // all functors allow linear access, except scalar_identity_op. So we fix here a quick meta
179 // to indicate whether a functor allows linear access, just always answering 'yes' except for
180 // scalar_identity_op.
181 template<typename Functor> struct functor_has_linear_access { enum { ret = 1 }; };
182 template<typename Scalar> struct functor_has_linear_access<scalar_identity_op<Scalar> > { enum { ret = 0 }; };
183 
184 } // end namespace internal
185 
186 } // end namespace Eigen
187 
188 #endif // EIGEN_NULLARY_FUNCTORS_H
Namespace containing all symbols from the Eigen library.
Definition: Core:271
EIGEN_DEFAULT_DENSE_INDEX_TYPE Index
The Index type as used for the API.
Definition: XprHelper.h:35
Definition: Eigen_Colamd.h:50