Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mdivide_left.hpp
Go to the documentation of this file.
1 #ifndef STAN__AGRAD__FWD__MATRIX__COLUMNS_MDIVIDE_LEFT_HPP
2 #define STAN__AGRAD__FWD__MATRIX__COLUMNS_MDIVIDE_LEFT_HPP
3 
4 #include <vector>
18 
19 namespace stan {
20  namespace agrad {
21 
22  template <typename T, int R1,int C1,int R2,int C2>
23  inline
24  Eigen::Matrix<fvar<T>,R1,C2>
25  mdivide_left(const Eigen::Matrix<fvar<T>,R1,C1> &A,
26  const Eigen::Matrix<fvar<T>,R2,C2> &b) {
27 
28  using stan::math::multiply;
30  stan::math::check_square("mdivide_left(%1%)",A,"A",(double*)0);
31  stan::math::check_multiplicable("mdivide_left(%1%)",A,"A",
32  b,"b",(double*)0);
33 
34  Eigen::Matrix<T,R1,C2> inv_A_mult_b(A.rows(),b.cols());
35  Eigen::Matrix<T,R1,C2> inv_A_mult_deriv_b(A.rows(),b.cols());
36  Eigen::Matrix<T,R1,C1> inv_A_mult_deriv_A(A.rows(),A.cols());
37  Eigen::Matrix<T,R1,C1> val_A(A.rows(),A.cols());
38  Eigen::Matrix<T,R1,C1> deriv_A(A.rows(),A.cols());
39  Eigen::Matrix<T,R2,C2> val_b(b.rows(),b.cols());
40  Eigen::Matrix<T,R2,C2> deriv_b(b.rows(),b.cols());
41 
42  for (int j = 0; j < A.cols(); j++) {
43  for(int i = 0; i < A.rows(); i++) {
44  val_A(i,j) = A(i,j).val_;
45  deriv_A(i,j) = A(i,j).d_;
46  }
47  }
48 
49  for (int j = 0; j < b.cols(); j++) {
50  for(int i = 0; i < b.rows(); i++) {
51  val_b(i,j) = b(i,j).val_;
52  deriv_b(i,j) = b(i,j).d_;
53  }
54  }
55 
56  inv_A_mult_b = mdivide_left(val_A, val_b);
57  inv_A_mult_deriv_b = mdivide_left(val_A, deriv_b);
58  inv_A_mult_deriv_A = mdivide_left(val_A, deriv_A);
59 
60  Eigen::Matrix<T,R1,C2> deriv(A.rows(), b.cols());
61  deriv = inv_A_mult_deriv_b - multiply(inv_A_mult_deriv_A, inv_A_mult_b);
62 
63  return stan::agrad::to_fvar(inv_A_mult_b, deriv);
64  }
65 
66  template <typename T, int R1,int C1,int R2,int C2>
67  inline
68  Eigen::Matrix<fvar<T>,R1,C2>
69  mdivide_left(const Eigen::Matrix<double,R1,C1> &A,
70  const Eigen::Matrix<fvar<T>,R2,C2> &b) {
71 
72  using stan::math::multiply;
74  stan::math::check_square("mdivide_left(%1%)",A,"A",(double*)0);
75  stan::math::check_multiplicable("mdivide_left(%1%)",A,"A",
76  b,"b",(double*)0);
77 
78  Eigen::Matrix<T,R2,C2> val_b(b.rows(),b.cols());
79  Eigen::Matrix<T,R2,C2> deriv_b(b.rows(),b.cols());
80 
81  for (int j = 0; j < b.cols(); j++) {
82  for(int i = 0; i < b.rows(); i++) {
83  val_b(i,j) = b(i,j).val_;
84  deriv_b(i,j) = b(i,j).d_;
85  }
86  }
87 
88  return stan::agrad::to_fvar(mdivide_left(A, val_b),
89  mdivide_left(A, deriv_b));
90  }
91 
92  template <typename T, int R1,int C1,int R2,int C2>
93  inline
94  Eigen::Matrix<fvar<T>,R1,C2>
95  mdivide_left(const Eigen::Matrix<fvar<T>,R1,C1> &A,
96  const Eigen::Matrix<double,R2,C2> &b) {
97 
98  using stan::math::multiply;
100  stan::math::check_square("mdivide_left(%1%)",A,"A",(double*)0);
101  stan::math::check_multiplicable("mdivide_left(%1%)",A,"A",
102  b,"b",(double*)0);
103 
104  Eigen::Matrix<T,R1,C2>
105  inv_A_mult_b(A.rows(),b.cols());
106  Eigen::Matrix<T,R1,C1> inv_A_mult_deriv_A(A.rows(),A.cols());
107  Eigen::Matrix<T,R1,C1> val_A(A.rows(),A.cols());
108  Eigen::Matrix<T,R1,C1> deriv_A(A.rows(),A.cols());
109 
110  for (int j = 0; j < A.cols(); j++) {
111  for(int i = 0; i < A.rows(); i++) {
112  val_A(i,j) = A(i,j).val_;
113  deriv_A(i,j) = A(i,j).d_;
114  }
115  }
116 
117  inv_A_mult_b = mdivide_left(val_A, b);
118  inv_A_mult_deriv_A = mdivide_left(val_A, deriv_A);
119 
120  Eigen::Matrix<T,R1,C2> deriv(A.rows(), b.cols());
121  deriv = -multiply(inv_A_mult_deriv_A, inv_A_mult_b);
122 
123  return stan::agrad::to_fvar(inv_A_mult_b, deriv);
124  }
125  }
126 }
127 #endif
Eigen::Matrix< fvar< T >, R1, C2 > mdivide_left(const Eigen::Matrix< fvar< T >, R1, C1 > &A, const Eigen::Matrix< fvar< T >, R2, C2 > &b)
fvar< T > to_fvar(const T &x)
Definition: to_fvar.hpp:17
Eigen::Matrix< fvar< T >, R1, C1 > multiply(const Eigen::Matrix< fvar< T >, R1, C1 > &m, const fvar< T > &c)
Definition: multiply.hpp:21
bool check_multiplicable(const char *function, const T1 &y1, const char *name1, const T2 &y2, const char *name2, T_result *result)
boost::enable_if_c< boost::is_arithmetic< T >::value, Eigen::Matrix< double, R, C > >::type multiply(const Eigen::Matrix< double, R, C > &m, T c)
Return specified matrix multiplied by specified scalar.
Definition: multiply.hpp:25
bool check_square(const char *function, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y, const char *name, T_result *result)
Return true if the specified matrix is square.
Eigen::Matrix< typename boost::math::tools::promote_args< T1, T2 >::type, R1, C2 > mdivide_left(const Eigen::Matrix< T1, R1, C1 > &A, const Eigen::Matrix< T2, R2, C2 > &b)
Returns the solution of the system Ax=b.

     [ Stan Home Page ] © 2011–2014, Stan Development Team.