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
|
#include <forstio/test/suite.hpp>
#include "../c++/data.hpp"
#include "../c++/data_math.hpp"
#include "../c++/math.hpp"
#include "../c++/csv.hpp"
#include <iostream>
namespace {
namespace sch {
using namespace saw::schema;
}
/*
SAW_TEST("Math/Basic"){
using namespace saw;
data<sch::Vector<sch::Float64,2u>> a;
data<sch::Vector<sch::Float64,2u>> b;
auto c = a + b;
}
*/
SAW_TEST("Math/Tensor Construction"){
using namespace saw;
data<sch::Vector<sch::Int32,3u>> b{1,3,5};
/**
* A one dimensional index keeps the inner braces, since a single
* element has to stay explicit to not clash with data<UInt64>.
*/
SAW_EXPECT(b.at({{0u}}).get() == 1, "Wrong value at 0");
SAW_EXPECT(b.at({{1u}}).get() == 3, "Wrong value at 1");
SAW_EXPECT(b.at({{2u}}).get() == 5, "Wrong value at 2");
/**
* From two dimensions up the index forwards, since it is a FixedArray
* itself.
*
* The elements are given in flat order, and get_flat_index runs the
* stride from dimension zero up, so the first index varies fastest.
*/
data<sch::Matrix<sch::Float64,2u,2u>> c{1.0, 2.0, 3.0, 4.0};
SAW_EXPECT(c.at({0u,0u}).get() == 1.0, "Wrong value at 0,0");
SAW_EXPECT(c.at({1u,0u}).get() == 2.0, "Wrong value at 1,0");
SAW_EXPECT(c.at({0u,1u}).get() == 3.0, "Wrong value at 0,1");
SAW_EXPECT(c.at({1u,1u}).get() == 4.0, "Wrong value at 1,1");
}
SAW_TEST("Math/Tensor Fill"){
using namespace saw;
data<schema::Tensor<sch::Float64,2u,2u>> tensor;
math::fill(tensor,{2.5});
bool cf{true};
for(uint64_t i = 0u; i < 2u; ++i){
for(uint64_t j = 0u; j < 2u; ++j){
SAW_EXPECT((tensor.at({{i,j}}).get() == 2.5), "Wasn't filled with value 2.5");
}
}
}
SAW_TEST("Math/Tensor"){
using namespace saw;
data<sch::Matrix<sch::Float64,2u,2u> > a;
{
a.at({{0u,0u}}) = 1.0;
a.at({{0u,1u}}) = 2.0;
a.at({{1u,0u}}) = 3.0;
a.at({{1u,1u}}) = 4.0;
}
data<sch::Matrix<sch::Float64,2u,2u> > b;
{
b.at({{0u,0u}}) = 2.0;
b.at({{0u,1u}}) = 3.0;
b.at({{1u,0u}}) = 4.0;
b.at({{1u,1u}}) = 5.0;
}
auto c = a + b;
{
SAW_EXPECT(c.at({{0u,0u}}).get() == 3.0, std::string{"Unexpected value at (0,0): "} + std::to_string(c.at({{0u,0u}}).get()) );
SAW_EXPECT(c.at({{0u,1u}}).get() == 5.0, std::string{"Unexpected value at (0,1): "} + std::to_string(c.at({{0u,1u}}).get()));
SAW_EXPECT(c.at({{1u,0u}}).get() == 7.0, std::string{"Unexpected value at (1,0): "} + std::to_string(c.at({{1u,0u}}).get()));
SAW_EXPECT(c.at({{1u,1u}}).get() == 9.0, std::string{"Unexpected value at (1,1): "} + std::to_string(c.at({{1u,1u}}).get()));
}
auto d = b - a;
{
SAW_EXPECT(d.at({{0u,0u}}).get() == 1.0, std::string{"Unexpected value at (0,0): "} + std::to_string(d.at({{0u,0u}}).get()) );
SAW_EXPECT(d.at({{0u,1u}}).get() == 1.0, std::string{"Unexpected value at (0,1): "} + std::to_string(d.at({{0u,1u}}).get()));
SAW_EXPECT(d.at({{1u,0u}}).get() == 1.0, std::string{"Unexpected value at (1,0): "} + std::to_string(d.at({{1u,0u}}).get()));
SAW_EXPECT(d.at({{1u,1u}}).get() == 1.0, std::string{"Unexpected value at (1,1): "} + std::to_string(d.at({{1u,1u}}).get()));
}
auto e = -a;
}
SAW_TEST("Math/Rotate"){
using namespace saw;
data<sch::Vector<sch::Float64, 2u>> a;
{
a.at({{0u}}) = 2.0;
a.at({{1u}}) = 1.0;
}
data<sch::Scalar<sch::Float64>> b;
b.at({}) = 1.4;
auto c = math::rotate(a,b);
}
SAW_TEST("Math/Dot"){
using namespace saw;
data<sch::Vector<sch::Float64, 2u>> a;
{
a.at({{0u}}) = 2.0;
a.at({{1u}}) = 1.0;
}
data<sch::Vector<sch::Float64, 2u>> b;
{
b.at({{0u}}) = -1.0;
b.at({{1u}}) = 5.0;
}
auto c = math::dot(a,b);
SAW_EXPECT(c.at({}).get() == 3.0, std::string{"Unexpected value for dot product "} + std::to_string(c.at({}).get()) );
}
SAW_TEST("Math/Dot Self"){
using namespace saw;
data<sch::Vector<sch::Float64, 2u>> a;
{
a.at({{0u}}) = 2.0;
a.at({{1u}}) = 1.0;
}
auto c = math::dot(a,a);
SAW_EXPECT(c.at({}).get() == 5.0, std::string{"Unexpected value for dot product "} + std::to_string(c.at({}).get()) );
}
SAW_TEST("Math/Dot and Sqrt"){
using namespace saw;
data<sch::Vector<sch::Float64, 2u>> a;
{
a.at({{0u}}) = 3.0;
a.at({{1u}}) = 4.0;
}
auto c = math::dot(a,a);
auto d = math::sqrt(c);
SAW_EXPECT(d.at({}).get() == 5.0, std::string{"Unexpected value for dot + sqrt "} + std::to_string(d.at({}).get()) );
}
SAW_TEST("Math/Normalize"){
using namespace saw;
data<sch::Vector<sch::Float64, 2u>> a;
{
a.at({{0u}}) = 5.0;
a.at({{1u}}) = 0.0;
}
auto c = math::normalize(a);
SAW_EXPECT(c.at({{0u}}).get() == 1.0, std::string{"Unexpected value for dot + sqrt "} + std::to_string(c.at({{0u}}).get()) );
SAW_EXPECT(c.at({{1u}}).get() == 0.0, std::string{"Unexpected value for dot + sqrt "} + std::to_string(c.at({{1u}}).get()) );
}
SAW_TEST("Math/Matrix Multiply"){
using namespace saw;
data<sch::Matrix<sch::Int64, 2u, 2u>> a;
{
a.at({{0u,0u}}) = 1;
a.at({{0u,1u}}) = 3;
a.at({{1u,0u}}) = 2;
a.at({{1u,1u}}) = 4;
}
data<sch::Matrix<sch::Int64, 2u, 1u>> b;
{
b.at({{0u,0u}}) = -1;
b.at({{1u,0u}}) = 2;
}
auto c = math::multiply(a,b);
SAW_EXPECT(c.at({{0u,0u}}).get() == 5, std::string{"Unexpected value for Matrix Multiply (0,0): "} + std::to_string(c.at({{0u,0u}}).get()) );
SAW_EXPECT(c.at({{1u,0u}}).get() == 6, std::string{"Unexpected value for Matrix Multiply (1,0): "} + std::to_string(c.at({{1u,0u}}).get()) );
}
}
|