Skip to content

Commit 85c9e5e

Browse files
committed
Move serializer definition into Math proper
1 parent fd77364 commit 85c9e5e

7 files changed

Lines changed: 24 additions & 22 deletions

File tree

stan/math/fwd/functor/fvar_finite_diff.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <stan/math/prim/functor/finite_diff_gradient_auto.hpp>
77
#include <stan/math/prim/fun/value_of.hpp>
88
#include <stan/math/prim/fun/sum.hpp>
9-
#include <test/unit/math/serializer.hpp>
9+
#include <stan/math/prim/fun/serializer.hpp>
1010

1111
namespace stan {
1212
namespace math {
@@ -66,13 +66,13 @@ auto fvar_finite_diff(const F& func, const TArgs&... args) {
6666
using FvarT = return_type_t<TArgs...>;
6767
using FvarInnerT = typename FvarT::Scalar;
6868

69-
auto serialised_args = stan::test::serialize<FvarInnerT>(value_of(args)...);
69+
auto serialised_args = serialize<FvarInnerT>(value_of(args)...);
7070

7171
// Create a 'wrapper' functor which will take the flattened column-vector
7272
// and transform it to individual arguments which are passed to the
7373
// user-provided functor
7474
auto serial_functor = [&](const auto& v) {
75-
return func(stan::test::to_deserializer(v).read(args)...);
75+
return func(to_deserializer(v).read(args)...);
7676
};
7777

7878
FvarInnerT rtn_value;
@@ -84,7 +84,7 @@ auto fvar_finite_diff(const F& func, const TArgs&... args) {
8484
// Use a fold-expression to aggregate tangents for input arguments
8585
(void)std::initializer_list<int>{
8686
(rtn_grad += internal::aggregate_tangent(
87-
stan::test::to_deserializer(grad).read(args), args),
87+
to_deserializer(grad).read(args), args),
8888
0)...};
8989

9090
return FvarT(rtn_value, rtn_grad);

stan/math/fwd/functor/integrate_1d.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <stan/math/prim/functor/integrate_1d.hpp>
66
#include <stan/math/prim/fun/value_of.hpp>
77
#include <stan/math/prim/meta/forward_as.hpp>
8+
#include <stan/math/prim/functor/apply.hpp>
89
#include <stan/math/fwd/functor/fvar_finite_diff.hpp>
910

1011
namespace stan {

stan/math/prim/fun.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,7 @@
305305
#include <stan/math/prim/fun/scaled_add.hpp>
306306
#include <stan/math/prim/fun/sd.hpp>
307307
#include <stan/math/prim/fun/segment.hpp>
308+
#include <stan/math/prim/fun/serializer.hpp>
308309
#include <stan/math/prim/fun/sign.hpp>
309310
#include <stan/math/prim/fun/signbit.hpp>
310311
#include <stan/math/prim/fun/simplex_constrain.hpp>
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
#ifndef TEST_UNIT_MATH_SERIALIZER_HPP
2-
#define TEST_UNIT_MATH_SERIALIZER_HPP
1+
#ifndef STAN_MATH_PRIM_FUN_SERIALIZER_HPP
2+
#define STAN_MATH_PRIM_FUN_SERIALIZER_HPP
33

4-
#include <stan/math.hpp>
4+
#include <stan/math/prim/fun/to_vector.hpp>
55
#include <complex>
66
#include <string>
77
#include <vector>
88

99
namespace stan {
10-
namespace test {
10+
namespace math {
1111

1212
/**
1313
* A class to store a sequence of values which can be deserialized
@@ -47,7 +47,7 @@ struct deserializer {
4747
* @param vals values to deserialize
4848
*/
4949
explicit deserializer(const Eigen::Matrix<T, -1, 1>& v_vals)
50-
: position_(0), vals_(math::to_array_1d(v_vals)) {}
50+
: position_(0), vals_(to_array_1d(v_vals)) {}
5151

5252
/**
5353
* Read a scalar conforming to the shape of the specified argument,
@@ -94,8 +94,8 @@ struct deserializer {
9494
*/
9595
template <typename U, require_std_vector_t<U>* = nullptr,
9696
require_not_st_complex<U>* = nullptr>
97-
typename stan::math::promote_scalar_type<T, U>::type read(const U& x) {
98-
typename stan::math::promote_scalar_type<T, U>::type y;
97+
typename promote_scalar_type<T, U>::type read(const U& x) {
98+
typename promote_scalar_type<T, U>::type y;
9999
y.reserve(x.size());
100100
for (size_t i = 0; i < x.size(); ++i)
101101
y.push_back(read(x[i]));
@@ -113,9 +113,9 @@ struct deserializer {
113113
* @return deserialized value with shape and size matching argument
114114
*/
115115
template <typename U, require_std_vector_st<is_complex, U>* = nullptr>
116-
typename stan::math::promote_scalar_type<std::complex<T>, U>::type read(
116+
typename promote_scalar_type<std::complex<T>, U>::type read(
117117
const U& x) {
118-
typename stan::math::promote_scalar_type<std::complex<T>, U>::type y;
118+
typename promote_scalar_type<std::complex<T>, U>::type y;
119119
y.reserve(x.size());
120120
for (size_t i = 0; i < x.size(); ++i)
121121
y.push_back(read(x[i]));
@@ -258,7 +258,7 @@ struct serializer {
258258
* @return serialized values
259259
*/
260260
const Eigen::Matrix<T, -1, 1>& vector_vals() {
261-
return math::to_vector(vals_);
261+
return to_vector(vals_);
262262
}
263263
};
264264

@@ -338,7 +338,7 @@ std::vector<real_return_t<T>> serialize_return(const T& x) {
338338
*/
339339
template <typename... Ts>
340340
Eigen::VectorXd serialize_args(const Ts... xs) {
341-
return math::to_vector(serialize<double>(xs...));
341+
return to_vector(serialize<double>(xs...));
342342
}
343343

344344
} // namespace test

test/unit/math/serializer_test.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
#include <test/unit/math/serializer.hpp>
1+
#include <stan/math/prim/fun/serializer.hpp>
22
#include <gtest/gtest.h>
33
#include <complex>
44
#include <vector>
55

66
TEST(testUnitMathSerializer, serializer_deserializer) {
7-
stan::test::serializer<double> s;
7+
stan::math::serializer<double> s;
88

99
s.write(3.2);
1010
s.write(-1);
@@ -30,7 +30,7 @@ TEST(testUnitMathSerializer, serializer_deserializer) {
3030
for (size_t i = 0; i < expected.size(); ++i)
3131
EXPECT_EQ(expected[i], s.vals_[i]);
3232

33-
stan::test::deserializer<double> d = stan::test::to_deserializer(s.vals_);
33+
stan::math::deserializer<double> d = stan::math::to_deserializer(s.vals_);
3434

3535
EXPECT_EQ(3.2, d.read(0.0));
3636
EXPECT_EQ(-1, d.read(0.0));
@@ -58,15 +58,15 @@ TEST(testUnitMathSerializer, serializer_deserializer) {
5858
}
5959

6060
TEST(testUnitMathSerializer, serialize) {
61-
std::vector<double> xs = stan::test::serialize<double>();
61+
std::vector<double> xs = stan::math::serialize<double>();
6262
EXPECT_EQ(0, xs.size());
6363

6464
double a = 2;
6565
std::complex<double> b(1, 2);
6666
std::vector<double> c{3, 4, 5};
6767
Eigen::MatrixXd d(2, 3);
6868
d << -1, -2, -3, -4, -5, -6;
69-
std::vector<double> ys = stan::test::serialize<double>(a, b, c, d);
69+
std::vector<double> ys = stan::math::serialize<double>(a, b, c, d);
7070

7171
std::vector<double> expected{2, 1, 2, 3, 4, 5, -1, -4, -2, -5, -3, -6};
7272
for (size_t i = 0; i < expected.size(); ++i)

test/unit/math/test_ad.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <test/unit/math/ad_tolerances.hpp>
77
#include <test/unit/math/is_finite.hpp>
88
#include <test/unit/math/expect_near_rel.hpp>
9-
#include <test/unit/math/serializer.hpp>
9+
#include <stan/math/prim/fun/serializer.hpp>
1010
#include <test/unit/math/test_ad_matvar.hpp>
1111
#include <test/unit/util.hpp>
1212
#include <gtest/gtest.h>

test/unit/math/test_ad_matvar.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <test/unit/math/ad_tolerances.hpp>
77
#include <test/unit/math/is_finite.hpp>
88
#include <test/unit/math/expect_near_rel.hpp>
9-
#include <test/unit/math/serializer.hpp>
9+
#include <stan/math/prim/fun/serializer.hpp>
1010
#include <test/unit/util.hpp>
1111
#include <gtest/gtest.h>
1212
#include <algorithm>

0 commit comments

Comments
 (0)