Stan Math Library  2.9.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, const 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(const 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) {
80  "m1", m1,
81  "m2", m2);
82  Eigen::Matrix<fvar<T>, R1, C2> result(m1.rows(), m2.cols());
83  for (size_type i = 0; i < m1.rows(); i++) {
84  Eigen::Matrix<fvar<T>, 1, C1> crow = m1.row(i);
85  for (size_type j = 0; j < m2.cols(); j++) {
86  Eigen::Matrix<fvar<T>, R2, 1> ccol = m2.col(j);
87  result(i, j) = stan::math::dot_product(crow, ccol);
88  }
89  }
90  return result;
91  }
92 
93  template<typename T, int R1, int C1, int R2, int C2>
94  inline
95  Eigen::Matrix<fvar<T>, R1, C2>
96  multiply(const Eigen::Matrix<fvar<T>, R1, C1>& m1,
97  const Eigen::Matrix<double, R2, C2>& m2) {
99  "m1", m1,
100  "m2", m2);
101  Eigen::Matrix<fvar<T>, R1, C2> result(m1.rows(), m2.cols());
102  for (size_type i = 0; i < m1.rows(); i++) {
103  Eigen::Matrix<fvar<T>, 1, C1> crow = m1.row(i);
104  for (size_type j = 0; j < m2.cols(); j++) {
105  Eigen::Matrix<double, R2, 1> ccol = m2.col(j);
106  result(i, j) = stan::math::dot_product(crow, ccol);
107  }
108  }
109  return result;
110  }
111 
112  template<typename T, int R1, int C1, int R2, int C2>
113  inline
114  Eigen::Matrix<fvar<T>, R1, C2>
115  multiply(const Eigen::Matrix<double, R1, C1>& m1,
116  const Eigen::Matrix<fvar<T>, R2, C2>& m2) {
118  "m1", m1,
119  "m2", m2);
120  Eigen::Matrix<fvar<T>, R1, C2> result(m1.rows(), m2.cols());
121  for (size_type i = 0; i < m1.rows(); i++) {
122  Eigen::Matrix<double, 1, C1> crow = m1.row(i);
123  for (size_type j = 0; j < m2.cols(); j++) {
124  Eigen::Matrix<fvar<T>, R2, 1> ccol = m2.col(j);
125  result(i, j) = stan::math::dot_product(crow, ccol);
126  }
127  }
128  return result;
129  }
130 
131  template <typename T, int C1, int R2>
132  inline
133  fvar<T>
134  multiply(const Eigen::Matrix<fvar<T>, 1, C1>& rv,
135  const Eigen::Matrix<fvar<T>, R2, 1>& v) {
136  if (rv.size() != v.size())
137  throw std::domain_error("row vector and vector must be same length "
138  "in multiply");
139  return dot_product(rv, v);
140  }
141 
142  template <typename T, int C1, int R2>
143  inline
144  fvar<T>
145  multiply(const Eigen::Matrix<fvar<T>, 1, C1>& rv,
146  const Eigen::Matrix<double, R2, 1>& v) {
147  if (rv.size() != v.size())
148  throw std::domain_error("row vector and vector must be same length "
149  "in multiply");
150  return dot_product(rv, v);
151  }
152 
153  template <typename T, int C1, int R2>
154  inline
155  fvar<T>
156  multiply(const Eigen::Matrix<double, 1, C1>& rv,
157  const Eigen::Matrix<fvar<T>, R2, 1>& v) {
158  if (rv.size() != v.size())
159  throw std::domain_error("row vector and vector must be same length "
160  "in multiply");
161  return dot_product(rv, v);
162  }
163  }
164 }
165 #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
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.
fvar< T > dot_product(const Eigen::Matrix< fvar< T >, R1, C1 > &v1, const Eigen::Matrix< fvar< T >, R2, C2 > &v2)
Definition: dot_product.hpp:20
void domain_error(const char *function, const char *name, const T &y, const char *msg1, const char *msg2)
Throw a domain error with a consistently formatted message.

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