Automatic Differentiation
 
Loading...
Searching...
No Matches
as_array_or_scalar.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_AS_ARRAY_OR_SCALAR_HPP
2#define STAN_MATH_PRIM_FUN_AS_ARRAY_OR_SCALAR_HPP
3
6#include <vector>
7
8namespace stan {
9namespace math {
10
18template <typename T, require_stan_scalar_t<T>* = nullptr>
19inline T as_array_or_scalar(T&& v) {
20 return std::forward<T>(v);
21}
22
30template <typename T, require_stan_scalar_t<T>* = nullptr>
31inline T& as_array_or_scalar(T& v) {
32 return v;
33}
34
42template <typename T, require_eigen_array_t<T>* = nullptr>
43inline T as_array_or_scalar(T&& v) {
44 return std::forward<T>(v);
45}
46
54template <typename T, typename = require_eigen_t<T>,
55 require_not_eigen_array_t<T>* = nullptr>
56inline auto as_array_or_scalar(T&& v) {
57 return make_holder([](auto& x) { return x.array(); }, std::forward<T>(v));
58}
59
67template <typename T, require_std_vector_t<T>* = nullptr,
68 require_not_std_vector_t<value_type_t<T>>* = nullptr>
69inline auto as_array_or_scalar(T&& v) {
70 using T_map
71 = Eigen::Map<const Eigen::Array<value_type_t<T>, Eigen::Dynamic, 1>>;
72 return make_holder([](auto& x) { return T_map(x.data(), x.size()); },
73 std::forward<T>(v));
74}
75
83template <typename T, require_std_vector_vt<is_std_vector, T>* = nullptr,
84 require_std_vector_vt<is_stan_scalar, value_type_t<T>>* = nullptr>
85inline auto as_array_or_scalar(T&& v) {
86 Eigen::Array<scalar_type_t<T>, -1, -1> ret(v.size(), v[0].size());
87 for (size_t i = 0; i < v.size(); ++i) {
88 ret.row(i) = Eigen::Map<const Eigen::Array<scalar_type_t<T>, 1, -1>>(
89 v[i].data(), v[i].size());
90 }
91 return ret;
92}
93
94} // namespace math
95} // namespace stan
96
97#endif
T as_array_or_scalar(T &&v)
Returns specified input value.
auto make_holder(const F &func, Args &&... args)
Constructs an expression from given arguments using given functor.
Definition holder.hpp:352
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...