Stan  2.5.0
probability, sampling & optimization
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
mdivide_right_tri_low.hpp
Go to the documentation of this file.
1 #ifndef STAN__AGRAD__FWD__MATRIX__MDIVIDE_RIGHT_TRI_LOW_HPP
2 #define STAN__AGRAD__FWD__MATRIX__MDIVIDE_RIGHT_TRI_LOW_HPP
3 
4 #include <vector>
14 #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_right_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_right_tri_low(%1%)",b,"b",(double*)0);
27  stan::math::check_multiplicable("mdivide_right_tri_low(%1%)",A,"A",
28  b,"b",(double*)0);
29 
30  Eigen::Matrix<T,R1,C2> A_mult_inv_b(A.rows(),b.cols());
31  Eigen::Matrix<T,R1,C2> deriv_A_mult_inv_b(A.rows(),b.cols());
32  Eigen::Matrix<T,R2,C2> deriv_b_mult_inv_b(b.rows(),b.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_b.setZero();
38  deriv_b.setZero();
39 
40  for (size_type j = 0; j < A.cols(); j++) {
41  for(size_type i = 0; 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 = j; 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  A_mult_inv_b = mdivide_right(val_A, val_b);
55  deriv_A_mult_inv_b = mdivide_right(deriv_A, val_b);
56  deriv_b_mult_inv_b = mdivide_right(deriv_b, val_b);
57 
58  Eigen::Matrix<T,R1,C2> deriv(A.rows(), b.cols());
59  deriv = deriv_A_mult_inv_b - multiply(A_mult_inv_b, deriv_b_mult_inv_b);
60 
61  return stan::agrad::to_fvar(A_mult_inv_b, deriv);
62  }
63 
64  template <typename T, int R1,int C1,int R2,int C2>
65  inline
66  Eigen::Matrix<fvar<T>,R1,C2>
67  mdivide_right_tri_low(const Eigen::Matrix<fvar<T>,R1,C1> &A,
68  const Eigen::Matrix<double,R2,C2> &b) {
69 
70  using stan::math::multiply;
72  stan::math::check_square("mdivide_right_tri_low(%1%)",b,"b",(double*)0);
73  stan::math::check_multiplicable("mdivide_right_tri_low(%1%)",A,"A",
74  b,"b",(double*)0);
75 
76  Eigen::Matrix<T,R2,C2> deriv_b_mult_inv_b(b.rows(),b.cols());
77  Eigen::Matrix<T,R1,C1> val_A(A.rows(),A.cols());
78  Eigen::Matrix<T,R1,C1> deriv_A(A.rows(),A.cols());
79  Eigen::Matrix<T,R2,C2> val_b(b.rows(),b.cols());
80  val_b.setZero();
81 
82  for (int j = 0; j < A.cols(); j++) {
83  for(int i = 0; i < A.rows(); i++) {
84  val_A(i,j) = A(i,j).val_;
85  deriv_A(i,j) = A(i,j).d_;
86  }
87  }
88 
89  for (size_type j = 0; j < b.cols(); j++) {
90  for(size_type i = j; i < b.rows(); i++) {
91  val_b(i,j) = b(i,j);
92  }
93  }
94 
95  return stan::agrad::to_fvar(mdivide_right(val_A, val_b),
96  mdivide_right(deriv_A, val_b));
97  }
98 
99  template <typename T, int R1,int C1,int R2,int C2>
100  inline
101  Eigen::Matrix<fvar<T>,R1,C2>
102  mdivide_right_tri_low(const Eigen::Matrix<double,R1,C1> &A,
103  const Eigen::Matrix<fvar<T>,R2,C2> &b) {
104 
105  using stan::math::multiply;
107  stan::math::check_square("mdivide_right_tri_low(%1%)",b,"b",(double*)0);
108  stan::math::check_multiplicable("mdivide_right_tri_low(%1%)",A,"A",
109  b,"b",(double*)0);
110 
111  Eigen::Matrix<T,R1,C2>
112  A_mult_inv_b(A.rows(),b.cols());
113  Eigen::Matrix<T,R2,C2> deriv_b_mult_inv_b(b.rows(),b.cols());
114  Eigen::Matrix<T,R2,C2> val_b(b.rows(),b.cols());
115  Eigen::Matrix<T,R2,C2> deriv_b(b.rows(),b.cols());
116  val_b.setZero();
117  deriv_b.setZero();
118 
119  for (int j = 0; j < b.cols(); j++) {
120  for(int i = j; i < b.rows(); i++) {
121  val_b(i,j) = b(i,j).val_;
122  deriv_b(i,j) = b(i,j).d_;
123  }
124  }
125 
126  A_mult_inv_b = mdivide_right(A, val_b);
127  deriv_b_mult_inv_b = mdivide_right(deriv_b, val_b);
128 
129  Eigen::Matrix<T,R1,C2>
130  deriv(A.rows(), b.cols());
131  deriv = -multiply(A_mult_inv_b, deriv_b_mult_inv_b);
132 
133  return stan::agrad::to_fvar(A_mult_inv_b, deriv);
134  }
135  }
136 }
137 #endif
Eigen::Matrix< fvar< T >, R1, C2 > mdivide_right(const Eigen::Matrix< fvar< T >, R1, C1 > &A, const Eigen::Matrix< fvar< T >, R2, C2 > &b)
Eigen::Matrix< typename boost::math::tools::promote_args< T1, T2 >::type, R1, C2 > mdivide_right(const Eigen::Matrix< T1, R1, C1 > &b, const Eigen::Matrix< T2, R2, C2 > &A)
Returns the solution of the system Ax=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
Eigen::Matrix< fvar< T >, R1, C1 > mdivide_right_tri_low(const Eigen::Matrix< fvar< T >, R1, C1 > &A, const Eigen::Matrix< fvar< T >, R2, C2 > &b)
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.

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