Automatic Differentiation
 
Loading...
Searching...
No Matches
mdivide_left_tri_low.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_FWD_FUN_MDIVIDE_LEFT_TRI_LOW_HPP
2#define STAN_MATH_FWD_FUN_MDIVIDE_LEFT_TRI_LOW_HPP
3
14namespace stan {
15namespace math {
16
17template <typename T1, typename T2,
18 require_all_eigen_vt<is_fvar, T1, T2>* = nullptr,
19 require_vt_same<T1, T2>* = nullptr>
20inline Eigen::Matrix<value_type_t<T1>, std::decay_t<T1>::RowsAtCompileTime,
21 std::decay_t<T2>::ColsAtCompileTime>
22mdivide_left_tri_low(T1&& A, T2&& b) {
23 check_square("mdivide_left_tri_low", "A", A);
24 check_multiplicable("mdivide_left_tri_low", "A", A, "b", b);
25 if (A.size() == 0) {
26 return {0, b.cols()};
27 }
28 decltype(auto) b_ref = to_ref(std::forward<T2>(b));
29 decltype(auto) A_ref = to_ref(std::forward<T1>(A));
30 auto inv_A_mult_b
31 = eval(mdivide_left_tri<Eigen::Lower>(A_ref.val(), b_ref.val()));
32 return to_fvar(
33 inv_A_mult_b,
34 subtract(mdivide_left_tri<Eigen::Lower>(A_ref.val(), b_ref.d()),
35 multiply(mdivide_left_tri<Eigen::Lower>(
36 A_ref.val(),
37 A_ref.d().template triangularView<Eigen::Lower>()),
38 inv_A_mult_b)));
39}
40
41template <typename T1, typename T2, require_eigen_t<T1>* = nullptr,
42 require_vt_same<double, T1>* = nullptr,
43 require_eigen_vt<is_fvar, T2>* = nullptr>
44inline Eigen::Matrix<value_type_t<T2>, std::decay_t<T1>::RowsAtCompileTime,
45 std::decay_t<T2>::ColsAtCompileTime>
46mdivide_left_tri_low(T1&& A, T2&& b) {
47 check_square("mdivide_left_tri_low", "A", A);
48 check_multiplicable("mdivide_left_tri_low", "A", A, "b", b);
49 if (A.size() == 0) {
50 return {0, b.cols()};
51 }
52 decltype(auto) A_ref = to_ref(std::forward<T1>(A));
53 decltype(auto) b_ref = to_ref(std::forward<T2>(b));
54 return to_fvar(mdivide_left_tri<Eigen::Lower>(A_ref, b_ref.val()),
55 mdivide_left_tri<Eigen::Lower>(A_ref, b_ref.d()));
56}
57
58template <typename T1, typename T2, require_eigen_vt<is_fvar, T1>* = nullptr,
59 require_eigen_vt<std::is_floating_point, T2>* = nullptr>
60inline Eigen::Matrix<value_type_t<T1>, std::decay_t<T1>::RowsAtCompileTime,
61 std::decay_t<T2>::ColsAtCompileTime>
62mdivide_left_tri_low(T1&& A, T2&& b) {
63 check_square("mdivide_left_tri_low", "A", A);
64 check_multiplicable("mdivide_left_tri_low", "A", A, "b", b);
65 if (A.size() == 0) {
66 return {0, b.cols()};
67 }
68 decltype(auto) A_ref = to_ref(std::forward<T1>(A));
69 auto inv_A_mult_b
70 = eval(mdivide_left_tri<Eigen::Lower>(A_ref.val(), std::forward<T2>(b)));
71 return to_fvar(
72 inv_A_mult_b,
73 -multiply(
74 mdivide_left_tri<Eigen::Lower>(
75 A_ref.val(), A_ref.d().template triangularView<Eigen::Lower>()),
76 inv_A_mult_b));
77}
78
79} // namespace math
80} // namespace stan
81#endif
subtraction_< as_operation_cl_t< T_a >, as_operation_cl_t< T_b > > subtract(T_a &&a, T_b &&b)
void check_square(const char *function, const char *name, const T_y &y)
Check if the specified matrix is square.
auto multiply(Mat1 &&m1, Mat2 &&m2)
Return the product of the specified matrices.
Definition multiply.hpp:20
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.
T eval(T &&arg)
Inputs which have a plain_type equal to the own time are forwarded unmodified (for Eigen expressions ...
Definition eval.hpp:20
fvar< T > to_fvar(const T &x)
Definition to_fvar.hpp:15
ref_type_t< T && > to_ref(T &&a)
This evaluates expensive Eigen expressions.
Definition to_ref.hpp:18
Eigen::Matrix< value_type_t< T1 >, std::decay_t< T1 >::RowsAtCompileTime, std::decay_t< T2 >::ColsAtCompileTime > mdivide_left_tri_low(T1 &&A, T2 &&b)
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...