Stan Math Library  2.20.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 
6 #include <stan/math/fwd/core.hpp>
9 
10 namespace stan {
11 namespace math {
12 
13 template <typename T, int R1, int C1>
14 inline Eigen::Matrix<fvar<T>, R1, C1> multiply(
15  const Eigen::Matrix<fvar<T>, R1, C1>& m, const fvar<T>& c) {
16  Eigen::Matrix<fvar<T>, R1, C1> res(m.rows(), m.cols());
17  for (int i = 0; i < m.rows(); i++) {
18  for (int j = 0; j < m.cols(); j++)
19  res(i, j) = c * m(i, j);
20  }
21  return res;
22 }
23 
24 template <typename T, int R2, int C2>
25 inline Eigen::Matrix<fvar<T>, R2, C2> multiply(
26  const Eigen::Matrix<fvar<T>, R2, C2>& m, double c) {
27  Eigen::Matrix<fvar<T>, R2, C2> res(m.rows(), m.cols());
28  for (int i = 0; i < m.rows(); i++) {
29  for (int j = 0; j < m.cols(); j++)
30  res(i, j) = c * m(i, j);
31  }
32  return res;
33 }
34 
35 template <typename T, int R1, int C1>
36 inline Eigen::Matrix<fvar<T>, R1, C1> multiply(
37  const Eigen::Matrix<double, R1, C1>& m, const fvar<T>& c) {
38  Eigen::Matrix<fvar<T>, R1, C1> res(m.rows(), m.cols());
39  for (int i = 0; i < m.rows(); i++) {
40  for (int j = 0; j < m.cols(); j++)
41  res(i, j) = c * m(i, j);
42  }
43  return res;
44 }
45 
46 template <typename T, int R1, int C1>
47 inline Eigen::Matrix<fvar<T>, R1, C1> multiply(
48  const fvar<T>& c, const Eigen::Matrix<fvar<T>, R1, C1>& m) {
49  return multiply(m, c);
50 }
51 
52 template <typename T, int R1, int C1>
53 inline Eigen::Matrix<fvar<T>, R1, C1> multiply(
54  double c, const Eigen::Matrix<fvar<T>, R1, C1>& m) {
55  return multiply(m, c);
56 }
57 
58 template <typename T, int R1, int C1>
59 inline Eigen::Matrix<fvar<T>, R1, C1> multiply(
60  const fvar<T>& c, const Eigen::Matrix<double, R1, C1>& m) {
61  return multiply(m, c);
62 }
63 
64 template <typename T, int R1, int C1, int R2, int C2>
65 inline Eigen::Matrix<fvar<T>, R1, C2> multiply(
66  const Eigen::Matrix<fvar<T>, R1, C1>& m1,
67  const Eigen::Matrix<fvar<T>, R2, C2>& m2) {
68  check_multiplicable("multiply", "m1", m1, "m2", m2);
69  Eigen::Matrix<fvar<T>, R1, C2> result(m1.rows(), m2.cols());
70  for (size_type i = 0; i < m1.rows(); i++) {
71  Eigen::Matrix<fvar<T>, 1, C1> crow = m1.row(i);
72  for (size_type j = 0; j < m2.cols(); j++) {
73  Eigen::Matrix<fvar<T>, R2, 1> ccol = m2.col(j);
74  result(i, j) = dot_product(crow, ccol);
75  }
76  }
77  return result;
78 }
79 
80 template <typename T, int R1, int C1, int R2, int C2>
81 inline Eigen::Matrix<fvar<T>, R1, C2> multiply(
82  const Eigen::Matrix<fvar<T>, R1, C1>& m1,
83  const Eigen::Matrix<double, R2, C2>& m2) {
84  check_multiplicable("multiply", "m1", m1, "m2", m2);
85  Eigen::Matrix<fvar<T>, R1, C2> result(m1.rows(), m2.cols());
86  for (size_type i = 0; i < m1.rows(); i++) {
87  Eigen::Matrix<fvar<T>, 1, C1> crow = m1.row(i);
88  for (size_type j = 0; j < m2.cols(); j++) {
89  Eigen::Matrix<double, R2, 1> ccol = m2.col(j);
90  result(i, j) = dot_product(crow, ccol);
91  }
92  }
93  return result;
94 }
95 
96 template <typename T, int R1, int C1, int R2, int C2>
97 inline Eigen::Matrix<fvar<T>, R1, C2> multiply(
98  const Eigen::Matrix<double, R1, C1>& m1,
99  const Eigen::Matrix<fvar<T>, R2, C2>& m2) {
100  check_multiplicable("multiply", "m1", m1, "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<double, 1, C1> crow = m1.row(i);
104  for (size_type j = 0; j < m2.cols(); j++) {
105  Eigen::Matrix<fvar<T>, R2, 1> ccol = m2.col(j);
106  result(i, j) = dot_product(crow, ccol);
107  }
108  }
109  return result;
110 }
111 
112 template <typename T, int C1, int R2>
113 inline fvar<T> multiply(const Eigen::Matrix<fvar<T>, 1, C1>& rv,
114  const Eigen::Matrix<fvar<T>, R2, 1>& v) {
115  check_multiplicable("multiply", "rv", rv, "v", v);
116  return dot_product(rv, v);
117 }
118 
119 template <typename T, int C1, int R2>
120 inline fvar<T> multiply(const Eigen::Matrix<fvar<T>, 1, C1>& rv,
121  const Eigen::Matrix<double, R2, 1>& v) {
122  check_multiplicable("multiply", "rv", rv, "v", v);
123  return dot_product(rv, v);
124 }
125 
126 template <typename T, int C1, int R2>
127 inline fvar<T> multiply(const Eigen::Matrix<double, 1, C1>& rv,
128  const Eigen::Matrix<fvar<T>, R2, 1>& v) {
129  check_multiplicable("multiply", "rv", rv, "v", v);
130  return dot_product(rv, v);
131 }
132 
133 } // namespace math
134 } // namespace stan
135 #endif
Eigen::Matrix< fvar< T >, R1, C1 > multiply(const Eigen::Matrix< fvar< T >, R1, C1 > &m, const fvar< T > &c)
Definition: multiply.hpp:14
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:11
fvar< T > dot_product(const Eigen::Matrix< fvar< T >, R1, C1 > &v1, const Eigen::Matrix< fvar< T >, R2, C2 > &v2)
Definition: dot_product.hpp:14
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.
This template class represents scalars used in forward-mode automatic differentiation, which consist of values and directional derivatives of the specified template type.
Definition: fvar.hpp:41

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