Stan Math Library  2.12.0
reverse mode automatic differentiation
to_fvar.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_FWD_MAT_FUN_TO_FVAR_HPP
2 #define STAN_MATH_FWD_MAT_FUN_TO_FVAR_HPP
3 
5 #include <stan/math/fwd/core.hpp>
8 
9 namespace stan {
10  namespace math {
11 
12  template<int R, int C, typename T>
13  inline
14  Eigen::Matrix<T, R, C>
15  to_fvar(const Eigen::Matrix<T, R, C>& m) {
16  return m;
17  }
18 
19  template<int R, int C>
20  inline
21  Eigen::Matrix<fvar<double>, R, C>
22  to_fvar(const Eigen::Matrix<double, R, C>& m) {
23  Eigen::Matrix<fvar<double>, R, C> m_fd(m.rows(), m.cols());
24  for (int i = 0; i < m.size(); ++i)
25  m_fd(i) = m(i);
26  return m_fd;
27  }
28 
29  template<typename T, int R, int C>
30  inline
31  Eigen::Matrix<fvar<T>, R, C>
32  to_fvar(const Eigen::Matrix<T, R, C>& val,
33  const Eigen::Matrix<T, R, C>& deriv) {
34  check_matching_dims("to_fvar",
35  "value", val,
36  "deriv", deriv);
37  Eigen::Matrix<fvar<T>, R, C> ret(val.rows(), val.cols());
38  for (int i = 0; i < val.rows(); i++) {
39  for (int j = 0; j < val.cols(); j++) {
40  ret(i, j).val_ = val(i, j);
41  ret(i, j).d_ = deriv(i, j);
42  }
43  }
44  return ret;
45  }
46 
47  }
48 }
49 #endif
std::vector< fvar< T > > to_fvar(const std::vector< T > &v)
Definition: to_fvar.hpp:14
bool check_matching_dims(const char *function, const char *name1, const Eigen::Matrix< T1, R1, C1 > &y1, const char *name2, const Eigen::Matrix< T2, R2, C2 > &y2)
Return true if the two matrices are of the same size.

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