Stan Math Library  2.14.0
reverse mode automatic differentiation
multiply.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_FWD_MAT_FUN_MULTIPLY_HPP
2 #define STAN_MATH_FWD_MAT_FUN_MULTIPLY_HPP
3 
7 #include <stan/math/fwd/core.hpp>
11 #include <boost/math/tools/promotion.hpp>
12 #include <vector>
13 
14 namespace stan {
15  namespace math {
16 
17  template<typename T, int R1, int C1>
18  inline
19  Eigen::Matrix<fvar<T>, R1, C1>
20  multiply(const Eigen::Matrix<fvar<T>, R1, C1>& m, const fvar<T>& c) {
21  Eigen::Matrix<fvar<T>, R1, C1> res(m.rows(), m.cols());
22  for (int i = 0; i < m.rows(); i++) {
23  for (int j = 0; j < m.cols(); j++)
24  res(i, j) = c * m(i, j);
25  }
26  return res;
27  }
28 
29  template<typename T, int R2, int C2>
30  inline
31  Eigen::Matrix<fvar<T>, R2, C2>
32  multiply(const Eigen::Matrix<fvar<T>, R2, C2>& m, double c) {
33  Eigen::Matrix<fvar<T>, R2, C2> res(m.rows(), m.cols());
34  for (int i = 0; i < m.rows(); i++) {
35  for (int j = 0; j < m.cols(); j++)
36  res(i, j) = c * m(i, j);
37  }
38  return res;
39  }
40 
41  template<typename T, int R1, int C1>
42  inline
43  Eigen::Matrix<fvar<T>, R1, C1>
44  multiply(const Eigen::Matrix<double, R1, C1>& m, const fvar<T>& c) {
45  Eigen::Matrix<fvar<T>, R1, C1> res(m.rows(), m.cols());
46  for (int i = 0; i < m.rows(); i++) {
47  for (int j = 0; j < m.cols(); j++)
48  res(i, j) = c * m(i, j);
49  }
50  return res;
51  }
52 
53  template<typename T, int R1, int C1>
54  inline
55  Eigen::Matrix<fvar<T>, R1, C1>
56  multiply(const fvar<T>& c, const Eigen::Matrix<fvar<T>, R1, C1>& m) {
57  return multiply(m, c);
58  }
59 
60  template<typename T, int R1, int C1>
61  inline
62  Eigen::Matrix<fvar<T>, R1, C1>
63  multiply(double c, const Eigen::Matrix<fvar<T>, R1, C1>& m) {
64  return multiply(m, c);
65  }
66 
67  template<typename T, int R1, int C1>
68  inline
69  Eigen::Matrix<fvar<T>, R1, C1>
70  multiply(const fvar<T>& c, const Eigen::Matrix<double, R1, C1>& m) {
71  return multiply(m, c);
72  }
73 
74  template<typename T, int R1, int C1, int R2, int C2>
75  inline
76  Eigen::Matrix<fvar<T>, R1, C2>
77  multiply(const Eigen::Matrix<fvar<T>, R1, C1>& m1,
78  const Eigen::Matrix<fvar<T>, R2, C2>& m2) {
79  check_multiplicable("multiply", "m1", m1, "m2", m2);
80  Eigen::Matrix<fvar<T>, R1, C2> result(m1.rows(), m2.cols());
81  for (size_type i = 0; i < m1.rows(); i++) {
82  Eigen::Matrix<fvar<T>, 1, C1> crow = m1.row(i);
83  for (size_type j = 0; j < m2.cols(); j++) {
84  Eigen::Matrix<fvar<T>, R2, 1> ccol = m2.col(j);
85  result(i, j) = dot_product(crow, ccol);
86  }
87  }
88  return result;
89  }
90 
91  template<typename T, int R1, int C1, int R2, int C2>
92  inline
93  Eigen::Matrix<fvar<T>, R1, C2>
94  multiply(const Eigen::Matrix<fvar<T>, R1, C1>& m1,
95  const Eigen::Matrix<double, R2, C2>& m2) {
96  check_multiplicable("multiply", "m1", m1, "m2", m2);
97  Eigen::Matrix<fvar<T>, R1, C2> result(m1.rows(), m2.cols());
98  for (size_type i = 0; i < m1.rows(); i++) {
99  Eigen::Matrix<fvar<T>, 1, C1> crow = m1.row(i);
100  for (size_type j = 0; j < m2.cols(); j++) {
101  Eigen::Matrix<double, R2, 1> ccol = m2.col(j);
102  result(i, j) = dot_product(crow, ccol);
103  }
104  }
105  return result;
106  }
107 
108  template<typename T, int R1, int C1, int R2, int C2>
109  inline
110  Eigen::Matrix<fvar<T>, R1, C2>
111  multiply(const Eigen::Matrix<double, R1, C1>& m1,
112  const Eigen::Matrix<fvar<T>, R2, C2>& m2) {
113  check_multiplicable("multiply",
114  "m1", m1,
115  "m2", m2);
116  Eigen::Matrix<fvar<T>, R1, C2> result(m1.rows(), m2.cols());
117  for (size_type i = 0; i < m1.rows(); i++) {
118  Eigen::Matrix<double, 1, C1> crow = m1.row(i);
119  for (size_type j = 0; j < m2.cols(); j++) {
120  Eigen::Matrix<fvar<T>, R2, 1> ccol = m2.col(j);
121  result(i, j) = dot_product(crow, ccol);
122  }
123  }
124  return result;
125  }
126 
127  template <typename T, int C1, int R2>
128  inline
129  fvar<T>
130  multiply(const Eigen::Matrix<fvar<T>, 1, C1>& rv,
131  const Eigen::Matrix<fvar<T>, R2, 1>& v) {
132  check_multiplicable("multiply", "rv", rv, "v", v);
133  return dot_product(rv, v);
134  }
135 
136  template <typename T, int C1, int R2>
137  inline
138  fvar<T>
139  multiply(const Eigen::Matrix<fvar<T>, 1, C1>& rv,
140  const Eigen::Matrix<double, R2, 1>& v) {
141  check_multiplicable("multiply", "rv", rv, "v", v);
142  return dot_product(rv, v);
143  }
144 
145  template <typename T, int C1, int R2>
146  inline
147  fvar<T>
148  multiply(const Eigen::Matrix<double, 1, C1>& rv,
149  const Eigen::Matrix<fvar<T>, R2, 1>& v) {
150  check_multiplicable("multiply", "rv", rv, "v", v);
151  return dot_product(rv, v);
152  }
153 
154  }
155 }
156 #endif
Eigen::Matrix< fvar< T >, R1, C1 > multiply(const Eigen::Matrix< fvar< T >, R1, C1 > &m, const fvar< T > &c)
Definition: multiply.hpp:20
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
fvar< T > dot_product(const Eigen::Matrix< fvar< T >, R1, C1 > &v1, const Eigen::Matrix< fvar< T >, R2, C2 > &v2)
Definition: dot_product.hpp:18
void check_multiplicable(const char *function, const char *name1, const T1 &y1, const char *name2, const T2 &y2)
Check if the matrices can be multiplied.

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