logistic_sigmoid.hpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright Matt Borland 2025.
  2. // Use, modification and distribution are subject to the
  3. // Boost Software License, Version 1.0.
  4. // (See accompanying file LICENSE_1_0.txt
  5. // or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. #ifndef BOOST_MATH_SF_EXPIT_HPP
  7. #define BOOST_MATH_SF_EXPIT_HPP
  8. #include <boost/math/policies/policy.hpp>
  9. #include <boost/math/tools/precision.hpp>
  10. #include <cmath>
  11. namespace boost {
  12. namespace math {
  13. template <typename RealType, typename Policy>
  14. RealType logistic_sigmoid(RealType x, const Policy&)
  15. {
  16. BOOST_MATH_STD_USING
  17. using promoted_real_type = typename policies::evaluation<RealType, Policy>::type;
  18. if(-x >= tools::log_max_value<RealType>())
  19. {
  20. return static_cast<RealType>(0);
  21. }
  22. if(-x <= -tools::log_max_value<RealType>())
  23. {
  24. return static_cast<RealType>(1);
  25. }
  26. const auto res {static_cast<RealType>(1 / (1 + exp(static_cast<promoted_real_type>(-x))))};
  27. return res;
  28. }
  29. template <typename RealType>
  30. RealType logistic_sigmoid(RealType x)
  31. {
  32. return logistic_sigmoid(x, policies::policy<>());
  33. }
  34. } // namespace math
  35. } // namespace boost
  36. #endif // BOOST_MATH_SF_EXPIT_HPP