10#ifndef EIGEN_NULLARY_FUNCTORS_H
11#define EIGEN_NULLARY_FUNCTORS_H
14#include "../InternalHeaderCheck.h"
20template <
typename Scalar>
21struct scalar_constant_op {
22 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE scalar_constant_op(
const scalar_constant_op& other) : m_other(other.m_other) {}
23 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE scalar_constant_op(
const Scalar& other) : m_other(other) {}
24 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar operator()()
const {
return m_other; }
25 template <
typename PacketType>
26 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const PacketType packetOp()
const {
27 return internal::pset1<PacketType>(m_other);
31template <
typename Scalar>
32struct functor_traits<scalar_constant_op<Scalar> > {
35 PacketAccess = packet_traits<Scalar>::Vectorizable,
40template <
typename Scalar>
41struct scalar_identity_op {
42 template <
typename IndexType>
43 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar operator()(IndexType row, IndexType col)
const {
44 return row == col ? Scalar(1) : Scalar(0);
47template <
typename Scalar>
48struct functor_traits<scalar_identity_op<Scalar> > {
49 enum { Cost = NumTraits<Scalar>::AddCost, PacketAccess =
false, IsRepeatable =
true };
52template <
typename Scalar,
bool IsInteger>
53struct linspaced_op_impl;
55template <
typename Scalar>
56struct linspaced_op_impl<Scalar, false> {
57 typedef typename NumTraits<Scalar>::Real RealScalar;
59 EIGEN_DEVICE_FUNC linspaced_op_impl(
const Scalar& low,
const Scalar& high, Index num_steps)
62 m_size1(num_steps == 1 ? 1 : num_steps - 1),
63 m_step(num_steps == 1 ? Scalar() : Scalar((high - low) / RealScalar(num_steps - 1))),
64 m_flip(numext::
abs(high) < numext::
abs(low)) {}
66 template <
typename IndexType>
67 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar operator()(IndexType i)
const {
69 return (i == 0) ? m_low : Scalar(m_high - RealScalar(m_size1 - i) * m_step);
71 return (i == m_size1) ? m_high : Scalar(m_low + RealScalar(i) * m_step);
74 template <
typename Packet,
typename IndexType>
75 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Packet packetOp(IndexType i)
const {
79 Packet pi = plset<Packet>(Scalar(i - m_size1));
80 Packet res = padd(pset1<Packet>(m_high), pmul(pset1<Packet>(m_step), pi));
81 if (EIGEN_PREDICT_TRUE(i != 0))
return res;
82 Packet mask = pcmp_lt(pset1<Packet>(0), plset<Packet>(0));
83 return pselect<Packet>(mask, res, pset1<Packet>(m_low));
85 Packet pi = plset<Packet>(Scalar(i));
86 Packet res = padd(pset1<Packet>(m_low), pmul(pset1<Packet>(m_step), pi));
87 if (EIGEN_PREDICT_TRUE(i != m_size1 - unpacket_traits<Packet>::size + 1))
return res;
88 Packet mask = pcmp_lt(plset<Packet>(0), pset1<Packet>(unpacket_traits<Packet>::size - 1));
89 return pselect<Packet>(mask, res, pset1<Packet>(m_high));
100template <
typename Scalar>
101struct linspaced_op_impl<Scalar, true> {
102 EIGEN_DEVICE_FUNC linspaced_op_impl(
const Scalar& low,
const Scalar& high, Index num_steps)
104 m_multiplier((high - low) / convert_index<Scalar>(num_steps <= 1 ? 1 : num_steps - 1)),
105 m_divisor(convert_index<Scalar>((high >= low ? num_steps : -num_steps) + (high - low)) /
106 ((numext::
abs(high - low) + 1) == 0 ? 1 : (numext::
abs(high - low) + 1))),
107 m_use_divisor(num_steps > 1 && (numext::
abs(high - low) + 1) < num_steps) {}
109 template <
typename IndexType>
110 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar operator()(IndexType i)
const {
112 return m_low + convert_index<Scalar>(i) / m_divisor;
114 return m_low + convert_index<Scalar>(i) * m_multiplier;
118 const Scalar m_multiplier;
119 const Scalar m_divisor;
120 const bool m_use_divisor;
128template <
typename Scalar>
130template <
typename Scalar>
131struct functor_traits<linspaced_op<Scalar> > {
135 (!NumTraits<Scalar>::IsInteger) && packet_traits<Scalar>::HasSetLinear && packet_traits<Scalar>::HasBlend,
141template <
typename Scalar>
143 EIGEN_DEVICE_FUNC linspaced_op(
const Scalar& low,
const Scalar& high, Index num_steps)
144 : impl((num_steps == 1 ? high : low), high, num_steps) {}
146 template <
typename IndexType>
147 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar operator()(IndexType i)
const {
151 template <
typename Packet,
typename IndexType>
152 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Packet packetOp(IndexType i)
const {
153 return impl.template packetOp<Packet>(i);
158 const linspaced_op_impl<Scalar, NumTraits<Scalar>::IsInteger> impl;
161template <
typename Scalar>
162struct equalspaced_op {
163 typedef typename NumTraits<Scalar>::Real RealScalar;
165 EIGEN_DEVICE_FUNC equalspaced_op(
const Scalar& start,
const Scalar& step) : m_start(start), m_step(step) {}
166 template <
typename IndexType>
167 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE
const Scalar operator()(IndexType i)
const {
168 return m_start + m_step *
static_cast<Scalar
>(i);
170 template <
typename Packet,
typename IndexType>
171 EIGEN_DEVICE_FUNC EIGEN_STRONG_INLINE Packet packetOp(IndexType i)
const {
172 const Packet cst_start = pset1<Packet>(m_start);
173 const Packet cst_step = pset1<Packet>(m_step);
174 const Packet cst_lin0 = plset<Packet>(Scalar(0));
175 const Packet cst_offset = pmadd(cst_lin0, cst_step, cst_start);
177 Packet i_packet = pset1<Packet>(
static_cast<Scalar
>(i));
178 return pmadd(i_packet, cst_step, cst_offset);
180 const Scalar m_start;
184template <
typename Scalar>
185struct functor_traits<equalspaced_op<Scalar> > {
187 Cost = NumTraits<Scalar>::AddCost + NumTraits<Scalar>::MulCost,
189 packet_traits<Scalar>::HasSetLinear && packet_traits<Scalar>::HasMul && packet_traits<Scalar>::HasAdd,
198template <
typename Functor>
199struct functor_has_linear_access {
200 enum { ret = !has_binary_operator<Functor>::value };
205#if !(EIGEN_COMP_MSVC || EIGEN_COMP_GNUC || (EIGEN_COMP_ICC >= 1600))
206template <
typename Scalar,
typename IndexType>
207struct has_nullary_operator<scalar_constant_op<Scalar>, IndexType> {
210template <
typename Scalar,
typename IndexType>
211struct has_unary_operator<scalar_constant_op<Scalar>, IndexType> {
214template <
typename Scalar,
typename IndexType>
215struct has_binary_operator<scalar_constant_op<Scalar>, IndexType> {
219template <
typename Scalar,
typename IndexType>
220struct has_nullary_operator<scalar_identity_op<Scalar>, IndexType> {
223template <
typename Scalar,
typename IndexType>
224struct has_unary_operator<scalar_identity_op<Scalar>, IndexType> {
227template <
typename Scalar,
typename IndexType>
228struct has_binary_operator<scalar_identity_op<Scalar>, IndexType> {
232template <
typename Scalar,
typename IndexType>
233struct has_nullary_operator<linspaced_op<Scalar>, IndexType> {
236template <
typename Scalar,
typename IndexType>
237struct has_unary_operator<linspaced_op<Scalar>, IndexType> {
240template <
typename Scalar,
typename IndexType>
241struct has_binary_operator<linspaced_op<Scalar>, IndexType> {
245template <
typename Scalar,
typename IndexType>
246struct has_nullary_operator<scalar_random_op<Scalar>, IndexType> {
249template <
typename Scalar,
typename IndexType>
250struct has_unary_operator<scalar_random_op<Scalar>, IndexType> {
253template <
typename Scalar,
typename IndexType>
254struct has_binary_operator<scalar_random_op<Scalar>, IndexType> {
Namespace containing all symbols from the Eigen library.
Definition Core:137
const Eigen::CwiseUnaryOp< Eigen::internal::scalar_abs_op< typename Derived::Scalar >, const Derived > abs(const Eigen::ArrayBase< Derived > &x)