1#ifndef STAN_MATH_FWD_FUN_LOG_MIX_HPP
2#define STAN_MATH_FWD_FUN_LOG_MIX_HPP
28template <
typename T_theta,
typename T_lambda1,
typename T_lambda2,
int N>
30 const T_theta& theta,
const T_lambda1& lambda1,
const T_lambda2& lambda2,
33 auto lam2_m_lam1 = lambda2 - lambda1;
34 auto exp_lam2_m_lam1 =
exp(lam2_m_lam1);
35 auto one_m_exp_lam2_m_lam1 = 1.0 - exp_lam2_m_lam1;
36 auto one_m_t = 1.0 - theta;
37 auto one_m_t_prod_exp_lam2_m_lam1 = one_m_t * exp_lam2_m_lam1;
38 auto t_plus_one_m_t_prod_exp_lam2_m_lam1
39 = theta + one_m_t_prod_exp_lam2_m_lam1;
40 auto one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1
41 = 1.0 / t_plus_one_m_t_prod_exp_lam2_m_lam1;
43 unsigned int offset = 0;
44 if (std::is_same<T_theta, partial_return_type>::value) {
45 partials_array[offset]
46 = one_m_exp_lam2_m_lam1 * one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1;
49 if (std::is_same<T_lambda1, partial_return_type>::value) {
50 partials_array[offset] = theta * one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1;
53 if (std::is_same<T_lambda2, partial_return_type>::value) {
54 partials_array[offset] = one_m_t_prod_exp_lam2_m_lam1
55 * one_d_t_plus_one_m_t_prod_exp_lam2_m_lam1;
101 fvar<T> partial_deriv_array[3];
105 + lambda1.
d_ *
value_of(partial_deriv_array[1])
106 + lambda2.
d_ *
value_of(partial_deriv_array[2]));
108 fvar<T> partial_deriv_array[3];
112 + lambda1.
d_ *
value_of(partial_deriv_array[2])
113 + lambda2.
d_ *
value_of(partial_deriv_array[1]));
117template <
typename T,
typename P, require_all_arithmetic_t<P>* =
nullptr>
120 if (lambda1.
val_ > lambda2) {
121 fvar<T> partial_deriv_array[2];
125 + lambda1.
d_ *
value_of(partial_deriv_array[1]));
127 fvar<T> partial_deriv_array[2];
131 + lambda1.
d_ *
value_of(partial_deriv_array[1]));
135template <
typename T,
typename P, require_all_arithmetic_t<P>* =
nullptr>
138 if (lambda1 > lambda2.
val_) {
139 fvar<T> partial_deriv_array[2];
143 + lambda2.
d_ *
value_of(partial_deriv_array[1]));
145 fvar<T> partial_deriv_array[2];
149 + lambda2.
d_ *
value_of(partial_deriv_array[1]));
153template <
typename T,
typename P, require_all_arithmetic_t<P>* =
nullptr>
157 fvar<T> partial_deriv_array[2];
161 + lambda2.
d_ *
value_of(partial_deriv_array[1]));
163 fvar<T> partial_deriv_array[2];
167 + lambda2.
d_ *
value_of(partial_deriv_array[0]));
171template <
typename T,
typename P1,
typename P2,
174 if (lambda1 > lambda2) {
175 fvar<T> partial_deriv_array[1];
180 fvar<T> partial_deriv_array[1];
183 -theta.
d_ *
value_of(partial_deriv_array[0]));
187template <
typename T,
typename P1,
typename P2,
190 if (lambda1.
val_ > lambda2) {
191 fvar<T> partial_deriv_array[1];
194 lambda1.
d_ *
value_of(partial_deriv_array[0]));
196 fvar<T> partial_deriv_array[1];
199 lambda1.
d_ *
value_of(partial_deriv_array[0]));
203template <
typename T,
typename P1,
typename P2,
206 if (lambda1 > lambda2.
val_) {
207 fvar<T> partial_deriv_array[1];
210 lambda2.
d_ *
value_of(partial_deriv_array[0]));
212 fvar<T> partial_deriv_array[1];
215 lambda2.
d_ *
value_of(partial_deriv_array[0]));
require_all_t< std::is_arithmetic< std::decay_t< Types > >... > require_all_arithmetic_t
Require all of the types satisfy std::is_arithmetic.
typename boost::math::tools::promote_args< Args... >::type promote_args_t
Convenience alias for boost tools promote_args.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
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 ...
void log_mix_partial_helper(const T_theta &theta, const T_lambda1 &lambda1, const T_lambda2 &lambda2, promote_args_t< T_theta, T_lambda1, T_lambda2 >(&partials_array)[N])
fvar< T > exp(const fvar< T > &x)
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Scalar val_
The value of this variable.
Scalar d_
The tangent (derivative) of this variable.
This template class represents scalars used in forward-mode automatic differentiation,...