Automatic Differentiation
 
Loading...
Searching...
No Matches
unit_vector_constrain.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_REV_CONSTRAINT_UNIT_VECTOR_CONSTRAIN_HPP
2#define STAN_MATH_OPENCL_REV_CONSTRAINT_UNIT_VECTOR_CONSTRAIN_HPP
3#ifdef STAN_OPENCL
4
8
9namespace stan {
10namespace math {
11
20template <typename T,
21 require_all_kernel_expressions_and_none_scalar_t<T>* = nullptr>
23 const var_value<T>& A) {
24 using std::sqrt;
25 const double r = sqrt(dot_self(A.val()));
26 return make_callback_var(
27 elt_divide(A.val(), r),
28 [A, r](vari_value<matrix_cl<double>>& res) mutable {
29 A.adj() += elt_divide(res.adj(), r)
30 - A.val() * (dot_product(A.val(), res.adj()) / (r * r * r));
31 });
32}
33
43template <typename T,
46 var& lp) {
47 using std::sqrt;
48 double r = dot_self(A.val());
49 lp -= 0.5 * r;
50 r = sqrt(r);
51 return make_callback_var(
52 elt_divide(A.val(), r),
53 [A, r, lp](vari_value<matrix_cl<double>>& res) mutable {
54 A.adj() += elt_divide(res.adj(), r)
55 - A.val() * (dot_product(A.val(), res.adj()) / (r * r * r))
56 - A.val() * lp.adj();
57 });
58}
59
60} // namespace math
61} // namespace stan
62
63#endif
64#endif
Represents an arithmetic matrix on the OpenCL device.
Definition matrix_cl.hpp:47
elt_divide_< as_operation_cl_t< T_a >, as_operation_cl_t< T_b > > elt_divide(T_a &&a, T_b &&b)
require_all_t< is_kernel_expression_and_not_scalar< Types >... > require_all_kernel_expressions_and_none_scalar_t
Enables a template if all given types are non-scalar types that are a valid kernel generator expressi...
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 unit_vector_constrain(const EigMat &y)
fvar< T > sqrt(const fvar< T > &x)
Definition sqrt.hpp:17
auto dot_self(const T &a)
Returns squared norm of a vector or matrix.
Definition dot_self.hpp:21
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...