1#ifndef STAN_MATH_REV_FUN_MDIVIDE_LEFT_SPD_HPP
2#define STAN_MATH_REV_FUN_MDIVIDE_LEFT_SPD_HPP
17template <
int R1,
int C1,
int R2,
int C2>
22 Eigen::LLT<Eigen::Matrix<double, R1, C1>>
llt_;
23 Eigen::Matrix<double, R2, C2>
C_;
26template <
int R1,
int C1,
int R2,
int C2>
37 const Eigen::Matrix<var, R2, C2> &B)
54 alloc_->llt_ = A.val().llt();
59 =
alloc_->C_.unaryExpr([](
double x) {
return new vari(x,
false); });
64 alloc_->llt_.solveInPlace(adjB);
66 -= adjB *
alloc_->C_.transpose();
71template <
int R1,
int C1,
int R2,
int C2>
81 const Eigen::Matrix<var, R2, C2> &B)
99 =
alloc_->C_.unaryExpr([](
double x) {
return new vari(x,
false); });
104 alloc_->llt_.solveInPlace(adjB);
109template <
int R1,
int C1,
int R2,
int C2>
119 const Eigen::Matrix<double, R2, C2> &B)
131 alloc_->llt_ = A.val().llt();
136 =
alloc_->C_.unaryExpr([](
double x) {
return new vari(x,
false); });
148 typename EigMat1,
typename EigMat2,
150inline Eigen::Matrix<
var, EigMat1::RowsAtCompileTime,
151 EigMat2::ColsAtCompileTime>
153 constexpr int R1 = EigMat1::RowsAtCompileTime;
154 constexpr int C1 = EigMat1::ColsAtCompileTime;
155 constexpr int R2 = EigMat2::RowsAtCompileTime;
156 constexpr int C2 = EigMat2::ColsAtCompileTime;
157 static constexpr const char *function =
"mdivide_left_spd";
159 const auto &A_ref =
to_ref(A);
163 return {0, b.cols()};
173 Eigen::Matrix<var, R1, C2> res(b.rows(), b.cols());
174 res.vi() = Eigen::Map<matrix_vi>(&baseVari->
variRefC_[0], b.rows(), b.cols());
178template <
typename EigMat1,
typename EigMat2,
181inline Eigen::Matrix<
var, EigMat1::RowsAtCompileTime,
182 EigMat2::ColsAtCompileTime>
184 constexpr int R1 = EigMat1::RowsAtCompileTime;
185 constexpr int C1 = EigMat1::ColsAtCompileTime;
186 constexpr int R2 = EigMat2::RowsAtCompileTime;
187 constexpr int C2 = EigMat2::ColsAtCompileTime;
188 static constexpr const char *function =
"mdivide_left_spd";
190 const auto &A_ref =
to_ref(A);
194 return {0, b.cols()};
204 Eigen::Matrix<var, R1, C2> res(b.rows(), b.cols());
205 res.vi() = Eigen::Map<matrix_vi>(&baseVari->
variRefC_[0], b.rows(), b.cols());
209template <
typename EigMat1,
typename EigMat2,
212inline Eigen::Matrix<
var, EigMat1::RowsAtCompileTime,
213 EigMat2::ColsAtCompileTime>
215 constexpr int R1 = EigMat1::RowsAtCompileTime;
216 constexpr int C1 = EigMat1::ColsAtCompileTime;
217 constexpr int R2 = EigMat2::RowsAtCompileTime;
218 constexpr int C2 = EigMat2::ColsAtCompileTime;
219 static constexpr const char *function =
"mdivide_left_spd";
221 const auto &A_ref =
to_ref(A);
225 return {0, b.cols()};
232 internal::mdivide_left_spd_dv_vari<R1, C1, R2, C2> *baseVari
233 =
new internal::mdivide_left_spd_dv_vari<R1, C1, R2, C2>(A_ref, b);
235 Eigen::Matrix<var, R1, C2> res(b.rows(), b.cols());
236 res.vi() = Eigen::Map<matrix_vi>(&baseVari->variRefC_[0], b.rows(), b.cols());
259template <
typename T1,
typename T2, require_all_matrix_t<T1, T2> * =
nullptr,
260 require_any_var_matrix_t<T1, T2> * =
nullptr>
266 return ret_type(ret_val_type(0, B.cols()));
278 auto A_llt = arena_A.val().llt();
288 arena_A_llt.template triangularView<Eigen::Lower>().solveInPlace(adjB);
289 arena_A_llt.template triangularView<Eigen::Lower>()
293 arena_A.adj() -= adjB * res.val_op().transpose();
294 arena_B.adj() += adjB;
297 return ret_type(res);
304 auto A_llt = arena_A.val().llt();
314 arena_A_llt.template triangularView<Eigen::Lower>().solveInPlace(adjB);
315 arena_A_llt.template triangularView<Eigen::Lower>()
319 arena_A.adj() -= adjB * res.val().transpose().eval();
322 return ret_type(res);
330 auto A_llt = A_ref.llt();
340 arena_A_llt.template triangularView<Eigen::Lower>().solveInPlace(adjB);
341 arena_A_llt.template triangularView<Eigen::Lower>()
345 arena_B.adj() += adjB;
348 return ret_type(res);
A chainable_alloc is an object which is constructed and destructed normally but the memory lifespan i...
virtual ~mdivide_left_spd_alloc()
Eigen::Matrix< double, R2, C2 > C_
Eigen::LLT< Eigen::Matrix< double, R1, C1 > > llt_
mdivide_left_spd_dv_vari(const Eigen::Matrix< double, R1, C1 > &A, const Eigen::Matrix< var, R2, C2 > &B)
mdivide_left_spd_alloc< R1, C1, R2, C2 > * alloc_
mdivide_left_spd_alloc< R1, C1, R2, C2 > * alloc_
mdivide_left_spd_vd_vari(const Eigen::Matrix< var, R1, C1 > &A, const Eigen::Matrix< double, R2, C2 > &B)
mdivide_left_spd_alloc< R1, C1, R2, C2 > * alloc_
mdivide_left_spd_vv_vari(const Eigen::Matrix< var, R1, C1 > &A, const Eigen::Matrix< var, R2, C2 > &B)
require_t< container_type_check_base< is_eigen_matrix_base, value_type_t, TypeCheck, Check... > > require_eigen_matrix_base_vt
Require type satisfies is_eigen_matrix_base.
require_all_t< container_type_check_base< is_eigen_matrix_base, value_type_t, TypeCheck, Check >... > require_all_eigen_matrix_base_vt
Require all of the types satisfy is_eigen_matrix_base.
void check_symmetric(const char *function, const char *name, const matrix_cl< T > &y)
Check if the matrix_cl is symmetric.
int64_t cols(const T_x &x)
Returns the number of columns in the specified kernel generator expression.
int64_t rows(const T_x &x)
Returns the number of rows in the specified kernel generator expression.
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
Eigen::Matrix< return_type_t< EigMat1, EigMat2 >, EigMat1::RowsAtCompileTime, EigMat2::ColsAtCompileTime > mdivide_left_spd(const EigMat1 &A, const EigMat2 &b)
Returns the solution of the system Ax=b where A is symmetric positive definite.
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.
void check_pos_definite(const char *function, const char *name, const EigMat &y)
Check if the specified square, symmetric matrix is positive definite.
vari_value< double > vari
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
void check_not_nan(const char *function, const char *name, const T_y &y)
Check if y is not NaN.
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic > matrix_d
Type for matrix of double values.
typename plain_type< T >::type plain_type_t
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 ...
This struct always provides access to the autodiff stack using the singleton pattern.