Automatic Differentiation
 
Loading...
Searching...
No Matches
diag_pre_multiply.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_REV_DIAG_PRE_MULTIPLY_HPP
2#define STAN_MATH_OPENCL_REV_DIAG_PRE_MULTIPLY_HPP
3#ifdef STAN_OPENCL
4
11
12namespace stan {
13namespace math {
14
27template <
28 typename T1, typename T2, require_any_var_t<T1, T2>* = nullptr,
29 require_all_nonscalar_prim_or_rev_kernel_expression_t<T1, T2>* = nullptr>
31 arena_t<T1> v1_arena = std::forward<T1>(v1);
32 arena_t<T2> v2_arena = std::forward<T2>(v2);
33
34 matrix_cl<double> res_val
35 = diag_pre_multiply(value_of(v1_arena), value_of(v2_arena));
36
37 return make_callback_var(
38 res_val,
39 [v1_arena, v2_arena](const vari_value<matrix_cl<double>>& res) mutable {
40 if (v1_arena.cols() == 1) {
41 if (!is_constant<std::decay_t<T1>>::value) {
42 adjoint_of(v1_arena)
43 += rowwise_sum(elt_multiply(res.adj(), value_of(v2_arena)));
44 }
45 if (!is_constant<std::decay_t<T2>>::value) {
46 adjoint_of(v2_arena) += elt_multiply(
47 res.adj(), rowwise_broadcast(value_of(v1_arena)));
48 }
49 } else {
50 if (!is_constant<std::decay_t<T1>>::value) {
51 adjoint_of(transpose(v1_arena))
52 += rowwise_sum(elt_multiply(res.adj(), value_of(v2_arena)));
53 }
54 if (!is_constant<std::decay_t<T2>>::value) {
55 adjoint_of(v2_arena) += elt_multiply(
56 res.adj(), rowwise_broadcast(transpose(value_of(v1_arena))));
57 }
58 }
59 });
60}
61
62} // namespace math
63} // namespace stan
64
65#endif
66#endif
Represents an arithmetic matrix on the OpenCL device.
Definition matrix_cl.hpp:47
elt_multiply_< as_operation_cl_t< T_a >, as_operation_cl_t< T_b > > elt_multiply(T_a &&a, T_b &&b)
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 diag_pre_multiply(const T1 &m1, const T2 &m2)
Return the product of the diagonal matrix formed from the vector or row_vector and a matrix.
var_value< plain_type_t< T > > make_callback_var(T &&value, F &&functor)
Creates a new var initialized with a callback_vari with a given value and reverse-pass callback funct...
auto & adjoint_of(const T &x)
Returns a reference to a variable's adjoint.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
typename internal::arena_type_impl< std::decay_t< T > >::type arena_t
Determines a type that can be used in place of T that does any dynamic allocations on the AD stack.
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 ...