1#ifndef STAN_MATH_OPENCL_PRIM_LOG_MIX_HPP
2#define STAN_MATH_OPENCL_PRIM_LOG_MIX_HPP
36template <
typename T_theta_cl,
typename T_lambda_cl,
38 T_lambda_cl>* =
nullptr>
39inline auto log_mix(
const T_theta_cl& theta,
const T_lambda_cl& lambda) {
40 static constexpr const char* function =
"log_mix(OpenCL)";
46 const size_t N =
max_size(theta, lambda);
53 const auto& theta_val =
value_of(theta_col);
54 const auto& lambda_val =
value_of(lambda);
56 auto check_lambda_not_nan
57 =
check_cl(function,
"lambda", lambda_val,
"not NaN");
58 auto lambda_not_nan = !isnan(lambda_val);
59 auto check_theta_bounded
60 =
check_cl(function,
"theta", theta_val,
"in the interval[0, 1]");
61 auto theta_bounded = 0.0 <= theta_val && theta_val <= 1.0;
64 auto lambda_p_log_theta_expr = lambda_val +
log(theta_bc);
67 if (theta.cols() == lambda.cols()) {
68 results(check_lambda_not_nan, check_theta_bounded, lambda_p_log_theta,
69 lambda_p_log_theta_colwise_max)
70 =
expressions(lambda_not_nan, theta_bounded, lambda_p_log_theta_expr,
73 results(check_lambda_not_nan, lambda_p_log_theta,
74 lambda_p_log_theta_colwise_max)
75 =
expressions(lambda_not_nan, lambda_p_log_theta_expr,
77 check_theta_bounded = theta_bounded;
79 while (lambda_p_log_theta_colwise_max.
rows() > 1) {
80 lambda_p_log_theta_colwise_max
81 =
colwise_max(lambda_p_log_theta_colwise_max).eval();
85 while (sum_exp.
rows() > 1) {
89 auto logp_vec_expr =
transpose(lambda_p_log_theta_colwise_max +
log(sum_exp));
100 auto lambda_deriv_expr =
elt_multiply(derivs_expr, theta_bc);
107 partials<1>(ops_partials) = std::move(lambda_deriv);
112 partials<0>(ops_partials) =
rowwise_sum(derivs_expr);
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 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 as_column_vector_or_scalar(T &&a)
as_column_vector_or_scalar of a kernel generator expression.
auto transpose(Arg &&a)
Transposes a kernel generator expression.
auto rowwise_broadcast(T &&a)
Broadcast an expression in rowwise dimmension.
auto rowwise_sum(T &&a)
Rowwise sum reduction of a kernel generator expression.
auto colwise_max(T &&a)
Column wise max - 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.
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)
void check_consistent_sizes(const char *)
Trivial no input case, this function is a no-op.
fvar< T > log_mix(const fvar< T > &theta, const fvar< T > &lambda1, const fvar< T > &lambda2)
Return the log mixture density with specified mixing proportion and log densities and its derivative ...
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.
auto make_partials_propagator(Ops &&... ops)
Construct an partials_propagator.
fvar< T > exp(const fvar< T > &x)
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
bool isnan(const stan::math::var &a)
Checks if the given number is NaN.
Metaprogramming struct to detect whether a given type is constant in the mathematical sense (not the ...
Extends std::true_type when instantiated with zero or more template parameters, all of which extend t...