16 #ifndef OPENKALMAN_EIGEN_TRAITS_FUNCTORS_UNARY_HPP 17 #define OPENKALMAN_EIGEN_TRAITS_FUNCTORS_UNARY_HPP 19 #include <type_traits> 26 template<
typename Operation>
27 struct UnaryFunctorTraits
40 #ifndef __cpp_concepts 41 template<
typename UnaryOp,
typename =
void>
44 template<
typename UnaryOp>
51 template<
typename UnaryOp>
63 template<
typename Scalar>
72 template<
typename Scalar>
85 template<
typename Scalar>
89 template<
typename Scalar>
92 struct Op { constexpr
auto operator()(Scalar arg)
const {
return values::abs(arg); } };
99 template<
typename Scalar>
107 template<
typename Scalar>
112 constexpr
auto operator()(Scalar arg)
const 116 return r * r + i * i;
119 struct Op2 { constexpr
auto operator()(Scalar arg)
const {
return arg * arg; } };
122 if constexpr (values::complex<Scalar>)
return Op1{};
130 template<
typename Scalar>
133 struct Op { constexpr
auto operator()(Scalar arg)
const {
return values::conj(arg); } };
140 template<
typename Scalar>
145 constexpr
auto operator()(Scalar arg)
const {
158 template<
typename Scalar,
typename NewType>
163 constexpr
auto operator()(Scalar arg)
const 165 return static_cast<NewType
>(
static_cast<typename Eigen::NumTraits<NewType>::Real
>(arg));
168 struct Op2 { constexpr
auto operator()(Scalar arg)
const {
return static_cast<NewType
>(arg); } };
171 if constexpr (not Eigen::NumTraits<Scalar>::IsComplex and Eigen::NumTraits<NewType>::IsComplex)
181 #if EIGEN_VERSION_AT_LEAST(3,4,0) 182 template<
typename Scalar,
int N>
185 struct Op { constexpr
auto operator()(Scalar arg)
const {
return arg >> N; } };
192 template<
typename Scalar,
int N>
195 struct Op { constexpr
auto operator()(Scalar arg)
const {
return arg << N; } };
203 template<
typename Scalar>
206 struct Op { constexpr
auto operator()(Scalar arg)
const {
return values::real(arg); } };
213 template<
typename Scalar>
216 struct Op { constexpr
auto operator()(Scalar arg)
const {
return values::imag(arg); } };
223 template<
typename Scalar>
228 template<
typename Scalar>
233 template<
typename Scalar>
236 struct Op { constexpr
auto operator()(Scalar arg)
const {
return values::exp(arg); } };
243 #if EIGEN_VERSION_AT_LEAST(3,4,0) 244 template<
typename Scalar>
247 struct Op { constexpr
auto operator()(Scalar arg)
const {
return values::expm1(arg); } };
255 template<
typename Scalar>
258 struct Op { constexpr
auto operator()(Scalar arg)
const {
return values::log(arg); } };
265 template<
typename Scalar>
268 struct Op { constexpr
auto operator()(Scalar arg)
const {
return values::log1p(arg); } };
275 template<
typename Scalar>
280 constexpr
auto operator()(Scalar arg)
const 282 using S = std::decay_t<decltype(values::real(arg))>;
292 #if EIGEN_VERSION_AT_LEAST(3,4,0) 293 template<
typename Scalar>
298 constexpr
auto operator()(Scalar arg)
const 300 using S = std::decay_t<decltype(values::real(arg))>;
311 template<
typename Scalar>
314 struct Op { constexpr
auto operator()(Scalar arg)
const {
return values::sqrt(arg); } };
321 template<
typename Scalar>
326 constexpr
auto operator()(Scalar arg)
const 328 if (arg == Scalar{0})
return values::internal::NaN<Scalar>();
338 template<
typename Scalar>
341 struct Op { constexpr
auto operator()(Scalar arg)
const {
return values::cos(arg); } };
348 template<
typename Scalar>
351 struct Op { constexpr
auto operator()(Scalar arg)
const {
return values::sin(arg); } };
358 template<
typename Scalar>
361 struct Op { constexpr
auto operator()(Scalar arg)
const {
return values::tan(arg); } };
368 template<
typename Scalar>
371 struct Op { constexpr
auto operator()(Scalar arg)
const {
return values::acos(arg); } };
378 template<
typename Scalar>
381 struct Op { constexpr
auto operator()(Scalar arg)
const {
return values::asin(arg); } };
388 template<
typename Scalar>
391 struct Op { constexpr
auto operator()(Scalar arg)
const {
return values::atan(arg); } };
398 template<
typename Scalar>
401 struct Op { constexpr
auto operator()(Scalar arg)
const {
return values::tanh(arg); } };
408 #if EIGEN_VERSION_AT_LEAST(3,4,0) 409 template<
typename Scalar>
412 struct Op { constexpr
auto operator()(Scalar arg)
const {
return values::atanh(arg); } };
420 template<
typename Scalar>
423 struct Op { constexpr
auto operator()(Scalar arg)
const {
return values::sinh(arg); } };
430 #if EIGEN_VERSION_AT_LEAST(3,4,0) 431 template<
typename Scalar>
434 struct Op { constexpr
auto operator()(Scalar arg)
const {
return values::asinh(arg); } };
442 template<
typename Scalar>
445 struct Op { constexpr
auto operator()(Scalar arg)
const {
return values::cosh(arg); } };
452 #if EIGEN_VERSION_AT_LEAST(3,4,0) 453 template<
typename Scalar>
456 struct Op { constexpr
auto operator()(Scalar arg)
const {
return values::acosh(arg); } };
464 template<
typename Scalar>
469 constexpr
auto operator()(Scalar arg)
const 471 if (arg == Scalar{0})
return values::internal::NaN<Scalar>();
472 else return static_cast<Scalar
>(1) / arg;
481 template<
typename Scalar>
484 struct Op { constexpr
auto operator()(Scalar arg)
const {
return arg * arg; } };
491 template<
typename Scalar>
494 struct Op { constexpr
auto operator()(Scalar arg)
const {
return arg * arg * arg; } };
510 template<
typename Scalar>
518 #if EIGEN_VERSION_AT_LEAST(3,4,0) 519 template<
typename Scalar>
524 constexpr
auto operator()(Scalar arg)
const {
return Scalar{1} / (Scalar{1} + (values::exp(-arg))); }
533 template<
typename BinaryOp>
537 static constexpr
bool preserves_triangle = BinaryTraits::binary_functor_type == BinaryFunctorType::product;
540 template<
typename UnaryOp,
typename XprType>
541 static constexpr
auto get_constant(
const Eigen::CwiseUnaryOp<UnaryOp, XprType>& arg)
543 using CFunctor = Eigen::internal::scalar_constant_op<scalar_type_of_t<XprType>>;
544 using ConstType = Eigen::CwiseNullaryOp<CFunctor, XprType>;
545 using BinaryOpType = Eigen::CwiseBinaryOp<BinaryOp, ConstType, XprType>;
546 const auto& x = arg.nestedExpression();
547 BinaryOpType bin {ConstType{x.rows(), x.cols(), CFunctor{arg.functor().m_value}}, x, arg.functor()};
551 template<
typename UnaryOp,
typename XprType>
552 static constexpr
auto get_constant_diagonal(
const Eigen::CwiseUnaryOp<UnaryOp, XprType>& arg)
554 using CFunctor = Eigen::internal::scalar_constant_op<scalar_type_of_t<XprType>>;
555 using ConstType = Eigen::CwiseNullaryOp<CFunctor, XprType>;
556 using BinaryOpType = Eigen::CwiseBinaryOp<BinaryOp, ConstType, XprType>;
557 const auto& x = arg.nestedExpression();
558 BinaryOpType bin {ConstType{x.rows(), x.cols(), CFunctor{arg.functor().m_value}}, x, arg.functor()};
564 template<
typename BinaryOp>
568 static constexpr
bool preserves_triangle = BinaryTraits::binary_functor_type == BinaryFunctorType::product;
571 template<
typename UnaryOp,
typename XprType>
572 static constexpr
auto get_constant(
const Eigen::CwiseUnaryOp<UnaryOp, XprType>& arg)
574 using CFunctor = Eigen::internal::scalar_constant_op<scalar_type_of_t<XprType>>;
575 using ConstType = Eigen::CwiseNullaryOp<CFunctor, XprType>;
576 using BinaryOpType = Eigen::CwiseBinaryOp<BinaryOp, XprType, ConstType>;
577 const auto& x = arg.nestedExpression();
578 BinaryOpType bin {x, ConstType{x.rows(), x.cols(), CFunctor{arg.functor().m_value}}, arg.functor()};
582 template<
typename UnaryOp,
typename XprType>
583 static constexpr
auto get_constant_diagonal(
const Eigen::CwiseUnaryOp<UnaryOp, XprType>& arg)
585 using CFunctor = Eigen::internal::scalar_constant_op<scalar_type_of_t<XprType>>;
586 using ConstType = Eigen::CwiseNullaryOp<CFunctor, XprType>;
587 using BinaryOpType = Eigen::CwiseBinaryOp<BinaryOp, XprType, ConstType>;
588 const auto& x = arg.nestedExpression();
589 BinaryOpType bin {x, ConstType{x.rows(), x.cols(), CFunctor{arg.functor().m_value}}, arg.functor()};
597 #endif //OPENKALMAN_EIGEN_TRAITS_FUNCTORS_UNARY_HPP constexpr auto log(const Arg &arg)
Constexpr alternative to the std::log function.
Definition: log.hpp:47
constexpr auto cosh(const Arg &arg)
Constexpr alternative to the std::cosh function.
Definition: cosh.hpp:44
constexpr auto atanh(const Arg &arg)
Constexpr alternative to the std::atanh function.
Definition: atanh.hpp:42
Definition: eigen-forward-declarations.hpp:41
constexpr auto imag(Arg arg)
A constexpr function to obtain the imaginary part of a (complex) number.
Definition: imag.hpp:40
Definition: eigen-comma-initializers.hpp:20
Definition: tuple_reverse.hpp:103
constexpr bool value
T is numerical value or is reducible to a numerical value.
Definition: value.hpp:31
constexpr auto sin(const Arg &arg)
Constexpr alternative to the std::sin function.
Definition: sin.hpp:43
The constant associated with T, assuming T is a constant_matrix.
Definition: constant_coefficient.hpp:36
constexpr auto acosh(const Arg &arg)
Constexpr alternative to the std::acosh function.
Definition: acosh.hpp:43
constexpr auto atan(const Arg &arg)
Constexpr alternative to the std::atan function.
Definition: atan.hpp:42
static constexpr auto constexpr_operation()=delete
Construct Operation or (preferably) an equivalent constexpr operation equivalent to Operation...
constexpr auto cos(const Arg &arg, std::enable_if_t< values::value< Arg >, int >=0)
Constexpr alternative to the std::cos function.
Definition: cos.hpp:43
constexpr auto log1p(const Arg &arg)
Constexpr alternative to the std::log1p function, where log1p(x) = log(x+1).
Definition: log1p.hpp:60
The constant associated with T, assuming T is a constant_diagonal_matrix.
Definition: constant_diagonal_coefficient.hpp:32
constexpr auto conj(const Arg &arg)
A constexpr function for the complex conjugate of a (complex) number.
Definition: conj.hpp:39
constexpr auto sqrt(const Arg &arg)
A constexpr alternative to std::sqrt.
Definition: sqrt.hpp:46
constexpr auto atan2(const Y &y_arg, const X &x_arg)
Constexpr alternative to the std::atan2 function.
Definition: atan2.hpp:46
Definition: eigen-forward-declarations.hpp:22
Definition: eigen-forward-declarations.hpp:61
constexpr auto real(Arg arg)
A constexpr function to obtain the real part of a (complex) number.
Definition: real.hpp:40
constexpr auto tan(const Arg &arg)
Constexpr alternative to the std::tan function.
Definition: tan.hpp:42
constexpr auto acos(const Arg &arg)
Constexpr alternative to the std::acos function.
Definition: acos.hpp:41
static constexpr bool preserves_hermitian
Whether the operation applied to a hermitian matrix always yields a hermitian matrix.
Definition: unary.hpp:36
constexpr auto abs(const Arg &arg)
A constexpr alternative to std::abs.
Definition: abs.hpp:38
constexpr bool constexpr_unary_operation_defined
Whether there is a constexpr version of functor UnaryOp.
Definition: unary.hpp:55
constexpr auto asinh(const Arg &arg)
Constexpr alternative to the std::asinh function.
Definition: asinh.hpp:43
constexpr auto asin(const Arg &arg)
Constexpr alternative to the std::asin function.
Definition: asin.hpp:44
static constexpr bool preserves_triangle
Whether the operation applied to a triangular matrix always yields a triangular matrix.
Definition: unary.hpp:33
constexpr auto expm1(const Arg &arg)
Constexpr alternative to the std::expm1 function (exponential function minus 1).
Definition: expm1.hpp:48
constexpr auto sinh(const Arg &arg)
Constexpr alternative to the std::sinh function.
Definition: sinh.hpp:48