Automatic Differentiation
 
Loading...
Searching...
No Matches
mdivide_left_tri_low.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_OPENCL_REV_MDIVIDE_LEFT_TRI_LOW_HPP
2#define STAN_MATH_OPENCL_REV_MDIVIDE_LEFT_TRI_LOW_HPP
3#ifdef STAN_OPENCL
4
11
12namespace stan {
13namespace math {
14
26template <
27 typename T1, typename T2,
28 require_all_nonscalar_prim_or_rev_kernel_expression_t<T1, T2>* = nullptr,
29 require_any_var_t<T1, T2>* = nullptr>
31 check_square("mdivide_left_tri_low", "A", A);
32 check_multiplicable("mdivide_left_tri_low", "A", A, "b", b);
33 if (A.size() == 0 || b.size() == 0) {
34 return var_value<matrix_cl<double>>(matrix_cl<double>(A.rows(), b.cols()));
35 }
36 arena_t<T1> A_arena = std::forward<T1>(A);
37 arena_t<T2> b_arena = std::forward<T2>(b);
39 = tri_inverse<matrix_cl_view::Lower>(value_of(A_arena));
40 return make_callback_var(
41 A_tri_inv * value_of(b_arena),
42 [A_arena, b_arena, A_tri_inv](const vari_value<matrix_cl<double>>& res) {
43 matrix_cl<double> adjB = transpose(A_tri_inv) * res.adj();
45 matrix_cl<double> adjA = adjB * transpose(res.val());
47 adjoint_of(A_arena) -= adjA;
48 }
50 adjoint_of(b_arena) += adjB;
51 }
52 });
53}
54
63template <typename T,
66 const var_value<T>& A) {
67 check_square("mdivide_left_tri_low", "A", A);
68 if (A.size() == 0) {
69 return A;
70 }
71 return make_callback_var(
73 [A](const vari_value<matrix_cl<double>>& res) {
74 matrix_cl<double> res_val_transpose = transpose(res.val());
76 = res_val_transpose * res.adj() * res_val_transpose;
78 adjoint_of(A) -= adjA;
79 });
80}
81
82} // namespace math
83} // namespace stan
84
85#endif
86#endif
A variant of matrix_cl that schedules its destructor to be called, so it can be used on the AD stack.
const matrix_cl_view & view() const
Definition matrix_cl.hpp:70
Represents an arithmetic matrix on the OpenCL device.
Definition matrix_cl.hpp:47
auto transpose(Arg &&a)
Transposes a kernel generator expression.
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...
void check_square(const char *function, const char *name, const T_y &y)
Check if the specified matrix is square.
void check_multiplicable(const char *function, const char *name1, const T1 &y1, const char *name2, const T2 &y2)
Check if the matrices can be multiplied.
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
Eigen::Matrix< value_type_t< T1 >, T1::RowsAtCompileTime, T2::ColsAtCompileTime > mdivide_left_tri_low(const T1 &A, const T2 &b)
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 ...