|
| 1 | +#ifndef STAN_MATH_OPENCL_REV_MDIVIDE_RIGHT_TRI_LOW_HPP |
| 2 | +#define STAN_MATH_OPENCL_REV_MDIVIDE_RIGHT_TRI_LOW_HPP |
| 3 | +#ifdef STAN_OPENCL |
| 4 | + |
| 5 | +#include <stan/math/opencl/rev/arena_type.hpp> |
| 6 | +#include <stan/math/opencl/matrix_cl.hpp> |
| 7 | +#include <stan/math/opencl/prim/mdivide_left_tri_low.hpp> |
| 8 | +#include <stan/math/rev/core.hpp> |
| 9 | +#include <stan/math/rev/fun/adjoint_of.hpp> |
| 10 | +#include <stan/math/rev/fun/value_of.hpp> |
| 11 | + |
| 12 | +namespace stan { |
| 13 | +namespace math { |
| 14 | + |
| 15 | +/** |
| 16 | + * Returns the solution of the system Ax=b when A is lower triangular. |
| 17 | + * |
| 18 | + * @tparam T1 type of elements in A |
| 19 | + * @tparam T2 type of elements in b |
| 20 | + * @param A Triangular matrix. |
| 21 | + * @param b Right hand side matrix or vector. |
| 22 | + * @return x = A^-1 b, solution of the linear system. |
| 23 | + * @throws std::domain_error if A is not square or the rows of b don't |
| 24 | + * match the size of A. |
| 25 | + */ |
| 26 | +template < |
| 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> |
| 30 | +inline var_value<matrix_cl<double>> mdivide_right_tri_low(T2&& b, T1&& A) { |
| 31 | + check_square("mdivide_right_tri_low", "A", A); |
| 32 | + check_multiplicable("mdivide_right_tri_low", "b", b, "A", A); |
| 33 | + if (A.size() == 0 || b.size() == 0) { |
| 34 | + return var_value<matrix_cl<double>>(matrix_cl<double>(b.rows(), A.cols())); |
| 35 | + } |
| 36 | + arena_t<T1> A_arena = std::forward<T1>(A); |
| 37 | + arena_t<T2> b_arena = std::forward<T2>(b); |
| 38 | + arena_matrix_cl<double> A_tri_inv |
| 39 | + = tri_inverse<matrix_cl_view::Lower>(value_of(A_arena)); |
| 40 | + return make_callback_var( |
| 41 | + value_of(b_arena) * A_tri_inv, |
| 42 | + [A_arena, b_arena, A_tri_inv](const vari_value<matrix_cl<double>>& res) { |
| 43 | + matrix_cl<double> adjB = res.adj() * transpose(A_tri_inv); |
| 44 | + if (!is_constant<T1>::value) { |
| 45 | + matrix_cl<double> adjA = transpose(res.val()) * adjB; |
| 46 | + adjA.view(matrix_cl_view::Lower); |
| 47 | + adjoint_of(A_arena) -= adjA; |
| 48 | + } |
| 49 | + if (!is_constant<T2>::value) { |
| 50 | + adjoint_of(b_arena) += adjB; |
| 51 | + } |
| 52 | + }); |
| 53 | +} |
| 54 | + |
| 55 | +} // namespace math |
| 56 | +} // namespace stan |
| 57 | + |
| 58 | +#endif |
| 59 | +#endif |
0 commit comments