Automatic Differentiation
 
Loading...
Searching...
No Matches
scalar_seq_view.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_SCALAR_SEQ_VIEW_HPP
2#define STAN_MATH_PRIM_FUN_SCALAR_SEQ_VIEW_HPP
3
6#include <type_traits>
7#include <utility>
8
9namespace stan {
17template <typename C, typename = void>
19
20template <typename C>
22 public:
23 template <typename T,
25 explicit scalar_seq_view(T&& c) : c_(std::forward<T>(c)) {}
26
32 inline auto operator[](size_t i) const { return c_.coeff(i); }
33
34 inline auto size() const noexcept { return c_.size(); }
35
36 inline const value_type_t<C>* data() const noexcept { return c_.data(); }
37 inline value_type_t<C>* data() noexcept { return c_.data(); }
38
39 template <typename T = C, require_st_arithmetic<T>* = nullptr>
40 inline decltype(auto) val(size_t i) const {
41 return c_.coeffRef(i);
42 }
43
44 template <typename T = C, require_st_autodiff<T>* = nullptr>
45 inline decltype(auto) val(size_t i) const {
46 return c_.coeffRef(i).val();
47 }
48
49 private:
51};
52
53template <typename C>
55 public:
56 template <typename T,
58 explicit scalar_seq_view(T&& c) : c_(std::forward<T>(c)) {}
59
65 inline auto operator[](size_t i) const { return c_.coeff(i); }
66 inline const auto* data() const noexcept { return c_.vi_; }
67 inline auto* data() noexcept { return c_.vi_; }
68
69 inline auto size() const noexcept { return c_.size(); }
70
71 template <typename T = C, require_st_autodiff<T>* = nullptr>
72 inline auto val(size_t i) const {
73 return c_.val().coeff(i);
74 }
75
76 template <typename T = C, require_st_autodiff<T>* = nullptr>
77 inline auto& val(size_t i) {
78 return c_.val().coeffRef(i);
79 }
80
81 private:
82 std::decay_t<C> c_;
83};
84
85template <typename C>
87 public:
88 template <typename T,
90 explicit scalar_seq_view(T&& c) : c_(std::forward<T>(c)) {}
91
97 inline auto operator[](size_t i) const { return c_[i]; }
98 inline auto size() const noexcept { return c_.size(); }
99 inline const auto* data() const noexcept { return c_.data(); }
100
101 template <typename T = C, require_st_arithmetic<T>* = nullptr>
102 inline decltype(auto) val(size_t i) const {
103 return c_[i];
104 }
105
106 template <typename T = C, require_st_autodiff<T>* = nullptr>
107 inline decltype(auto) val(size_t i) const {
108 return c_[i].val();
109 }
110
111 private:
112 const C& c_;
113};
114
115template <typename C>
116class scalar_seq_view<C, require_t<std::is_pointer<C>>> {
117 public:
118 template <typename T,
120 explicit scalar_seq_view(const T& c) : c_(c) {}
121
127 inline auto operator[](size_t i) const { return c_[i]; }
128 inline auto size() const noexcept {
129 static_assert(1, "Cannot Return Size of scalar_seq_view with pointer type");
130 }
131 inline const auto* data() const noexcept { return &c_[0]; }
132
133 template <typename T = C, require_st_arithmetic<T>* = nullptr>
134 inline decltype(auto) val(size_t i) const {
135 return c_[i];
136 }
137
138 template <typename T = C, require_st_autodiff<T>* = nullptr>
139 inline decltype(auto) val(size_t i) const {
140 return c_[i].val();
141 }
142
143 private:
144 const C& c_;
145};
146
152template <typename C>
154 public:
155 explicit scalar_seq_view(const C& t) noexcept : t_(t) {}
156
157 inline decltype(auto) operator[](int /* i */) const noexcept { return t_; }
158
159 template <typename T = C, require_st_arithmetic<T>* = nullptr>
160 inline decltype(auto) val(int /* i */) const noexcept {
161 return t_;
162 }
163
164 template <typename T = C, require_st_autodiff<T>* = nullptr>
165 inline decltype(auto) val(int /* i */) const noexcept {
166 return t_.val();
167 }
168
169 static constexpr auto size() { return 1; }
170 inline const auto* data() const noexcept { return &t_; }
171 inline auto* data() noexcept { return &t_; }
172
173 private:
174 std::decay_t<C> t_;
175};
176} // namespace stan
177#endif
auto operator[](size_t i) const
Segfaults if out of bounds.
const value_type_t< C > * data() const noexcept
auto operator[](size_t i) const
Segfaults if out of bounds.
auto operator[](size_t i) const
Segfaults if out of bounds.
auto operator[](size_t i) const
Segfaults if out of bounds.
scalar_seq_view provides a uniform sequence-like wrapper around either a scalar or a sequence of scal...
require_t< is_eigen_vector< std::decay_t< T > > > require_eigen_vector_t
Require type satisfies is_eigen_vector.
require_t< std::is_same< std::decay_t< T >, std::decay_t< S > > > require_same_t
Require types T and S satisfies std::is_same.
require_t< is_stan_scalar< std::decay_t< T > > > require_stan_scalar_t
Require type satisfies is_stan_scalar.
require_t< is_std_vector< std::decay_t< T > > > require_std_vector_t
Require type satisfies is_std_vector.
typename value_type< T >::type value_type_t
Helper function for accessing underlying type.
require_t< is_var_matrix< std::decay_t< T > > > require_var_matrix_t
Require type satisfies is_var_matrix.
typename plain_type< T >::type plain_type_t
typename ref_type_if< true, T >::type ref_type_t
Definition ref_type.hpp:55
std::enable_if_t< Check::value > require_t
If condition is true, template is enabled.
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
STL namespace.