Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mdivide_left_tri_low.hpp
Go to the documentation of this file.
1 #ifndef STAN__AGRAD__FWD__MATRIX__MDIVIDE_LEFT_TRI_LOW_HPP
2 #define STAN__AGRAD__FWD__MATRIX__MDIVIDE_LEFT_TRI_LOW_HPP
3 
4 #include <vector>
13 #include <stan/agrad/fwd/fvar.hpp>
15 
16 namespace stan {
17  namespace agrad {
18 
19  template<typename T,int R1, int C1, int R2, int C2>
20  inline
21  Eigen::Matrix<fvar<T>,R1,C1>
22  mdivide_left_tri_low(const Eigen::Matrix<fvar<T>, R1, C1>& A,
23  const Eigen::Matrix<fvar<T>, R2, C2>& b) {
24  using stan::math::multiply;
26  stan::math::check_square("mdivide_left_tri_low(%1%)",A,"A",(double*)0);
27  stan::math::check_multiplicable("mdivide_left_tri_low(%1%)",A,"A",
28  b,"b",(double*)0);
29 
30  Eigen::Matrix<T,R1,C2> inv_A_mult_b(A.rows(),b.cols());
31  Eigen::Matrix<T,R1,C2> inv_A_mult_deriv_b(A.rows(),b.cols());
32  Eigen::Matrix<T,R1,C1> inv_A_mult_deriv_A(A.rows(),A.cols());
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_A.setZero();
38  deriv_A.setZero();
39 
40  for (size_type j = 0; j < A.cols(); j++) {
41  for(size_type i = j; i < A.rows(); i++) {
42  val_A(i,j) = A(i,j).val_;
43  deriv_A(i,j) = A(i,j).d_;
44  }
45  }
46 
47  for (size_type j = 0; j < b.cols(); j++) {
48  for(size_type i = 0; i < b.rows(); i++) {
49  val_b(i,j) = b(i,j).val_;
50  deriv_b(i,j) = b(i,j).d_;
51  }
52  }
53 
54  inv_A_mult_b = mdivide_left(val_A, val_b);
55  inv_A_mult_deriv_b = mdivide_left(val_A, deriv_b);
56  inv_A_mult_deriv_A = mdivide_left(val_A, deriv_A);
57 
58  Eigen::Matrix<T,R1,C2> deriv(A.rows(), b.cols());
59  deriv = inv_A_mult_deriv_b - multiply(inv_A_mult_deriv_A, inv_A_mult_b);
60 
61  return stan::agrad::to_fvar(inv_A_mult_b, deriv);
62  }
63 
64  template<typename T,int R1, int C1, int R2, int C2>
65  inline
66  Eigen::Matrix<fvar<T>,R1,C1>
67  mdivide_left_tri_low(const Eigen::Matrix<double, R1, C1>& A,
68  const Eigen::Matrix<fvar<T>, R2, C2>& b) {
69  using stan::math::multiply;
71  stan::math::check_square("mdivide_left_tri_low(%1%)",A,"A",(double*)0);
72  stan::math::check_multiplicable("mdivide_left_tri_low(%1%)",A,"A",
73  b,"b",(double*)0);
74 
75  Eigen::Matrix<T,R1,C2> inv_A_mult_b(A.rows(),b.cols());
76  Eigen::Matrix<T,R1,C2> inv_A_mult_deriv_b(A.rows(),b.cols());
77  Eigen::Matrix<T,R2,C2> val_b(b.rows(),b.cols());
78  Eigen::Matrix<T,R2,C2> deriv_b(b.rows(),b.cols());
79  Eigen::Matrix<double,R1,C1> val_A(A.rows(),A.cols());
80  val_A.setZero();
81 
82  for (size_type j = 0; j < A.cols(); j++) {
83  for(size_type i = j; i < A.rows(); i++) {
84  val_A(i,j) = A(i,j);
85  }
86  }
87 
88  for (size_type j = 0; j < b.cols(); j++) {
89  for(size_type i = 0; i < b.rows(); i++) {
90  val_b(i,j) = b(i,j).val_;
91  deriv_b(i,j) = b(i,j).d_;
92  }
93  }
94 
95  inv_A_mult_b = mdivide_left(val_A, val_b);
96  inv_A_mult_deriv_b = mdivide_left(val_A, deriv_b);
97 
98  Eigen::Matrix<T,R1,C2> deriv(A.rows(), b.cols());
99  deriv = inv_A_mult_deriv_b;
100 
101  return stan::agrad::to_fvar(inv_A_mult_b, deriv);
102  }
103 
104  template<typename T,int R1, int C1, int R2, int C2>
105  inline
106  Eigen::Matrix<fvar<T>,R1,C1>
107  mdivide_left_tri_low(const Eigen::Matrix<fvar<T>, R1, C1>& A,
108  const Eigen::Matrix<double, R2, C2>& b) {
109  using stan::math::multiply;
111  stan::math::check_square("mdivide_left_tri_low(%1%)",A,"A",(double*)0);
112  stan::math::check_multiplicable("mdivide_left_tri_low(%1%)",A,"A",
113  b,"b",(double*)0);
114 
115  Eigen::Matrix<T,R1,C2> inv_A_mult_b(A.rows(),b.cols());
116  Eigen::Matrix<T,R1,C1> inv_A_mult_deriv_A(A.rows(),A.cols());
117  Eigen::Matrix<T,R1,C1> val_A(A.rows(),A.cols());
118  Eigen::Matrix<T,R1,C1> deriv_A(A.rows(),A.cols());
119  val_A.setZero();
120  deriv_A.setZero();
121 
122  for (size_type j = 0; j < A.cols(); j++) {
123  for(size_type i = j; i < A.rows(); i++) {
124  val_A(i,j) = A(i,j).val_;
125  deriv_A(i,j) = A(i,j).d_;
126  }
127  }
128 
129  inv_A_mult_b = mdivide_left(val_A, b);
130  inv_A_mult_deriv_A = mdivide_left(val_A, deriv_A);
131 
132  Eigen::Matrix<T,R1,C2> deriv(A.rows(), b.cols());
133  deriv = -multiply(inv_A_mult_deriv_A, inv_A_mult_b);
134 
135  return stan::agrad::to_fvar(inv_A_mult_b, deriv);
136  }
137  }
138 }
139 #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)
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic >::Index size_type
Definition: typedefs.hpp:14
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
Eigen::Matrix< fvar< T >, R1, C1 > mdivide_left_tri_low(const Eigen::Matrix< fvar< T >, R1, C1 > &A, const Eigen::Matrix< fvar< T >, R2, C2 > &b)
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.