1#ifndef STAN_MATH_OPENCL_PRIM_DIRICHLET_LPDF_HPP 
    2#define STAN_MATH_OPENCL_PRIM_DIRICHLET_LPDF_HPP 
   58template <
bool propto, 
typename T_prob_cl, 
typename T_prior_size_cl,
 
   60              T_prob_cl, T_prior_size_cl>* = 
nullptr,
 
   61          require_any_not_stan_scalar_t<T_prob_cl, T_prior_size_cl>* = 
nullptr>
 
   63    const T_prob_cl& theta, 
const T_prior_size_cl& alpha) {
 
   64  static constexpr const char* function = 
"dirichlet_lpdf(OpenCL)";
 
   76  const auto& theta_val = 
value_of(theta);
 
   77  const auto& alpha_val = 
value_of(alpha);
 
   79  auto check_alpha_positive
 
   80      = 
check_cl(function, 
"prior sample sizes", alpha_val, 
"positive");
 
   81  auto alpha_positive = alpha_val > 0.0;
 
   82  auto check_theta_nonnegative
 
   83      = 
check_cl(function, 
"probabilities", theta_val, 
"nonnegative");
 
   84  auto theta_nonnegative = theta_val >= 0.0;
 
   96  if (theta.cols() > alpha.cols()) {
 
  100    results(check_alpha_positive, alpha_csum_cl, lgamma_alpha_csum_cl,
 
  116    results(check_theta_nonnegative, theta_csum_cl, theta_log_alpha_m_1_sum_cl,
 
  117            theta_deriv_cl, alpha_deriv_cl)
 
  119            theta_nonnegative, theta_csum,
 
  121                theta_log_alpha_m_1_sum),
 
  122            calc_if<is_autodiff_v<T_prob_cl>>(theta_deriv),
 
  123            calc_if<is_autodiff_v<T_prior_size_cl>>(alpha_deriv));
 
  128      while (alpha_csum_cl.
rows() > 1) {
 
  134        alpha_csum_cl = std::move(alpha_csum_cl2);
 
  135        lgamma_alpha_csum_cl = std::move(lgamma_alpha_csum_cl2);
 
  138    while (theta_csum_cl.
rows() > 1) {
 
  144    if (alpha.cols() > theta.cols()) {
 
  146      results(check_theta_nonnegative, theta_csum_cl, log_theta_cl)
 
  148              theta_nonnegative, theta_csum,
 
  154      auto theta_log_alpha_m_1_sum
 
  162      results(check_alpha_positive, alpha_csum_cl, lgamma_alpha_csum_cl,
 
  163              theta_log_alpha_m_1_sum_cl, theta_deriv_cl, alpha_deriv_cl)
 
  172                  theta_log_alpha_m_1_sum),
 
  173              calc_if<is_autodiff_v<T_prob_cl>>(theta_deriv),
 
  174              calc_if<is_autodiff_v<T_prior_size_cl>>(alpha_deriv));
 
  176      while (alpha_csum_cl.
rows() > 1) {
 
  185          alpha_csum_cl = std::move(alpha_csum_cl2);
 
  186          lgamma_alpha_csum_cl = std::move(lgamma_alpha_csum_cl2);
 
  190      check_cl(function, 
"sum of probabilities", theta_sum, 
"equal to 1")
 
  200      results(check_alpha_positive, check_theta_nonnegative, theta_csum_cl,
 
  201              alpha_csum_cl, lgamma_alpha_csum_cl, theta_log_alpha_m_1_sum_cl,
 
  202              theta_deriv_cl, alpha_deriv_cl)
 
  204              alpha_positive, theta_nonnegative, theta_csum,
 
  211                  theta_log_alpha_m_1_sum),
 
  212              calc_if<is_autodiff_v<T_prob_cl>>(theta_deriv),
 
  213              calc_if<is_autodiff_v<T_prior_size_cl>>(alpha_deriv));
 
  215      while (theta_csum_cl.
rows() > 1) {
 
  219        results(theta_csum_cl2, alpha_csum_cl2, lgamma_alpha_csum_cl2)
 
  226        theta_csum_cl = std::move(theta_csum_cl2);
 
  228          alpha_csum_cl = std::move(alpha_csum_cl2);
 
  229          lgamma_alpha_csum_cl = std::move(lgamma_alpha_csum_cl2);
 
  235  if (theta.cols() >= alpha.cols()) {
 
  246    if (theta.cols() > alpha.cols()) {
 
  262  if constexpr (is_autodiff_v<T_prob_cl>) {
 
  263    if (theta.cols() < alpha.cols()) {
 
  264      partials<0>(ops_partials) = 
rowwise_sum(theta_deriv_cl);
 
  266      partials<0>(ops_partials) = std::move(theta_deriv_cl);
 
  269  if constexpr (is_autodiff_v<T_prior_size_cl>) {
 
  270    if (theta.cols() > alpha.cols()) {
 
  272          = 
digamma(alpha_csum_cl) * 
static_cast<double>(theta.cols());
 
  273      partials<1>(ops_partials)
 
  280  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 ...
 
Template metaprogram to calculate whether a summand needs to be included in a proportional (log) prob...