chromium/third_party/eigen3/src/Eigen/src/Core/functors/UnaryFunctors.h

// This file is part of Eigen, a lightweight C++ template library
// for linear algebra.
//
// Copyright (C) 2008-2016 Gael Guennebaud <[email protected]>
//
// This Source Code Form is subject to the terms of the Mozilla
// Public License v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.

#ifndef EIGEN_UNARY_FUNCTORS_H
#define EIGEN_UNARY_FUNCTORS_H

// IWYU pragma: private
#include "../InternalHeaderCheck.h"

namespace Eigen {

namespace internal {

/** \internal
 * \brief Template functor to compute the opposite of a scalar
 *
 * \sa class CwiseUnaryOp, MatrixBase::operator-
 */
template <typename Scalar>
struct scalar_opposite_op {};
functor_traits<scalar_opposite_op<Scalar>>;

/** \internal
 * \brief Template functor to compute the absolute value of a scalar
 *
 * \sa class CwiseUnaryOp, Cwise::abs
 */
template <typename Scalar>
struct scalar_abs_op {};
functor_traits<scalar_abs_op<Scalar>>;

/** \internal
 * \brief Template functor to compute the score of a scalar, to chose a pivot
 *
 * \sa class CwiseUnaryOp
 */
template <typename Scalar>
struct scalar_score_coeff_op : scalar_abs_op<Scalar> {};
functor_traits<scalar_score_coeff_op<Scalar>>;

/* Avoid recomputing abs when we know the score and they are the same. Not a true Eigen functor.  */
template <typename Scalar, typename = void>
struct abs_knowing_score {};
abs_knowing_score<Scalar, typename scalar_score_coeff_op<Scalar>::Score_is_abs>;

/** \internal
 * \brief Template functor to compute the squared absolute value of a scalar
 *
 * \sa class CwiseUnaryOp, Cwise::abs2
 */
template <typename Scalar>
struct scalar_abs2_op {};
functor_traits<scalar_abs2_op<Scalar>>;

/** \internal
 * \brief Template functor to compute the conjugate of a complex value
 *
 * \sa class CwiseUnaryOp, MatrixBase::conjugate()
 */
template <typename Scalar>
struct scalar_conjugate_op {};
functor_traits<scalar_conjugate_op<Scalar>>;

/** \internal
 * \brief Template functor to compute the phase angle of a complex
 *
 * \sa class CwiseUnaryOp, Cwise::arg
 */
template <typename Scalar>
struct scalar_arg_op {};
functor_traits<scalar_arg_op<Scalar>>;

/** \internal
 * \brief Template functor to compute the complex argument, returned as a complex type
 *
 * \sa class CwiseUnaryOp, Cwise::carg
 */
template <typename Scalar>
struct scalar_carg_op {};
functor_traits<scalar_carg_op<Scalar>>;

/** \internal
 * \brief Template functor to cast a scalar to another type
 *
 * \sa class CwiseUnaryOp, MatrixBase::cast()
 */
template <typename Scalar, typename NewType>
struct scalar_cast_op {};

functor_traits<scalar_cast_op<Scalar, NewType>>;

/** \internal
 * `core_cast_op` serves to distinguish the vectorized implementation from that of the legacy `scalar_cast_op` for
 * backwards compatibility. The manner in which packet ops are handled is defined by the specialized unary_evaluator:
 * `unary_evaluator<CwiseUnaryOp<core_cast_op<SrcType, DstType>, ArgType>, IndexBased>` in CoreEvaluators.h
 * Otherwise, the non-vectorized behavior is identical to that of `scalar_cast_op`
 */
template <typename SrcType, typename DstType>
struct core_cast_op : scalar_cast_op<SrcType, DstType> {};

functor_traits<core_cast_op<SrcType, DstType>>;

/** \internal
 * \brief Template functor to arithmetically shift a scalar right by a number of bits
 *
 * \sa class CwiseUnaryOp, MatrixBase::shift_right()
 */
template <typename Scalar, int N>
struct scalar_shift_right_op {};
functor_traits<scalar_shift_right_op<Scalar, N>>;

/** \internal
 * \brief Template functor to logically shift a scalar left by a number of bits
 *
 * \sa class CwiseUnaryOp, MatrixBase::shift_left()
 */
template <typename Scalar, int N>
struct scalar_shift_left_op {};
functor_traits<scalar_shift_left_op<Scalar, N>>;

/** \internal
 * \brief Template functor to extract the real part of a complex
 *
 * \sa class CwiseUnaryOp, MatrixBase::real()
 */
template <typename Scalar>
struct scalar_real_op {};
functor_traits<scalar_real_op<Scalar>>;

/** \internal
 * \brief Template functor to extract the imaginary part of a complex
 *
 * \sa class CwiseUnaryOp, MatrixBase::imag()
 */
template <typename Scalar>
struct scalar_imag_op {};
functor_traits<scalar_imag_op<Scalar>>;

/** \internal
 * \brief Template functor to extract the real part of a complex as a reference
 *
 * \sa class CwiseUnaryOp, MatrixBase::real()
 */
template <typename Scalar>
struct scalar_real_ref_op {};
functor_traits<scalar_real_ref_op<Scalar>>;

/** \internal
 * \brief Template functor to extract the imaginary part of a complex as a reference
 *
 * \sa class CwiseUnaryOp, MatrixBase::imag()
 */
template <typename Scalar>
struct scalar_imag_ref_op {};
functor_traits<scalar_imag_ref_op<Scalar>>;

/** \internal
 *
 * \brief Template functor to compute the exponential of a scalar
 *
 * \sa class CwiseUnaryOp, Cwise::exp()
 */
template <typename Scalar>
struct scalar_exp_op {};
functor_traits<scalar_exp_op<Scalar>>;

/** \internal
 *
 * \brief Template functor to compute the exponential of a scalar - 1.
 *
 * \sa class CwiseUnaryOp, ArrayBase::expm1()
 */
template <typename Scalar>
struct scalar_expm1_op {};
functor_traits<scalar_expm1_op<Scalar>>;

/** \internal
 *
 * \brief Template functor to compute the logarithm of a scalar
 *
 * \sa class CwiseUnaryOp, ArrayBase::log()
 */
template <typename Scalar>
struct scalar_log_op {};
functor_traits<scalar_log_op<Scalar>>;

/** \internal
 *
 * \brief Template functor to compute the logarithm of 1 plus a scalar value
 *
 * \sa class CwiseUnaryOp, ArrayBase::log1p()
 */
template <typename Scalar>
struct scalar_log1p_op {};
functor_traits<scalar_log1p_op<Scalar>>;

/** \internal
 *
 * \brief Template functor to compute the base-10 logarithm of a scalar
 *
 * \sa class CwiseUnaryOp, Cwise::log10()
 */
template <typename Scalar>
struct scalar_log10_op {};
functor_traits<scalar_log10_op<Scalar>>;

/** \internal
 *
 * \brief Template functor to compute the base-2 logarithm of a scalar
 *
 * \sa class CwiseUnaryOp, Cwise::log2()
 */
template <typename Scalar>
struct scalar_log2_op {};
functor_traits<scalar_log2_op<Scalar>>;

/** \internal
 * \brief Template functor to compute the square root of a scalar
 * \sa class CwiseUnaryOp, Cwise::sqrt()
 */
template <typename Scalar>
struct scalar_sqrt_op {};
functor_traits<scalar_sqrt_op<Scalar>>;

// Boolean specialization to eliminate -Wimplicit-conversion-floating-point-to-bool warnings.
template <>
struct scalar_sqrt_op<bool> {};
template <>
struct functor_traits<scalar_sqrt_op<bool>> {};

/** \internal
 * \brief Template functor to compute the cube root of a scalar
 * \sa class CwiseUnaryOp, Cwise::sqrt()
 */
template <typename Scalar>
struct scalar_cbrt_op {};

functor_traits<scalar_cbrt_op<Scalar>>;

/** \internal
 * \brief Template functor to compute the reciprocal square root of a scalar
 * \sa class CwiseUnaryOp, Cwise::rsqrt()
 */
template <typename Scalar>
struct scalar_rsqrt_op {};

functor_traits<scalar_rsqrt_op<Scalar>>;

/** \internal
 * \brief Template functor to compute the cosine of a scalar
 * \sa class CwiseUnaryOp, ArrayBase::cos()
 */
template <typename Scalar>
struct scalar_cos_op {};
functor_traits<scalar_cos_op<Scalar>>;

/** \internal
 * \brief Template functor to compute the sine of a scalar
 * \sa class CwiseUnaryOp, ArrayBase::sin()
 */
template <typename Scalar>
struct scalar_sin_op {};
functor_traits<scalar_sin_op<Scalar>>;

/** \internal
 * \brief Template functor to compute the tan of a scalar
 * \sa class CwiseUnaryOp, ArrayBase::tan()
 */
template <typename Scalar>
struct scalar_tan_op {};
functor_traits<scalar_tan_op<Scalar>>;

/** \internal
 * \brief Template functor to compute the arc cosine of a scalar
 * \sa class CwiseUnaryOp, ArrayBase::acos()
 */
template <typename Scalar>
struct scalar_acos_op {};
functor_traits<scalar_acos_op<Scalar>>;

/** \internal
 * \brief Template functor to compute the arc sine of a scalar
 * \sa class CwiseUnaryOp, ArrayBase::asin()
 */
template <typename Scalar>
struct scalar_asin_op {};
functor_traits<scalar_asin_op<Scalar>>;

/** \internal
 * \brief Template functor to compute the atan of a scalar
 * \sa class CwiseUnaryOp, ArrayBase::atan()
 */
template <typename Scalar>
struct scalar_atan_op {};
functor_traits<scalar_atan_op<Scalar>>;

/** \internal
 * \brief Template functor to compute the tanh of a scalar
 * \sa class CwiseUnaryOp, ArrayBase::tanh()
 */
template <typename Scalar>
struct scalar_tanh_op {};

functor_traits<scalar_tanh_op<Scalar>>;

/** \internal
 * \brief Template functor to compute the atanh of a scalar
 * \sa class CwiseUnaryOp, ArrayBase::atanh()
 */
template <typename Scalar>
struct scalar_atanh_op {};

functor_traits<scalar_atanh_op<Scalar>>;

/** \internal
 * \brief Template functor to compute the sinh of a scalar
 * \sa class CwiseUnaryOp, ArrayBase::sinh()
 */
template <typename Scalar>
struct scalar_sinh_op {};
functor_traits<scalar_sinh_op<Scalar>>;

/** \internal
 * \brief Template functor to compute the asinh of a scalar
 * \sa class CwiseUnaryOp, ArrayBase::asinh()
 */
template <typename Scalar>
struct scalar_asinh_op {};

functor_traits<scalar_asinh_op<Scalar>>;

/** \internal
 * \brief Template functor to compute the cosh of a scalar
 * \sa class CwiseUnaryOp, ArrayBase::cosh()
 */
template <typename Scalar>
struct scalar_cosh_op {};
functor_traits<scalar_cosh_op<Scalar>>;

/** \internal
 * \brief Template functor to compute the acosh of a scalar
 * \sa class CwiseUnaryOp, ArrayBase::acosh()
 */
template <typename Scalar>
struct scalar_acosh_op {};

functor_traits<scalar_acosh_op<Scalar>>;

/** \internal
 * \brief Template functor to compute the inverse of a scalar
 * \sa class CwiseUnaryOp, Cwise::inverse()
 */
template <typename Scalar>
struct scalar_inverse_op {};
functor_traits<scalar_inverse_op<Scalar>>;

/** \internal
 * \brief Template functor to compute the square of a scalar
 * \sa class CwiseUnaryOp, Cwise::square()
 */
template <typename Scalar>
struct scalar_square_op {};
functor_traits<scalar_square_op<Scalar>>;

// Boolean specialization to avoid -Wint-in-bool-context warnings on GCC.
template <>
struct scalar_square_op<bool> {};
template <>
struct functor_traits<scalar_square_op<bool>> {};

/** \internal
 * \brief Template functor to compute the cube of a scalar
 * \sa class CwiseUnaryOp, Cwise::cube()
 */
template <typename Scalar>
struct scalar_cube_op {};
functor_traits<scalar_cube_op<Scalar>>;

// Boolean specialization to avoid -Wint-in-bool-context warnings on GCC.
template <>
struct scalar_cube_op<bool> {};
template <>
struct functor_traits<scalar_cube_op<bool>> {};

/** \internal
 * \brief Template functor to compute the rounded value of a scalar
 * \sa class CwiseUnaryOp, ArrayBase::round()
 */
template <typename Scalar>
struct scalar_round_op {};
functor_traits<scalar_round_op<Scalar>>;

/** \internal
 * \brief Template functor to compute the floor of a scalar
 * \sa class CwiseUnaryOp, ArrayBase::floor()
 */
template <typename Scalar>
struct scalar_floor_op {};
functor_traits<scalar_floor_op<Scalar>>;

/** \internal
 * \brief Template functor to compute the rounded (with current rounding mode)  value of a scalar
 * \sa class CwiseUnaryOp, ArrayBase::rint()
 */
template <typename Scalar>
struct scalar_rint_op {};
functor_traits<scalar_rint_op<Scalar>>;

/** \internal
 * \brief Template functor to compute the ceil of a scalar
 * \sa class CwiseUnaryOp, ArrayBase::ceil()
 */
template <typename Scalar>
struct scalar_ceil_op {};
functor_traits<scalar_ceil_op<Scalar>>;

/** \internal
 * \brief Template functor to compute the truncation of a scalar
 * \sa class CwiseUnaryOp, ArrayBase::floor()
 */
template <typename Scalar>
struct scalar_trunc_op {};
functor_traits<scalar_trunc_op<Scalar>>;

/** \internal
 * \brief Template functor to compute whether a scalar is NaN
 * \sa class CwiseUnaryOp, ArrayBase::isnan()
 */
template <typename Scalar, bool UseTypedPredicate = false>
struct scalar_isnan_op {};

scalar_isnan_op<Scalar, true>;

functor_traits<scalar_isnan_op<Scalar, UseTypedPredicate>>;

/** \internal
 * \brief Template functor to check whether a scalar is +/-inf
 * \sa class CwiseUnaryOp, ArrayBase::isinf()
 */
template <typename Scalar, bool UseTypedPredicate = false>
struct scalar_isinf_op {};

scalar_isinf_op<Scalar, true>;
functor_traits<scalar_isinf_op<Scalar, UseTypedPredicate>>;

/** \internal
 * \brief Template functor to check whether a scalar has a finite value
 * \sa class CwiseUnaryOp, ArrayBase::isfinite()
 */
template <typename Scalar, bool UseTypedPredicate = false>
struct scalar_isfinite_op {};

scalar_isfinite_op<Scalar, true>;
functor_traits<scalar_isfinite_op<Scalar, UseTypedPredicate>>;

/** \internal
 * \brief Template functor to compute the logical not of a scalar as if it were a boolean
 *
 * \sa class CwiseUnaryOp, ArrayBase::operator!
 */
template <typename Scalar>
struct scalar_boolean_not_op {};
functor_traits<scalar_boolean_not_op<Scalar>>;

template <typename Scalar, bool IsComplex = NumTraits<Scalar>::IsComplex>
struct bitwise_unary_impl {};

bitwise_unary_impl<Scalar, true>;

/** \internal
 * \brief Template functor to compute the bitwise not of a scalar
 *
 * \sa class CwiseUnaryOp, ArrayBase::operator~
 */
template <typename Scalar>
struct scalar_bitwise_not_op {};
functor_traits<scalar_bitwise_not_op<Scalar>>;

/** \internal
 * \brief Template functor to compute the signum of a scalar
 * \sa class CwiseUnaryOp, Cwise::sign()
 */
template <typename Scalar>
struct scalar_sign_op {};

functor_traits<scalar_sign_op<Scalar>>;

// Real-valued implementation.
template <typename T, typename EnableIf = void>
struct scalar_logistic_op_impl {};

// Complex-valud implementation.
scalar_logistic_op_impl<T, std::enable_if_t<NumTraits<T>::IsComplex>>;

/** \internal
 * \brief Template functor to compute the logistic function of a scalar
 * \sa class CwiseUnaryOp, ArrayBase::logistic()
 */
template <typename T>
struct scalar_logistic_op : scalar_logistic_op_impl<T> {};

// TODO(rmlarsen): Enable the following on host when integer_packet is defined
// for the relevant packet types.
#ifndef EIGEN_GPUCC

/** \internal
 * \brief Template specialization of the logistic function for float.
 * Computes S(x) = exp(x) / (1 + exp(x)), where exp(x) is implemented
 * using an algorithm partly adopted from the implementation of
 * pexp_float. See the individual steps described in the code below.
 * Note that compared to pexp, we use an additional outer multiplicative
 * range reduction step using the identity exp(x) = exp(x/2)^2.
 * This prevert us from having to call ldexp on values that could produce
 * a denormal result, which allows us to call the faster implementation in
 * pldexp_fast_impl<Packet>::run(p, m).
 * The final squaring, however, doubles the error bound on the final
 * approximation. Exhaustive testing shows that we have a worst case error
 * of 4.5 ulps (compared to computing S(x) in double precision), which is
 * acceptable.
 */
template <>
struct scalar_logistic_op<float> {};
#endif  // #ifndef EIGEN_GPU_COMPILE_PHASE

functor_traits<scalar_logistic_op<T>>;

template <typename Scalar, typename ExponentScalar, bool IsBaseInteger = NumTraits<Scalar>::IsInteger,
          bool IsExponentInteger = NumTraits<ExponentScalar>::IsInteger,
          bool IsBaseComplex = NumTraits<Scalar>::IsComplex,
          bool IsExponentComplex = NumTraits<ExponentScalar>::IsComplex>
struct scalar_unary_pow_op {};

template <typename T>
constexpr int exponent_digits() {}

template <typename From, typename To>
struct is_floating_exactly_representable {};

// Specialization for real, non-integer types, non-complex types.
scalar_unary_pow_op<Scalar, ExponentScalar, false, false, false, false>;

scalar_unary_pow_op<Scalar, ExponentScalar, BaseIsInteger, true, false, false>;

functor_traits<scalar_unary_pow_op<Scalar, ExponentScalar>>;

}  // end namespace internal

}  // end namespace Eigen

#endif  // EIGEN_FUNCTORS_H