Stan Math Library  2.20.0
reverse mode automatic differentiation
apply_scalar_unary.hpp
Go to the documentation of this file.
1 #ifndef STAN_MATH_PRIM_MAT_VECTORIZE_APPLY_SCALAR_UNARY_HPP
2 #define STAN_MATH_PRIM_MAT_VECTORIZE_APPLY_SCALAR_UNARY_HPP
3 
5 #include <vector>
6 
7 namespace stan {
8 namespace math {
9 
34 template <typename F, typename T>
39  typedef typename Eigen::internal::traits<T>::Scalar scalar_t;
40 
45  typedef Eigen::Matrix<scalar_t, T::RowsAtCompileTime, T::ColsAtCompileTime>
47 
56  static inline return_t apply(const T& x) {
57  return x.unaryExpr(
58  [](scalar_t x) { return apply_scalar_unary<F, scalar_t>::apply(x); });
59  }
60 };
61 
68 template <typename F>
69 struct apply_scalar_unary<F, double> {
73  typedef double return_t;
74 
84  static inline return_t apply(double x) { return F::fun(x); }
85 };
86 
95 template <typename F>
96 struct apply_scalar_unary<F, int> {
100  typedef double return_t;
101 
111  static inline return_t apply(int x) { return F::fun(static_cast<double>(x)); }
112 };
113 
123 template <typename F, typename T>
124 struct apply_scalar_unary<F, std::vector<T> > {
129  typedef typename std::vector<typename apply_scalar_unary<F, T>::return_t>
131 
141  static inline return_t apply(const std::vector<T>& x) {
142  return_t fx(x.size());
143  for (size_t i = 0; i < x.size(); ++i)
144  fx[i] = apply_scalar_unary<F, T>::apply(x[i]);
145  return fx;
146  }
147 };
148 
149 } // namespace math
150 } // namespace stan
151 #endif
static return_t apply(double x)
Apply the function specified by F to the specified argument.
Eigen::Matrix< scalar_t, T::RowsAtCompileTime, T::ColsAtCompileTime > return_t
Return type for applying the function elementwise to a matrix expression template of type T...
static return_t apply(const std::vector< T > &x)
Apply the function specified by F elementwise to the specified argument.
static return_t apply(int x)
Apply the function specified by F to the specified argument.
double return_t
The return type, double.
std::vector< typename apply_scalar_unary< F, T >::return_t > return_t
Return type, which is calculated recursively as a standard vector of the return type of the contained...
Eigen::internal::traits< T >::Scalar scalar_t
Type of underlying scalar for the matrix type T.
Base template class for vectorization of unary scalar functions defined by a template class F to a sc...
double return_t
The return type, double.
static return_t apply(const T &x)
Return the result of applying the function defined by the template parameter F to the specified matri...

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