1#ifndef STAN_MATH_OPENCL_PRIM_DIRICHLET_LPDF_HPP
2#define STAN_MATH_OPENCL_PRIM_DIRICHLET_LPDF_HPP
57template <
bool propto,
typename T_prob_cl,
typename T_prior_size_cl,
59 T_prob_cl, T_prior_size_cl>* =
nullptr,
60 require_any_not_stan_scalar_t<T_prob_cl, T_prior_size_cl>* =
nullptr>
62 const T_prob_cl& theta,
const T_prior_size_cl& alpha) {
63 static constexpr const char* function =
"dirichlet_lpdf(OpenCL)";
75 const auto& theta_val =
value_of(theta);
76 const auto& alpha_val =
value_of(alpha);
78 auto check_alpha_positive
79 =
check_cl(function,
"prior sample sizes", alpha_val,
"positive");
80 auto alpha_positive = alpha_val > 0.0;
81 auto check_theta_nonnegative
82 =
check_cl(function,
"probabilities", theta_val,
"nonnegative");
83 auto theta_nonnegative = theta_val >= 0.0;
95 if (theta.cols() > alpha.cols()) {
99 results(check_alpha_positive, alpha_csum_cl, lgamma_alpha_csum_cl,
115 results(check_theta_nonnegative, theta_csum_cl, theta_log_alpha_m_1_sum_cl,
116 theta_deriv_cl, alpha_deriv_cl)
118 theta_nonnegative, theta_csum,
120 theta_log_alpha_m_1_sum),
127 while (alpha_csum_cl.
rows() > 1) {
133 alpha_csum_cl = std::move(alpha_csum_cl2);
134 lgamma_alpha_csum_cl = std::move(lgamma_alpha_csum_cl2);
137 while (theta_csum_cl.
rows() > 1) {
143 if (alpha.cols() > theta.cols()) {
145 results(check_theta_nonnegative, theta_csum_cl, log_theta_cl)
147 theta_nonnegative, theta_csum,
153 auto theta_log_alpha_m_1_sum
161 results(check_alpha_positive, alpha_csum_cl, lgamma_alpha_csum_cl,
162 theta_log_alpha_m_1_sum_cl, theta_deriv_cl, alpha_deriv_cl)
171 theta_log_alpha_m_1_sum),
175 while (alpha_csum_cl.
rows() > 1) {
184 alpha_csum_cl = std::move(alpha_csum_cl2);
185 lgamma_alpha_csum_cl = std::move(lgamma_alpha_csum_cl2);
189 check_cl(function,
"sum of probabilities", theta_sum,
"equal to 1")
199 results(check_alpha_positive, check_theta_nonnegative, theta_csum_cl,
200 alpha_csum_cl, lgamma_alpha_csum_cl, theta_log_alpha_m_1_sum_cl,
201 theta_deriv_cl, alpha_deriv_cl)
203 alpha_positive, theta_nonnegative, theta_csum,
210 theta_log_alpha_m_1_sum),
214 while (theta_csum_cl.
rows() > 1) {
218 results(theta_csum_cl2, alpha_csum_cl2, lgamma_alpha_csum_cl2)
225 theta_csum_cl = std::move(theta_csum_cl2);
227 alpha_csum_cl = std::move(alpha_csum_cl2);
228 lgamma_alpha_csum_cl = std::move(lgamma_alpha_csum_cl2);
234 if (theta.cols() >= alpha.cols()) {
245 if (theta.cols() > alpha.cols()) {
262 if (theta.cols() < alpha.cols()) {
263 partials<0>(ops_partials) =
rowwise_sum(theta_deriv_cl);
265 partials<0>(ops_partials) = std::move(theta_deriv_cl);
269 if (theta.cols() > alpha.cols()) {
271 =
digamma(alpha_csum_cl) *
static_cast<double>(theta.cols());
272 partials<1>(ops_partials)
279 return ops_partials.build(lp);
Represents an arithmetic matrix on the OpenCL device.
elt_multiply_< as_operation_cl_t< T_a >, as_operation_cl_t< T_b > > elt_multiply(T_a &&a, T_b &&b)
auto sum_2d(T &&a)
Two dimensional sum - reduction of a kernel generator expression.
auto check_cl(const char *function, const char *var_name, T &&y, const char *must_be)
Constructs a check on opencl matrix or expression.
results_cl< T_results... > results(T_results &&... results)
Deduces types for constructing results_cl object.
auto transpose(Arg &&a)
Transposes a kernel generator expression.
elt_divide_< as_operation_cl_t< T_a >, as_operation_cl_t< T_b > > elt_divide(T_a &&a, T_b &&b)
auto rowwise_sum(T &&a)
Rowwise sum reduction of a kernel generator expression.
calc_if_< true, as_operation_cl_t< T > > calc_if(T &&a)
auto colwise_broadcast(T &&a)
Broadcast an expression in colwise dimmension.
auto colwise_sum(T &&a)
Column wise sum - reduction of a kernel generator expression.
expressions_cl< T_expressions... > expressions(T_expressions &&... expressions)
Deduces types for constructing expressions_cl object.
return_type_t< T_prob_cl, T_prior_size_cl > dirichlet_lpdf(const T_prob_cl &theta, const T_prior_size_cl &alpha)
The log of the Dirichlet density for the given theta and a vector of prior sample sizes,...
auto from_matrix_cl(const T &src)
Copies the source matrix that is stored on the OpenCL device to the destination Eigen matrix.
require_all_t< is_prim_or_rev_kernel_expression< std::decay_t< Types > >... > require_all_prim_or_rev_kernel_expression_t
Require type satisfies is_prim_or_rev_kernel_expression.
typename return_type< Ts... >::type return_type_t
Convenience type for the return type of the specified template parameters.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
fvar< T > log(const fvar< T > &x)
auto rowwise_optional_broadcast(T &&a)
Broadcast an expression in rowwise dimmension if the number of columns equals to 1.
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
fvar< T > lgamma(const fvar< T > &x)
Return the natural logarithm of the gamma function applied to the specified argument.
auto sum(const std::vector< T > &m)
Return the sum of the entries of the specified standard vector.
int64_t max_size(const T1 &x1, const Ts &... xs)
Calculate the size of the largest input.
const double CONSTRAINT_TOLERANCE
The tolerance for checking arithmetic bounds in rank and in simplexes.
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
fvar< T > digamma(const fvar< T > &x)
Return the derivative of the log gamma function at the specified argument.
fvar< T > fabs(const fvar< T > &x)
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Metaprogramming struct to detect whether a given type is constant in the mathematical sense (not the ...
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...