Stan Math Library  2.20.0
reverse mode automatic differentiation
divide.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_FWD_MAT_FUN_DIVIDE_HPP
2 #define STAN_MATH_FWD_MAT_FUN_DIVIDE_HPP
3 
5 #include <stan/math/fwd/core.hpp>
6 
7 namespace stan {
8 namespace math {
9 
10 template <typename T, int R, int C>
11 inline Eigen::Matrix<fvar<T>, R, C> divide(
12  const Eigen::Matrix<fvar<T>, R, C>& v, const fvar<T>& c) {
13  Eigen::Matrix<fvar<T>, R, C> res(v.rows(), v.cols());
14  for (int i = 0; i < v.rows(); i++) {
15  for (int j = 0; j < v.cols(); j++)
16  res(i, j) = v(i, j) / c;
17  }
18  return res;
19 }
20 
21 template <typename T, int R, int C>
22 inline Eigen::Matrix<fvar<T>, R, C> divide(
23  const Eigen::Matrix<fvar<T>, R, C>& v, double c) {
24  Eigen::Matrix<fvar<T>, R, C> res(v.rows(), v.cols());
25  for (int i = 0; i < v.rows(); i++) {
26  for (int j = 0; j < v.cols(); j++)
27  res(i, j) = v(i, j) / c;
28  }
29  return res;
30 }
31 
32 template <typename T, int R, int C>
33 inline Eigen::Matrix<fvar<T>, R, C> divide(const Eigen::Matrix<double, R, C>& v,
34  const fvar<T>& c) {
35  Eigen::Matrix<fvar<T>, R, C> res(v.rows(), v.cols());
36  for (int i = 0; i < v.rows(); i++) {
37  for (int j = 0; j < v.cols(); j++)
38  res(i, j) = v(i, j) / c;
39  }
40  return res;
41 }
42 
43 template <typename T, int R, int C>
44 inline Eigen::Matrix<fvar<T>, R, C> operator/(
45  const Eigen::Matrix<fvar<T>, R, C>& v, const fvar<T>& c) {
46  return divide(v, c);
47 }
48 
49 template <typename T, int R, int C>
50 inline Eigen::Matrix<fvar<T>, R, C> operator/(
51  const Eigen::Matrix<fvar<T>, R, C>& v, double c) {
52  return divide(v, c);
53 }
54 
55 template <typename T, int R, int C>
56 inline Eigen::Matrix<fvar<T>, R, C> operator/(
57  const Eigen::Matrix<double, R, C>& v, const fvar<T>& c) {
58  return divide(v, c);
59 }
60 } // namespace math
61 } // namespace stan
62 #endif
fvar< T > operator/(const fvar< T > &x1, const fvar< T > &x2)
Return the result of dividing the first argument by the second.
Eigen::Matrix< fvar< T >, R, C > divide(const Eigen::Matrix< fvar< T >, R, C > &v, const fvar< T > &c)
Definition: divide.hpp:11
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.