1 #ifndef STAN_MATH_PRIM_FUN_STEP_HPP 2 #define STAN_MATH_PRIM_FUN_STEP_HPP 3 4 #include <stan/math/prim/meta.hpp> 5 #include <stan/math/prim/functor/apply_scalar_unary.hpp> 6 7 namespace stan { 8 namespace math { 9 10 /** 11 * The step, or Heaviside, function. 12 * 13 * The function is defined by 14 * 15 * <code>step(y) = (y < 0.0) ? 0 : 1</code>. 16 * 17 \f[ 18 \mbox{step}(x) = 19 \begin{cases} 20 0 & \mbox{if } x \le 0 \\ 21 1 & \mbox{if } x \ge 0 \\[6pt] 22 0 & \mbox{if } x = \textrm{NaN} 23 \end{cases} 24 \f] 25 * 26 * @tparam T type of value 27 * @param y value 28 * @return zero if the value is less than zero, and one otherwise 29 */ 30 template <typename T, require_stan_scalar_t<T>* = nullptr> step(const T & y)31inline T step(const T& y) { 32 return y < 0.0 ? 0 : 1; 33 } 34 35 /** 36 * Structure to wrap `step()` so it can be vectorized. 37 */ 38 struct step_fun { 39 /** 40 * Return zero if the value is less than zero and one otherwise 41 * 42 * @tparam T type of argument 43 * @param y argument 44 * @return step(y) 45 */ 46 template <typename T> funstan::math::step_fun47 static inline T fun(const T& y) { 48 return step(y); 49 } 50 }; 51 52 /** 53 * Return the elementwise application of `step()` to 54 * specified argument container. 55 * 56 * @tparam T type of container 57 * @param x container 58 * @return Elementwise step of members of container. 59 */ 60 template <typename T, require_container_t<T>* = nullptr> step(const T & x)61inline auto step(const T& x) { 62 return apply_scalar_unary<step_fun, T>::apply(x); 63 } 64 65 } // namespace math 66 } // namespace stan 67 #endif 68