Stan Math Library  2.20.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 Eigen::Matrix<T, R, C> to_fvar(const Eigen::Matrix<T, R, C>& m) {
14  return m;
15 }
16 
17 template <int R, int C>
18 inline Eigen::Matrix<fvar<double>, R, C> to_fvar(
19  const Eigen::Matrix<double, R, C>& m) {
20  Eigen::Matrix<fvar<double>, R, C> m_fd(m.rows(), m.cols());
21  for (int i = 0; i < m.size(); ++i)
22  m_fd(i) = m(i);
23  return m_fd;
24 }
25 
26 template <typename T, int R, int C>
27 inline Eigen::Matrix<fvar<T>, R, C> to_fvar(
28  const Eigen::Matrix<T, R, C>& val, const Eigen::Matrix<T, R, C>& deriv) {
29  check_matching_dims("to_fvar", "value", val, "deriv", deriv);
30  Eigen::Matrix<fvar<T>, R, C> ret(val.rows(), val.cols());
31  for (int i = 0; i < val.rows(); i++) {
32  for (int j = 0; j < val.cols(); j++) {
33  ret(i, j).val_ = val(i, j);
34  ret(i, j).d_ = deriv(i, j);
35  }
36  }
37  return ret;
38 }
39 
40 } // namespace math
41 } // namespace stan
42 #endif
std::vector< fvar< T > > to_fvar(const std::vector< T > &v)
Definition: to_fvar.hpp:12
void check_matching_dims(const char *function, const char *name1, const matrix_cl &y1, const char *name2, const matrix_cl &y2)
Check if two matrix_cls have the same dimensions.

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