-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Math_unittest.cc
214 lines (177 loc) · 6.08 KB
/
Math_unittest.cc
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2013 Thoralf Klein
* Written (W) 2013 Soumyajit De
*/
#include <shogun/lib/common.h>
#include <shogun/lib/SGVector.h>
#include <shogun/mathematics/Math.h>
#include <gtest/gtest.h>
using namespace shogun;
TEST(CMath, qsort_test)
{
// testing qsort on list of zero elements
CMath::qsort((int32_t *)NULL, 0);
// testing qsort on list of one element
int32_t * v1 = SG_CALLOC(int32_t, 1);
CMath::qsort(v1, 1);
SG_FREE(v1);
}
TEST(CMath, qsort_ptr_test)
{
// testing qsort on list of zero pointers
CMath::qsort((int32_t **)NULL, 0);
// testing qsort on list of one pointer
int32_t ** v1 = SG_CALLOC(int32_t *, 1);
CMath::qsort(v1, 1);
SG_FREE(v1);
}
TEST(CMath, qsort_index_test)
{
// testing qsort_index on list of zero elements
CMath::qsort_index((int32_t *)NULL, (int32_t *)NULL, 0);
// testing qsort_index on list of one element
int32_t * v1 = SG_CALLOC(int32_t, 1);
int32_t * i1 = SG_CALLOC(int32_t, 1);
CMath::qsort_index(v1, i1, 1);
SG_FREE(v1);
SG_FREE(i1);
}
TEST(CMath, qsort_backward_index_test)
{
// testing qsort_backward_index on list of zero elements
CMath::qsort_backward_index((int32_t *)NULL, (int32_t *)NULL, 0);
// testing qsort_backward_index on list of one element
int32_t * v1 = SG_CALLOC(int32_t, 1);
int32_t * i1 = SG_CALLOC(int32_t, 1);
CMath::qsort_backward_index(v1, i1, 1);
SG_FREE(v1);
SG_FREE(i1);
}
TEST(CMath, parallel_qsort_index_test)
{
// testing parallel_qsort_index on list of zero elements
CMath::parallel_qsort_index((int32_t *)NULL, (int32_t *)NULL, 0, 8);
// testing parallel_qsort_index on list of one element
int32_t * v1 = SG_CALLOC(int32_t, 1);
int32_t * i1 = SG_CALLOC(int32_t, 1);
CMath::parallel_qsort_index(v1, i1, 1, 8);
SG_FREE(v1);
SG_FREE(i1);
}
TEST(CMath, float64_tests)
{
// round, ceil, floor
EXPECT_NEAR(CMath::round(7.5), 8.0, 1E-15);
EXPECT_NEAR(CMath::round(7.5-1E-15), 7.0, 1E-15);
EXPECT_NEAR(CMath::floor(8-1E-15), 7.0, 1E-15);
EXPECT_NEAR(CMath::ceil(7+1E-15), 8.0, 1E-15);
float64_t a=5.78123516567856743364;
// x^2, x^(1/2)
EXPECT_NEAR(CMath::sq(a), 33.42268004087848964900, 1E-15);
EXPECT_NEAR(CMath::sqrt(33.42268004087848964900), a, 1E-15);
EXPECT_NEAR(CMath::pow(a, 2), 33.42268004087848964900, 1E-15);
EXPECT_NEAR(CMath::pow(33.42268004087848964900, 0.5), a, 1E-15);
// e^x, log_{b}(x)
EXPECT_NEAR(CMath::exp(a), 324.15933372813628920994, 1E-15);
EXPECT_NEAR(CMath::log2(a), 2.53137775864743908016, 1E-15);
EXPECT_NEAR(CMath::log10(a), 0.76202063570953693095, 1E-15);
// exp and log identities
EXPECT_NEAR(CMath::log(CMath::exp(a)), a, 1E-15);
EXPECT_NEAR(CMath::exp(CMath::log(a)), a, 1E-15);
// trigonometric functions
EXPECT_NEAR(CMath::sin(a), -0.48113603605414501097, 1E-15);
EXPECT_NEAR(CMath::sinh(a), 162.07812441272406545067, 1E-13);
EXPECT_NEAR(CMath::asin(a-5.0), 0.89664205584230471935, 1E-15);
EXPECT_NEAR(CMath::cos(a), 0.87664594609802681813, 1E-15);
EXPECT_NEAR(CMath::cosh(a), 162.08120931541219533756, 1E-15);
EXPECT_NEAR(CMath::acos(a-5.0), 0.67415427095259194967, 1E-15);
EXPECT_NEAR(CMath::tan(a), -0.54883734784344084812, 1E-15);
EXPECT_NEAR(CMath::tanh(a), 0.99998096693194016282, 1E-15);
EXPECT_NEAR(CMath::atan(a), 1.39951769800256187182, 1E-15);
// trigonometric identities
EXPECT_NEAR(CMath::sq(CMath::sin(a))+CMath::sq(CMath::cos(a)),
1.0, 1E-15);
EXPECT_NEAR(CMath::sq(1.0/CMath::cos(a))-CMath::sq(CMath::tan(a)),
1.0, 1E-15);
EXPECT_NEAR(CMath::sq(1.0/CMath::sin(a))-CMath::sq(1.0/CMath::tan(a)),
1.0, 1E-15);
// misc
SGVector<float64_t> vec(10);
for (index_t i=0; i<10; ++i)
{
vec[i]=i%2==0 ? i : 0.0;
}
EXPECT_EQ(CMath::get_num_nonzero(vec.vector, 10), 4);
}
TEST(CMath, linspace_test)
{
// Number of points used to divide the interval
int32_t n = 100;
// Start and end of the interval
float64_t start = 0.0, end = 1.0;
SGVector<float64_t> vec(100);
CMath::linspace(vec.vector, start, end, n);
// The first and last elements are tested outside the loop, because
// linspace sets them directly using the arguments
EXPECT_EQ(vec[0], start);
EXPECT_EQ(vec[n-1], end);
float64_t val = start;
for (index_t i = 1; i < n-1; ++i)
{
val += (end-start)/(n-1);
EXPECT_EQ(vec[i], val);
}
}
TEST(CMath, log_sum_exp)
{
SGVector<float64_t> values(3);
values.range_fill();
EXPECT_NEAR(CMath::log_sum_exp(values), 2.4076059644443801, 1e-15);
}
TEST(CMath, log_mean_exp)
{
SGVector<float64_t> values(3);
values.range_fill();
EXPECT_NEAR(CMath::log_mean_exp(values), 1.3089936757762706, 1e-15);
}
TEST(CMath, strtofloat)
{
float32_t float_result = 0;
EXPECT_TRUE(CMath::strtof("nan", &float_result));
EXPECT_TRUE(CMath::is_nan(float_result));
EXPECT_TRUE(CMath::strtof("inf", &float_result));
EXPECT_TRUE(CMath::is_infinity(float_result));
EXPECT_TRUE(CMath::strtof("-inf", &float_result));
EXPECT_DOUBLE_EQ(-CMath::INFTY, float_result);
EXPECT_TRUE(CMath::strtof("1.2345", &float_result));
EXPECT_FLOAT_EQ(1.2345, float_result);
}
TEST(CMath, strtodouble)
{
float64_t double_result = 0;
EXPECT_TRUE(CMath::strtod("nan", &double_result));
EXPECT_TRUE(CMath::is_nan(double_result));
EXPECT_TRUE(CMath::strtod("inf", &double_result));
EXPECT_TRUE(CMath::is_infinity(double_result));
EXPECT_TRUE(CMath::strtod("-inf", &double_result));
EXPECT_DOUBLE_EQ(-CMath::INFTY, double_result);
EXPECT_TRUE(CMath::strtod("1.234567890123", &double_result));
EXPECT_DOUBLE_EQ(1.234567890123, double_result);
}
TEST(CMath, strtolongdouble)
{
floatmax_t long_double_result = 0;
EXPECT_TRUE(CMath::strtold("nan", &long_double_result));
EXPECT_TRUE(CMath::is_nan(long_double_result));
EXPECT_TRUE(CMath::strtold("inf", &long_double_result));
EXPECT_TRUE(CMath::is_infinity(long_double_result));
EXPECT_TRUE(CMath::strtold("-inf", &long_double_result));
EXPECT_DOUBLE_EQ(-CMath::INFTY, long_double_result);
EXPECT_TRUE(CMath::strtold("1.234567890123", &long_double_result));
EXPECT_EQ(long_double_result, 1.234567890123);
}