forked from arrayfire/arrayfire-binary-python-wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_matrix_operations.py
301 lines (236 loc) · 8.47 KB
/
test_matrix_operations.py
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
import random
import pytest
import arrayfire_wrapper.lib as wrapper
from arrayfire_wrapper.defines import AFArray
from arrayfire_wrapper.dtypes import Dtype, b8, c32, c64, f16, f32, f64, s16, s32, s64, u8, u16, u32, u64
from .utility_functions import check_type_supported
@pytest.mark.parametrize(
"shape",
[(1, 1), (10, 10), (100, 100), (1000, 1000), (10000, 10000)],
)
def test_det_type(shape: tuple) -> None:
"""Test if det returns a complex number"""
arr = wrapper.randn(shape, f32)
determinant = wrapper.det(arr)
assert isinstance(determinant, complex)
@pytest.mark.parametrize(
"shape",
[
(15, 17),
(105, 325),
(567, 803),
(5324, 7865),
(10, 10, 10),
(100, 100, 100),
(1000, 1000, 1000),
(10000, 10000, 10000),
(10, 10, 10, 10),
(100, 100, 100, 100),
(1000, 1000, 1000, 1000),
(10000, 10000, 10000, 10000),
],
)
def test_det_invalid_shape(shape: tuple) -> None:
"""Test if det can properly handle invalid shapes"""
with pytest.raises(RuntimeError):
arr = wrapper.randn(shape, f32)
wrapper.det(arr)
@pytest.mark.parametrize("dtype", [s16, s32, s64, u8, u16, u32, u64, f16, b8])
def test_det_invalid_dtype(dtype: Dtype) -> None:
"""Test if det can properly handle invalid dtypes"""
with pytest.raises(RuntimeError):
shape = (10, 10)
arr = wrapper.identity(shape, dtype)
wrapper.det(arr)
@pytest.mark.parametrize("dtype", [f32, f64, c32, c64])
def test_det_valid_dtype(dtype: Dtype) -> None:
"""Test if det can properly handle all valid dtypes"""
check_type_supported(dtype)
shape = (10, 10)
arr = wrapper.identity(shape, dtype)
determinant = wrapper.det(arr)
assert isinstance(determinant, complex)
@pytest.mark.parametrize(
"shape",
[(1, 1), (10, 10), (100, 100), (1000, 1000), (10000, 10000)],
)
def test_inverse_type(shape: tuple) -> None:
"""Test if inverse returns an AFArray"""
arr = wrapper.randn(shape, f32)
inv = wrapper.inverse(arr, wrapper.MatProp(0))
assert isinstance(inv, AFArray)
@pytest.mark.parametrize(
"shape",
[
(15, 17),
(105, 325),
(567, 803),
(5324, 7865),
(10, 10, 10),
(100, 100, 100),
(1000, 1000, 1000),
(10000, 10000, 10000),
(10, 10, 10, 10),
(100, 100, 100, 100),
(1000, 1000, 1000, 1000),
(10000, 10000, 10000, 10000),
],
)
def test_inverse_invalid_shape(shape: tuple) -> None:
"""Test if inverse can properly handle invalid shapes"""
with pytest.raises(RuntimeError):
arr = wrapper.randn(shape, f32)
wrapper.inverse(arr, wrapper.MatProp(0))
@pytest.mark.parametrize("dtype", [s16, s32, s64, u8, u16, u32, u64, f16, b8])
def test_inverse_invalid_dtype(dtype: Dtype) -> None:
"""Test if inverse can properly handle invalid dtypes"""
with pytest.raises(RuntimeError):
shape = (10, 10)
arr = wrapper.identity(shape, dtype)
wrapper.inverse(arr, wrapper.MatProp(0))
@pytest.mark.parametrize("dtype", [f32, f64, c32, c64])
def test_inverse_valid_dtype(dtype: Dtype) -> None:
"""Test if inverse can properly handle all valid dtypes"""
check_type_supported(dtype)
shape = (10, 10)
arr = wrapper.identity(shape, dtype)
wrapper.inverse(arr, wrapper.MatProp(0))
@pytest.mark.parametrize(
"shape",
[(1, 1), (10, 10), (100, 100), (1000, 1000), (10000, 10000)],
)
def test_norm_output_type(shape: tuple) -> None:
"""Test if norm returns a float"""
arr = wrapper.randn(shape, f32)
nor = wrapper.norm(arr, wrapper.Norm(2), 1, 1)
assert isinstance(nor, float)
@pytest.mark.parametrize(
"norm",
[0, 1, 2, 3, 4, 5, 7], # VECTOR_1 # VECTOR_INF # VECTOR_2 # VECTOR_3 # MATRIX_1 # MATRIX_INF # MATRIX_L_PQ
)
def test_norm_types(norm: wrapper.Norm) -> None:
"""Test if norm can handle all valid norm types"""
shape = (3, 1)
arr = wrapper.randn(shape, f32)
nor = wrapper.norm(arr, wrapper.Norm(norm), 1, 2)
assert isinstance(nor, float)
@pytest.mark.parametrize(
"shape",
[
(10, 10, 10),
(100, 100, 100),
(1000, 1000, 1000),
(10000, 10000, 10000),
(10, 10, 10, 10),
(100, 100, 100, 100),
(1000, 1000, 1000, 1000),
(10000, 10000, 10000, 10000),
],
)
def test_norm_invalid_shape(shape: tuple) -> None:
"""Test if norm can properly handle invalid shapes"""
with pytest.raises(RuntimeError):
arr = wrapper.randn(shape, f32)
wrapper.norm(arr, wrapper.Norm(0), 1, 1)
@pytest.mark.parametrize("dtype", [s16, s32, s64, u8, u16, u32, u64, f16, b8])
def test_norm_invalid_dtype(dtype: Dtype) -> None:
"""Test if norm can properly handle invalid dtypes"""
with pytest.raises(RuntimeError):
shape = (10, 10)
arr = wrapper.identity(shape, dtype)
wrapper.norm(arr, wrapper.Norm(0), 1, 1)
@pytest.mark.parametrize("dtype", [f32, f64, c32, c64])
def test_norm_valid_dtype(dtype: Dtype) -> None:
"""Test if norm can properly handle all valid dtypes"""
check_type_supported(dtype)
shape = (10, 10)
arr = wrapper.identity(shape, dtype)
wrapper.norm(arr, wrapper.Norm(0), 1, 1)
# pinverse tests
@pytest.mark.parametrize(
"shape",
[
(1, 1),
(10, 10),
(100, 100),
(1000, 1000),
(1, 1, 1),
(10, 10, 10),
(100, 100, 100),
(1, 1, 1, 1),
(10, 10, 10, 10),
(100, 100, 100, 100),
],
)
def test_pinverse_output_type(shape: tuple) -> None:
"""Test if pinverse returns an AFArray"""
arr = wrapper.randn(shape, f32)
pin = wrapper.pinverse(arr, 1e-6, wrapper.MatProp(0))
assert isinstance(pin, AFArray)
@pytest.mark.parametrize("dtype", [s16, s32, s64, u8, u16, u32, u64, f16, b8])
def test_pinverse_invalid_dtype(dtype: Dtype) -> None:
"""Test if pinverse can properly handle invalid dtypes"""
with pytest.raises(RuntimeError):
shape = (10, 10)
arr = wrapper.identity(shape, dtype)
wrapper.pinverse(arr, 1e-6, wrapper.MatProp(0))
@pytest.mark.parametrize("dtype", [f32, f64, c32, c64])
def test_pinverse_valid_dtype(dtype: Dtype) -> None:
"""Test if pinverse can properly handle all valid dtypes"""
check_type_supported(dtype)
shape = (10, 10)
arr = wrapper.identity(shape, dtype)
pin = wrapper.pinverse(arr, 1e-6, wrapper.MatProp(0))
assert isinstance(pin, AFArray)
@pytest.mark.parametrize("tolerance", [-0.0001, -1, -10, -100, -1000])
def test_pinverse_invalid_tol(tolerance: int) -> None:
"""Test if pinverse can properly handle invalid tolerance values"""
with pytest.raises(RuntimeError):
shape = (10, 10)
arr = wrapper.identity(shape, f32)
wrapper.pinverse(arr, tolerance, wrapper.MatProp(0))
# rank tests
@pytest.mark.parametrize(
"shape",
[(1, 1), (10, 10), (100, 100), (1000, 1000), (random.randint(1, 1000), random.randint(1, 1000))],
)
def test_rank_output_type(shape: tuple) -> None:
"""Test if rank returns an AFArray"""
arr = wrapper.randn(shape, f32)
rk = wrapper.rank(arr, 1e-6)
assert isinstance(rk, int)
@pytest.mark.parametrize(
"shape",
[
(10, 10, 10),
(100, 100, 100),
(1000, 1000, 1000),
(10000, 10000, 10000),
(random.randint(1, 1000), random.randint(1, 1000), random.randint(1, 1000)),
(10, 10, 10, 10),
(100, 100, 100, 100),
(1000, 1000, 1000, 1000),
(10000, 10000, 10000, 10000),
(random.randint(1, 1000), random.randint(1, 1000), random.randint(1, 1000), random.randint(1, 10000)),
],
)
def test_rank_invalid_shape(shape: tuple) -> None:
"""Test if rank can properly handle invalid shapes"""
with pytest.raises(RuntimeError):
arr = wrapper.randn(shape, f32)
wrapper.rank(arr, 1e-6)
@pytest.mark.parametrize("dtype", [s16, s32, s64, u8, u16, u32, u64, f16, b8])
def test_rank_invalid_dtype(dtype: Dtype) -> None:
"""Test if rank can properly handle invalid dtypes"""
with pytest.raises(RuntimeError):
shape = (10, 10)
arr = wrapper.identity(shape, dtype)
wrapper.rank(arr, 1e-6)
@pytest.mark.parametrize("dtype", [f32, f64, c32, c64])
def test_rank_valid_dtype(dtype: Dtype) -> None:
"""Test if rank can properly handle all valid dtypes"""
check_type_supported(dtype)
shape = (10, 10)
arr = wrapper.identity(shape, dtype)
rk = wrapper.rank(arr, 1e-6)
assert isinstance(rk, int)