Stan Math Library  2.12.0
reverse mode automatic differentiation
promoter.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_FUN_PROMOTER_HPP
2 #define STAN_MATH_PRIM_MAT_FUN_PROMOTER_HPP
3 
5 #include <vector>
6 
7 namespace stan {
8  namespace math {
9  // from input type F to output type T
10 
11  // scalar, F != T (base template)
12  template <typename F, typename T>
13  struct promoter {
14  inline static void promote(const F& u, T& t) {
15  t = u;
16  }
17  inline static T promote_to(const F& u) {
18  return u;
19  }
20  };
21  // scalar, F == T
22  template <typename T>
23  struct promoter<T, T> {
24  inline static void promote(const T& u, T& t) {
25  t = u;
26  }
27  inline static T promote_to(const T& u) {
28  return u;
29  }
30  };
31 
32  // std::vector, F != T
33  template <typename F, typename T>
34  struct promoter<std::vector<F>, std::vector<T> > {
35  inline static void promote(const std::vector<F>& u,
36  std::vector<T>& t) {
37  t.resize(u.size());
38  for (size_t i = 0; i < u.size(); ++i)
39  promoter<F, T>::promote(u[i], t[i]);
40  }
41  inline static std::vector<T>
42  promote_to(const std::vector<F>& u) {
43  std::vector<T> t;
44  promoter<std::vector<F>, std::vector<T> >::promote(u, t);
45  return t;
46  }
47  };
48  // std::vector, F == T
49  template <typename T>
50  struct promoter<std::vector<T>, std::vector<T> > {
51  inline static void promote(const std::vector<T>& u,
52  std::vector<T>& t) {
53  t = u;
54  }
55  inline static std::vector<T> promote_to(const std::vector<T>& u) {
56  return u;
57  }
58  };
59 
60  // Eigen::Matrix, F != T
61  template <typename F, typename T, int R, int C>
62  struct promoter<Eigen::Matrix<F, R, C>, Eigen::Matrix<T, R, C> > {
63  inline static void promote(const Eigen::Matrix<F, R, C>& u,
64  Eigen::Matrix<T, R, C>& t) {
65  t.resize(u.rows(), u.cols());
66  for (int i = 0; i < u.size(); ++i)
67  promoter<F, T>::promote(u(i), t(i));
68  }
69  inline static Eigen::Matrix<T, R, C>
70  promote_to(const Eigen::Matrix<F, R, C>& u) {
71  Eigen::Matrix<T, R, C> t;
73  Eigen::Matrix<T, R, C> >::promote(u, t);
74  return t;
75  }
76  };
77  // Eigen::Matrix, F == T
78  template <typename T, int R, int C>
79  struct promoter<Eigen::Matrix<T, R, C>, Eigen::Matrix<T, R, C> > {
80  inline static void promote(const Eigen::Matrix<T, R, C>& u,
81  Eigen::Matrix<T, R, C>& t) {
82  t = u;
83  }
84  inline static Eigen::Matrix<T, R, C>
85  promote_to(const Eigen::Matrix<T, R, C>& u) {
86  return u;
87  }
88  };
89 
90  }
91 }
92 
93 #endif
static void promote(const T &u, T &t)
Definition: promoter.hpp:24
static void promote(const std::vector< T > &u, std::vector< T > &t)
Definition: promoter.hpp:51
static void promote(const Eigen::Matrix< F, R, C > &u, Eigen::Matrix< T, R, C > &t)
Definition: promoter.hpp:63
(Expert) Numerical traits for algorithmic differentiation variables.
static void promote(const std::vector< F > &u, std::vector< T > &t)
Definition: promoter.hpp:35
static T promote_to(const T &u)
Definition: promoter.hpp:27
static void promote(const Eigen::Matrix< T, R, C > &u, Eigen::Matrix< T, R, C > &t)
Definition: promoter.hpp:80
static Eigen::Matrix< T, R, C > promote_to(const Eigen::Matrix< T, R, C > &u)
Definition: promoter.hpp:85
static void promote(const F &u, T &t)
Definition: promoter.hpp:14
static T promote_to(const F &u)
Definition: promoter.hpp:17
static std::vector< T > promote_to(const std::vector< F > &u)
Definition: promoter.hpp:42
static std::vector< T > promote_to(const std::vector< T > &u)
Definition: promoter.hpp:55
static Eigen::Matrix< T, R, C > promote_to(const Eigen::Matrix< F, R, C > &u)
Definition: promoter.hpp:70

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