Automatic Differentiation
 
Loading...
Searching...
No Matches
to_fvar.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_FWD_FUN_TO_FVAR_HPP
2#define STAN_MATH_FWD_FUN_TO_FVAR_HPP
3
8#include <vector>
9
10namespace stan {
11namespace math {
12
13template <typename T, require_stan_scalar_t<T>* = nullptr,
14 require_not_fvar_t<T>* = nullptr>
15inline fvar<T> to_fvar(const T& x) {
16 return fvar<T>(x);
17}
18
25template <typename T, require_fvar_t<scalar_type_t<T>>* = nullptr>
26inline T&& to_fvar(T&& x) {
27 return std::forward<T>(x);
28}
29
30template <typename T>
31inline std::vector<fvar<T>> to_fvar(const std::vector<T>& v) {
32 std::vector<fvar<T>> x(v.size());
33 for (size_t i = 0; i < v.size(); ++i) {
34 x[i] = T(v[i]);
35 }
36 return x;
37}
38
39template <typename T>
40inline std::vector<fvar<T>> to_fvar(const std::vector<T>& v,
41 const std::vector<T>& d) {
42 std::vector<fvar<T>> x(v.size());
43 for (size_t i = 0; i < v.size(); ++i) {
44 x[i] = fvar<T>(v[i], d[i]);
45 }
46 return x;
47}
48
49template <typename T, require_eigen_t<T>* = nullptr,
50 require_not_eigen_vt<is_fvar, T>* = nullptr>
52 promote_scalar_t<fvar<value_type_t<T>>, T> m_fd(m.rows(), m.cols());
53 m_fd.val() = m;
54 m_fd.d() = plain_type_t<T>::Constant(m.rows(), m.cols(), 0);
55 return m_fd;
56}
57
58template <typename T1, typename T2, require_all_eigen_t<T1, T2>* = nullptr,
59 require_vt_same<T1, T2>* = nullptr>
61 const T2& deriv) {
62 check_matching_dims("to_fvar", "value", val, "deriv", deriv);
63 promote_scalar_t<fvar<value_type_t<T1>>, T1> ret(val.rows(), val.cols());
64 ret.val() = val;
65 ret.d() = deriv;
66 return ret;
67}
68
69} // namespace math
70} // namespace stan
71#endif
typename promote_scalar_type< std::decay_t< T >, std::decay_t< S > >::type promote_scalar_t
void check_matching_dims(const char *function, const char *name1, const T1 &y1, const char *name2, const T2 &y2)
Check if the two containers have the same dimensions.
fvar< T > to_fvar(const T &x)
Definition to_fvar.hpp:15
typename plain_type< T >::type plain_type_t
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
This template class represents scalars used in forward-mode automatic differentiation,...
Definition fvar.hpp:40