forked from xtensor-stack/xtensor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_zarray.cpp
More file actions
128 lines (98 loc) · 4.28 KB
/
Copy pathtest_zarray.cpp
File metadata and controls
128 lines (98 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/***************************************************************************
* Copyright (c) Johan Mabille, Sylvain Corlay and Wolf Vollprecht *
* Copyright (c) QuantStack *
* *
* Distributed under the terms of the BSD 3-Clause License. *
* *
* The full license is in the file LICENSE, distributed with this software. *
****************************************************************************/
#include "gtest/gtest.h"
#include "xtensor/zarray.hpp"
#include "xtensor/zfunction.hpp"
#ifndef XTENSOR_DISABLE_EXCEPTIONS
namespace xt
{
using namespace xt;
TEST(zarray, value_semantics)
{
xarray<double> a = {{1., 2.}, {3., 4.}};
xarray<double> ra = {{2., 2.}, {3., 4.}};
zarray da(a);
da.get_array<double>()(0, 0) = 2.;
EXPECT_EQ(a, ra);
}
// TODO : move to dedicated test file
TEST(zarray, dispatching)
{
using dispatcher_type = zdispatcher_t<math::exp_fun, 1>;
dispatcher_type::init();
xarray<double> a = {{0.5, 1.5}, {2.5, 3.5}};
xarray<double> expa = {{std::exp(0.5), std::exp(1.5)}, {std::exp(2.5), std::exp(3.5)}};
xarray<double> res;
zarray za(a);
zarray zres(res);
dispatcher_type::dispatch(za.get_implementation(), zres.get_implementation());
EXPECT_EQ(expa, res);
}
// TODO: move to dedicated test file
TEST(zarray, zfunction)
{
using exp_dispatcher_type = zdispatcher_t<math::exp_fun, 1>;
exp_dispatcher_type::init();
using add_dispatcher_type = zdispatcher_t<detail::plus, 2>;
add_dispatcher_type::init();
using nested_zfunction_type = zfunction<math::exp_fun, const zarray&>;
using zfunction_type = zfunction<detail::plus, const zarray&, nested_zfunction_type>;
xarray<double> a = {{0.5, 1.5}, {2.5, 3.5}};
xarray<double> b = {{-0.2, 2.4}, {1.3, 4.7}};
xarray<double> res;
zarray za(a);
zarray zb(b);
zarray zres(res);
zfunction_type f(zplus(), za, nested_zfunction_type(zexp(), zb));
f.assign_to(zres.get_implementation());
auto expected = xarray<double>::from_shape({2, 2});
std::transform(a.cbegin(), a.cend(), b.cbegin(), expected.begin(),
[](const double& lhs, const double& rhs) { return lhs + std::exp(rhs); });
EXPECT_TRUE(all(isclose(res, expected)));
size_t res_index = f.get_result_type_index();
EXPECT_EQ(res_index, ztyped_array<double>::get_class_static_index());
}
TEST(zarray, operations)
{
using exp_dispatcher_type = zdispatcher_t<math::exp_fun, 1>;
exp_dispatcher_type::init();
using add_dispatcher_type = zdispatcher_t<detail::plus, 2>;
add_dispatcher_type::init();
xarray<double> a = {{0.5, 1.5}, {2.5, 3.5}};
xarray<double> b = {{-0.2, 2.4}, {1.3, 4.7}};
xarray<double> res;
zarray za(a);
zarray zb(b);
zarray zres(res);
auto f = za + xt::exp(zb);
f.assign_to(zres.get_implementation());
auto expected = xarray<double>::from_shape({2, 2});
std::transform(a.cbegin(), a.cend(), b.cbegin(), expected.begin(),
[](const double& lhs, const double& rhs) { return lhs + std::exp(rhs); });
EXPECT_TRUE(all(isclose(res, expected)));
}
TEST(zarray, assign)
{
using exp_dispatcher_type = zdispatcher_t<math::exp_fun, 1>;
exp_dispatcher_type::init();
using add_dispatcher_type = zdispatcher_t<detail::plus, 2>;
add_dispatcher_type::init();
xarray<double> a = {{0.5, 1.5}, {2.5, 3.5}};
xarray<double> b = {{-0.2, 2.4}, {1.3, 4.7}};
zarray za(a);
zarray zb(b);
zarray zres = za + xt::exp(zb);
auto expected = xarray<double>::from_shape({2, 2});
std::transform(a.cbegin(), a.cend(), b.cbegin(), expected.begin(),
[](const double& lhs, const double& rhs) { return lhs + std::exp(rhs); });
const auto& res = zres.get_array<double>();
EXPECT_TRUE(all(isclose(res, expected)));
}
}
#endif