-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_dmatrix.t
226 lines (208 loc) · 6.91 KB
/
test_dmatrix.t
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
215
216
217
218
219
220
221
222
223
224
225
226
-- SPDX-FileCopyrightText: 2024 René Hiemstra <[email protected]>
-- SPDX-FileCopyrightText: 2024 Torsten Keßler <[email protected]>
--
-- SPDX-License-Identifier: MIT
import "terratest/terratest"
local dmatrix = require("dmatrix")
local dvector = require("dvector")
local alloc = require("alloc")
local complex = require("complex")
local nfloat = require("nfloat")
local concepts = require("concepts")
local cfloat = complex.complex(float)
local cdouble = complex.complex(double)
local cint = complex.complex(int64)
local float128 = nfloat.FixedFloat(128)
local cfloat128 = complex.complex(float128)
local DefaultAlloc = alloc.DefaultAllocator()
for _, T in pairs({double, float, int64, cdouble, cfloat, cint, float128, cfloat128}) do
local Vec = dvector.DynamicVector(T)
local Mat = dmatrix.DynamicMatrix(T)
testenv(T) "Basic operations" do
testset "Init" do
terracode
var alloc: DefaultAlloc
var rows = 3
var cols = 2
var m = Mat.new(&alloc, rows, cols)
end
test m:rows() == rows
test m:cols() == cols
end
testset "Zeros" do
terracode
var alloc: DefaultAlloc
var rows = 3
var cols = 2
var m = Mat.zeros(&alloc, rows, cols)
end
test m:rows() == rows
test m:cols() == cols
for i = 0, 2 do
for j = 0, 1 do
test m:get(i, j) == 0
end
end
end
testset "From" do
terracode
var alloc: DefaultAlloc
var m = Mat.from(&alloc, {{1, 2, 3}, {4, 5, 6}})
end
test m:rows() == 2
test m:cols() == 3
for i = 0, 1 do
for j = 0, 2 do
test m:get(i, j) == j + 3 * i + 1
end
end
end
testset "Like" do
terracode
var alloc: DefaultAlloc
var rows = 7
var cols = 4
var a = Mat.new(&alloc, rows, cols)
var b = Mat.like(&alloc, &a)
end
test a:rows() == b:rows()
test a:cols() == b:cols()
end
testset "Zeros like" do
terracode
var alloc: DefaultAlloc
var rows = 7
var cols = 4
var a = Mat.new(&alloc, rows, cols)
var b = Mat.zeros_like(&alloc, &a)
end
test a:rows() == b:rows()
test a:cols() == b:cols()
for i = 0, 6 do
for j = 0, 3 do
test b:get(i, j) == 0
end
end
end
end
testenv(T) "Matrix base" do
testset "Fill" do
terracode
var alloc: DefaultAlloc
var a = Mat.new(&alloc, 2, 2)
a:fill(3)
end
for i = 0, 1 do
for j = 0, 1 do
test a:get(i, j) == 3
end
end
end
testset "Clear" do
terracode
var alloc: DefaultAlloc
var a = Mat.new(&alloc, 2, 2)
a:clear()
end
for i = 0, 1 do
for j = 0, 1 do
test a:get(i, j) == 0
end
end
end
testset "Copy" do
terracode
var alloc: DefaultAlloc
var a = Mat.from(&alloc, {{1, 2}, {3, 4}, {5, 6}})
var b = Mat.like(&alloc, &a)
var c = Mat.new(&alloc, 2, 3)
b:copy(false, &a)
c:copy(true, &a)
end
for i = 0, 2 do
for j = 0, 1 do
test b:get(i, j) == a:get(i, j)
test c:get(j, i) == a:get(i, j)
end
end
end
testset "Swap" do
terracode
var alloc: DefaultAlloc
var a = Mat.from(&alloc, {{1, 2}, {3, 4}, {5, 6}})
var b = Mat.from(&alloc, {{5, 6}, {1, 2}, {3, 4}})
var c = Mat.like(&alloc, &a)
var d = Mat.like(&alloc, &b)
c:copy(false, &a)
d:copy(false, &b)
a:swap(false, &b)
end
for i = 0, 2 do
for j = 0, 1 do
test a:get(i, j) == d:get(i, j)
test b:get(i, j) == c:get(i, j)
end
end
end
testset "Apply" do
terracode
var alloc: DefaultAlloc
var a = Mat.from(&alloc, {{1, 2}, {3, 4}, {5, 6}})
var x = Vec.from(&alloc, 1, -1)
var y = Vec.zeros(&alloc, 3)
var yref = Vec.from(&alloc, -1, -1, -1)
a:apply(false, [T](1), &x, [T](0), &y)
end
for i = 0, 2 do
test y:get(i) == yref:get(i)
end
end
testset "Mul" do
terracode
var alloc: DefaultAlloc
var a = Mat.from(&alloc, {{1, 2}, {3, 4}})
var b = Mat.from(&alloc, {{2, -1}, {-1, 2}})
var c = Mat.zeros_like(&alloc, &a)
c:mul([T](0), [T](1), false, &a, false, &b)
var cref = Mat.from(&alloc, {{0, 3}, {2, 5}})
var ct = Mat.zeros_like(&alloc, &a)
var ctref = Mat.from(&alloc, {{-1, 5}, {0, 6}})
ct:mul([T](0), [T](1), true, &a, false, &b)
end
test c:rows() == 2
test c:cols() == 2
for i = 0, 1 do
for j = 0, 1 do
test c:get(i, j) == cref:get(i, j)
end
end
test ct:rows() == 2
test ct:cols() == 2
for i = 0, 1 do
for j = 0, 1 do
test ct:get(i, j) == ctref:get(i, j)
end
end
end
if concepts.Complex(T) then
testset "Mul complex" do
terracode
var alloc: DefaultAlloc
var I = T.unit()
var a = Mat.from(&alloc, {{1 + 4 * I, 2 + 3 * I}, {3 + 2 * I, 4 + I}})
var b = Mat.from(&alloc, {{2, -1 - I}, {-1 + I, 2}})
var c = Mat.zeros_like(&alloc, &a)
var cref = Mat.from(&alloc, {{-3 + 7 * I, 7 + I}, {1 + 7 * I, 7 - 3 * I}})
c:mul([T](0), [T](1), false, &a, true, &b)
end
test c:rows() == 2
test c:cols() == 2
for i = 0, 1 do
for j = 0, 1 do
test c:get(i, j) == cref:get(i, j)
end
end
end
end
end
end