test_blas.py
39.3 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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
#
# Created by: Pearu Peterson, April 2002
#
__usage__ = """
Build linalg:
python setup.py build
Run tests if scipy is installed:
python -c 'import scipy;scipy.linalg.test()'
"""
import math
import pytest
import numpy as np
from numpy.testing import (assert_equal, assert_almost_equal, assert_,
assert_array_almost_equal, assert_allclose)
from pytest import raises as assert_raises
from numpy import float32, float64, complex64, complex128, arange, triu, \
tril, zeros, tril_indices, ones, mod, diag, append, eye, \
nonzero
from numpy.random import rand, seed
from scipy.linalg import _fblas as fblas, get_blas_funcs, toeplitz, solve
try:
from scipy.linalg import _cblas as cblas
except ImportError:
cblas = None
REAL_DTYPES = [float32, float64]
COMPLEX_DTYPES = [complex64, complex128]
DTYPES = REAL_DTYPES + COMPLEX_DTYPES
def test_get_blas_funcs():
# check that it returns Fortran code for arrays that are
# fortran-ordered
f1, f2, f3 = get_blas_funcs(
('axpy', 'axpy', 'axpy'),
(np.empty((2, 2), dtype=np.complex64, order='F'),
np.empty((2, 2), dtype=np.complex128, order='C'))
)
# get_blas_funcs will choose libraries depending on most generic
# array
assert_equal(f1.typecode, 'z')
assert_equal(f2.typecode, 'z')
if cblas is not None:
assert_equal(f1.module_name, 'cblas')
assert_equal(f2.module_name, 'cblas')
# check defaults.
f1 = get_blas_funcs('rotg')
assert_equal(f1.typecode, 'd')
# check also dtype interface
f1 = get_blas_funcs('gemm', dtype=np.complex64)
assert_equal(f1.typecode, 'c')
f1 = get_blas_funcs('gemm', dtype='F')
assert_equal(f1.typecode, 'c')
# extended precision complex
f1 = get_blas_funcs('gemm', dtype=np.longcomplex)
assert_equal(f1.typecode, 'z')
# check safe complex upcasting
f1 = get_blas_funcs('axpy',
(np.empty((2, 2), dtype=np.float64),
np.empty((2, 2), dtype=np.complex64))
)
assert_equal(f1.typecode, 'z')
def test_get_blas_funcs_alias():
# check alias for get_blas_funcs
f, g = get_blas_funcs(('nrm2', 'dot'), dtype=np.complex64)
assert f.typecode == 'c'
assert g.typecode == 'c'
f, g, h = get_blas_funcs(('dot', 'dotc', 'dotu'), dtype=np.float64)
assert f is g
assert f is h
class TestCBLAS1Simple(object):
def test_axpy(self):
for p in 'sd':
f = getattr(cblas, p+'axpy', None)
if f is None:
continue
assert_array_almost_equal(f([1, 2, 3], [2, -1, 3], a=5),
[7, 9, 18])
for p in 'cz':
f = getattr(cblas, p+'axpy', None)
if f is None:
continue
assert_array_almost_equal(f([1, 2j, 3], [2, -1, 3], a=5),
[7, 10j-1, 18])
class TestFBLAS1Simple(object):
def test_axpy(self):
for p in 'sd':
f = getattr(fblas, p+'axpy', None)
if f is None:
continue
assert_array_almost_equal(f([1, 2, 3], [2, -1, 3], a=5),
[7, 9, 18])
for p in 'cz':
f = getattr(fblas, p+'axpy', None)
if f is None:
continue
assert_array_almost_equal(f([1, 2j, 3], [2, -1, 3], a=5),
[7, 10j-1, 18])
def test_copy(self):
for p in 'sd':
f = getattr(fblas, p+'copy', None)
if f is None:
continue
assert_array_almost_equal(f([3, 4, 5], [8]*3), [3, 4, 5])
for p in 'cz':
f = getattr(fblas, p+'copy', None)
if f is None:
continue
assert_array_almost_equal(f([3, 4j, 5+3j], [8]*3), [3, 4j, 5+3j])
def test_asum(self):
for p in 'sd':
f = getattr(fblas, p+'asum', None)
if f is None:
continue
assert_almost_equal(f([3, -4, 5]), 12)
for p in ['sc', 'dz']:
f = getattr(fblas, p+'asum', None)
if f is None:
continue
assert_almost_equal(f([3j, -4, 3-4j]), 14)
def test_dot(self):
for p in 'sd':
f = getattr(fblas, p+'dot', None)
if f is None:
continue
assert_almost_equal(f([3, -4, 5], [2, 5, 1]), -9)
def test_complex_dotu(self):
for p in 'cz':
f = getattr(fblas, p+'dotu', None)
if f is None:
continue
assert_almost_equal(f([3j, -4, 3-4j], [2, 3, 1]), -9+2j)
def test_complex_dotc(self):
for p in 'cz':
f = getattr(fblas, p+'dotc', None)
if f is None:
continue
assert_almost_equal(f([3j, -4, 3-4j], [2, 3j, 1]), 3-14j)
def test_nrm2(self):
for p in 'sd':
f = getattr(fblas, p+'nrm2', None)
if f is None:
continue
assert_almost_equal(f([3, -4, 5]), math.sqrt(50))
for p in ['c', 'z', 'sc', 'dz']:
f = getattr(fblas, p+'nrm2', None)
if f is None:
continue
assert_almost_equal(f([3j, -4, 3-4j]), math.sqrt(50))
def test_scal(self):
for p in 'sd':
f = getattr(fblas, p+'scal', None)
if f is None:
continue
assert_array_almost_equal(f(2, [3, -4, 5]), [6, -8, 10])
for p in 'cz':
f = getattr(fblas, p+'scal', None)
if f is None:
continue
assert_array_almost_equal(f(3j, [3j, -4, 3-4j]), [-9, -12j, 12+9j])
for p in ['cs', 'zd']:
f = getattr(fblas, p+'scal', None)
if f is None:
continue
assert_array_almost_equal(f(3, [3j, -4, 3-4j]), [9j, -12, 9-12j])
def test_swap(self):
for p in 'sd':
f = getattr(fblas, p+'swap', None)
if f is None:
continue
x, y = [2, 3, 1], [-2, 3, 7]
x1, y1 = f(x, y)
assert_array_almost_equal(x1, y)
assert_array_almost_equal(y1, x)
for p in 'cz':
f = getattr(fblas, p+'swap', None)
if f is None:
continue
x, y = [2, 3j, 1], [-2, 3, 7-3j]
x1, y1 = f(x, y)
assert_array_almost_equal(x1, y)
assert_array_almost_equal(y1, x)
def test_amax(self):
for p in 'sd':
f = getattr(fblas, 'i'+p+'amax')
assert_equal(f([-2, 4, 3]), 1)
for p in 'cz':
f = getattr(fblas, 'i'+p+'amax')
assert_equal(f([-5, 4+3j, 6]), 1)
# XXX: need tests for rot,rotm,rotg,rotmg
class TestFBLAS2Simple(object):
def test_gemv(self):
for p in 'sd':
f = getattr(fblas, p+'gemv', None)
if f is None:
continue
assert_array_almost_equal(f(3, [[3]], [-4]), [-36])
assert_array_almost_equal(f(3, [[3]], [-4], 3, [5]), [-21])
for p in 'cz':
f = getattr(fblas, p+'gemv', None)
if f is None:
continue
assert_array_almost_equal(f(3j, [[3-4j]], [-4]), [-48-36j])
assert_array_almost_equal(f(3j, [[3-4j]], [-4], 3, [5j]),
[-48-21j])
def test_ger(self):
for p in 'sd':
f = getattr(fblas, p+'ger', None)
if f is None:
continue
assert_array_almost_equal(f(1, [1, 2], [3, 4]), [[3, 4], [6, 8]])
assert_array_almost_equal(f(2, [1, 2, 3], [3, 4]),
[[6, 8], [12, 16], [18, 24]])
assert_array_almost_equal(f(1, [1, 2], [3, 4],
a=[[1, 2], [3, 4]]), [[4, 6], [9, 12]])
for p in 'cz':
f = getattr(fblas, p+'geru', None)
if f is None:
continue
assert_array_almost_equal(f(1, [1j, 2], [3, 4]),
[[3j, 4j], [6, 8]])
assert_array_almost_equal(f(-2, [1j, 2j, 3j], [3j, 4j]),
[[6, 8], [12, 16], [18, 24]])
for p in 'cz':
for name in ('ger', 'gerc'):
f = getattr(fblas, p+name, None)
if f is None:
continue
assert_array_almost_equal(f(1, [1j, 2], [3, 4]),
[[3j, 4j], [6, 8]])
assert_array_almost_equal(f(2, [1j, 2j, 3j], [3j, 4j]),
[[6, 8], [12, 16], [18, 24]])
def test_syr_her(self):
x = np.arange(1, 5, dtype='d')
resx = np.triu(x[:, np.newaxis] * x)
resx_reverse = np.triu(x[::-1, np.newaxis] * x[::-1])
y = np.linspace(0, 8.5, 17, endpoint=False)
z = np.arange(1, 9, dtype='d').view('D')
resz = np.triu(z[:, np.newaxis] * z)
resz_reverse = np.triu(z[::-1, np.newaxis] * z[::-1])
rehz = np.triu(z[:, np.newaxis] * z.conj())
rehz_reverse = np.triu(z[::-1, np.newaxis] * z[::-1].conj())
w = np.c_[np.zeros(4), z, np.zeros(4)].ravel()
for p, rtol in zip('sd', [1e-7, 1e-14]):
f = getattr(fblas, p+'syr', None)
if f is None:
continue
assert_allclose(f(1.0, x), resx, rtol=rtol)
assert_allclose(f(1.0, x, lower=True), resx.T, rtol=rtol)
assert_allclose(f(1.0, y, incx=2, offx=2, n=4), resx, rtol=rtol)
# negative increments imply reversed vectors in blas
assert_allclose(f(1.0, y, incx=-2, offx=2, n=4),
resx_reverse, rtol=rtol)
a = np.zeros((4, 4), 'f' if p == 's' else 'd', 'F')
b = f(1.0, x, a=a, overwrite_a=True)
assert_allclose(a, resx, rtol=rtol)
b = f(2.0, x, a=a)
assert_(a is not b)
assert_allclose(b, 3*resx, rtol=rtol)
assert_raises(Exception, f, 1.0, x, incx=0)
assert_raises(Exception, f, 1.0, x, offx=5)
assert_raises(Exception, f, 1.0, x, offx=-2)
assert_raises(Exception, f, 1.0, x, n=-2)
assert_raises(Exception, f, 1.0, x, n=5)
assert_raises(Exception, f, 1.0, x, lower=2)
assert_raises(Exception, f, 1.0, x, a=np.zeros((2, 2), 'd', 'F'))
for p, rtol in zip('cz', [1e-7, 1e-14]):
f = getattr(fblas, p+'syr', None)
if f is None:
continue
assert_allclose(f(1.0, z), resz, rtol=rtol)
assert_allclose(f(1.0, z, lower=True), resz.T, rtol=rtol)
assert_allclose(f(1.0, w, incx=3, offx=1, n=4), resz, rtol=rtol)
# negative increments imply reversed vectors in blas
assert_allclose(f(1.0, w, incx=-3, offx=1, n=4),
resz_reverse, rtol=rtol)
a = np.zeros((4, 4), 'F' if p == 'c' else 'D', 'F')
b = f(1.0, z, a=a, overwrite_a=True)
assert_allclose(a, resz, rtol=rtol)
b = f(2.0, z, a=a)
assert_(a is not b)
assert_allclose(b, 3*resz, rtol=rtol)
assert_raises(Exception, f, 1.0, x, incx=0)
assert_raises(Exception, f, 1.0, x, offx=5)
assert_raises(Exception, f, 1.0, x, offx=-2)
assert_raises(Exception, f, 1.0, x, n=-2)
assert_raises(Exception, f, 1.0, x, n=5)
assert_raises(Exception, f, 1.0, x, lower=2)
assert_raises(Exception, f, 1.0, x, a=np.zeros((2, 2), 'd', 'F'))
for p, rtol in zip('cz', [1e-7, 1e-14]):
f = getattr(fblas, p+'her', None)
if f is None:
continue
assert_allclose(f(1.0, z), rehz, rtol=rtol)
assert_allclose(f(1.0, z, lower=True), rehz.T.conj(), rtol=rtol)
assert_allclose(f(1.0, w, incx=3, offx=1, n=4), rehz, rtol=rtol)
# negative increments imply reversed vectors in blas
assert_allclose(f(1.0, w, incx=-3, offx=1, n=4),
rehz_reverse, rtol=rtol)
a = np.zeros((4, 4), 'F' if p == 'c' else 'D', 'F')
b = f(1.0, z, a=a, overwrite_a=True)
assert_allclose(a, rehz, rtol=rtol)
b = f(2.0, z, a=a)
assert_(a is not b)
assert_allclose(b, 3*rehz, rtol=rtol)
assert_raises(Exception, f, 1.0, x, incx=0)
assert_raises(Exception, f, 1.0, x, offx=5)
assert_raises(Exception, f, 1.0, x, offx=-2)
assert_raises(Exception, f, 1.0, x, n=-2)
assert_raises(Exception, f, 1.0, x, n=5)
assert_raises(Exception, f, 1.0, x, lower=2)
assert_raises(Exception, f, 1.0, x, a=np.zeros((2, 2), 'd', 'F'))
def test_syr2(self):
x = np.arange(1, 5, dtype='d')
y = np.arange(5, 9, dtype='d')
resxy = np.triu(x[:, np.newaxis] * y + y[:, np.newaxis] * x)
resxy_reverse = np.triu(x[::-1, np.newaxis] * y[::-1]
+ y[::-1, np.newaxis] * x[::-1])
q = np.linspace(0, 8.5, 17, endpoint=False)
for p, rtol in zip('sd', [1e-7, 1e-14]):
f = getattr(fblas, p+'syr2', None)
if f is None:
continue
assert_allclose(f(1.0, x, y), resxy, rtol=rtol)
assert_allclose(f(1.0, x, y, n=3), resxy[:3, :3], rtol=rtol)
assert_allclose(f(1.0, x, y, lower=True), resxy.T, rtol=rtol)
assert_allclose(f(1.0, q, q, incx=2, offx=2, incy=2, offy=10),
resxy, rtol=rtol)
assert_allclose(f(1.0, q, q, incx=2, offx=2, incy=2, offy=10, n=3),
resxy[:3, :3], rtol=rtol)
# negative increments imply reversed vectors in blas
assert_allclose(f(1.0, q, q, incx=-2, offx=2, incy=-2, offy=10),
resxy_reverse, rtol=rtol)
a = np.zeros((4, 4), 'f' if p == 's' else 'd', 'F')
b = f(1.0, x, y, a=a, overwrite_a=True)
assert_allclose(a, resxy, rtol=rtol)
b = f(2.0, x, y, a=a)
assert_(a is not b)
assert_allclose(b, 3*resxy, rtol=rtol)
assert_raises(Exception, f, 1.0, x, y, incx=0)
assert_raises(Exception, f, 1.0, x, y, offx=5)
assert_raises(Exception, f, 1.0, x, y, offx=-2)
assert_raises(Exception, f, 1.0, x, y, incy=0)
assert_raises(Exception, f, 1.0, x, y, offy=5)
assert_raises(Exception, f, 1.0, x, y, offy=-2)
assert_raises(Exception, f, 1.0, x, y, n=-2)
assert_raises(Exception, f, 1.0, x, y, n=5)
assert_raises(Exception, f, 1.0, x, y, lower=2)
assert_raises(Exception, f, 1.0, x, y,
a=np.zeros((2, 2), 'd', 'F'))
def test_her2(self):
x = np.arange(1, 9, dtype='d').view('D')
y = np.arange(9, 17, dtype='d').view('D')
resxy = x[:, np.newaxis] * y.conj() + y[:, np.newaxis] * x.conj()
resxy = np.triu(resxy)
resxy_reverse = x[::-1, np.newaxis] * y[::-1].conj()
resxy_reverse += y[::-1, np.newaxis] * x[::-1].conj()
resxy_reverse = np.triu(resxy_reverse)
u = np.c_[np.zeros(4), x, np.zeros(4)].ravel()
v = np.c_[np.zeros(4), y, np.zeros(4)].ravel()
for p, rtol in zip('cz', [1e-7, 1e-14]):
f = getattr(fblas, p+'her2', None)
if f is None:
continue
assert_allclose(f(1.0, x, y), resxy, rtol=rtol)
assert_allclose(f(1.0, x, y, n=3), resxy[:3, :3], rtol=rtol)
assert_allclose(f(1.0, x, y, lower=True), resxy.T.conj(),
rtol=rtol)
assert_allclose(f(1.0, u, v, incx=3, offx=1, incy=3, offy=1),
resxy, rtol=rtol)
assert_allclose(f(1.0, u, v, incx=3, offx=1, incy=3, offy=1, n=3),
resxy[:3, :3], rtol=rtol)
# negative increments imply reversed vectors in blas
assert_allclose(f(1.0, u, v, incx=-3, offx=1, incy=-3, offy=1),
resxy_reverse, rtol=rtol)
a = np.zeros((4, 4), 'F' if p == 'c' else 'D', 'F')
b = f(1.0, x, y, a=a, overwrite_a=True)
assert_allclose(a, resxy, rtol=rtol)
b = f(2.0, x, y, a=a)
assert_(a is not b)
assert_allclose(b, 3*resxy, rtol=rtol)
assert_raises(Exception, f, 1.0, x, y, incx=0)
assert_raises(Exception, f, 1.0, x, y, offx=5)
assert_raises(Exception, f, 1.0, x, y, offx=-2)
assert_raises(Exception, f, 1.0, x, y, incy=0)
assert_raises(Exception, f, 1.0, x, y, offy=5)
assert_raises(Exception, f, 1.0, x, y, offy=-2)
assert_raises(Exception, f, 1.0, x, y, n=-2)
assert_raises(Exception, f, 1.0, x, y, n=5)
assert_raises(Exception, f, 1.0, x, y, lower=2)
assert_raises(Exception, f, 1.0, x, y,
a=np.zeros((2, 2), 'd', 'F'))
def test_gbmv(self):
seed(1234)
for ind, dtype in enumerate(DTYPES):
n = 7
m = 5
kl = 1
ku = 2
# fake a banded matrix via toeplitz
A = toeplitz(append(rand(kl+1), zeros(m-kl-1)),
append(rand(ku+1), zeros(n-ku-1)))
A = A.astype(dtype)
Ab = zeros((kl+ku+1, n), dtype=dtype)
# Form the banded storage
Ab[2, :5] = A[0, 0] # diag
Ab[1, 1:6] = A[0, 1] # sup1
Ab[0, 2:7] = A[0, 2] # sup2
Ab[3, :4] = A[1, 0] # sub1
x = rand(n).astype(dtype)
y = rand(m).astype(dtype)
alpha, beta = dtype(3), dtype(-5)
func, = get_blas_funcs(('gbmv',), dtype=dtype)
y1 = func(m=m, n=n, ku=ku, kl=kl, alpha=alpha, a=Ab,
x=x, y=y, beta=beta)
y2 = alpha * A.dot(x) + beta * y
assert_array_almost_equal(y1, y2)
def test_sbmv_hbmv(self):
seed(1234)
for ind, dtype in enumerate(DTYPES):
n = 6
k = 2
A = zeros((n, n), dtype=dtype)
Ab = zeros((k+1, n), dtype=dtype)
# Form the array and its packed banded storage
A[arange(n), arange(n)] = rand(n)
for ind2 in range(1, k+1):
temp = rand(n-ind2)
A[arange(n-ind2), arange(ind2, n)] = temp
Ab[-1-ind2, ind2:] = temp
A = A.astype(dtype)
A = A + A.T if ind < 2 else A + A.conj().T
Ab[-1, :] = diag(A)
x = rand(n).astype(dtype)
y = rand(n).astype(dtype)
alpha, beta = dtype(1.25), dtype(3)
if ind > 1:
func, = get_blas_funcs(('hbmv',), dtype=dtype)
else:
func, = get_blas_funcs(('sbmv',), dtype=dtype)
y1 = func(k=k, alpha=alpha, a=Ab, x=x, y=y, beta=beta)
y2 = alpha * A.dot(x) + beta * y
assert_array_almost_equal(y1, y2)
def test_spmv_hpmv(self):
seed(1234)
for ind, dtype in enumerate(DTYPES+COMPLEX_DTYPES):
n = 3
A = rand(n, n).astype(dtype)
if ind > 1:
A += rand(n, n)*1j
A = A.astype(dtype)
A = A + A.T if ind < 4 else A + A.conj().T
c, r = tril_indices(n)
Ap = A[r, c]
x = rand(n).astype(dtype)
y = rand(n).astype(dtype)
xlong = arange(2*n).astype(dtype)
ylong = ones(2*n).astype(dtype)
alpha, beta = dtype(1.25), dtype(2)
if ind > 3:
func, = get_blas_funcs(('hpmv',), dtype=dtype)
else:
func, = get_blas_funcs(('spmv',), dtype=dtype)
y1 = func(n=n, alpha=alpha, ap=Ap, x=x, y=y, beta=beta)
y2 = alpha * A.dot(x) + beta * y
assert_array_almost_equal(y1, y2)
# Test inc and offsets
y1 = func(n=n-1, alpha=alpha, beta=beta, x=xlong, y=ylong, ap=Ap,
incx=2, incy=2, offx=n, offy=n)
y2 = (alpha * A[:-1, :-1]).dot(xlong[3::2]) + beta * ylong[3::2]
assert_array_almost_equal(y1[3::2], y2)
assert_almost_equal(y1[4], ylong[4])
def test_spr_hpr(self):
seed(1234)
for ind, dtype in enumerate(DTYPES+COMPLEX_DTYPES):
n = 3
A = rand(n, n).astype(dtype)
if ind > 1:
A += rand(n, n)*1j
A = A.astype(dtype)
A = A + A.T if ind < 4 else A + A.conj().T
c, r = tril_indices(n)
Ap = A[r, c]
x = rand(n).astype(dtype)
alpha = (DTYPES+COMPLEX_DTYPES)[mod(ind, 4)](2.5)
if ind > 3:
func, = get_blas_funcs(('hpr',), dtype=dtype)
y2 = alpha * x[:, None].dot(x[None, :].conj()) + A
else:
func, = get_blas_funcs(('spr',), dtype=dtype)
y2 = alpha * x[:, None].dot(x[None, :]) + A
y1 = func(n=n, alpha=alpha, ap=Ap, x=x)
y1f = zeros((3, 3), dtype=dtype)
y1f[r, c] = y1
y1f[c, r] = y1.conj() if ind > 3 else y1
assert_array_almost_equal(y1f, y2)
def test_spr2_hpr2(self):
seed(1234)
for ind, dtype in enumerate(DTYPES):
n = 3
A = rand(n, n).astype(dtype)
if ind > 1:
A += rand(n, n)*1j
A = A.astype(dtype)
A = A + A.T if ind < 2 else A + A.conj().T
c, r = tril_indices(n)
Ap = A[r, c]
x = rand(n).astype(dtype)
y = rand(n).astype(dtype)
alpha = dtype(2)
if ind > 1:
func, = get_blas_funcs(('hpr2',), dtype=dtype)
else:
func, = get_blas_funcs(('spr2',), dtype=dtype)
u = alpha.conj() * x[:, None].dot(y[None, :].conj())
y2 = A + u + u.conj().T
y1 = func(n=n, alpha=alpha, x=x, y=y, ap=Ap)
y1f = zeros((3, 3), dtype=dtype)
y1f[r, c] = y1
y1f[[1, 2, 2], [0, 0, 1]] = y1[[1, 3, 4]].conj()
assert_array_almost_equal(y1f, y2)
def test_tbmv(self):
seed(1234)
for ind, dtype in enumerate(DTYPES):
n = 10
k = 3
x = rand(n).astype(dtype)
A = zeros((n, n), dtype=dtype)
# Banded upper triangular array
for sup in range(k+1):
A[arange(n-sup), arange(sup, n)] = rand(n-sup)
# Add complex parts for c,z
if ind > 1:
A[nonzero(A)] += 1j * rand((k+1)*n-(k*(k+1)//2)).astype(dtype)
# Form the banded storage
Ab = zeros((k+1, n), dtype=dtype)
for row in range(k+1):
Ab[-row-1, row:] = diag(A, k=row)
func, = get_blas_funcs(('tbmv',), dtype=dtype)
y1 = func(k=k, a=Ab, x=x)
y2 = A.dot(x)
assert_array_almost_equal(y1, y2)
y1 = func(k=k, a=Ab, x=x, diag=1)
A[arange(n), arange(n)] = dtype(1)
y2 = A.dot(x)
assert_array_almost_equal(y1, y2)
y1 = func(k=k, a=Ab, x=x, diag=1, trans=1)
y2 = A.T.dot(x)
assert_array_almost_equal(y1, y2)
y1 = func(k=k, a=Ab, x=x, diag=1, trans=2)
y2 = A.conj().T.dot(x)
assert_array_almost_equal(y1, y2)
def test_tbsv(self):
seed(1234)
for ind, dtype in enumerate(DTYPES):
n = 6
k = 3
x = rand(n).astype(dtype)
A = zeros((n, n), dtype=dtype)
# Banded upper triangular array
for sup in range(k+1):
A[arange(n-sup), arange(sup, n)] = rand(n-sup)
# Add complex parts for c,z
if ind > 1:
A[nonzero(A)] += 1j * rand((k+1)*n-(k*(k+1)//2)).astype(dtype)
# Form the banded storage
Ab = zeros((k+1, n), dtype=dtype)
for row in range(k+1):
Ab[-row-1, row:] = diag(A, k=row)
func, = get_blas_funcs(('tbsv',), dtype=dtype)
y1 = func(k=k, a=Ab, x=x)
y2 = solve(A, x)
assert_array_almost_equal(y1, y2)
y1 = func(k=k, a=Ab, x=x, diag=1)
A[arange(n), arange(n)] = dtype(1)
y2 = solve(A, x)
assert_array_almost_equal(y1, y2)
y1 = func(k=k, a=Ab, x=x, diag=1, trans=1)
y2 = solve(A.T, x)
assert_array_almost_equal(y1, y2)
y1 = func(k=k, a=Ab, x=x, diag=1, trans=2)
y2 = solve(A.conj().T, x)
assert_array_almost_equal(y1, y2)
def test_tpmv(self):
seed(1234)
for ind, dtype in enumerate(DTYPES):
n = 10
x = rand(n).astype(dtype)
# Upper triangular array
A = triu(rand(n, n)) if ind < 2 else triu(rand(n, n)+rand(n, n)*1j)
# Form the packed storage
c, r = tril_indices(n)
Ap = A[r, c]
func, = get_blas_funcs(('tpmv',), dtype=dtype)
y1 = func(n=n, ap=Ap, x=x)
y2 = A.dot(x)
assert_array_almost_equal(y1, y2)
y1 = func(n=n, ap=Ap, x=x, diag=1)
A[arange(n), arange(n)] = dtype(1)
y2 = A.dot(x)
assert_array_almost_equal(y1, y2)
y1 = func(n=n, ap=Ap, x=x, diag=1, trans=1)
y2 = A.T.dot(x)
assert_array_almost_equal(y1, y2)
y1 = func(n=n, ap=Ap, x=x, diag=1, trans=2)
y2 = A.conj().T.dot(x)
assert_array_almost_equal(y1, y2)
def test_tpsv(self):
seed(1234)
for ind, dtype in enumerate(DTYPES):
n = 10
x = rand(n).astype(dtype)
# Upper triangular array
A = triu(rand(n, n)) if ind < 2 else triu(rand(n, n)+rand(n, n)*1j)
A += eye(n)
# Form the packed storage
c, r = tril_indices(n)
Ap = A[r, c]
func, = get_blas_funcs(('tpsv',), dtype=dtype)
y1 = func(n=n, ap=Ap, x=x)
y2 = solve(A, x)
assert_array_almost_equal(y1, y2)
y1 = func(n=n, ap=Ap, x=x, diag=1)
A[arange(n), arange(n)] = dtype(1)
y2 = solve(A, x)
assert_array_almost_equal(y1, y2)
y1 = func(n=n, ap=Ap, x=x, diag=1, trans=1)
y2 = solve(A.T, x)
assert_array_almost_equal(y1, y2)
y1 = func(n=n, ap=Ap, x=x, diag=1, trans=2)
y2 = solve(A.conj().T, x)
assert_array_almost_equal(y1, y2)
def test_trmv(self):
seed(1234)
for ind, dtype in enumerate(DTYPES):
n = 3
A = (rand(n, n)+eye(n)).astype(dtype)
x = rand(3).astype(dtype)
func, = get_blas_funcs(('trmv',), dtype=dtype)
y1 = func(a=A, x=x)
y2 = triu(A).dot(x)
assert_array_almost_equal(y1, y2)
y1 = func(a=A, x=x, diag=1)
A[arange(n), arange(n)] = dtype(1)
y2 = triu(A).dot(x)
assert_array_almost_equal(y1, y2)
y1 = func(a=A, x=x, diag=1, trans=1)
y2 = triu(A).T.dot(x)
assert_array_almost_equal(y1, y2)
y1 = func(a=A, x=x, diag=1, trans=2)
y2 = triu(A).conj().T.dot(x)
assert_array_almost_equal(y1, y2)
def test_trsv(self):
seed(1234)
for ind, dtype in enumerate(DTYPES):
n = 15
A = (rand(n, n)+eye(n)).astype(dtype)
x = rand(n).astype(dtype)
func, = get_blas_funcs(('trsv',), dtype=dtype)
y1 = func(a=A, x=x)
y2 = solve(triu(A), x)
assert_array_almost_equal(y1, y2)
y1 = func(a=A, x=x, lower=1)
y2 = solve(tril(A), x)
assert_array_almost_equal(y1, y2)
y1 = func(a=A, x=x, diag=1)
A[arange(n), arange(n)] = dtype(1)
y2 = solve(triu(A), x)
assert_array_almost_equal(y1, y2)
y1 = func(a=A, x=x, diag=1, trans=1)
y2 = solve(triu(A).T, x)
assert_array_almost_equal(y1, y2)
y1 = func(a=A, x=x, diag=1, trans=2)
y2 = solve(triu(A).conj().T, x)
assert_array_almost_equal(y1, y2)
class TestFBLAS3Simple(object):
def test_gemm(self):
for p in 'sd':
f = getattr(fblas, p+'gemm', None)
if f is None:
continue
assert_array_almost_equal(f(3, [3], [-4]), [[-36]])
assert_array_almost_equal(f(3, [3], [-4], 3, [5]), [-21])
for p in 'cz':
f = getattr(fblas, p+'gemm', None)
if f is None:
continue
assert_array_almost_equal(f(3j, [3-4j], [-4]), [[-48-36j]])
assert_array_almost_equal(f(3j, [3-4j], [-4], 3, [5j]), [-48-21j])
def _get_func(func, ps='sdzc'):
"""Just a helper: return a specified BLAS function w/typecode."""
for p in ps:
f = getattr(fblas, p+func, None)
if f is None:
continue
yield f
class TestBLAS3Symm(object):
def setup_method(self):
self.a = np.array([[1., 2.],
[0., 1.]])
self.b = np.array([[1., 0., 3.],
[0., -1., 2.]])
self.c = np.ones((2, 3))
self.t = np.array([[2., -1., 8.],
[3., 0., 9.]])
def test_symm(self):
for f in _get_func('symm'):
res = f(a=self.a, b=self.b, c=self.c, alpha=1., beta=1.)
assert_array_almost_equal(res, self.t)
res = f(a=self.a.T, b=self.b, lower=1, c=self.c, alpha=1., beta=1.)
assert_array_almost_equal(res, self.t)
res = f(a=self.a, b=self.b.T, side=1, c=self.c.T,
alpha=1., beta=1.)
assert_array_almost_equal(res, self.t.T)
def test_summ_wrong_side(self):
f = getattr(fblas, 'dsymm', None)
if f is not None:
assert_raises(Exception, f, **{'a': self.a, 'b': self.b,
'alpha': 1, 'side': 1})
# `side=1` means C <- B*A, hence shapes of A and B are to be
# compatible. Otherwise, f2py exception is raised
def test_symm_wrong_uplo(self):
"""SYMM only considers the upper/lower part of A. Hence setting
wrong value for `lower` (default is lower=0, meaning upper triangle)
gives a wrong result.
"""
f = getattr(fblas, 'dsymm', None)
if f is not None:
res = f(a=self.a, b=self.b, c=self.c, alpha=1., beta=1.)
assert np.allclose(res, self.t)
res = f(a=self.a, b=self.b, lower=1, c=self.c, alpha=1., beta=1.)
assert not np.allclose(res, self.t)
class TestBLAS3Syrk(object):
def setup_method(self):
self.a = np.array([[1., 0.],
[0., -2.],
[2., 3.]])
self.t = np.array([[1., 0., 2.],
[0., 4., -6.],
[2., -6., 13.]])
self.tt = np.array([[5., 6.],
[6., 13.]])
def test_syrk(self):
for f in _get_func('syrk'):
c = f(a=self.a, alpha=1.)
assert_array_almost_equal(np.triu(c), np.triu(self.t))
c = f(a=self.a, alpha=1., lower=1)
assert_array_almost_equal(np.tril(c), np.tril(self.t))
c0 = np.ones(self.t.shape)
c = f(a=self.a, alpha=1., beta=1., c=c0)
assert_array_almost_equal(np.triu(c), np.triu(self.t+c0))
c = f(a=self.a, alpha=1., trans=1)
assert_array_almost_equal(np.triu(c), np.triu(self.tt))
# prints '0-th dimension must be fixed to 3 but got 5',
# FIXME: suppress?
# FIXME: how to catch the _fblas.error?
def test_syrk_wrong_c(self):
f = getattr(fblas, 'dsyrk', None)
if f is not None:
assert_raises(Exception, f, **{'a': self.a, 'alpha': 1.,
'c': np.ones((5, 8))})
# if C is supplied, it must have compatible dimensions
class TestBLAS3Syr2k(object):
def setup_method(self):
self.a = np.array([[1., 0.],
[0., -2.],
[2., 3.]])
self.b = np.array([[0., 1.],
[1., 0.],
[0, 1.]])
self.t = np.array([[0., -1., 3.],
[-1., 0., 0.],
[3., 0., 6.]])
self.tt = np.array([[0., 1.],
[1., 6]])
def test_syr2k(self):
for f in _get_func('syr2k'):
c = f(a=self.a, b=self.b, alpha=1.)
assert_array_almost_equal(np.triu(c), np.triu(self.t))
c = f(a=self.a, b=self.b, alpha=1., lower=1)
assert_array_almost_equal(np.tril(c), np.tril(self.t))
c0 = np.ones(self.t.shape)
c = f(a=self.a, b=self.b, alpha=1., beta=1., c=c0)
assert_array_almost_equal(np.triu(c), np.triu(self.t+c0))
c = f(a=self.a, b=self.b, alpha=1., trans=1)
assert_array_almost_equal(np.triu(c), np.triu(self.tt))
# prints '0-th dimension must be fixed to 3 but got 5', FIXME: suppress?
def test_syr2k_wrong_c(self):
f = getattr(fblas, 'dsyr2k', None)
if f is not None:
assert_raises(Exception, f, **{'a': self.a,
'b': self.b,
'alpha': 1.,
'c': np.zeros((15, 8))})
# if C is supplied, it must have compatible dimensions
class TestSyHe(object):
"""Quick and simple tests for (zc)-symm, syrk, syr2k."""
def setup_method(self):
self.sigma_y = np.array([[0., -1.j],
[1.j, 0.]])
def test_symm_zc(self):
for f in _get_func('symm', 'zc'):
# NB: a is symmetric w/upper diag of ONLY
res = f(a=self.sigma_y, b=self.sigma_y, alpha=1.)
assert_array_almost_equal(np.triu(res), np.diag([1, -1]))
def test_hemm_zc(self):
for f in _get_func('hemm', 'zc'):
# NB: a is hermitian w/upper diag of ONLY
res = f(a=self.sigma_y, b=self.sigma_y, alpha=1.)
assert_array_almost_equal(np.triu(res), np.diag([1, 1]))
def test_syrk_zr(self):
for f in _get_func('syrk', 'zc'):
res = f(a=self.sigma_y, alpha=1.)
assert_array_almost_equal(np.triu(res), np.diag([-1, -1]))
def test_herk_zr(self):
for f in _get_func('herk', 'zc'):
res = f(a=self.sigma_y, alpha=1.)
assert_array_almost_equal(np.triu(res), np.diag([1, 1]))
def test_syr2k_zr(self):
for f in _get_func('syr2k', 'zc'):
res = f(a=self.sigma_y, b=self.sigma_y, alpha=1.)
assert_array_almost_equal(np.triu(res), 2.*np.diag([-1, -1]))
def test_her2k_zr(self):
for f in _get_func('her2k', 'zc'):
res = f(a=self.sigma_y, b=self.sigma_y, alpha=1.)
assert_array_almost_equal(np.triu(res), 2.*np.diag([1, 1]))
class TestTRMM(object):
"""Quick and simple tests for dtrmm."""
def setup_method(self):
self.a = np.array([[1., 2., ],
[-2., 1.]])
self.b = np.array([[3., 4., -1.],
[5., 6., -2.]])
self.a2 = np.array([[1, 1, 2, 3],
[0, 1, 4, 5],
[0, 0, 1, 6],
[0, 0, 0, 1]], order="f")
self.b2 = np.array([[1, 4], [2, 5], [3, 6], [7, 8], [9, 10]],
order="f")
@pytest.mark.parametrize("dtype_", DTYPES)
def test_side(self, dtype_):
trmm = get_blas_funcs("trmm", dtype=dtype_)
# Provide large A array that works for side=1 but not 0 (see gh-10841)
assert_raises(Exception, trmm, 1.0, self.a2, self.b2)
res = trmm(1.0, self.a2.astype(dtype_), self.b2.astype(dtype_),
side=1)
k = self.b2.shape[1]
assert_allclose(res, self.b2 @ self.a2[:k, :k], rtol=0.,
atol=100*np.finfo(dtype_).eps)
def test_ab(self):
f = getattr(fblas, 'dtrmm', None)
if f is not None:
result = f(1., self.a, self.b)
# default a is upper triangular
expected = np.array([[13., 16., -5.],
[5., 6., -2.]])
assert_array_almost_equal(result, expected)
def test_ab_lower(self):
f = getattr(fblas, 'dtrmm', None)
if f is not None:
result = f(1., self.a, self.b, lower=True)
expected = np.array([[3., 4., -1.],
[-1., -2., 0.]]) # now a is lower triangular
assert_array_almost_equal(result, expected)
def test_b_overwrites(self):
# BLAS dtrmm modifies B argument in-place.
# Here the default is to copy, but this can be overridden
f = getattr(fblas, 'dtrmm', None)
if f is not None:
for overwr in [True, False]:
bcopy = self.b.copy()
result = f(1., self.a, bcopy, overwrite_b=overwr)
# C-contiguous arrays are copied
assert_(bcopy.flags.f_contiguous is False and
np.may_share_memory(bcopy, result) is False)
assert_equal(bcopy, self.b)
bcopy = np.asfortranarray(self.b.copy()) # or just transpose it
result = f(1., self.a, bcopy, overwrite_b=True)
assert_(bcopy.flags.f_contiguous is True and
np.may_share_memory(bcopy, result) is True)
assert_array_almost_equal(bcopy, result)
def test_trsm():
seed(1234)
for ind, dtype in enumerate(DTYPES):
tol = np.finfo(dtype).eps*1000
func, = get_blas_funcs(('trsm',), dtype=dtype)
# Test protection against size mismatches
A = rand(4, 5).astype(dtype)
B = rand(4, 4).astype(dtype)
alpha = dtype(1)
assert_raises(Exception, func, alpha, A, B)
assert_raises(Exception, func, alpha, A.T, B)
n = 8
m = 7
alpha = dtype(-2.5)
A = (rand(m, m) if ind < 2 else rand(m, m) + rand(m, m)*1j) + eye(m)
A = A.astype(dtype)
Au = triu(A)
Al = tril(A)
B1 = rand(m, n).astype(dtype)
B2 = rand(n, m).astype(dtype)
x1 = func(alpha=alpha, a=A, b=B1)
assert_equal(B1.shape, x1.shape)
x2 = solve(Au, alpha*B1)
assert_allclose(x1, x2, atol=tol)
x1 = func(alpha=alpha, a=A, b=B1, trans_a=1)
x2 = solve(Au.T, alpha*B1)
assert_allclose(x1, x2, atol=tol)
x1 = func(alpha=alpha, a=A, b=B1, trans_a=2)
x2 = solve(Au.conj().T, alpha*B1)
assert_allclose(x1, x2, atol=tol)
x1 = func(alpha=alpha, a=A, b=B1, diag=1)
Au[arange(m), arange(m)] = dtype(1)
x2 = solve(Au, alpha*B1)
assert_allclose(x1, x2, atol=tol)
x1 = func(alpha=alpha, a=A, b=B2, diag=1, side=1)
x2 = solve(Au.conj().T, alpha*B2.conj().T)
assert_allclose(x1, x2.conj().T, atol=tol)
x1 = func(alpha=alpha, a=A, b=B2, diag=1, side=1, lower=1)
Al[arange(m), arange(m)] = dtype(1)
x2 = solve(Al.conj().T, alpha*B2.conj().T)
assert_allclose(x1, x2.conj().T, atol=tol)