|
12 | 12 |
|
13 | 13 | namespace stan { |
14 | 14 | namespace math { |
15 | | - |
| 15 | +/* |
16 | 16 | template <typename EigMat1, typename EigMat2, |
17 | | - require_all_eigen_vt<is_fvar, EigMat1, EigMat2>* = nullptr, |
18 | | - require_vt_same<EigMat1, EigMat2>* = nullptr> |
19 | | -inline Eigen::Matrix<value_type_t<EigMat1>, EigMat1::RowsAtCompileTime, |
20 | | - EigMat2::ColsAtCompileTime> |
21 | | -mdivide_right(const EigMat1& A, const EigMat2& b) { |
22 | | - using T = typename value_type_t<EigMat1>::Scalar; |
23 | | - constexpr int R1 = EigMat1::RowsAtCompileTime; |
24 | | - constexpr int C1 = EigMat1::ColsAtCompileTime; |
25 | | - constexpr int R2 = EigMat2::RowsAtCompileTime; |
26 | | - constexpr int C2 = EigMat2::ColsAtCompileTime; |
27 | | - |
28 | | - check_square("mdivide_right", "b", b); |
29 | | - check_multiplicable("mdivide_right", "A", A, "b", b); |
30 | | - if (b.size() == 0) { |
31 | | - return {A.rows(), 0}; |
| 17 | + require_all_eigen_vt<is_fvar, EigMat1, EigMat2>* = nullptr> |
| 18 | +inline auto |
| 19 | +mdivide_right(const EigMat1& b, const EigMat2& A) { |
| 20 | + std::cout << "\nUsing 1: " << "\n"; |
| 21 | + using A_fvar_inner_type = typename value_type_t<EigMat2>::Scalar; |
| 22 | + using b_fvar_inner_type = typename value_type_t<EigMat1>::Scalar; |
| 23 | + using inner_ret_t = return_type_t<A_fvar_inner_type, b_fvar_inner_type>; |
| 24 | + constexpr auto R1 = EigMat1::RowsAtCompileTime; |
| 25 | + constexpr auto C1 = EigMat1::ColsAtCompileTime; |
| 26 | + constexpr auto R2 = EigMat2::RowsAtCompileTime; |
| 27 | + constexpr auto C2 = EigMat2::ColsAtCompileTime; |
| 28 | +
|
| 29 | + check_square("mdivide_right", "A", A); |
| 30 | + check_multiplicable("mdivide_right", "b", b, "A", A); |
| 31 | + if (A.size() == 0) { |
| 32 | + using ret_t = decltype(mdivide_right(b.val(), A.val()).eval()); |
| 33 | + return promote_scalar_t<fvar<inner_ret_t>, ret_t>{b.rows(), 0}; |
32 | 34 | } |
33 | 35 |
|
34 | | - Eigen::Matrix<T, R1, C1> val_A(A.rows(), A.cols()); |
35 | | - Eigen::Matrix<T, R1, C1> deriv_A(A.rows(), A.cols()); |
36 | | - Eigen::Matrix<T, R2, C2> val_b(b.rows(), b.cols()); |
37 | | - Eigen::Matrix<T, R2, C2> deriv_b(b.rows(), b.cols()); |
| 36 | + Eigen::Matrix<A_fvar_inner_type, R2, C2> val_A(A.rows(), A.cols()); |
| 37 | + Eigen::Matrix<A_fvar_inner_type, R2, C2> deriv_A(A.rows(), A.cols()); |
38 | 38 |
|
39 | | - const Eigen::Ref<const plain_type_t<EigMat1>>& A_ref = A; |
| 39 | + const auto& A_ref = to_ref(A); |
40 | 40 | for (int j = 0; j < A.cols(); j++) { |
41 | 41 | for (int i = 0; i < A.rows(); i++) { |
42 | 42 | val_A.coeffRef(i, j) = A_ref.coeff(i, j).val_; |
43 | 43 | deriv_A.coeffRef(i, j) = A_ref.coeff(i, j).d_; |
44 | 44 | } |
45 | 45 | } |
46 | 46 |
|
47 | | - const Eigen::Ref<const plain_type_t<EigMat2>>& b_ref = b; |
48 | | - for (int j = 0; j < b.cols(); j++) { |
49 | | - for (int i = 0; i < b.rows(); i++) { |
| 47 | + Eigen::Matrix<b_fvar_inner_type, R1, C1> val_b(b.rows(), b.cols()); |
| 48 | + Eigen::Matrix<b_fvar_inner_type, R1, C1> deriv_b(b.rows(), b.cols()); |
| 49 | + const auto& b_ref = to_ref(b); |
| 50 | + for (Eigen::Index j = 0; j < b.cols(); j++) { |
| 51 | + for (Eigen::Index i = 0; i < b.rows(); i++) { |
50 | 52 | val_b.coeffRef(i, j) = b_ref.coeff(i, j).val_; |
51 | 53 | deriv_b.coeffRef(i, j) = b_ref.coeff(i, j).d_; |
52 | 54 | } |
53 | 55 | } |
54 | | - |
55 | | - Eigen::Matrix<T, R1, C2> A_mult_inv_b = mdivide_right(val_A, val_b); |
56 | | - |
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)); |
| 56 | + auto A_mult_inv_b = mdivide_right(val_b, val_A).eval(); |
| 57 | + promote_scalar_t<fvar<inner_ret_t>, decltype(A_mult_inv_b)> |
| 58 | +ret(A_mult_inv_b.rows(), A_mult_inv_b.cols()); ret.val() = A_mult_inv_b; ret.d() |
| 59 | += mdivide_right(deriv_b, val_A) |
| 60 | + - multiply(A_mult_inv_b, mdivide_right(deriv_A, val_A)); |
| 61 | + return ret; |
60 | 62 | } |
61 | 63 |
|
62 | 64 | template <typename EigMat1, typename EigMat2, |
63 | 65 | require_eigen_vt<is_fvar, EigMat1>* = nullptr, |
64 | | - require_eigen_vt<std::is_arithmetic, EigMat2>* = nullptr> |
65 | | -inline Eigen::Matrix<value_type_t<EigMat1>, EigMat1::RowsAtCompileTime, |
66 | | - EigMat2::ColsAtCompileTime> |
67 | | -mdivide_right(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", "b", b); |
73 | | - check_multiplicable("mdivide_right", "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 | | - return to_fvar(mdivide_right(val_A, b), mdivide_right(deriv_A, b)); |
90 | | -} |
| 66 | + require_eigen_vt<is_var_or_arithmetic, EigMat2>* = nullptr> |
| 67 | + inline auto |
| 68 | + mdivide_right(const EigMat1& b, const EigMat2& A) { |
| 69 | + using T_return = return_type_t<EigMat1, EigMat2>; |
| 70 | + check_square("mdivide_right", "A", A); |
| 71 | + check_multiplicable("mdivide_right", "b", b, "A", A); |
| 72 | + if (A.size() == 0) { |
| 73 | + using ret_type = decltype(A.transpose().template |
| 74 | +cast<T_return>().lu().solve(b.template |
| 75 | +cast<T_return>().transpose()).transpose().eval()); return ret_type{b.rows(), 0}; |
| 76 | + } |
| 77 | + return A.transpose().template cast<T_return>().lu().solve(b.template |
| 78 | +cast<T_return>().transpose()).transpose().eval(); |
| 79 | + } |
91 | 80 |
|
92 | 81 | template <typename EigMat1, typename EigMat2, |
93 | | - require_eigen_vt<std::is_arithmetic, EigMat1>* = nullptr, |
| 82 | + require_eigen_vt<is_var_or_arithmetic, EigMat1>* = nullptr, |
94 | 83 | require_eigen_vt<is_fvar, EigMat2>* = nullptr> |
95 | | -inline Eigen::Matrix<value_type_t<EigMat2>, EigMat1::RowsAtCompileTime, |
96 | | - EigMat2::ColsAtCompileTime> |
97 | | -mdivide_right(const EigMat1& A, const EigMat2& b) { |
98 | | - using T = typename value_type_t<EigMat2>::Scalar; |
99 | | - constexpr int R1 = EigMat1::RowsAtCompileTime; |
100 | | - constexpr int C1 = EigMat1::ColsAtCompileTime; |
101 | | - constexpr int R2 = EigMat2::RowsAtCompileTime; |
102 | | - constexpr int C2 = EigMat2::ColsAtCompileTime; |
103 | | - |
104 | | - check_square("mdivide_right", "b", b); |
105 | | - check_multiplicable("mdivide_right", "A", A, "b", b); |
106 | | - if (b.size() == 0) { |
107 | | - return {A.rows(), 0}; |
108 | | - } |
109 | | - |
110 | | - Eigen::Matrix<T, R2, C2> val_b(b.rows(), b.cols()); |
111 | | - Eigen::Matrix<T, R2, C2> deriv_b(b.rows(), b.cols()); |
112 | | - |
113 | | - const Eigen::Ref<const plain_type_t<EigMat2>>& b_ref = b; |
114 | | - for (int j = 0; j < b.cols(); j++) { |
115 | | - for (int i = 0; i < b.rows(); i++) { |
116 | | - val_b.coeffRef(i, j) = b_ref.coeff(i, j).val_; |
117 | | - deriv_b.coeffRef(i, j) = b_ref.coeff(i, j).d_; |
118 | | - } |
119 | | - } |
120 | | - |
121 | | - Eigen::Matrix<T, R1, C2> A_mult_inv_b = mdivide_right(A, val_b); |
122 | | - |
123 | | - return to_fvar(A_mult_inv_b, -A_mult_inv_b * mdivide_right(deriv_b, val_b)); |
124 | | -} |
125 | | - |
| 84 | + inline auto |
| 85 | + mdivide_right(const EigMat1& b, const EigMat2& A) { |
| 86 | + using T_return = return_type_t<EigMat1, EigMat2>; |
| 87 | + check_square("mdivide_right", "A", A); |
| 88 | + check_multiplicable("mdivide_right", "b", b, "A", A); |
| 89 | + if (A.size() == 0) { |
| 90 | + using ret_type = decltype(A.transpose().template |
| 91 | +cast<T_return>().lu().solve(b.template |
| 92 | +cast<T_return>().transpose()).transpose().eval()); return ret_type{b.rows(), 0}; |
| 93 | + } |
| 94 | + return A.transpose().template cast<T_return>().lu().solve(b.template |
| 95 | +cast<T_return>().transpose()).transpose().eval(); |
| 96 | + } |
| 97 | +*/ |
126 | 98 | } // namespace math |
127 | 99 | } // namespace stan |
128 | 100 | #endif |
0 commit comments