| Stan Math Library 2.6.3 probability, sampling & optimization

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerator Friends Macros
mdivide_right.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_FWD_MAT_FUN_MDIVIDE_RIGHT_HPP
2 #define STAN_MATH_FWD_MAT_FUN_MDIVIDE_RIGHT_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_right.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/core.hpp>
10 #include <stan/math/fwd/mat/fun/typedefs.hpp>
11 #include <stan/math/fwd/mat/fun/inverse.hpp>
12 #include <stan/math/fwd/mat/fun/multiply.hpp>
13 #include <stan/math/prim/mat/fun/multiply.hpp>
14 #include <stan/math/fwd/mat/fun/to_fvar.hpp>
15 #include <stan/math/prim/mat/fun/inverse.hpp>
16 #include <vector>
17
18 namespace stan {
19 namespace math {
20
21 template <typename T, int R1, int C1, int R2, int C2>
22 inline
23 Eigen::Matrix<fvar<T>, R1, C2>
24 mdivide_right(const Eigen::Matrix<fvar<T>, R1, C1> &A,
25 const Eigen::Matrix<fvar<T>, R2, C2> &b) {
26 using stan::math::multiply;
27 using stan::math::mdivide_right;
28 stan::math::check_square("mdivide_right", "b", b);
29 stan::math::check_multiplicable("mdivide_right",
30 "A", A,
31 "b", b);
32
33 Eigen::Matrix<T, R1, C2> A_mult_inv_b(A.rows(), b.cols());
34 Eigen::Matrix<T, R1, C2> deriv_A_mult_inv_b(A.rows(), b.cols());
35 Eigen::Matrix<T, R2, C2> deriv_b_mult_inv_b(b.rows(), b.cols());
36 Eigen::Matrix<T, R1, C1> val_A(A.rows(), A.cols());
37 Eigen::Matrix<T, R1, C1> deriv_A(A.rows(), A.cols());
38 Eigen::Matrix<T, R2, C2> val_b(b.rows(), b.cols());
39 Eigen::Matrix<T, R2, C2> deriv_b(b.rows(), b.cols());
40
41 for (int j = 0; j < A.cols(); j++) {
42 for (int 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 (int j = 0; j < b.cols(); j++) {
49 for (int 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 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(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", "b", b);
73 stan::math::check_multiplicable("mdivide_right",
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
81 for (int j = 0; j < A.cols(); j++) {
82 for (int i = 0; i < A.rows(); i++) {
83 val_A(i, j) = A(i, j).val_;
84 deriv_A(i, j) = A(i, j).d_;
85 }
86 }
87
88 return stan::math::to_fvar(mdivide_right(val_A, b),
89 mdivide_right(deriv_A, 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_right(const Eigen::Matrix<double, R1, C1> &A,
96 const Eigen::Matrix<fvar<T>, R2, C2> &b) {
97 using stan::math::multiply;
98 using stan::math::mdivide_right;
99 stan::math::check_square("mdivide_right", "b", b);
100 stan::math::check_multiplicable("mdivide_right",
101 "A", A,
102 "b", b);
103 Eigen::Matrix<T, R1, C2>
104 A_mult_inv_b(A.rows(), b.cols());
105 Eigen::Matrix<T, R2, C2> deriv_b_mult_inv_b(b.rows(), b.cols());
106 Eigen::Matrix<T, R2, C2> val_b(b.rows(), b.cols());
107 Eigen::Matrix<T, R2, C2> deriv_b(b.rows(), b.cols());
108
109 for (int j = 0; j < b.cols(); j++) {
110 for (int i = 0; i < b.rows(); i++) {
111 val_b(i, j) = b(i, j).val_;
112 deriv_b(i, j) = b(i, j).d_;
113 }
114 }
115
116 A_mult_inv_b = mdivide_right(A, val_b);
117 deriv_b_mult_inv_b = mdivide_right(deriv_b, val_b);
118
119 Eigen::Matrix<T, R1, C2>
120 deriv(A.rows(), b.cols());
121 deriv = -multiply(A_mult_inv_b, deriv_b_mult_inv_b);
122
123 return stan::math::to_fvar(A_mult_inv_b, deriv);
124 }
125 }
126 }
127 #endif
typedefs.hpp
multiply.hpp
core.hpp
check_multiplicable.hpp
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:20
multiply.hpp
stan::math::to_fvar
fvar< T > to_fvar(const T &x)
Definition: to_fvar.hpp:16
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
inverse.hpp
Eigen.hpp
to_fvar.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
stan::math::fvar
Definition: fvar.hpp:13
typedefs.hpp
check_square.hpp

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