1 #ifndef STAN_MATH_ARR_SCAL_FUN_INVERSE_SOFTMAX_HPP
2 #define STAN_MATH_ARR_SCAL_FUN_INVERSE_SOFTMAX_HPP
3 
4 #include <stan/math/prim/meta.hpp>
5 #include <stan/math/prim/err.hpp>
6 #include <stan/math/prim/fun/log.hpp>
7 #include <cmath>
8 
9 namespace stan {
10 namespace math {
11 
12 /**
13  * Writes the inverse softmax of the simplex argument into the second
14  * argument.  See <code>softmax</code> for the inverse
15  * function and a definition of the relation.
16  *
17  * The inverse softmax function is defined by
18  *
19  * \f$\mbox{inverse\_softmax}(x)[i] = \log x[i]\f$.
20  *
21  * This function defines the inverse of <code>softmax</code>
22  * up to a scaling factor.
23  *
24  * Because of the definition, values of 0.0 in the simplex
25  * are converted to negative infinity, and values of 1.0
26  * are converted to 0.0.
27  *
28  * There is no check that the input vector is a valid simplex vector.
29  *
30  * @tparam Vector type of the simplex vector
31  * @param simplex Simplex vector input.
32  * @param y Vector into which the inverse softmax is written.
33  * @throw std::invalid_argument if size of the input and
34  *    output vectors differ.
35  */
36 template <typename Vector, require_vector_t<Vector>* = nullptr>
inverse_softmax(const Vector & simplex,Vector & y)37 void inverse_softmax(const Vector& simplex, Vector& y) {
38   check_matching_sizes("inverse_softmax", "simplex", simplex, "y", y);
39   y = log(simplex);
40 }
41 
42 }  // namespace math
43 }  // namespace stan
44 #endif
45