File indexing completed on 2025-12-16 10:13:27
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef EIGEN_NULLARY_FUNCTORS_H
0011 #define EIGEN_NULLARY_FUNCTORS_H
0012
0013 namespace Eigen {
0014
0015 namespace internal {
0016
0017 template<typename Scalar>
0018 struct scalar_constant_op {
0019 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE scalar_constant_op(const scalar_constant_op& other) : m_other(other.m_other) { }
0020 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE scalar_constant_op(const Scalar& other) : m_other(other) { }
0021 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() () const { return m_other; }
0022 template<typename PacketType>
0023 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const PacketType packetOp() const { return internal::pset1<PacketType>(m_other); }
0024 const Scalar m_other;
0025 };
0026 template<typename Scalar>
0027 struct functor_traits<scalar_constant_op<Scalar> >
0028 { enum { Cost = 0 ,
0029 PacketAccess = packet_traits<Scalar>::Vectorizable, IsRepeatable = true }; };
0030
0031 template<typename Scalar> struct scalar_identity_op {
0032 EIGEN_EMPTY_STRUCT_CTOR(scalar_identity_op)
0033 template<typename IndexType>
0034 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() (IndexType row, IndexType col) const { return row==col ? Scalar(1) : Scalar(0); }
0035 };
0036 template<typename Scalar>
0037 struct functor_traits<scalar_identity_op<Scalar> >
0038 { enum { Cost = NumTraits<Scalar>::AddCost, PacketAccess = false, IsRepeatable = true }; };
0039
0040 template <typename Scalar, bool IsInteger> struct linspaced_op_impl;
0041
0042 template <typename Scalar>
0043 struct linspaced_op_impl<Scalar,false>
0044 {
0045 typedef typename NumTraits<Scalar>::Real RealScalar;
0046
0047 EIGEN_DEVICE_FUNC linspaced_op_impl(const Scalar& low, const Scalar& high, Index num_steps) :
0048 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))),
0049 m_flip(numext::abs(high)<numext::abs(low))
0050 {}
0051
0052 template<typename IndexType>
0053 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() (IndexType i) const {
0054 if(m_flip)
0055 return (i==0)? m_low : Scalar(m_high - RealScalar(m_size1-i)*m_step);
0056 else
0057 return (i==m_size1)? m_high : Scalar(m_low + RealScalar(i)*m_step);
0058 }
0059
0060 template<typename Packet, typename IndexType>
0061 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(IndexType i) const
0062 {
0063
0064
0065 if(m_flip)
0066 {
0067 Packet pi = plset<Packet>(Scalar(i-m_size1));
0068 Packet res = padd(pset1<Packet>(m_high), pmul(pset1<Packet>(m_step), pi));
0069 if (EIGEN_PREDICT_TRUE(i != 0)) return res;
0070 Packet mask = pcmp_lt(pset1<Packet>(0), plset<Packet>(0));
0071 return pselect<Packet>(mask, res, pset1<Packet>(m_low));
0072 }
0073 else
0074 {
0075 Packet pi = plset<Packet>(Scalar(i));
0076 Packet res = padd(pset1<Packet>(m_low), pmul(pset1<Packet>(m_step), pi));
0077 if(EIGEN_PREDICT_TRUE(i != m_size1-unpacket_traits<Packet>::size+1)) return res;
0078 Packet mask = pcmp_lt(plset<Packet>(0), pset1<Packet>(unpacket_traits<Packet>::size-1));
0079 return pselect<Packet>(mask, res, pset1<Packet>(m_high));
0080 }
0081 }
0082
0083 const Scalar m_low;
0084 const Scalar m_high;
0085 const Index m_size1;
0086 const Scalar m_step;
0087 const bool m_flip;
0088 };
0089
0090 template <typename Scalar>
0091 struct linspaced_op_impl<Scalar,true>
0092 {
0093 EIGEN_DEVICE_FUNC linspaced_op_impl(const Scalar& low, const Scalar& high, Index num_steps) :
0094 m_low(low),
0095 m_multiplier((high-low)/convert_index<Scalar>(num_steps<=1 ? 1 : num_steps-1)),
0096 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))),
0097 m_use_divisor(num_steps>1 && (numext::abs(high-low)+1)<num_steps)
0098 {}
0099
0100 template<typename IndexType>
0101 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
0102 const Scalar operator() (IndexType i) const
0103 {
0104 if(m_use_divisor) return m_low + convert_index<Scalar>(i)/m_divisor;
0105 else return m_low + convert_index<Scalar>(i)*m_multiplier;
0106 }
0107
0108 const Scalar m_low;
0109 const Scalar m_multiplier;
0110 const Scalar m_divisor;
0111 const bool m_use_divisor;
0112 };
0113
0114
0115
0116
0117
0118
0119 template <typename Scalar> struct linspaced_op;
0120 template <typename Scalar> struct functor_traits< linspaced_op<Scalar> >
0121 {
0122 enum
0123 {
0124 Cost = 1,
0125 PacketAccess = (!NumTraits<Scalar>::IsInteger) && packet_traits<Scalar>::HasSetLinear && packet_traits<Scalar>::HasBlend,
0126
0127 IsRepeatable = true
0128 };
0129 };
0130 template <typename Scalar> struct linspaced_op
0131 {
0132 EIGEN_DEVICE_FUNC linspaced_op(const Scalar& low, const Scalar& high, Index num_steps)
0133 : impl((num_steps==1 ? high : low),high,num_steps)
0134 {}
0135
0136 template<typename IndexType>
0137 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Scalar operator() (IndexType i) const { return impl(i); }
0138
0139 template<typename Packet,typename IndexType>
0140 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE const Packet packetOp(IndexType i) const { return impl.template packetOp<Packet>(i); }
0141
0142
0143
0144 const linspaced_op_impl<Scalar,NumTraits<Scalar>::IsInteger> impl;
0145 };
0146
0147
0148
0149
0150
0151 template<typename Functor> struct functor_has_linear_access { enum { ret = !has_binary_operator<Functor>::value }; };
0152
0153
0154
0155 #if !( (EIGEN_COMP_MSVC>1600) || (EIGEN_GNUC_AT_LEAST(4,8)) || (EIGEN_COMP_ICC>=1600))
0156 template<typename Scalar,typename IndexType>
0157 struct has_nullary_operator<scalar_constant_op<Scalar>,IndexType> { enum { value = 1}; };
0158 template<typename Scalar,typename IndexType>
0159 struct has_unary_operator<scalar_constant_op<Scalar>,IndexType> { enum { value = 0}; };
0160 template<typename Scalar,typename IndexType>
0161 struct has_binary_operator<scalar_constant_op<Scalar>,IndexType> { enum { value = 0}; };
0162
0163 template<typename Scalar,typename IndexType>
0164 struct has_nullary_operator<scalar_identity_op<Scalar>,IndexType> { enum { value = 0}; };
0165 template<typename Scalar,typename IndexType>
0166 struct has_unary_operator<scalar_identity_op<Scalar>,IndexType> { enum { value = 0}; };
0167 template<typename Scalar,typename IndexType>
0168 struct has_binary_operator<scalar_identity_op<Scalar>,IndexType> { enum { value = 1}; };
0169
0170 template<typename Scalar,typename IndexType>
0171 struct has_nullary_operator<linspaced_op<Scalar>,IndexType> { enum { value = 0}; };
0172 template<typename Scalar,typename IndexType>
0173 struct has_unary_operator<linspaced_op<Scalar>,IndexType> { enum { value = 1}; };
0174 template<typename Scalar,typename IndexType>
0175 struct has_binary_operator<linspaced_op<Scalar>,IndexType> { enum { value = 0}; };
0176
0177 template<typename Scalar,typename IndexType>
0178 struct has_nullary_operator<scalar_random_op<Scalar>,IndexType> { enum { value = 1}; };
0179 template<typename Scalar,typename IndexType>
0180 struct has_unary_operator<scalar_random_op<Scalar>,IndexType> { enum { value = 0}; };
0181 template<typename Scalar,typename IndexType>
0182 struct has_binary_operator<scalar_random_op<Scalar>,IndexType> { enum { value = 0}; };
0183 #endif
0184
0185 }
0186
0187 }
0188
0189 #endif