1#ifndef STAN_MATH_FWD_FUN_LOG_SOFTMAX_HPP
2#define STAN_MATH_FWD_FUN_LOG_SOFTMAX_HPP
23template <
typename T, require_std_vector_st<is_fvar, T>* =
nullptr>
37template <
typename Vec, require_eigen_vector_vt<is_fvar, Vec>* =
nullptr>
39 using vec = std::decay_t<Vec>;
40 constexpr int Rows = vec::RowsAtCompileTime;
41 constexpr int Cols = vec::ColsAtCompileTime;
43 decltype(
auto) x_ref =
to_ref(std::forward<Vec>(x));
44 if (x_ref.size() == 0) {
45 return Eigen::Matrix<fvar<T>, Rows, Cols>{};
48 const auto d_in = x_ref.d();
49 const auto dot_sd = s.dot(d_in);
50 Eigen::Matrix<fvar<T>, Rows, Cols> result(x_ref.size());
51 result.val() = s.array().log().matrix();
52 result.d() = (d_in.array() - dot_sd).matrix();
typename value_type< T >::type value_type_t
Helper function for accessing underlying type.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
auto softmax(T &&x)
Return the softmax of each vector in a container of fvar values.
auto log_softmax(T &&x)
Return the log softmax of each vector in a container of fvar values.
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...