-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_level1_complex.py
More file actions
416 lines (315 loc) · 16.8 KB
/
Copy pathtest_level1_complex.py
File metadata and controls
416 lines (315 loc) · 16.8 KB
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
"""Readable independent and differential checks for complex BLAS Level 1."""
from __future__ import annotations
import numpy as np
import pytest
from .helpers import (
assert_allclose_for_dtype,
assert_storage_unchanged,
logical_vector,
with_logical_vector,
)
pytestmark = [pytest.mark.fortran_end_to_end, pytest.mark.real_library]
def test_caxpy(prik_blas, f2py_blas):
alpha = np.complex64(1.5 - 0.5j)
x = np.array([1.0 + 2.0j, -2.0 + 1.0j, 0.5 - 3.0j], dtype=np.complex64)
original_y = np.array([3.0 - 1.0j, 2.0 + 4.0j, -5.0 + 0.5j], dtype=np.complex64)
prik_x, f2py_x = x.copy(), x.copy()
prik_y, f2py_y = original_y.copy(), original_y.copy()
prik_scalars = prik_blas.caxpy(np.int32(3), alpha, prik_x, np.int32(1), prik_y, np.int32(1))
f2py_result = f2py_blas.caxpy(np.int32(3), alpha, f2py_x, np.int32(1), f2py_y, np.int32(1))
expected_y = alpha * x + original_y
assert_allclose_for_dtype(prik_y, expected_y)
assert_allclose_for_dtype(f2py_y, expected_y)
assert_allclose_for_dtype(prik_y, f2py_y)
assert prik_scalars == (np.int32(3), alpha, np.int32(1), np.int32(1))
assert f2py_result is None
assert_storage_unchanged(prik_x, x)
assert_storage_unchanged(f2py_x, x)
def test_zaxpy(prik_blas, f2py_blas):
alpha = np.complex128(-0.25 + 2.0j)
x = np.array([2.0 - 1.0j, 3.0 + 0.5j, -1.0 + 4.0j], dtype=np.complex128)
original_y = np.array([1.0 + 3.0j, -2.0 + 1.0j, 5.0 - 0.5j], dtype=np.complex128)
prik_x, f2py_x = x.copy(), x.copy()
prik_y, f2py_y = original_y.copy(), original_y.copy()
prik_scalars = prik_blas.zaxpy(np.int32(3), alpha, prik_x, np.int32(1), prik_y, np.int32(1))
f2py_result = f2py_blas.zaxpy(np.int32(3), alpha, f2py_x, np.int32(1), f2py_y, np.int32(1))
expected_y = alpha * x + original_y
assert_allclose_for_dtype(prik_y, expected_y)
assert_allclose_for_dtype(f2py_y, expected_y)
assert_allclose_for_dtype(prik_y, f2py_y)
assert prik_scalars == (np.int32(3), alpha, np.int32(1), np.int32(1))
assert f2py_result is None
assert_storage_unchanged(prik_x, x)
assert_storage_unchanged(f2py_x, x)
def test_scasum(prik_blas, f2py_blas):
x = np.array([1.0 - 2.0j, 90.0j, -3.0 + 4.0j], dtype=np.complex64)
prik_x, f2py_x = x.copy(), x.copy()
prik_value, *_ = prik_blas.scasum(np.int32(2), prik_x, np.int32(2))
f2py_value = f2py_blas.scasum(np.int32(2), f2py_x, np.int32(2))
expected = np.float32(1.0 + 2.0 + 3.0 + 4.0)
assert_allclose_for_dtype(prik_value, expected, operation_size=2)
assert_allclose_for_dtype(f2py_value, expected, operation_size=2)
assert_allclose_for_dtype(prik_value, f2py_value, operation_size=2)
assert_storage_unchanged(prik_x, x)
assert_storage_unchanged(f2py_x, x)
def test_dzasum(prik_blas, f2py_blas):
x = np.array([1.5 - 2.5j, -3.0 + 4.0j], dtype=np.complex128)
prik_x, f2py_x = x.copy(), x.copy()
prik_value, *_ = prik_blas.dzasum(np.int32(2), prik_x, np.int32(1))
f2py_value = f2py_blas.dzasum(np.int32(2), f2py_x, np.int32(1))
expected = np.float64(1.5 + 2.5 + 3.0 + 4.0)
assert_allclose_for_dtype(prik_value, expected, operation_size=2)
assert_allclose_for_dtype(f2py_value, expected, operation_size=2)
assert_allclose_for_dtype(prik_value, f2py_value, operation_size=2)
assert_storage_unchanged(prik_x, x)
assert_storage_unchanged(f2py_x, x)
def test_ccopy(prik_blas, f2py_blas):
x = np.array([1.0 + 2.0j, 90.0j, -3.0 + 4.0j], dtype=np.complex64)
original_y = np.array([5.0 - 1.0j, 80.0j, 6.0 + 2.0j], dtype=np.complex64)
prik_x, f2py_x = x.copy(), x.copy()
prik_y, f2py_y = original_y.copy(), original_y.copy()
prik_blas.ccopy(np.int32(2), prik_x, np.int32(2), prik_y, np.int32(2))
f2py_blas.ccopy(np.int32(2), f2py_x, np.int32(2), f2py_y, np.int32(2))
expected_y = with_logical_vector(original_y, logical_vector(x, 2, 2), 2, 2)
assert_allclose_for_dtype(prik_y, expected_y)
assert_allclose_for_dtype(f2py_y, expected_y)
assert_allclose_for_dtype(prik_y, f2py_y)
assert_storage_unchanged(prik_x, x)
assert_storage_unchanged(f2py_x, x)
def test_zcopy(prik_blas, f2py_blas):
x = np.array([1.0 + 2.0j, -3.0 + 4.0j], dtype=np.complex128)
original_y = np.array([5.0 - 1.0j, 6.0 + 2.0j], dtype=np.complex128)
prik_x, f2py_x = x.copy(), x.copy()
prik_y, f2py_y = original_y.copy(), original_y.copy()
prik_blas.zcopy(np.int32(2), prik_x, np.int32(-1), prik_y, np.int32(1))
f2py_blas.zcopy(np.int32(2), f2py_x, np.int32(-1), f2py_y, np.int32(1))
expected_y = x[::-1]
assert_allclose_for_dtype(prik_y, expected_y)
assert_allclose_for_dtype(f2py_y, expected_y)
assert_allclose_for_dtype(prik_y, f2py_y)
assert_storage_unchanged(prik_x, x)
assert_storage_unchanged(f2py_x, x)
def test_cdotc(prik_blas, f2py_blas):
x = np.array([1.0 + 2.0j, -3.0 + 1.0j], dtype=np.complex64)
y = np.array([2.0 - 1.0j, 4.0 + 3.0j], dtype=np.complex64)
prik_x, f2py_x = x.copy(), x.copy()
prik_y, f2py_y = y.copy(), y.copy()
prik_value, *_ = prik_blas.cdotc(np.int32(2), prik_x, np.int32(1), prik_y, np.int32(1))
f2py_value = f2py_blas.cdotc(np.int32(2), f2py_x, np.int32(1), f2py_y, np.int32(1))
expected = np.conj(x[0]) * y[0] + np.conj(x[1]) * y[1]
assert_allclose_for_dtype(prik_value, expected, operation_size=2)
assert_allclose_for_dtype(f2py_value, expected, operation_size=2)
assert_allclose_for_dtype(prik_value, f2py_value, operation_size=2)
assert_storage_unchanged(prik_x, x)
assert_storage_unchanged(f2py_x, x)
assert_storage_unchanged(prik_y, y)
assert_storage_unchanged(f2py_y, y)
def test_zdotc(prik_blas, f2py_blas):
x = np.array([1.0 + 2.0j, 90.0j, -3.0 + 1.0j], dtype=np.complex128)
y = np.array([2.0 - 1.0j, 4.0 + 3.0j], dtype=np.complex128)
prik_x, f2py_x = x.copy(), x.copy()
prik_y, f2py_y = y.copy(), y.copy()
prik_value, *_ = prik_blas.zdotc(np.int32(2), prik_x, np.int32(2), prik_y, np.int32(1))
f2py_value = f2py_blas.zdotc(np.int32(2), f2py_x, np.int32(2), f2py_y, np.int32(1))
expected = np.conj(x[0]) * y[0] + np.conj(x[2]) * y[1]
assert_allclose_for_dtype(prik_value, expected, operation_size=2)
assert_allclose_for_dtype(f2py_value, expected, operation_size=2)
assert_allclose_for_dtype(prik_value, f2py_value, operation_size=2)
assert_storage_unchanged(prik_x, x)
assert_storage_unchanged(f2py_x, x)
assert_storage_unchanged(prik_y, y)
assert_storage_unchanged(f2py_y, y)
def test_cdotu(prik_blas, f2py_blas):
x = np.array([1.0 + 2.0j, -3.0 + 1.0j], dtype=np.complex64)
y = np.array([2.0 - 1.0j, 4.0 + 3.0j], dtype=np.complex64)
prik_x, f2py_x = x.copy(), x.copy()
prik_y, f2py_y = y.copy(), y.copy()
prik_value, *_ = prik_blas.cdotu(np.int32(2), prik_x, np.int32(1), prik_y, np.int32(1))
f2py_value = f2py_blas.cdotu(np.int32(2), f2py_x, np.int32(1), f2py_y, np.int32(1))
expected = x[0] * y[0] + x[1] * y[1]
assert_allclose_for_dtype(prik_value, expected, operation_size=2)
assert_allclose_for_dtype(f2py_value, expected, operation_size=2)
assert_allclose_for_dtype(prik_value, f2py_value, operation_size=2)
assert_storage_unchanged(prik_x, x)
assert_storage_unchanged(f2py_x, x)
assert_storage_unchanged(prik_y, y)
assert_storage_unchanged(f2py_y, y)
def test_zdotu(prik_blas, f2py_blas):
x = np.array([1.0 + 2.0j, -3.0 + 1.0j], dtype=np.complex128)
y = np.array([2.0 - 1.0j, 4.0 + 3.0j], dtype=np.complex128)
prik_x, f2py_x = x.copy(), x.copy()
prik_y, f2py_y = y.copy(), y.copy()
prik_value, *_ = prik_blas.zdotu(np.int32(2), prik_x, np.int32(-1), prik_y, np.int32(1))
f2py_value = f2py_blas.zdotu(np.int32(2), f2py_x, np.int32(-1), f2py_y, np.int32(1))
expected = x[1] * y[0] + x[0] * y[1]
assert_allclose_for_dtype(prik_value, expected, operation_size=2)
assert_allclose_for_dtype(f2py_value, expected, operation_size=2)
assert_allclose_for_dtype(prik_value, f2py_value, operation_size=2)
assert_storage_unchanged(prik_x, x)
assert_storage_unchanged(f2py_x, x)
assert_storage_unchanged(prik_y, y)
assert_storage_unchanged(f2py_y, y)
def test_scnrm2(prik_blas, f2py_blas):
x = np.array([3.0 + 0.0j, 90.0j, 0.0 + 4.0j], dtype=np.complex64)
prik_x, f2py_x = x.copy(), x.copy()
prik_value, *_ = prik_blas.scnrm2(np.int32(2), prik_x, np.int32(2))
f2py_value = f2py_blas.scnrm2(np.int32(2), f2py_x, np.int32(2))
expected = np.float32(np.sqrt(3.0**2 + 4.0**2))
assert_allclose_for_dtype(prik_value, expected, operation_size=2)
assert_allclose_for_dtype(f2py_value, expected, operation_size=2)
assert_allclose_for_dtype(prik_value, f2py_value, operation_size=2)
assert_storage_unchanged(prik_x, x)
assert_storage_unchanged(f2py_x, x)
def test_dznrm2(prik_blas, f2py_blas):
x = np.array([1.0 + 2.0j, -3.0 + 4.0j], dtype=np.complex128)
prik_x, f2py_x = x.copy(), x.copy()
prik_value, *_ = prik_blas.dznrm2(np.int32(2), prik_x, np.int32(1))
f2py_value = f2py_blas.dznrm2(np.int32(2), f2py_x, np.int32(1))
expected = np.float64(np.sqrt(1.0**2 + 2.0**2 + 3.0**2 + 4.0**2))
assert_allclose_for_dtype(prik_value, expected, operation_size=4)
assert_allclose_for_dtype(f2py_value, expected, operation_size=4)
assert_allclose_for_dtype(prik_value, f2py_value, operation_size=4)
assert_storage_unchanged(prik_x, x)
assert_storage_unchanged(f2py_x, x)
def test_crotg(prik_blas, f2py_blas):
a, b = np.complex64(3.0 + 1.0j), np.complex64(4.0 - 2.0j)
f2py_a = np.array(a, dtype=np.complex64)
f2py_b = np.array(b, dtype=np.complex64)
f2py_c = np.array(0.0, dtype=np.float32)
f2py_s = np.array(0.0j, dtype=np.complex64)
result, prik_b, c, s = prik_blas.crotg(a, b, np.float32(0.0), np.complex64(0.0j))
f2py_result = f2py_blas.crotg(f2py_a, f2py_b, f2py_c, f2py_s)
assert_allclose_for_dtype(c * a + s * b, result, operation_size=2)
assert_allclose_for_dtype(-np.conj(s) * a + c * b, np.complex64(0.0j), operation_size=2)
assert_allclose_for_dtype(c * c + abs(s) ** 2, np.float32(1.0), operation_size=2)
assert_allclose_for_dtype([f2py_a, f2py_b, f2py_c, f2py_s], [result, prik_b, c, s])
assert f2py_result is None
def test_zrotg(prik_blas, f2py_blas):
a, b = np.complex128(3.0 + 1.0j), np.complex128(4.0 - 2.0j)
f2py_a = np.array(a, dtype=np.complex128)
f2py_b = np.array(b, dtype=np.complex128)
f2py_c = np.array(0.0, dtype=np.float64)
f2py_s = np.array(0.0j, dtype=np.complex128)
result, prik_b, c, s = prik_blas.zrotg(a, b, np.float64(0.0), np.complex128(0.0j))
f2py_result = f2py_blas.zrotg(f2py_a, f2py_b, f2py_c, f2py_s)
assert_allclose_for_dtype(c * a + s * b, result, operation_size=2)
assert_allclose_for_dtype(-np.conj(s) * a + c * b, np.complex128(0.0j), operation_size=2)
assert_allclose_for_dtype(c * c + abs(s) ** 2, np.float64(1.0), operation_size=2)
assert_allclose_for_dtype([f2py_a, f2py_b, f2py_c, f2py_s], [result, prik_b, c, s])
assert f2py_result is None
def test_cscal(prik_blas, f2py_blas):
alpha = np.complex64(1.0 - 2.0j)
original = np.array([2.0 + 1.0j, -3.0 + 4.0j], dtype=np.complex64)
prik_x, f2py_x = original.copy(), original.copy()
prik_blas.cscal(np.int32(2), alpha, prik_x, np.int32(1))
f2py_blas.cscal(np.int32(2), alpha, f2py_x, np.int32(1))
expected = alpha * original
assert_allclose_for_dtype(prik_x, expected)
assert_allclose_for_dtype(f2py_x, expected)
assert_allclose_for_dtype(prik_x, f2py_x)
def test_zscal(prik_blas, f2py_blas):
alpha = np.complex128(-0.5 + 1.5j)
original = np.array([2.0 + 1.0j, 90.0j, -3.0 + 4.0j], dtype=np.complex128)
prik_x, f2py_x = original.copy(), original.copy()
prik_blas.zscal(np.int32(2), alpha, prik_x, np.int32(2))
f2py_blas.zscal(np.int32(2), alpha, f2py_x, np.int32(2))
expected = with_logical_vector(original, alpha * original[::2], 2, 2)
assert_allclose_for_dtype(prik_x, expected)
assert_allclose_for_dtype(f2py_x, expected)
assert_allclose_for_dtype(prik_x, f2py_x)
def test_csscal(prik_blas, f2py_blas):
alpha = np.float32(-2.0)
original = np.array([2.0 + 1.0j, -3.0 + 4.0j], dtype=np.complex64)
prik_x, f2py_x = original.copy(), original.copy()
prik_blas.csscal(np.int32(2), alpha, prik_x, np.int32(1))
f2py_blas.csscal(np.int32(2), alpha, f2py_x, np.int32(1))
expected = alpha * original
assert_allclose_for_dtype(prik_x, expected)
assert_allclose_for_dtype(f2py_x, expected)
assert_allclose_for_dtype(prik_x, f2py_x)
def test_zdscal(prik_blas, f2py_blas):
alpha = np.float64(0.25)
original = np.array([2.0 + 1.0j, -3.0 + 4.0j], dtype=np.complex128)
prik_x, f2py_x = original.copy(), original.copy()
prik_blas.zdscal(np.int32(2), alpha, prik_x, np.int32(1))
f2py_blas.zdscal(np.int32(2), alpha, f2py_x, np.int32(1))
expected = alpha * original
assert_allclose_for_dtype(prik_x, expected)
assert_allclose_for_dtype(f2py_x, expected)
assert_allclose_for_dtype(prik_x, f2py_x)
def test_csrot(prik_blas, f2py_blas):
c, s = np.float32(0.6), np.float32(0.8)
x = np.array([1.0 + 2.0j, -2.0 + 1.0j], dtype=np.complex64)
y = np.array([3.0 - 1.0j, 4.0 + 2.0j], dtype=np.complex64)
prik_x, f2py_x = x.copy(), x.copy()
prik_y, f2py_y = y.copy(), y.copy()
prik_blas.csrot(np.int32(2), prik_x, np.int32(1), prik_y, np.int32(1), c, s)
f2py_blas.csrot(np.int32(2), f2py_x, np.int32(1), f2py_y, np.int32(1), c, s)
expected_x = c * x + s * y
expected_y = c * y - s * x
assert_allclose_for_dtype(prik_x, expected_x)
assert_allclose_for_dtype(f2py_x, expected_x)
assert_allclose_for_dtype(prik_y, expected_y)
assert_allclose_for_dtype(f2py_y, expected_y)
def test_zdrot(prik_blas, f2py_blas):
c, s = np.float64(0.8), np.float64(-0.6)
x = np.array([1.0 + 2.0j, -2.0 + 1.0j], dtype=np.complex128)
y = np.array([3.0 - 1.0j, 4.0 + 2.0j], dtype=np.complex128)
prik_x, f2py_x = x.copy(), x.copy()
prik_y, f2py_y = y.copy(), y.copy()
prik_blas.zdrot(np.int32(2), prik_x, np.int32(1), prik_y, np.int32(1), c, s)
f2py_blas.zdrot(np.int32(2), f2py_x, np.int32(1), f2py_y, np.int32(1), c, s)
expected_x = c * x + s * y
expected_y = c * y - s * x
assert_allclose_for_dtype(prik_x, expected_x)
assert_allclose_for_dtype(f2py_x, expected_x)
assert_allclose_for_dtype(prik_y, expected_y)
assert_allclose_for_dtype(f2py_y, expected_y)
def test_cswap(prik_blas, f2py_blas):
x = np.array([1.0 + 2.0j, 90.0j, -2.0 + 1.0j], dtype=np.complex64)
y = np.array([3.0 - 1.0j, 80.0j, 4.0 + 2.0j], dtype=np.complex64)
prik_x, f2py_x = x.copy(), x.copy()
prik_y, f2py_y = y.copy(), y.copy()
prik_blas.cswap(np.int32(2), prik_x, np.int32(2), prik_y, np.int32(2))
f2py_blas.cswap(np.int32(2), f2py_x, np.int32(2), f2py_y, np.int32(2))
expected_x = with_logical_vector(x, y[::2], 2, 2)
expected_y = with_logical_vector(y, x[::2], 2, 2)
assert_allclose_for_dtype(prik_x, expected_x)
assert_allclose_for_dtype(f2py_x, expected_x)
assert_allclose_for_dtype(prik_y, expected_y)
assert_allclose_for_dtype(f2py_y, expected_y)
def test_zswap(prik_blas, f2py_blas):
x = np.array([1.0 + 2.0j, -2.0 + 1.0j], dtype=np.complex128)
y = np.array([3.0 - 1.0j, 4.0 + 2.0j], dtype=np.complex128)
prik_x, f2py_x = x.copy(), x.copy()
prik_y, f2py_y = y.copy(), y.copy()
prik_blas.zswap(np.int32(2), prik_x, np.int32(-1), prik_y, np.int32(1))
f2py_blas.zswap(np.int32(2), f2py_x, np.int32(-1), f2py_y, np.int32(1))
expected_x = y[::-1]
expected_y = x[::-1]
assert_allclose_for_dtype(prik_x, expected_x)
assert_allclose_for_dtype(f2py_x, expected_x)
assert_allclose_for_dtype(prik_y, expected_y)
assert_allclose_for_dtype(f2py_y, expected_y)
def test_icamax(prik_blas, f2py_blas):
x = np.array([1.0 + 1.0j, -4.0 + 5.0j, 3.0 - 2.0j], dtype=np.complex64)
prik_x, f2py_x = x.copy(), x.copy()
prik_index, *_ = prik_blas.icamax(np.int32(3), prik_x, np.int32(1))
f2py_index = f2py_blas.icamax(np.int32(3), f2py_x, np.int32(1))
expected_native_one_based_index = 2
assert prik_index == expected_native_one_based_index
assert f2py_index == expected_native_one_based_index
assert prik_index == f2py_index
assert_storage_unchanged(prik_x, x)
assert_storage_unchanged(f2py_x, x)
def test_izamax(prik_blas, f2py_blas):
x = np.array([1.0 + 1.0j, 90.0j, -4.0 + 5.0j, 80.0j, 3.0 - 2.0j], dtype=np.complex128)
prik_x, f2py_x = x.copy(), x.copy()
prik_index, *_ = prik_blas.izamax(np.int32(3), prik_x, np.int32(2))
f2py_index = f2py_blas.izamax(np.int32(3), f2py_x, np.int32(2))
expected_native_one_based_index = 2
assert prik_index == expected_native_one_based_index
assert f2py_index == expected_native_one_based_index
assert prik_index == f2py_index
assert_storage_unchanged(prik_x, x)
assert_storage_unchanged(f2py_x, x)