@@ -49,15 +49,14 @@ namespace math {
4949 * @return Inverse logit of argument.
5050 */
5151inline double inv_logit (double a) {
52- using std::exp;
5352 if (a < 0 ) {
54- double exp_a = exp (a);
53+ double exp_a = std:: exp (a);
5554 if (a < LOG_EPSILON) {
5655 return exp_a;
5756 }
58- return exp_a / (1 + exp_a);
57+ return exp_a / (1.0 + exp_a);
5958 }
60- return inv (1 + exp (-a));
59+ return inv (1 + std:: exp (-a));
6160}
6261
6362/* *
@@ -74,22 +73,31 @@ struct inv_logit_fun {
7473 }
7574};
7675
76+
7777/* *
78- * Vectorized version of inv_logit().
78+ * Vectorized version of inv_logit() for Eigen types with arithmetic value type .
7979 *
80- * @tparam T type of container
81- * @param x container
80+ * @tparam T type of Eigen expression
81+ * @param x Eigen expression
8282 * @return Inverse logit applied to each value in x.
8383 */
84- template <
85- typename T, require_not_var_matrix_t <T>* = nullptr ,
86- require_all_not_nonscalar_prim_or_rev_kernel_expression_t <T>* = nullptr >
87- inline auto inv_logit (const T& x) {
88- return apply_scalar_unary<inv_logit_fun, T>::apply (x);
84+ template <typename T, require_eigen_t <T>* = nullptr ,
85+ require_not_vt_var<T>* = nullptr >
86+ inline auto inv_logit (T&& x) {
87+ return std::forward<T>(x).array ().logistic ().matrix ();
8988}
9089
91- // TODO(Tadej): Eigen is introducing their implementation logistic() of this
92- // in 3.4. Use that once we switch to Eigen 3.4
90+ /* *
91+ * Vectorized version of inv_logit() for std::vector.
92+ *
93+ * @tparam T type of std::vector
94+ * @param x std::vector
95+ * @return Inverse logit applied to each value in x.
96+ */
97+ template <typename T, require_std_vector_t <T>* = nullptr >
98+ inline auto inv_logit (T&& x) {
99+ return apply_scalar_unary<inv_logit_fun, std::decay_t <T>>::apply (std::forward<T>(x));
100+ }
93101
94102} // namespace math
95103} // namespace stan
0 commit comments