Eigen  3.2.91
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_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 // FIXME replace this packet test by a safe one
30 { enum { Cost = 1, 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> 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,false>
53 {
54  linspaced_op_impl(const Scalar& low, const Scalar& step) :
55  m_low(low), m_step(step),
56  m_packetStep(pset1<Packet>(unpacket_traits<Packet>::size*step)),
57  m_base(padd(pset1<Packet>(low), pmul(pset1<Packet>(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_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,true>
80 {
81  linspaced_op_impl(const Scalar& low, const Scalar& step) :
82  m_low(low), m_step(step),
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_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 // ----- Linspace functor ----------------------------------------------------------------
100 
101 // Forward declaration (we default to random access which does not really give
102 // us a speed gain when using packet access but it allows to use the functor in
103 // nested expressions).
104 template <typename Scalar, typename PacketType, bool RandomAccess = true> struct linspaced_op;
105 template <typename Scalar, typename PacketType, bool RandomAccess> struct functor_traits< linspaced_op<Scalar,PacketType,RandomAccess> >
106 { enum { Cost = 1, PacketAccess = packet_traits<Scalar>::HasSetLinear, IsRepeatable = true }; };
107 template <typename Scalar, typename PacketType, bool RandomAccess> struct linspaced_op
108 {
109  linspaced_op(const Scalar& low, const Scalar& high, Index num_steps) : impl((num_steps==1 ? high : low), (num_steps==1 ? Scalar() : (high-low)/Scalar(num_steps-1))) {}
110 
111  template<typename Index>
112  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() (Index i) const { return impl(i); }
113 
114  // We need this function when assigning e.g. a RowVectorXd to a MatrixXd since
115  // there row==0 and col is used for the actual iteration.
116  template<typename Index>
117  EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() (Index row, Index col) const
118  {
119  eigen_assert(col==0 || row==0);
120  return impl(col + row);
121  }
122 
123  template<typename Index, typename Packet>
124  EIGEN_STRONG_INLINE const Packet packetOp(Index i) const { return impl.packetOp(i); }
125 
126  // We need this function when assigning e.g. a RowVectorXd to a MatrixXd since
127  // there row==0 and col is used for the actual iteration.
128  template<typename Index, typename Packet>
129  EIGEN_STRONG_INLINE const Packet packetOp(Index row, Index col) const
130  {
131  eigen_assert(col==0 || row==0);
132  return impl.packetOp(col + row);
133  }
134 
135  // This proxy object handles the actual required temporaries, the different
136  // implementations (random vs. sequential access) as well as the
137  // correct piping to size 2/4 packet operations.
138  // TODO find a way to make the packet type configurable
139  const linspaced_op_impl<Scalar,PacketType,RandomAccess> impl;
140 };
141 
142 // all functors allow linear access, except scalar_identity_op. So we fix here a quick meta
143 // to indicate whether a functor allows linear access, just always answering 'yes' except for
144 // scalar_identity_op.
145 // FIXME move this to functor_traits adding a functor_default
146 template<typename Functor> struct functor_has_linear_access { enum { ret = 1 }; };
147 template<typename Scalar> struct functor_has_linear_access<scalar_identity_op<Scalar> > { enum { ret = 0 }; };
148 
149 } // end namespace internal
150 
151 } // end namespace Eigen
152 
153 #endif // EIGEN_NULLARY_FUNCTORS_H
Definition: LDLT.h:16
Definition: Eigen_Colamd.h:54