1#ifndef STAN_MATH_FWD_FUN_MDIVIDE_LEFT_HPP
2#define STAN_MATH_FWD_FUN_MDIVIDE_LEFT_HPP
15template <
typename T1,
typename T2,
16 require_all_eigen_vt<is_fvar, T1, T2>* =
nullptr,
17 require_vt_same<T1, T2>* =
nullptr>
18inline Eigen::Matrix<value_type_t<T1>, T1::RowsAtCompileTime,
19 T2::ColsAtCompileTime>
22 constexpr int S1 = T1::RowsAtCompileTime;
23 constexpr int C2 = T2::ColsAtCompileTime;
31 Eigen::Matrix<T, S1, C2> inv_A_mult_b(A.rows(), b.cols());
32 Eigen::Matrix<T, S1, C2> inv_A_mult_deriv_b(A.rows(), b.cols());
33 Eigen::Matrix<T, S1, S1> inv_A_mult_deriv_A(A.rows(), A.cols());
34 Eigen::Matrix<T, S1, S1> val_A(A.rows(), A.cols());
35 Eigen::Matrix<T, S1, S1> deriv_A(A.rows(), A.cols());
37 const Eigen::Ref<const plain_type_t<T2>>& b_ref = b;
38 const Eigen::Ref<const plain_type_t<T1>>& A_ref = A;
39 for (
int j = 0; j < A.cols(); j++) {
40 for (
int i = 0; i < A.rows(); i++) {
41 val_A(i, j) = A_ref(i, j).val_;
42 deriv_A(i, j) = A_ref(i, j).d_;
50 Eigen::Matrix<T, S1, C2> deriv(A.rows(), b.cols());
51 deriv = inv_A_mult_deriv_b -
multiply(inv_A_mult_deriv_A, inv_A_mult_b);
53 return to_fvar(inv_A_mult_b, deriv);
56template <
typename T1,
typename T2,
59inline Eigen::Matrix<value_type_t<T2>, T1::RowsAtCompileTime,
60 T2::ColsAtCompileTime>
68 const Eigen::Ref<const plain_type_t<T2>>& b_ref = b;
73template <
typename T1,
typename T2, require_eigen_vt<is_fvar, T1>* =
nullptr,
74 require_eigen_vt<std::is_arithmetic, T2>* =
nullptr>
75inline Eigen::Matrix<value_type_t<T1>, T1::RowsAtCompileTime,
76 T2::ColsAtCompileTime>
79 constexpr int S1 = T1::RowsAtCompileTime;
80 constexpr int C2 = T2::ColsAtCompileTime;
88 Eigen::Matrix<T, S1, C2> inv_A_mult_b(A.rows(), b.cols());
89 Eigen::Matrix<T, S1, S1> inv_A_mult_deriv_A(A.rows(), A.cols());
90 Eigen::Matrix<T, S1, S1> val_A(A.rows(), A.cols());
91 Eigen::Matrix<T, S1, S1> deriv_A(A.rows(), A.cols());
93 const Eigen::Ref<const plain_type_t<T1>>& A_ref = A;
94 for (
int j = 0; j < A.cols(); j++) {
95 for (
int i = 0; i < A.rows(); i++) {
96 val_A(i, j) = A_ref(i, j).val_;
97 deriv_A(i, j) = A_ref(i, j).d_;
104 Eigen::Matrix<T, S1, C2> deriv(A.rows(), b.cols());
105 deriv = -
multiply(inv_A_mult_deriv_A, inv_A_mult_b);
107 return to_fvar(inv_A_mult_b, deriv);
require_t< container_type_check_base< is_eigen, value_type_t, TypeCheck, Check... > > require_eigen_vt
Require type satisfies is_eigen.
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.
fvar< T > to_fvar(const T &x)
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 ...