Automatic Differentiation
 
Loading...
Searching...
No Matches
serializer.hpp
Go to the documentation of this file.
1#ifndef STAN_MATH_PRIM_FUN_SERIALIZER_HPP
2#define STAN_MATH_PRIM_FUN_SERIALIZER_HPP
3
7#include <complex>
8#include <string>
9#include <vector>
10
11namespace stan {
12namespace math {
13
21template <typename T>
26 typedef T scalar_t;
27
31 size_t position_;
32
36 const std::vector<T> vals_;
37
43 explicit deserializer(const std::vector<T>& vals)
44 : position_(0), vals_(vals) {}
45
51 explicit deserializer(const Eigen::Matrix<T, -1, 1>& v_vals)
52 : position_(0), vals_(to_array_1d(v_vals)) {}
53
64 template <typename U, require_stan_scalar_t<U>* = nullptr,
65 require_not_complex_t<U>* = nullptr>
66 T read(const U& x) {
67 return vals_[position_++];
68 }
69
80 template <typename U>
81 std::complex<T> read(const std::complex<U>& x) {
82 T re = read(x.real());
83 T im = read(x.imag());
84 return {re, im};
85 }
86
97 template <typename U, require_std_vector_t<U>* = nullptr,
98 require_not_st_complex<U>* = nullptr>
101 y.reserve(x.size());
102 for (size_t i = 0; i < x.size(); ++i)
103 y.push_back(read(x[i]));
104 return y;
105 }
106
117 template <typename U, require_std_vector_st<is_complex, U>* = nullptr>
120 y.reserve(x.size());
121 for (size_t i = 0; i < x.size(); ++i)
122 y.push_back(read(x[i]));
123 return y;
124 }
125
138 template <typename U, int R, int C>
139 Eigen::Matrix<T, R, C> read(const Eigen::Matrix<U, R, C>& x) {
140 Eigen::Matrix<T, R, C> y(x.rows(), x.cols());
141 for (int j = 0; j < x.cols(); ++j) {
142 for (int i = 0; i < x.rows(); ++i) {
143 y(i + j * x.rows()) = read(x(i, j));
144 }
145 }
146 return y;
147 }
148
161 template <typename U, int R, int C>
162 Eigen::Matrix<std::complex<T>, R, C> read(
163 const Eigen::Matrix<std::complex<U>, R, C>& x) {
164 Eigen::Matrix<std::complex<T>, R, C> y(x.rows(), x.cols());
165 for (int j = 0; j < x.cols(); ++j) {
166 for (int i = 0; i < x.rows(); ++i) {
167 y(i + j * x.rows()) = read(x(i, j));
168 }
169 }
170 return y;
171 }
172};
173
180template <typename T>
185 typedef T scalar_t;
186
190 std::vector<T> vals_;
191
196
203 template <typename U, require_stan_scalar_t<U>* = nullptr>
204 void write(const U& x) {
205 vals_.push_back(x);
206 }
207
214 template <typename U>
215 void write(const std::complex<U>& x) {
216 write(x.real());
217 write(x.imag());
218 }
219
226 template <typename U>
227 void write(const std::vector<U>& x) {
228 for (size_t i = 0; i < x.size(); ++i)
229 write(x[i]);
230 }
231
240 template <typename U, require_eigen_t<U>* = nullptr>
241 void write(const U& x) {
242 for (int j = 0; j < x.cols(); ++j) {
243 for (int i = 0; i < x.rows(); ++i) {
244 write(x.coeff(i, j));
245 }
246 }
247 }
248
254 const std::vector<T>& array_vals() { return vals_; }
255
261 const Eigen::Matrix<T, -1, 1>& vector_vals() { return to_vector(vals_); }
262};
263
271template <typename T>
272deserializer<T> to_deserializer(const std::vector<T>& vals) {
273 return deserializer<T>(vals);
274}
275
283template <typename T>
284deserializer<T> to_deserializer(const Eigen::Matrix<T, -1, 1>& vals) {
285 return deserializer<T>(vals);
286}
287
288template <typename T>
289deserializer<T> to_deserializer(const std::complex<T>& vals) {
290 return to_deserializer(std::vector<T>{vals.real(), vals.imag()});
291}
292
293template <typename U>
295
296template <typename U, typename T, typename... Ts>
297void serialize_helper(serializer<U>& s, const T& x, const Ts... xs) {
298 s.write(x);
299 serialize_helper(s, xs...);
300}
301
311template <typename U, typename... Ts>
312std::vector<U> serialize(const Ts... xs) {
314 serialize_helper(s, xs...);
315 return s.vals_;
316}
317
325template <typename T>
326std::vector<real_return_t<T>> serialize_return(const T& x) {
327 return serialize<real_return_t<T>>(x);
328}
329
338template <typename... Ts>
339Eigen::VectorXd serialize_args(const Ts... xs) {
340 return to_vector(serialize<double>(xs...));
341}
342
343} // namespace math
344} // namespace stan
345
346#endif
auto to_vector(T_x &&x)
Returns input matrix reshaped into a vector.
Definition to_vector.hpp:21
auto to_array_1d(T_x &&x)
Returns input matrix reshaped into a vector.
typename promote_scalar_type< std::decay_t< T >, std::decay_t< S > >::type promote_scalar_t
deserializer< T > to_deserializer(const std::vector< T > &vals)
Return a deserializer based on the specified values.
Eigen::VectorXd serialize_args(const Ts... xs)
Serialize the specified sequence of structured objects with double-based scalars into an Eigen vector...
std::vector< U > serialize(const Ts... xs)
Serialize the specified sequence of objects, which all must have scalar types assignable to the resul...
std::vector< real_return_t< T > > serialize_return(const T &x)
Serialized the specified single argument.
void serialize_helper(serializer< U > &s)
The lgamma implementation in stan-math is based on either the reentrant safe lgamma_r implementation ...
Definition fvar.hpp:9
deserializer(const std::vector< T > &vals)
Construct a deserializer from the specified sequence of values.
std::complex< T > read(const std::complex< U > &x)
Read a complex number conforming to the shape of the specified argument.
T scalar_t
Type of scalars in all objects.
Eigen::Matrix< std::complex< T >, R, C > read(const Eigen::Matrix< std::complex< U >, R, C > &x)
Read a standard vector of std::complex variables conforming to the shape of the specified argument,...
Eigen::Matrix< T, R, C > read(const Eigen::Matrix< U, R, C > &x)
Read a standard vector conforming to the shape of the specified argument, here an Eigen matrix,...
T read(const U &x)
Read a scalar conforming to the shape of the specified argument, here a scalar.
size_t position_
Current read position.
promote_scalar_t< std::complex< T >, U > read(const U &x)
Read a standard vector of std::complex variables conforming to the shape of the specified argument,...
deserializer(const Eigen::Matrix< T, -1, 1 > &v_vals)
Construct a deserializer from the specified sequence of values.
const std::vector< T > vals_
The sequence of values to deserialize.
promote_scalar_t< T, U > read(const U &x)
Read a standard vector conforming to the shape of the specified argument, here a standard vector.
A class to store a sequence of values which can be deserialized back into structured objects such as ...
const Eigen::Matrix< T, -1, 1 > & vector_vals()
Return the serialized values as an Eigen vector.
void write(const U &x)
Serialize the specified scalar.
void write(const std::vector< U > &x)
Serialize the specified standard vector.
std::vector< T > vals_
Container for serialized values.
void write(const std::complex< U > &x)
Serialize the specified complex number.
serializer()
Construct a serializer.
const std::vector< T > & array_vals()
Return the serialized values as a standard vector.
T scalar_t
Scalar type of serializer.
A structure to serialize structures to an internal stored sequence of scalars.