Automatic Differentiation
 
Loading...
Searching...
No Matches
mdivide_right_tri_low.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_FWD_FUN_MDIVIDE_RIGHT_TRI_LOW_HPP
2#define STAN_MATH_FWD_FUN_MDIVIDE_RIGHT_TRI_LOW_HPP
3
11
12namespace stan {
13namespace math {
14
15template <typename EigMat1, typename EigMat2,
16 require_all_eigen_vt<is_fvar, EigMat1, EigMat2>* = nullptr,
17 require_vt_same<EigMat1, EigMat2>* = nullptr>
18inline Eigen::Matrix<value_type_t<EigMat1>, EigMat1::RowsAtCompileTime,
19 EigMat2::ColsAtCompileTime>
20mdivide_right_tri_low(const EigMat1& A, const EigMat2& b) {
21 using T = typename value_type_t<EigMat1>::Scalar;
22 constexpr int R1 = EigMat1::RowsAtCompileTime;
23 constexpr int C1 = EigMat1::ColsAtCompileTime;
24 constexpr int R2 = EigMat2::RowsAtCompileTime;
25 constexpr int C2 = EigMat2::ColsAtCompileTime;
26
27 check_square("mdivide_right_tri_low", "b", b);
28 check_multiplicable("mdivide_right_tri_low", "A", A, "b", b);
29 if (b.size() == 0) {
30 return {A.rows(), 0};
31 }
32
33 Eigen::Matrix<T, R1, C1> val_A(A.rows(), A.cols());
34 Eigen::Matrix<T, R1, C1> deriv_A(A.rows(), A.cols());
35 Eigen::Matrix<T, R2, C2> val_b(b.rows(), b.cols());
36 Eigen::Matrix<T, R2, C2> deriv_b(b.rows(), b.cols());
37 val_b.setZero();
38 deriv_b.setZero();
39
40 const Eigen::Ref<const plain_type_t<EigMat1>>& A_ref = A;
41 for (int j = 0; j < A.cols(); j++) {
42 for (int i = 0; i < A.rows(); i++) {
43 val_A.coeffRef(i, j) = A_ref.coeff(i, j).val_;
44 deriv_A.coeffRef(i, j) = A_ref.coeff(i, j).d_;
45 }
46 }
47
48 const Eigen::Ref<const plain_type_t<EigMat2>>& b_ref = b;
49 for (int j = 0; j < b.cols(); j++) {
50 for (int i = j; i < b.rows(); i++) {
51 val_b.coeffRef(i, j) = b_ref.coeff(i, j).val_;
52 deriv_b.coeffRef(i, j) = b_ref.coeff(i, j).d_;
53 }
54 }
55
56 Eigen::Matrix<T, R1, C2> A_mult_inv_b = mdivide_right(val_A, val_b);
57 return to_fvar(A_mult_inv_b,
58 mdivide_right(deriv_A, val_b)
59 - A_mult_inv_b * mdivide_right(deriv_b, val_b));
60}
61
62template <typename EigMat1, typename EigMat2,
65inline Eigen::Matrix<value_type_t<EigMat1>, EigMat1::RowsAtCompileTime,
66 EigMat2::ColsAtCompileTime>
67mdivide_right_tri_low(const EigMat1& A, const EigMat2& b) {
68 using T = typename value_type_t<EigMat1>::Scalar;
69 constexpr int R1 = EigMat1::RowsAtCompileTime;
70 constexpr int C1 = EigMat1::ColsAtCompileTime;
71
72 check_square("mdivide_right_tri_low", "b", b);
73 check_multiplicable("mdivide_right_tri_low", "A", A, "b", b);
74 if (b.size() == 0) {
75 return {A.rows(), 0};
76 }
77
78 Eigen::Matrix<T, R1, C1> val_A(A.rows(), A.cols());
79 Eigen::Matrix<T, R1, C1> deriv_A(A.rows(), A.cols());
80
81 const Eigen::Ref<const plain_type_t<EigMat1>>& A_ref = A;
82 for (int j = 0; j < A.cols(); j++) {
83 for (int i = 0; i < A.rows(); i++) {
84 val_A.coeffRef(i, j) = A_ref.coeff(i, j).val_;
85 deriv_A.coeffRef(i, j) = A_ref.coeff(i, j).d_;
86 }
87 }
88
89 plain_type_t<EigMat2> val_b = b.template triangularView<Eigen::Lower>();
90
91 return to_fvar(mdivide_right(val_A, val_b), mdivide_right(deriv_A, val_b));
92}
93
94template <typename EigMat1, typename EigMat2,
95 require_eigen_vt<std::is_arithmetic, EigMat1>* = nullptr,
96 require_eigen_vt<is_fvar, EigMat2>* = nullptr>
97inline Eigen::Matrix<value_type_t<EigMat2>, EigMat1::RowsAtCompileTime,
98 EigMat2::ColsAtCompileTime>
99mdivide_right_tri_low(const EigMat1& A, const EigMat2& b) {
100 using T = typename value_type_t<EigMat2>::Scalar;
101 constexpr int R1 = EigMat1::RowsAtCompileTime;
102 constexpr int R2 = EigMat2::RowsAtCompileTime;
103 constexpr int C2 = EigMat2::ColsAtCompileTime;
104 check_square("mdivide_right_tri_low", "b", b);
105 check_multiplicable("mdivide_right_tri_low", "A", A, "b", b);
106 if (b.size() == 0) {
107 return {A.rows(), 0};
108 }
109
110 Eigen::Matrix<T, R1, C2> A_mult_inv_b(A.rows(), b.cols());
111 Eigen::Matrix<T, R2, C2> val_b(b.rows(), b.cols());
112 Eigen::Matrix<T, R2, C2> deriv_b(b.rows(), b.cols());
113 val_b.setZero();
114 deriv_b.setZero();
115
116 for (int j = 0; j < b.cols(); j++) {
117 for (int i = j; i < b.rows(); i++) {
118 val_b(i, j) = b(i, j).val_;
119 deriv_b(i, j) = b(i, j).d_;
120 }
121 }
122
123 A_mult_inv_b = mdivide_right(A, val_b);
124
125 return to_fvar(A_mult_inv_b,
126 -multiply(A_mult_inv_b, mdivide_right(deriv_b, val_b)));
127}
128
129} // namespace math
130} // namespace stan
131#endif
require_t< container_type_check_base< is_eigen, value_type_t, TypeCheck, Check... > > require_eigen_vt
Require type satisfies is_eigen.
Definition is_eigen.hpp:152
typename value_type< T >::type value_type_t
Helper function for accessing underlying type.
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.
Eigen::Matrix< value_type_t< EigMat1 >, EigMat1::RowsAtCompileTime, EigMat2::ColsAtCompileTime > mdivide_right_tri_low(const EigMat1 &A, const EigMat2 &b)
auto multiply(const Mat1 &m1, const Mat2 &m2)
Return the product of the specified matrices.
Definition multiply.hpp:19
Eigen::Matrix< value_type_t< EigMat1 >, EigMat1::RowsAtCompileTime, EigMat2::ColsAtCompileTime > mdivide_right(const EigMat1 &A, const EigMat2 &b)
fvar< T > to_fvar(const T &x)
Definition to_fvar.hpp:15
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...