Automatic Differentiation
 
Loading...
Searching...
No Matches
mdivide_left_ldlt.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_REV_FUN_MDIVIDE_LEFT_LDLT_HPP
2#define STAN_MATH_REV_FUN_MDIVIDE_LEFT_LDLT_HPP
3
11#include <memory>
12
13namespace stan {
14namespace math {
15
25template <typename T1, typename T2, require_all_matrix_t<T1, T2>* = nullptr,
26 require_any_st_var<T1, T2>* = nullptr>
27inline auto mdivide_left_ldlt(LDLT_factor<T1>& A, const T2& B) {
28 using ret_val_type
29 = Eigen::Matrix<double, Eigen::Dynamic, T2::ColsAtCompileTime>;
31
32 check_multiplicable("mdivide_left_ldlt", "A", A.matrix().val(), "B", B);
33
34 if (A.matrix().size() == 0) {
35 return ret_type(ret_val_type(0, B.cols()));
36 }
37
40 arena_t<promote_scalar_t<var, T1>> arena_A = A.matrix();
41 arena_t<ret_type> res = A.ldlt().solve(arena_B.val());
42 const auto* ldlt_ptr = make_chainable_ptr(A.ldlt());
43
44 reverse_pass_callback([arena_A, arena_B, ldlt_ptr, res]() mutable {
45 promote_scalar_t<double, T2> adjB = ldlt_ptr->solve(res.adj());
46
47 arena_A.adj() -= adjB * res.val_op().transpose();
48 arena_B.adj() += adjB;
49 });
50
51 return ret_type(res);
52 } else if (!is_constant<T1>::value) {
53 arena_t<promote_scalar_t<var, T1>> arena_A = A.matrix();
54 arena_t<ret_type> res = A.ldlt().solve(value_of(B));
55 const auto* ldlt_ptr = make_chainable_ptr(A.ldlt());
56
57 reverse_pass_callback([arena_A, ldlt_ptr, res]() mutable {
58 arena_A.adj() -= ldlt_ptr->solve(res.adj()) * res.val_op().transpose();
59 });
60
61 return ret_type(res);
62 } else {
64 arena_t<ret_type> res = A.ldlt().solve(arena_B.val());
65 const auto* ldlt_ptr = make_chainable_ptr(A.ldlt());
66
67 reverse_pass_callback([arena_B, ldlt_ptr, res]() mutable {
68 arena_B.adj() += ldlt_ptr->solve(res.adj());
69 });
70
71 return ret_type(res);
72 }
73}
74
75} // namespace math
76} // namespace stan
77#endif
LDLT_factor is a structure that holds a matrix of type T and the LDLT of its values.
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.
typename promote_scalar_type< std::decay_t< T >, std::decay_t< S > >::type promote_scalar_t
void reverse_pass_callback(F &&functor)
Puts a callback on the autodiff stack to be called in reverse pass.
T value_of(const fvar< T > &v)
Return the value of the specified variable.
Definition value_of.hpp:18
auto make_chainable_ptr(T &&obj)
Store the given object in a chainable_object so it is destructed only when the chainable stack memory...
Eigen::Matrix< value_type_t< EigMat >, Eigen::Dynamic, EigMat::ColsAtCompileTime > mdivide_left_ldlt(LDLT_factor< T > &A, const EigMat &b)
Returns the solution of the system Ax=b given an LDLT_factor of A.
std::conditional_t< is_any_var_matrix< ReturnType, Types... >::value, stan::math::var_value< stan::math::promote_scalar_t< double, ReturnType > >, stan::math::promote_scalar_t< stan::math::var_value< double >, ReturnType > > promote_var_matrix_t
Given an Eigen type and several inputs, determine if a matrix should be var<Matrix> or Matrix<var>.
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 ...