forked from xtensor-stack/xtensor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_xnorm.cpp
188 lines (160 loc) · 7.71 KB
/
test_xnorm.cpp
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/***************************************************************************
* 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/xnorm.hpp"
#include "xtensor/xarray.hpp"
#include "xtensor/xbuilder.hpp"
#include "xtensor/xview.hpp"
#include "xtensor/xnoalias.hpp"
#include <limits>
namespace xt
{
TEST(xnorm, norm_traits)
{
EXPECT_TRUE((std::is_same<norm_type_t<uint8_t>, int>::value));
EXPECT_TRUE((std::is_same<norm_type_t<int>, int>::value));
EXPECT_TRUE((std::is_same<norm_type_t<double>, double>::value));
EXPECT_TRUE((std::is_same<norm_type_t<std::vector<uint8_t>>, double>::value));
EXPECT_TRUE((std::is_same<norm_type_t<std::vector<int>>, double>::value));
EXPECT_TRUE((std::is_same<norm_type_t<std::vector<double>>, double>::value));
EXPECT_TRUE((std::is_same<norm_type_t<std::vector<long double>>, long double>::value));
EXPECT_TRUE((std::is_same<squared_norm_type_t<uint8_t>, int>::value));
EXPECT_TRUE((std::is_same<squared_norm_type_t<int>, int>::value));
EXPECT_TRUE((std::is_same<squared_norm_type_t<double>, double>::value));
EXPECT_TRUE((std::is_same<squared_norm_type_t<std::vector<uint8_t>>, uint64_t>::value));
EXPECT_TRUE((std::is_same<squared_norm_type_t<std::vector<int>>, uint64_t>::value));
EXPECT_TRUE((std::is_same<squared_norm_type_t<std::vector<double>>, double>::value));
EXPECT_TRUE((std::is_same<squared_norm_type_t<std::vector<long double>>, long double>::value));
}
TEST(xnorm, scalar)
{
EXPECT_EQ(norm_l0(2), 1u);
EXPECT_EQ(norm_l0(-2), 1u);
EXPECT_EQ(norm_l0(0), 0u);
EXPECT_EQ(norm_l0(2.0), 1u);
EXPECT_EQ(norm_l0(-2.0), 1u);
EXPECT_EQ(norm_l0(0.0), 0u);
EXPECT_EQ(norm_l1(2), 2);
EXPECT_EQ(norm_l1(-2), 2);
EXPECT_EQ(norm_l1(2.0), 2.0);
EXPECT_EQ(norm_l1(-2.0), 2.0);
EXPECT_EQ(norm_l2(2), 2);
EXPECT_EQ(norm_l2(-2), 2);
EXPECT_EQ(norm_l2(2.0), 2.0);
EXPECT_EQ(norm_l2(-2.0), 2.0);
EXPECT_EQ(norm_linf(2), 2);
EXPECT_EQ(norm_linf(-2), 2);
EXPECT_EQ(norm_linf(2.0), 2.0);
EXPECT_EQ(norm_linf(-2.0), 2.0);
EXPECT_EQ(norm_sq(2), 4);
EXPECT_EQ(norm_sq(-2), 4);
EXPECT_EQ(norm_sq(2.5), 6.25);
EXPECT_EQ(norm_sq(-2.5), 6.25);
EXPECT_EQ(norm_lp(0, 0), 0);
EXPECT_EQ(norm_lp(-2, 0), 1);
EXPECT_EQ(norm_lp(0, 1), 0);
EXPECT_EQ(norm_lp(-2, 1), 2);
}
TEST(xnorm, complex)
{
std::complex<double> c{3.0, -4.0};
EXPECT_EQ(norm_l0(c), 1u);
EXPECT_EQ(norm_lp(c, 0), 1.0);
EXPECT_EQ(norm_l1(c), 7.0);
EXPECT_EQ(norm_lp(c, 1), 7.0);
EXPECT_EQ(norm_sq(c), 25.0);
EXPECT_EQ(norm_lp_to_p(c, 2), 25.0);
EXPECT_EQ(norm_sq(c), std::norm(c));
EXPECT_EQ(norm_l2(c), 5.0);
EXPECT_EQ(norm_lp(c, 2), 5.0);
EXPECT_EQ(norm_l2(c), std::abs(c));
EXPECT_EQ(norm_linf(c), 4.0);
}
TEST(xnorm, scalar_array)
{
xarray<int> a = -ones<int>({9});
EXPECT_EQ(norm_l0(a)(), 9u);
EXPECT_EQ(norm_lp_to_p(a, 0.0)(), 9.0);
EXPECT_EQ(norm_l1(a)(), 9);
EXPECT_EQ(norm_lp(a, 1.0)(), 9.0);
EXPECT_EQ(norm_sq(a)(), 9);
EXPECT_EQ(norm_l2(a)(), 3.0);
EXPECT_EQ(norm_lp(a, 2.0)(), 3.0);
EXPECT_EQ(norm_linf(a)(), 1);
EXPECT_EQ(norm_l0(a, evaluation_strategy::immediate)(), 9u);
EXPECT_EQ(norm_lp_to_p(a, 0.0, evaluation_strategy::immediate)(), 9.0);
EXPECT_EQ(norm_l1(a, evaluation_strategy::immediate)(), 9);
EXPECT_EQ(norm_lp(a, 1.0, evaluation_strategy::immediate)(), 9.0);
EXPECT_EQ(norm_sq(a, evaluation_strategy::immediate)(), 9);
EXPECT_EQ(norm_l2(a, evaluation_strategy::immediate)(), 3.0);
EXPECT_EQ(norm_lp(a, 2.0, evaluation_strategy::immediate)(), 3.0);
EXPECT_EQ(norm_linf(a, evaluation_strategy::immediate)(), 1);
}
TEST(xnorm, complex_array)
{
xarray<std::complex<double>> a = -ones<std::complex<double>>({9});
auto l0_res = norm_l0(a)();
auto lp_to_p_res = norm_lp_to_p(a, 0.0)();
auto l1_res = norm_l1(a)();
auto lp_res1 = norm_lp(a, 1.0)();
auto sq_res = norm_sq(a)();
auto l2_res = norm_l2(a)();
auto lp_res2 = norm_lp(a, 2.0)();
auto linf_res = norm_linf(a)();
EXPECT_TRUE((std::is_same<decltype(l0_res), long long unsigned int>::value));
EXPECT_TRUE((std::is_same<decltype(lp_to_p_res), double>::value));
EXPECT_TRUE((std::is_same<decltype(l1_res), double>::value));
EXPECT_TRUE((std::is_same<decltype(lp_res1), double>::value));
EXPECT_TRUE((std::is_same<decltype(sq_res), double>::value));
EXPECT_TRUE((std::is_same<decltype(l2_res), double>::value));
EXPECT_TRUE((std::is_same<decltype(lp_res2), double>::value));
EXPECT_TRUE((std::is_same<decltype(linf_res), double>::value));
EXPECT_EQ(l0_res, 9u);
EXPECT_EQ(lp_to_p_res, 9.0);
EXPECT_EQ(l1_res, 9.0);
EXPECT_EQ(lp_res1, 9.0);
EXPECT_EQ(sq_res, 9.0);
EXPECT_EQ(l2_res, 3.0);
EXPECT_EQ(lp_res2, 3.0);
EXPECT_EQ(linf_res, 1.0);
EXPECT_EQ(norm_l0(a, {0}, evaluation_strategy::immediate)(), 9u);
EXPECT_EQ(norm_lp_to_p(a, 0.0, {0}, evaluation_strategy::immediate)(), 9.0);
EXPECT_EQ(norm_l1(a, {0}, evaluation_strategy::immediate)(), 9.0);
EXPECT_EQ(norm_lp(a, 1.0, {0}, evaluation_strategy::immediate)(), 9.0);
EXPECT_EQ(norm_sq(a, {0}, evaluation_strategy::immediate)(), 9.0);
EXPECT_EQ(norm_l2(a, {0}, evaluation_strategy::immediate)(), 3.0);
EXPECT_EQ(norm_lp(a, 2.0, {0}, evaluation_strategy::immediate)(), 3.0);
EXPECT_EQ(norm_linf(a, {0}, evaluation_strategy::immediate)(), 1.0);
EXPECT_EQ(norm_l0(a, {0}, evaluation_strategy::immediate)(), 9u);
EXPECT_EQ(norm_lp_to_p(a, 0.0, evaluation_strategy::immediate)(), 9.0);
EXPECT_EQ(norm_l1(a, evaluation_strategy::immediate)(), 9.0);
EXPECT_EQ(norm_lp(a, 1.0, evaluation_strategy::immediate)(), 9.0);
EXPECT_EQ(norm_sq(a, evaluation_strategy::immediate)(), 9.0);
EXPECT_EQ(norm_l2(a, evaluation_strategy::immediate)(), 3.0);
EXPECT_EQ(norm_lp(a, 2.0, evaluation_strategy::immediate)(), 3.0);
EXPECT_EQ(norm_linf(a, evaluation_strategy::immediate)(), 1.0);
}
TEST(xnorm, matrix)
{
xarray<double> a = {{ -1.0, 2.0},
{ -3.0, 4.0}};
EXPECT_EQ(norm_l0(a)(), 4u);
EXPECT_EQ(norm_l1(a)(), 10.0);
EXPECT_EQ(norm_sq(a)(), 30.0);
EXPECT_EQ(norm_linf(a)(), 4.0);
EXPECT_EQ(norm_induced_l1(a)(), 6.0);
EXPECT_EQ(norm_induced_linf(a)(), 7.0);
EXPECT_EQ(norm_l0(a, evaluation_strategy::immediate)(), 4u);
EXPECT_EQ(norm_l1(a, evaluation_strategy::immediate)(), 10.0);
EXPECT_EQ(norm_sq(a, evaluation_strategy::immediate)(), 30.0);
EXPECT_EQ(norm_linf(a, evaluation_strategy::immediate)(), 4.0);
EXPECT_EQ(norm_induced_l1(a, evaluation_strategy::immediate)(), 6.0);
EXPECT_EQ(norm_induced_linf(a, evaluation_strategy::immediate)(), 7.0);
}
} // namespace xt