Automatic Differentiation
 
Loading...
Searching...
No Matches
mdivide_left.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_FWD_FUN_MDIVIDE_LEFT_HPP
2#define STAN_MATH_FWD_FUN_MDIVIDE_LEFT_HPP
3
11#include <vector>
12
13namespace stan {
14namespace math {
15
16template <typename T1, typename T2,
17 require_all_eigen_vt<is_fvar, T1, T2>* = nullptr,
18 require_vt_same<T1, T2>* = nullptr>
19inline Eigen::Matrix<value_type_t<T1>, T1::RowsAtCompileTime,
20 T2::ColsAtCompileTime>
21mdivide_left(const T1& A, const T2& b) {
22 using T = typename value_type_t<T1>::Scalar;
23 constexpr int S1 = T1::RowsAtCompileTime;
24 constexpr int C2 = T2::ColsAtCompileTime;
25
26 check_square("mdivide_left", "A", A);
27 check_multiplicable("mdivide_left", "A", A, "b", b);
28 if (A.size() == 0) {
29 return {0, b.cols()};
30 }
31
32 Eigen::Matrix<T, S1, C2> inv_A_mult_b(A.rows(), b.cols());
33 Eigen::Matrix<T, S1, C2> inv_A_mult_deriv_b(A.rows(), b.cols());
34 Eigen::Matrix<T, S1, S1> inv_A_mult_deriv_A(A.rows(), A.cols());
35 Eigen::Matrix<T, S1, S1> val_A(A.rows(), A.cols());
36 Eigen::Matrix<T, S1, S1> deriv_A(A.rows(), A.cols());
37
38 const Eigen::Ref<const plain_type_t<T2>>& b_ref = b;
39 const Eigen::Ref<const plain_type_t<T1>>& A_ref = A;
40 for (int j = 0; j < A.cols(); j++) {
41 for (int i = 0; i < A.rows(); i++) {
42 val_A(i, j) = A_ref(i, j).val_;
43 deriv_A(i, j) = A_ref(i, j).d_;
44 }
45 }
46
47 inv_A_mult_b = mdivide_left(val_A, b_ref.val());
48 inv_A_mult_deriv_b = mdivide_left(val_A, b_ref.d());
49 inv_A_mult_deriv_A = mdivide_left(val_A, deriv_A);
50
51 Eigen::Matrix<T, S1, C2> deriv(A.rows(), b.cols());
52 deriv = inv_A_mult_deriv_b - multiply(inv_A_mult_deriv_A, inv_A_mult_b);
53
54 return to_fvar(inv_A_mult_b, deriv);
55}
56
57template <typename T1, typename T2,
60inline Eigen::Matrix<value_type_t<T2>, T1::RowsAtCompileTime,
61 T2::ColsAtCompileTime>
62mdivide_left(const T1& A, const T2& b) {
63 check_square("mdivide_left", "A", A);
64 check_multiplicable("mdivide_left", "A", A, "b", b);
65 if (A.size() == 0) {
66 return {0, b.cols()};
67 }
68
69 const Eigen::Ref<const plain_type_t<T2>>& b_ref = b;
70
71 return to_fvar(mdivide_left(A, b_ref.val()), mdivide_left(A, b_ref.d()));
72}
73
74template <typename T1, typename T2, require_eigen_vt<is_fvar, T1>* = nullptr,
75 require_eigen_vt<std::is_arithmetic, T2>* = nullptr>
76inline Eigen::Matrix<value_type_t<T1>, T1::RowsAtCompileTime,
77 T2::ColsAtCompileTime>
78mdivide_left(const T1& A, const T2& b) {
79 using T = typename value_type_t<T1>::Scalar;
80 constexpr int S1 = T1::RowsAtCompileTime;
81 constexpr int C2 = T2::ColsAtCompileTime;
82
83 check_square("mdivide_left", "A", A);
84 check_multiplicable("mdivide_left", "A", A, "b", b);
85 if (A.size() == 0) {
86 return {0, b.cols()};
87 }
88
89 Eigen::Matrix<T, S1, C2> inv_A_mult_b(A.rows(), b.cols());
90 Eigen::Matrix<T, S1, S1> inv_A_mult_deriv_A(A.rows(), A.cols());
91 Eigen::Matrix<T, S1, S1> val_A(A.rows(), A.cols());
92 Eigen::Matrix<T, S1, S1> deriv_A(A.rows(), A.cols());
93
94 const Eigen::Ref<const plain_type_t<T1>>& A_ref = A;
95 for (int j = 0; j < A.cols(); j++) {
96 for (int i = 0; i < A.rows(); i++) {
97 val_A(i, j) = A_ref(i, j).val_;
98 deriv_A(i, j) = A_ref(i, j).d_;
99 }
100 }
101
102 inv_A_mult_b = mdivide_left(val_A, b);
103 inv_A_mult_deriv_A = mdivide_left(val_A, deriv_A);
104
105 Eigen::Matrix<T, S1, C2> deriv(A.rows(), b.cols());
106 deriv = -multiply(inv_A_mult_deriv_A, inv_A_mult_b);
107
108 return to_fvar(inv_A_mult_b, deriv);
109}
110
111} // namespace math
112} // namespace stan
113#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:97
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.
auto multiply(const Mat1 &m1, const Mat2 &m2)
Return the product of the specified matrices.
Definition multiply.hpp:18
fvar< T > to_fvar(const T &x)
Definition to_fvar.hpp:15
Eigen::Matrix< value_type_t< T1 >, T1::RowsAtCompileTime, T2::ColsAtCompileTime > mdivide_left(const T1 &A, const T2 &b)
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...