| Stan Math Library 2.11.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

mdivide_left_tri_low.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_FWD_MAT_FUN_MDIVIDE_LEFT_TRI_LOW_HPP
2 #define STAN_MATH_FWD_MAT_FUN_MDIVIDE_LEFT_TRI_LOW_HPP
3
4 #include <stan/math/prim/mat/fun/Eigen.hpp>
5 #include <stan/math/prim/mat/fun/typedefs.hpp>
6 #include <stan/math/prim/mat/fun/mdivide_left.hpp>
7 #include <stan/math/prim/mat/err/check_multiplicable.hpp>
8 #include <stan/math/fwd/mat/fun/typedefs.hpp>
9 #include <stan/math/fwd/mat/fun/to_fvar.hpp>
10 #include <stan/math/fwd/mat/fun/multiply.hpp>
11 #include <stan/math/fwd/mat/fun/inverse.hpp>
12 #include <stan/math/fwd/core.hpp>
13 #include <stan/math/prim/mat/err/check_square.hpp>
14 #include <vector>
15
16 namespace stan {
17 namespace math {
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;
25 using stan::math::mdivide_left;
26 stan::math::check_square("mdivide_left_tri_low", "A", A);
27 stan::math::check_multiplicable("mdivide_left_tri_low",
28 "A", A,
29 "b", b);
30
31 Eigen::Matrix<T, R1, C2> inv_A_mult_b(A.rows(), b.cols());
32 Eigen::Matrix<T, R1, C2> inv_A_mult_deriv_b(A.rows(), b.cols());
33 Eigen::Matrix<T, R1, C1> inv_A_mult_deriv_A(A.rows(), A.cols());
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());
38 val_A.setZero();
39 deriv_A.setZero();
40
41 for (size_type j = 0; j < A.cols(); j++) {
42 for (size_type i = j; i < A.rows(); i++) {
43 val_A(i, j) = A(i, j).val_;
44 deriv_A(i, j) = A(i, j).d_;
45 }
46 }
47
48 for (size_type j = 0; j < b.cols(); j++) {
49 for (size_type i = 0; i < b.rows(); i++) {
50 val_b(i, j) = b(i, j).val_;
51 deriv_b(i, j) = b(i, j).d_;
52 }
53 }
54
55 inv_A_mult_b = mdivide_left(val_A, val_b);
56 inv_A_mult_deriv_b = mdivide_left(val_A, deriv_b);
57 inv_A_mult_deriv_A = mdivide_left(val_A, deriv_A);
58
59 Eigen::Matrix<T, R1, C2> deriv(A.rows(), b.cols());
60 deriv = inv_A_mult_deriv_b - multiply(inv_A_mult_deriv_A, inv_A_mult_b);
61
62 return stan::math::to_fvar(inv_A_mult_b, deriv);
63 }
64
65 template<typename T, int R1, int C1, int R2, int C2>
66 inline
67 Eigen::Matrix<fvar<T>, R1, C1>
68 mdivide_left_tri_low(const Eigen::Matrix<double, R1, C1>& A,
69 const Eigen::Matrix<fvar<T>, R2, C2>& b) {
70 using stan::math::multiply;
71 using stan::math::mdivide_left;
72 stan::math::check_square("mdivide_left_tri_low", "A", A);
73 stan::math::check_multiplicable("mdivide_left_tri_low",
74 "A", A,
75 "b", b);
76
77 Eigen::Matrix<T, R1, C2> inv_A_mult_b(A.rows(), b.cols());
78 Eigen::Matrix<T, R1, C2> inv_A_mult_deriv_b(A.rows(), b.cols());
79 Eigen::Matrix<T, R2, C2> val_b(b.rows(), b.cols());
80 Eigen::Matrix<T, R2, C2> deriv_b(b.rows(), b.cols());
81 Eigen::Matrix<double, R1, C1> val_A(A.rows(), A.cols());
82 val_A.setZero();
83
84 for (size_type j = 0; j < A.cols(); j++) {
85 for (size_type i = j; i < A.rows(); i++) {
86 val_A(i, j) = A(i, j);
87 }
88 }
89
90 for (size_type j = 0; j < b.cols(); j++) {
91 for (size_type i = 0; i < b.rows(); i++) {
92 val_b(i, j) = b(i, j).val_;
93 deriv_b(i, j) = b(i, j).d_;
94 }
95 }
96
97 inv_A_mult_b = mdivide_left(val_A, val_b);
98 inv_A_mult_deriv_b = mdivide_left(val_A, deriv_b);
99
100 Eigen::Matrix<T, R1, C2> deriv(A.rows(), b.cols());
101 deriv = inv_A_mult_deriv_b;
102
103 return stan::math::to_fvar(inv_A_mult_b, deriv);
104 }
105
106 template<typename T, int R1, int C1, int R2, int C2>
107 inline
108 Eigen::Matrix<fvar<T>, R1, C1>
109 mdivide_left_tri_low(const Eigen::Matrix<fvar<T>, R1, C1>& A,
110 const Eigen::Matrix<double, R2, C2>& b) {
111 using stan::math::multiply;
112 using stan::math::mdivide_left;
113 stan::math::check_square("mdivide_left_tri_low", "A", A);
114 stan::math::check_multiplicable("mdivide_left_tri_low",
115 "A", A,
116 "b", b);
117
118 Eigen::Matrix<T, R1, C2> inv_A_mult_b(A.rows(), b.cols());
119 Eigen::Matrix<T, R1, C1> inv_A_mult_deriv_A(A.rows(), A.cols());
120 Eigen::Matrix<T, R1, C1> val_A(A.rows(), A.cols());
121 Eigen::Matrix<T, R1, C1> deriv_A(A.rows(), A.cols());
122 val_A.setZero();
123 deriv_A.setZero();
124
125 for (size_type j = 0; j < A.cols(); j++) {
126 for (size_type i = j; i < A.rows(); i++) {
127 val_A(i, j) = A(i, j).val_;
128 deriv_A(i, j) = A(i, j).d_;
129 }
130 }
131
132 inv_A_mult_b = mdivide_left(val_A, b);
133 inv_A_mult_deriv_A = mdivide_left(val_A, deriv_A);
134
135 Eigen::Matrix<T, R1, C2> deriv(A.rows(), b.cols());
136 deriv = -multiply(inv_A_mult_deriv_A, inv_A_mult_b);
137
138 return stan::math::to_fvar(inv_A_mult_b, deriv);
139 }
140 }
141 }
142 #endif
stan::math::mdivide_left
Eigen::Matrix< fvar< T >, R1, C2 > mdivide_left(const Eigen::Matrix< fvar< T >, R1, C1 > &A, const Eigen::Matrix< fvar< T >, R2, C2 > &b)
Definition: mdivide_left.hpp:24
typedefs.hpp
core.hpp
check_multiplicable.hpp
stan
Definition: log_sum_exp.hpp:8
stan::math::multiply
Eigen::Matrix< fvar< T >, R1, C1 > multiply(const Eigen::Matrix< fvar< T >, R1, C1 > &m, const fvar< T > &c)
Definition: multiply.hpp:21
multiply.hpp
stan::math::to_fvar
std::vector< fvar< T > > to_fvar(const std::vector< T > &v)
Definition: to_fvar.hpp:14
stan::math::size_type
Eigen::Matrix< double, Eigen::Dynamic, Eigen::Dynamic >::Index size_type
Type for sizes and indexes in an Eigen matrix with double e.
Definition: typedefs.hpp:13
stan::math::check_multiplicable
bool check_multiplicable(const char *function, const char *name1, const T1 &y1, const char *name2, const T2 &y2)
Return true if the matrices can be multiplied.
Definition: check_multiplicable.hpp:33
inverse.hpp
Eigen.hpp
stan::math::mdivide_left_tri_low
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)
Definition: mdivide_left_tri_low.hpp:22
stan::math::check_square
bool check_square(const char *function, const char *name, const Eigen::Matrix< T_y, Eigen::Dynamic, Eigen::Dynamic > &y)
Return true if the specified matrix is square.
Definition: check_square.hpp:28
to_fvar.hpp
stan::math::fvar
Definition: fvar.hpp:13
typedefs.hpp
mdivide_left.hpp
check_square.hpp

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