Stan Math Library: stan/math/fwd/mat/fun/mdivide_right_tri_low.hpp Source File

| Stan Math Library 2.10.0 reverse mode automatic differentiation

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

mdivide_right_tri_low.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_FWD_MAT_FUN_MDIVIDE_RIGHT_TRI_LOW_HPP
2 #define STAN_MATH_FWD_MAT_FUN_MDIVIDE_RIGHT_TRI_LOW_HPP
3
4 #include <stan/math/prim/mat/fun/Eigen.hpp>
5 #include <stan/math/prim/mat/fun/mdivide_right.hpp>
6 #include <stan/math/prim/mat/fun/typedefs.hpp>
7 #include <stan/math/prim/mat/err/check_multiplicable.hpp>
8 #include <stan/math/prim/mat/err/check_square.hpp>
9 #include <stan/math/fwd/mat/fun/typedefs.hpp>
10 #include <stan/math/fwd/mat/fun/to_fvar.hpp>
11 #include <stan/math/fwd/mat/fun/multiply.hpp>
12 #include <stan/math/fwd/mat/fun/inverse.hpp>
13 #include <stan/math/fwd/core.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_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;
25 using stan::math::mdivide_right;
26 stan::math::check_square("mdivide_right_tri_low", "b", b);
27 stan::math::check_multiplicable("mdivide_right_tri_low",
28 "A", A,
29 "b", b);
30
31 Eigen::Matrix<T, R1, C2> A_mult_inv_b(A.rows(), b.cols());
32 Eigen::Matrix<T, R1, C2> deriv_A_mult_inv_b(A.rows(), b.cols());
33 Eigen::Matrix<T, R2, C2> deriv_b_mult_inv_b(b.rows(), b.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_b.setZero();
39 deriv_b.setZero();
40
41 for (size_type j = 0; j < A.cols(); j++) {
42 for (size_type i = 0; 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 = j; 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 A_mult_inv_b = mdivide_right(val_A, val_b);
56 deriv_A_mult_inv_b = mdivide_right(deriv_A, val_b);
57 deriv_b_mult_inv_b = mdivide_right(deriv_b, val_b);
58
59 Eigen::Matrix<T, R1, C2> deriv(A.rows(), b.cols());
60 deriv = deriv_A_mult_inv_b - multiply(A_mult_inv_b, deriv_b_mult_inv_b);
61
62 return stan::math::to_fvar(A_mult_inv_b, deriv);
63 }
64
65 template <typename T, int R1, int C1, int R2, int C2>
66 inline
67 Eigen::Matrix<fvar<T>, R1, C2>
68 mdivide_right_tri_low(const Eigen::Matrix<fvar<T>, R1, C1> &A,
69 const Eigen::Matrix<double, R2, C2> &b) {
70 using stan::math::multiply;
71 using stan::math::mdivide_right;
72 stan::math::check_square("mdivide_right_tri_low", "b", b);
73 stan::math::check_multiplicable("mdivide_right_tri_low",
74 "A", A,
75 "b", b);
76
77 Eigen::Matrix<T, R2, C2> deriv_b_mult_inv_b(b.rows(), b.cols());
78 Eigen::Matrix<T, R1, C1> val_A(A.rows(), A.cols());
79 Eigen::Matrix<T, R1, C1> deriv_A(A.rows(), A.cols());
80 Eigen::Matrix<T, R2, C2> val_b(b.rows(), b.cols());
81 val_b.setZero();
82
83 for (int j = 0; j < A.cols(); j++) {
84 for (int i = 0; i < A.rows(); i++) {
85 val_A(i, j) = A(i, j).val_;
86 deriv_A(i, j) = A(i, j).d_;
87 }
88 }
89
90 for (size_type j = 0; j < b.cols(); j++) {
91 for (size_type i = j; i < b.rows(); i++) {
92 val_b(i, j) = b(i, j);
93 }
94 }
95
96 return stan::math::to_fvar(mdivide_right(val_A, val_b),
97 mdivide_right(deriv_A, val_b));
98 }
99
100 template <typename T, int R1, int C1, int R2, int C2>
101 inline
102 Eigen::Matrix<fvar<T>, R1, C2>
103 mdivide_right_tri_low(const Eigen::Matrix<double, R1, C1> &A,
104 const Eigen::Matrix<fvar<T>, R2, C2> &b) {
105 using stan::math::multiply;
106 using stan::math::mdivide_right;
107 stan::math::check_square("mdivide_right_tri_low", "b", b);
108 stan::math::check_multiplicable("mdivide_right_tri_low",
109 "A", A,
110 "b", b);
111
112 Eigen::Matrix<T, R1, C2>
113 A_mult_inv_b(A.rows(), b.cols());
114 Eigen::Matrix<T, R2, C2> deriv_b_mult_inv_b(b.rows(), b.cols());
115 Eigen::Matrix<T, R2, C2> val_b(b.rows(), b.cols());
116 Eigen::Matrix<T, R2, C2> deriv_b(b.rows(), b.cols());
117 val_b.setZero();
118 deriv_b.setZero();
119
120 for (int j = 0; j < b.cols(); j++) {
121 for (int i = j; i < b.rows(); i++) {
122 val_b(i, j) = b(i, j).val_;
123 deriv_b(i, j) = b(i, j).d_;
124 }
125 }
126
127 A_mult_inv_b = mdivide_right(A, val_b);
128 deriv_b_mult_inv_b = mdivide_right(deriv_b, val_b);
129
130 Eigen::Matrix<T, R1, C2>
131 deriv(A.rows(), b.cols());
132 deriv = -multiply(A_mult_inv_b, deriv_b_mult_inv_b);
133
134 return stan::math::to_fvar(A_mult_inv_b, deriv);
135 }
136 }
137 }
138 #endif
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
mdivide_right.hpp
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
stan::math::mdivide_right
Eigen::Matrix< fvar< T >, R1, C2 > mdivide_right(const Eigen::Matrix< fvar< T >, R1, C1 > &A, const Eigen::Matrix< fvar< T >, R2, C2 > &b)
Definition: mdivide_right.hpp:24
to_fvar.hpp
stan::math::fvar
Definition: fvar.hpp:13
typedefs.hpp
stan::math::mdivide_right_tri_low
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)
Definition: mdivide_right_tri_low.hpp:22
check_square.hpp

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