1 #ifndef __STAN__MATH__FUNCTIONS__SOFTMAX_HPP__
2 #define __STAN__MATH__FUNCTIONS__SOFTMAX_HPP__
4 #include <boost/math/tools/promotion.hpp>
6 #include <boost/throw_exception.hpp>
13 template <
typename Vector,
typename Scalar>
14 Scalar maximum(
const Vector& x) {
16 BOOST_THROW_EXCEPTION(std::invalid_argument (
"x must have at least one element"));
74 template <
typename Vector,
typename Scalar>
75 void softmax(
const Vector& x, Vector& simplex) {
77 if (x.size() != simplex.size())
78 BOOST_THROW_EXCEPTION(std::invalid_argument (
"x.size() != simplex.size()"));
80 Scalar max_x = maximum<Vector,Scalar>(x);
82 simplex[i] =
exp(x[i]-max_x);