Stan Math Library  2.10.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 <stdexcept>
13 #include <vector>
14 
15 namespace stan {
16  namespace math {
17 
18  template<typename T, int R1, int C1>
19  inline
20  Eigen::Matrix<fvar<T>, R1, C1>
21  multiply(const Eigen::Matrix<fvar<T>, R1, C1>& m, const fvar<T>& c) {
22  Eigen::Matrix<fvar<T>, R1, C1> res(m.rows(), m.cols());
23  for (int i = 0; i < m.rows(); i++) {
24  for (int j = 0; j < m.cols(); j++)
25  res(i, j) = c * m(i, j);
26  }
27  return res;
28  }
29 
30  template<typename T, int R2, int C2>
31  inline
32  Eigen::Matrix<fvar<T>, R2, C2>
33  multiply(const Eigen::Matrix<fvar<T>, R2, C2>& m, const double c) {
34  Eigen::Matrix<fvar<T>, R2, C2> res(m.rows(), m.cols());
35  for (int i = 0; i < m.rows(); i++) {
36  for (int j = 0; j < m.cols(); j++)
37  res(i, j) = c * m(i, j);
38  }
39  return res;
40  }
41 
42  template<typename T, int R1, int C1>
43  inline
44  Eigen::Matrix<fvar<T>, R1, C1>
45  multiply(const Eigen::Matrix<double, R1, C1>& m, const fvar<T>& c) {
46  Eigen::Matrix<fvar<T>, R1, C1> res(m.rows(), m.cols());
47  for (int i = 0; i < m.rows(); i++) {
48  for (int j = 0; j < m.cols(); j++)
49  res(i, j) = c * m(i, j);
50  }
51  return res;
52  }
53 
54  template<typename T, int R1, int C1>
55  inline
56  Eigen::Matrix<fvar<T>, R1, C1>
57  multiply(const fvar<T>& c, const Eigen::Matrix<fvar<T>, R1, C1>& m) {
58  return multiply(m, c);
59  }
60 
61  template<typename T, int R1, int C1>
62  inline
63  Eigen::Matrix<fvar<T>, R1, C1>
64  multiply(const double c, const Eigen::Matrix<fvar<T>, R1, C1>& m) {
65  return multiply(m, c);
66  }
67 
68  template<typename T, int R1, int C1>
69  inline
70  Eigen::Matrix<fvar<T>, R1, C1>
71  multiply(const fvar<T>& c, const Eigen::Matrix<double, R1, C1>& m) {
72  return multiply(m, c);
73  }
74 
75  template<typename T, int R1, int C1, int R2, int C2>
76  inline
77  Eigen::Matrix<fvar<T>, R1, C2>
78  multiply(const Eigen::Matrix<fvar<T>, R1, C1>& m1,
79  const Eigen::Matrix<fvar<T>, R2, C2>& m2) {
81  "m1", m1,
82  "m2", m2);
83  Eigen::Matrix<fvar<T>, R1, C2> result(m1.rows(), m2.cols());
84  for (size_type i = 0; i < m1.rows(); i++) {
85  Eigen::Matrix<fvar<T>, 1, C1> crow = m1.row(i);
86  for (size_type j = 0; j < m2.cols(); j++) {
87  Eigen::Matrix<fvar<T>, R2, 1> ccol = m2.col(j);
88  result(i, j) = stan::math::dot_product(crow, ccol);
89  }
90  }
91  return result;
92  }
93 
94  template<typename T, int R1, int C1, int R2, int C2>
95  inline
96  Eigen::Matrix<fvar<T>, R1, C2>
97  multiply(const Eigen::Matrix<fvar<T>, R1, C1>& m1,
98  const Eigen::Matrix<double, R2, C2>& m2) {
100  "m1", m1,
101  "m2", m2);
102  Eigen::Matrix<fvar<T>, R1, C2> result(m1.rows(), m2.cols());
103  for (size_type i = 0; i < m1.rows(); i++) {
104  Eigen::Matrix<fvar<T>, 1, C1> crow = m1.row(i);
105  for (size_type j = 0; j < m2.cols(); j++) {
106  Eigen::Matrix<double, R2, 1> ccol = m2.col(j);
107  result(i, j) = stan::math::dot_product(crow, ccol);
108  }
109  }
110  return result;
111  }
112 
113  template<typename T, int R1, int C1, int R2, int C2>
114  inline
115  Eigen::Matrix<fvar<T>, R1, C2>
116  multiply(const Eigen::Matrix<double, R1, C1>& m1,
117  const Eigen::Matrix<fvar<T>, R2, C2>& m2) {
119  "m1", m1,
120  "m2", m2);
121  Eigen::Matrix<fvar<T>, R1, C2> result(m1.rows(), m2.cols());
122  for (size_type i = 0; i < m1.rows(); i++) {
123  Eigen::Matrix<double, 1, C1> crow = m1.row(i);
124  for (size_type j = 0; j < m2.cols(); j++) {
125  Eigen::Matrix<fvar<T>, R2, 1> ccol = m2.col(j);
126  result(i, j) = stan::math::dot_product(crow, ccol);
127  }
128  }
129  return result;
130  }
131 
132  template <typename T, int C1, int R2>
133  inline
134  fvar<T>
135  multiply(const Eigen::Matrix<fvar<T>, 1, C1>& rv,
136  const Eigen::Matrix<fvar<T>, R2, 1>& v) {
137  if (rv.size() != v.size())
138  throw std::domain_error("row vector and vector must be same length "
139  "in multiply");
140  return dot_product(rv, v);
141  }
142 
143  template <typename T, int C1, int R2>
144  inline
145  fvar<T>
146  multiply(const Eigen::Matrix<fvar<T>, 1, C1>& rv,
147  const Eigen::Matrix<double, R2, 1>& v) {
148  if (rv.size() != v.size())
149  throw std::domain_error("row vector and vector must be same length "
150  "in multiply");
151  return dot_product(rv, v);
152  }
153 
154  template <typename T, int C1, int R2>
155  inline
156  fvar<T>
157  multiply(const Eigen::Matrix<double, 1, C1>& rv,
158  const Eigen::Matrix<fvar<T>, R2, 1>& v) {
159  if (rv.size() != v.size())
160  throw std::domain_error("row vector and vector must be same length "
161  "in multiply");
162  return dot_product(rv, v);
163  }
164  }
165 }
166 #endif
Eigen::Matrix< fvar< T >, R1, C1 > multiply(const Eigen::Matrix< fvar< T >, R1, C1 > &m, const fvar< T > &c)
Definition: multiply.hpp:21
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–2016, Stan Development Team.