-
Notifications
You must be signed in to change notification settings - Fork 54
/
test_numpy_funcs.py
1556 lines (1326 loc) · 49.8 KB
/
test_numpy_funcs.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
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
import pytest
from numpy.random import rand, randint
from numpy import isclose
from pyccel.decorators import types
from pyccel.epyccel import epyccel
from conftest import *
# Functions still to be tested:
# full_like
# empty_like
# zeros_like
# ones_like
# array
# # ...
# norm
# int
# real
# imag
# float
# double
# mod
# float32
# float64
# int32
# int64
# complex128
# complex64
# matmul
# sum
# prod
# product
# linspace
# diag
# where
# cross
# # ---
def test_fabs_call():
@types('real')
def fabs_call(x):
from numpy import fabs
return fabs(x)
f1 = epyccel(fabs_call)
x = rand()
assert(isclose(f1(x), fabs_call(x), rtol=1e-15, atol=1e-15))
def test_fabs_phrase():
@types('real','real')
def fabs_phrase(x,y):
from numpy import fabs
a = fabs(x)*fabs(y)
return a
f2 = epyccel(fabs_phrase)
x = rand()
y = rand()
assert(isclose(f2(x,y), fabs_phrase(x,y), rtol=1e-15, atol=1e-15))
@pytest.mark.xfail(reason = "fabs should always return a float")
def test_fabs_return_type():
@types('int')
def fabs_return_type(x):
from numpy import fabs
a = fabs(x)
return a
f1 = epyccel(fabs_return_type)
x = randint(100)
assert(isclose(f1(x), fabs_return_type(x), rtol=1e-15, atol=1e-15))
assert(type(f1(x)) == type(fabs_return_type(x))) # pylint: disable=unidiomatic-typecheck
def test_absolute_call():
@types('real')
def absolute_call(x):
from numpy import absolute
return absolute(x)
f1 = epyccel(absolute_call)
x = rand()
assert(isclose(f1(x), absolute_call(x), rtol=1e-15, atol=1e-15))
def test_absolute_phrase():
@types('real','real')
def absolute_phrase(x,y):
from numpy import absolute
a = absolute(x)*absolute(y)
return a
f2 = epyccel(absolute_phrase)
x = rand()
y = rand()
assert(isclose(f2(x,y), absolute_phrase(x,y), rtol=1e-15, atol=1e-15))
def test_absolute_return_type():
@types('int')
def absolute_return_type(x):
from numpy import absolute
a = absolute(x)
return a
f1 = epyccel(absolute_return_type)
x = randint(100)
assert(isclose(f1(x), absolute_return_type(x), rtol=1e-15, atol=1e-15))
assert(type(f1(x)) == type(absolute_return_type(x).item())) # pylint: disable=unidiomatic-typecheck
def test_sin_call():
@types('real')
def sin_call(x):
from numpy import sin
return sin(x)
f1 = epyccel(sin_call)
x = rand()
assert(isclose(f1(x), sin_call(x), rtol=1e-15, atol=1e-15))
def test_sin_phrase():
@types('real','real')
def sin_phrase(x,y):
from numpy import sin
a = sin(x)+sin(y)
return a
f2 = epyccel(sin_phrase)
x = rand()
y = rand()
assert(isclose(f2(x,y), sin_phrase(x,y), rtol=1e-15, atol=1e-15))
def test_cos_call():
@types('real')
def cos_call(x):
from numpy import cos
return cos(x)
f1 = epyccel(cos_call)
x = rand()
assert(isclose(f1(x), cos_call(x), rtol=1e-15, atol=1e-15))
def test_cos_phrase():
@types('real','real')
def cos_phrase(x,y):
from numpy import cos
a = cos(x)+cos(y)
return a
f2 = epyccel(cos_phrase)
x = rand()
y = rand()
assert(isclose(f2(x,y), cos_phrase(x,y), rtol=1e-15, atol=1e-15))
def test_tan_call():
@types('real')
def tan_call(x):
from numpy import tan
return tan(x)
f1 = epyccel(tan_call)
x = rand()
assert(isclose(f1(x), tan_call(x), rtol=1e-15, atol=1e-15))
def test_tan_phrase():
@types('real','real')
def tan_phrase(x,y):
from numpy import tan
a = tan(x)+tan(y)
return a
f2 = epyccel(tan_phrase)
x = rand()
y = rand()
assert(isclose(f2(x,y), tan_phrase(x,y), rtol=1e-15, atol=1e-15))
def test_exp_call():
@types('real')
def exp_call(x):
from numpy import exp
return exp(x)
f1 = epyccel(exp_call)
x = rand()
assert(isclose(f1(x), exp_call(x), rtol=1e-15, atol=1e-15))
def test_exp_phrase():
@types('real','real')
def exp_phrase(x,y):
from numpy import exp
a = exp(x)+exp(y)
return a
f2 = epyccel(exp_phrase)
x = rand()
y = rand()
assert(isclose(f2(x,y), exp_phrase(x,y), rtol=1e-15, atol=1e-15))
def test_log_call():
@types('real')
def log_call(x):
from numpy import log
return log(x)
f1 = epyccel(log_call)
x = rand()
assert(isclose(f1(x), log_call(x), rtol=1e-15, atol=1e-15))
def test_log_phrase():
@types('real','real')
def log_phrase(x,y):
from numpy import log
a = log(x)+log(y)
return a
f2 = epyccel(log_phrase)
x = rand()
y = rand()
assert(isclose(f2(x,y), log_phrase(x,y), rtol=1e-15, atol=1e-15))
def test_arcsin_call():
@types('real')
def arcsin_call(x):
from numpy import arcsin
return arcsin(x)
f1 = epyccel(arcsin_call)
x = rand()
assert(isclose(f1(x), arcsin_call(x), rtol=1e-15, atol=1e-15))
def test_arcsin_phrase():
@types('real','real')
def arcsin_phrase(x,y):
from numpy import arcsin
a = arcsin(x)+arcsin(y)
return a
f2 = epyccel(arcsin_phrase)
x = rand()
y = rand()
assert(isclose(f2(x,y), arcsin_phrase(x,y), rtol=1e-15, atol=1e-15))
def test_arccos_call():
@types('real')
def arccos_call(x):
from numpy import arccos
return arccos(x)
f1 = epyccel(arccos_call)
x = rand()
assert(isclose(f1(x), arccos_call(x), rtol=1e-15, atol=1e-15))
def test_arccos_phrase():
@types('real','real')
def arccos_phrase(x,y):
from numpy import arccos
a = arccos(x)+arccos(y)
return a
f2 = epyccel(arccos_phrase)
x = rand()
y = rand()
assert(isclose(f2(x,y), arccos_phrase(x,y), rtol=1e-15, atol=1e-15))
def test_arctan_call():
@types('real')
def arctan_call(x):
from numpy import arctan
return arctan(x)
f1 = epyccel(arctan_call)
x = rand()
assert(isclose(f1(x), arctan_call(x), rtol=1e-15, atol=1e-15))
def test_arctan_phrase():
@types('real','real')
def arctan_phrase(x,y):
from numpy import arctan
a = arctan(x)+arctan(y)
return a
f2 = epyccel(arctan_phrase)
x = rand()
y = rand()
assert(isclose(f2(x,y), arctan_phrase(x,y), rtol=1e-15, atol=1e-15))
def test_sinh_call():
@types('real')
def sinh_call(x):
from numpy import sinh
return sinh(x)
f1 = epyccel(sinh_call)
x = rand()
assert(isclose(f1(x), sinh_call(x), rtol=1e-15, atol=1e-15))
def test_sinh_phrase():
@types('real','real')
def sinh_phrase(x,y):
from numpy import sinh
a = sinh(x)+sinh(y)
return a
f2 = epyccel(sinh_phrase)
x = rand()
y = rand()
assert(isclose(f2(x,y), sinh_phrase(x,y), rtol=1e-15, atol=1e-15))
def test_cosh_call():
@types('real')
def cosh_call(x):
from numpy import cosh
return cosh(x)
f1 = epyccel(cosh_call)
x = rand()
assert(isclose(f1(x), cosh_call(x), rtol=1e-15, atol=1e-15))
def test_cosh_phrase():
@types('real','real')
def cosh_phrase(x,y):
from numpy import cosh
a = cosh(x)+cosh(y)
return a
f2 = epyccel(cosh_phrase)
x = rand()
y = rand()
assert(isclose(f2(x,y), cosh_phrase(x,y), rtol=1e-15, atol=1e-15))
def test_tanh_call():
@types('real')
def tanh_call(x):
from numpy import tanh
return tanh(x)
f1 = epyccel(tanh_call)
x = rand()
assert(isclose(f1(x), tanh_call(x), rtol=1e-15, atol=1e-15))
def test_tanh_phrase():
@types('real','real')
def tanh_phrase(x,y):
from numpy import tanh
a = tanh(x)+tanh(y)
return a
f2 = epyccel(tanh_phrase)
x = rand()
y = rand()
assert(isclose(f2(x,y), tanh_phrase(x,y), rtol=1e-15, atol=1e-15))
def test_arctan2_call():
@types('real','real')
def arctan2_call(x,y):
from numpy import arctan2
return arctan2(x,y)
f1 = epyccel(arctan2_call)
x = rand()
y = rand()
assert(isclose(f1(x,y), arctan2_call(x,y), rtol=1e-15, atol=1e-15))
def test_arctan2_phrase():
@types('real','real','real')
def arctan2_phrase(x,y,z):
from numpy import arctan2
a = arctan2(x,y)+arctan2(x,z)
return a
f2 = epyccel(arctan2_phrase)
x = -rand()
y = rand()
z = rand()
assert(isclose(f2(x,y,z), arctan2_phrase(x,y,z), rtol=1e-15, atol=1e-15))
def test_sqrt_call():
@types('real')
def sqrt_call(x):
from numpy import sqrt
return sqrt(x)
f1 = epyccel(sqrt_call)
x = rand()
assert(isclose(f1(x), sqrt_call(x), rtol=1e-15, atol=1e-15))
def test_sqrt_phrase():
@types('real','real')
def sqrt_phrase(x,y):
from numpy import sqrt
a = sqrt(x)*sqrt(y)
return a
f2 = epyccel(sqrt_phrase)
x = rand()
y = rand()
assert(isclose(f2(x,y), sqrt_phrase(x,y), rtol=1e-15, atol=1e-15))
def test_sqrt_return_type():
@types('real')
def sqrt_return_type_real(x):
from numpy import sqrt
a = sqrt(x)
return a
@types('complex')
def sqrt_return_type_comp(x):
from numpy import sqrt
a = sqrt(x)
return a
f1 = epyccel(sqrt_return_type_real)
x = rand()
assert(isclose(f1(x), sqrt_return_type_real(x), rtol=1e-15, atol=1e-15))
assert(type(f1(x)) == type(sqrt_return_type_real(x).item())) # pylint: disable=unidiomatic-typecheck
f1 = epyccel(sqrt_return_type_comp)
x = rand() + 1j * rand()
assert(isclose(f1(x), sqrt_return_type_comp(x), rtol=1e-15, atol=1e-15))
assert(type(f1(x)) == type(sqrt_return_type_comp(x).item())) # pylint: disable=unidiomatic-typecheck
def test_floor_call():
@types('real')
def floor_call(x):
from numpy import floor
return floor(x)
f1 = epyccel(floor_call)
x = rand()
assert(isclose(f1(x), floor_call(x), rtol=1e-15, atol=1e-15))
def test_floor_phrase():
@types('real','real')
def floor_phrase(x,y):
from numpy import floor
a = floor(x)*floor(y)
return a
f2 = epyccel(floor_phrase)
x = rand()
y = rand()
assert(isclose(f2(x,y), floor_phrase(x,y), rtol=1e-15, atol=1e-15))
def test_floor_return_type():
@types('int')
def floor_return_type_int(x):
from numpy import floor
a = floor(x)
return a
@types('real')
def floor_return_type_real(x):
from numpy import floor
a = floor(x)
return a
f1 = epyccel(floor_return_type_int)
x = randint(100)
assert(isclose(f1(x), floor_return_type_int(x), rtol=1e-15, atol=1e-15))
assert(type(f1(x)) == type(floor_return_type_int(x).item())) # pylint: disable=unidiomatic-typecheck
f1 = epyccel(floor_return_type_real)
x = randint(100)
assert(isclose(f1(x), floor_return_type_real(x), rtol=1e-15, atol=1e-15))
assert(type(f1(x)) == type(floor_return_type_real(x).item())) # pylint: disable=unidiomatic-typecheck
def test_shape_indexed():
@types('int[:]')
def test_shape_1d(f):
from numpy import shape
return shape(f)[0]
@types('int[:,:]')
def test_shape_2d(f):
from numpy import shape
a = shape(f)
return a[0], a[1]
from numpy import empty
f1 = epyccel(test_shape_1d)
f2 = epyccel(test_shape_2d)
n1 = randint(20)
n2 = randint(20)
n3 = randint(20)
x1 = empty(n1,dtype = int)
x2 = empty((n2,n3), dtype = int)
assert(f1(x1) == test_shape_1d(x1))
assert(all(isclose(f2(x2), test_shape_2d(x2))))
def test_shape_tuple_output():
@types('int[:]')
def test_shape_1d(f):
from numpy import shape
s = shape(f)
return s[0]
@types('int[:]')
def test_shape_1d_tuple(f):
from numpy import shape
s, = shape(f)
return s
@types('int[:,:]')
def test_shape_2d(f):
from numpy import shape
a, b = shape(f)
return a, b
from numpy import empty
n1 = randint(20)
n2 = randint(20)
n3 = randint(20)
x1 = empty(n1,dtype = int)
x2 = empty((n2,n3), dtype = int)
f1 = epyccel(test_shape_1d)
assert(f1(x1) == test_shape_1d(x1))
f1_t = epyccel(test_shape_1d_tuple)
assert(f1_t(x1) == test_shape_1d_tuple(x1))
f2 = epyccel(test_shape_2d)
assert(f2(x2) == test_shape_2d(x2))
def test_shape_real():
@types('real[:]')
def test_shape_1d(f):
from numpy import shape
b = shape(f)
return b[0]
@types('real[:,:]')
def test_shape_2d(f):
from numpy import shape
a = shape(f)
return a[0], a[1]
from numpy import empty
f1 = epyccel(test_shape_1d)
f2 = epyccel(test_shape_2d)
n1 = randint(20)
n2 = randint(20)
n3 = randint(20)
x1 = empty(n1,dtype = float)
x2 = empty((n2,n3), dtype = float)
assert(f1(x1) == test_shape_1d(x1))
assert(f2(x2) == test_shape_2d(x2))
def test_shape_int():
@types('int[:]')
def test_shape_1d(f):
from numpy import shape
b = shape(f)
return b[0]
@types('int[:,:]')
def test_shape_2d(f):
from numpy import shape
a = shape(f)
return a[0], a[1]
f1 = epyccel(test_shape_1d)
f2 = epyccel(test_shape_2d)
from numpy import empty
n1 = randint(20)
n2 = randint(20)
n3 = randint(20)
x1 = empty(n1,dtype = int)
x2 = empty((n2,n3), dtype = int)
assert(f1(x1) == test_shape_1d(x1))
assert(f2(x2) == test_shape_2d(x2))
def test_shape_bool():
@types('bool[:]')
def test_shape_1d(f):
from numpy import shape
b = shape(f)
return b[0]
@types('bool[:,:]')
def test_shape_2d(f):
from numpy import shape
a = shape(f)
return a[0], a[1]
from numpy import empty
f1 = epyccel(test_shape_1d)
f2 = epyccel(test_shape_2d)
n1 = randint(20)
n2 = randint(20)
n3 = randint(20)
x1 = empty(n1,dtype = bool)
x2 = empty((n2,n3), dtype = bool)
assert(f1(x1) == test_shape_1d(x1))
assert(f2(x2) == test_shape_2d(x2))
def test_full_basic_int():
@types('int')
def create_full_shape_1d(n):
from numpy import full, shape
a = full(n,4)
s = shape(a)
return len(s),s[0]
@types('int')
def create_full_shape_2d(n):
from numpy import full, shape
a = full((n,n),4)
s = shape(a)
return len(s),s[0], s[1]
@types('int')
def create_full_val(val):
from numpy import full
a = full(3,val)
return a[0],a[1],a[2]
@types('int')
def create_full_arg_names(val):
from numpy import full
a = full(fill_value = val, shape = (2,3))
return a[0,0],a[0,1],a[0,2],a[1,0],a[1,1],a[1,2]
size = randint(10)
f_shape_1d = epyccel(create_full_shape_1d)
assert(f_shape_1d(size) == create_full_shape_1d(size))
f_shape_2d = epyccel(create_full_shape_2d)
assert(f_shape_2d(size) == create_full_shape_2d(size))
f_val = epyccel(create_full_val)
assert(f_val(size) == create_full_val(size))
assert(type(f_val(size)[0]) == type(create_full_val(size)[0].item())) # pylint: disable=unidiomatic-typecheck
f_arg_names = epyccel(create_full_arg_names)
assert(f_arg_names(size) == create_full_arg_names(size))
assert(type(f_arg_names(size)[0]) == type(create_full_arg_names(size)[0].item())) # pylint: disable=unidiomatic-typecheck
def test_full_basic_real():
@types('int')
def create_full_shape_1d(n):
from numpy import full, shape
a = full(n,4)
s = shape(a)
return len(s),s[0]
@types('int')
def create_full_shape_2d(n):
from numpy import full, shape
a = full((n,n),4)
s = shape(a)
return len(s),s[0], s[1]
@types('real')
def create_full_val(val):
from numpy import full
a = full(3,val)
return a[0],a[1],a[2]
@types('real')
def create_full_arg_names(val):
from numpy import full
a = full(fill_value = val, shape = (2,3))
return a[0,0],a[0,1],a[0,2],a[1,0],a[1,1],a[1,2]
size = randint(10)
val = rand()*5
f_shape_1d = epyccel(create_full_shape_1d)
assert(f_shape_1d(size) == create_full_shape_1d(size))
f_shape_2d = epyccel(create_full_shape_2d)
assert(f_shape_2d(size) == create_full_shape_2d(size))
f_val = epyccel(create_full_val)
assert(f_val(val) == create_full_val(val))
assert(type(f_val(val)[0]) == type(create_full_val(val)[0].item())) # pylint: disable=unidiomatic-typecheck
f_arg_names = epyccel(create_full_arg_names)
assert(f_arg_names(val) == create_full_arg_names(val))
assert(type(f_arg_names(val)[0]) == type(create_full_arg_names(val)[0].item())) # pylint: disable=unidiomatic-typecheck
@pytest.mark.xfail(reason = "f2py converts bools to int")
def test_full_basic_bool():
@types('int')
def create_full_shape_1d(n):
from numpy import full, shape
a = full(n,4)
s = shape(a)
return len(s),s[0]
@types('int')
def create_full_shape_2d(n):
from numpy import full, shape
a = full((n,n),4)
s = shape(a)
return len(s),s[0], s[1]
@types('bool')
def create_full_val(val):
from numpy import full
a = full(3,val)
return a[0],a[1],a[2]
@types('bool')
def create_full_arg_names(val):
from numpy import full
a = full(fill_value = val, shape = (2,3))
return a[0,0],a[0,1],a[0,2],a[1,0],a[1,1],a[1,2]
size = randint(10)
val = bool(randint(2))
f_shape_1d = epyccel(create_full_shape_1d)
assert(f_shape_1d(size) == create_full_shape_1d(size))
f_shape_2d = epyccel(create_full_shape_2d)
assert(f_shape_2d(size) == create_full_shape_2d(size))
f_val = epyccel(create_full_val)
assert(f_val(val) == create_full_val(val))
assert(type(f_val(val)[0]) == type(create_full_val(val)[0])) # pylint: disable=unidiomatic-typecheck
f_arg_names = epyccel(create_full_arg_names)
assert(f_arg_names(val) == create_full_arg_names(val))
assert(type(f_arg_names(val)[0]) == type(create_full_arg_names(val)[0])) # pylint: disable=unidiomatic-typecheck
def test_full_order():
@types('int','int')
def create_full_shape_C(n,m):
from numpy import full, shape
a = full((n,m),4, order = 'C')
s = shape(a)
return len(s),s[0], s[1]
@types('int','int')
def create_full_shape_F(n,m):
from numpy import full, shape
a = full((n,m),4, order = 'F')
s = shape(a)
return len(s),s[0], s[1]
size_1 = randint(10)
size_2 = randint(10)
f_shape_C = epyccel(create_full_shape_C)
assert(f_shape_C(size_1,size_2) == create_full_shape_C(size_1,size_2))
f_shape_F = epyccel(create_full_shape_F)
assert(f_shape_F(size_1,size_2) == create_full_shape_F(size_1,size_2))
def test_full_dtype():
@types('int')
def create_full_val_int_int(val):
from numpy import full
a = full(3,val,int)
return a[0]
@types('int')
def create_full_val_int_float(val):
from numpy import full
a = full(3,val,float)
return a[0]
@types('int')
def create_full_val_int_complex(val):
from numpy import full
a = full(3,val,complex)
return a[0]
@types('real')
def create_full_val_real_int32(val):
from numpy import full, int32
a = full(3,val,int32)
return a[0]
@types('real')
def create_full_val_real_float32(val):
from numpy import full, float32
a = full(3,val,float32)
return a[0]
@types('real')
def create_full_val_real_float64(val):
from numpy import full, float64
a = full(3,val,float64)
return a[0]
@types('real')
def create_full_val_real_complex64(val):
from numpy import full, complex64
a = full(3,val,complex64)
return a[0]
@types('real')
def create_full_val_real_complex128(val):
from numpy import full, complex128
a = full(3,val,complex128)
return a[0]
val_int = randint(100)
val_float = rand()*100
f_int_int = epyccel(create_full_val_int_int)
assert( f_int_int(val_int) == create_full_val_int_int(val_int))
assert(type(f_int_int(val_int)) == type(create_full_val_int_int(val_int).item())) # pylint: disable=unidiomatic-typecheck
f_int_float = epyccel(create_full_val_int_float)
assert(isclose( f_int_float(val_int) , create_full_val_int_float(val_int), rtol=1e-15, atol=1e-15))
assert(type(f_int_float(val_int)) == type(create_full_val_int_float(val_int).item())) # pylint: disable=unidiomatic-typecheck
f_int_complex = epyccel(create_full_val_int_complex)
assert(isclose( f_int_complex(val_int) , create_full_val_int_complex(val_int), rtol=1e-15, atol=1e-15))
assert(type(f_int_complex(val_int)) == type(create_full_val_int_complex(val_int).item())) # pylint: disable=unidiomatic-typecheck
f_real_int32 = epyccel(create_full_val_real_int32)
assert( f_real_int32(val_float) == create_full_val_real_int32(val_float))
assert(type(f_real_int32(val_float)) == type(create_full_val_real_int32(val_float).item())) # pylint: disable=unidiomatic-typecheck
f_real_float32 = epyccel(create_full_val_real_float32)
assert(isclose( f_real_float32(val_float) , create_full_val_real_float32(val_float), rtol=1e-15, atol=1e-15))
assert(type(f_real_float32(val_float)) == type(create_full_val_real_float32(val_float).item())) # pylint: disable=unidiomatic-typecheck
f_real_float64 = epyccel(create_full_val_real_float64)
assert(isclose( f_real_float64(val_float) , create_full_val_real_float64(val_float), rtol=1e-15, atol=1e-15))
assert(type(f_real_float64(val_float)) == type(create_full_val_real_float64(val_float).item())) # pylint: disable=unidiomatic-typecheck
f_real_complex64 = epyccel(create_full_val_real_complex64)
assert(isclose( f_real_complex64(val_float) , create_full_val_real_complex64(val_float), rtol=1e-15, atol=1e-15))
assert(type(f_real_complex64(val_float)) == type(create_full_val_real_complex64(val_float).item())) # pylint: disable=unidiomatic-typecheck
f_real_complex128 = epyccel(create_full_val_real_complex128)
assert(isclose( f_real_complex128(val_float) , create_full_val_real_complex128(val_float), rtol=1e-15, atol=1e-15))
assert(type(f_real_complex128(val_float)) == type(create_full_val_real_complex128(val_float).item())) # pylint: disable=unidiomatic-typecheck
def test_full_combined_args():
def create_full_1_shape():
from numpy import full, shape
a = full((2,1),4.0,int,'F')
s = shape(a)
return len(s),s[0],s[1]
def create_full_1_val():
from numpy import full
a = full((2,1),4.0,int,'F')
return a[0,0]
def create_full_2_shape():
from numpy import full, shape
a = full((4,2),dtype=float,fill_value=1)
s = shape(a)
return len(s),s[0],s[1]
def create_full_2_val():
from numpy import full
a = full((4,2),dtype=float,fill_value=1)
return a[0,0]
def create_full_3_shape():
from numpy import full, shape
a = full(order = 'F', shape = (4,2),dtype=complex,fill_value=1)
s = shape(a)
return len(s),s[0],s[1]
def create_full_3_val():
from numpy import full
a = full(order = 'F', shape = (4,2),dtype=complex,fill_value=1)
return a[0,0]
f1_shape = epyccel(create_full_1_shape)
f1_val = epyccel(create_full_1_val)
assert(f1_shape() == create_full_1_shape())
assert(f1_val() == create_full_1_val() )
assert(type(f1_val()) == type(create_full_1_val().item())) # pylint: disable=unidiomatic-typecheck
f2_shape = epyccel(create_full_2_shape)
f2_val = epyccel(create_full_2_val)
assert(f2_shape() == create_full_2_shape() )
assert(isclose(f2_val() , create_full_2_val() , rtol=1e-15, atol=1e-15))
assert(type(f2_val()) == type(create_full_2_val().item())) # pylint: disable=unidiomatic-typecheck
f3_shape = epyccel(create_full_3_shape)
f3_val = epyccel(create_full_3_val)
assert( f3_shape() == create_full_3_shape() )
assert(isclose( f3_val() , create_full_3_val() , rtol=1e-15, atol=1e-15))
assert(type(f3_val()) == type(create_full_3_val().item())) # pylint: disable=unidiomatic-typecheck
def test_empty_basic():
@types('int')
def create_empty_shape_1d(n):
from numpy import empty, shape
a = empty(n)
s = shape(a)
return len(s),s[0]
@types('int')
def create_empty_shape_2d(n):
from numpy import empty, shape
a = empty((n,n))
s = shape(a)
return len(s),s[0], s[1]
size = randint(10)
f_shape_1d = epyccel(create_empty_shape_1d)
assert( f_shape_1d(size) == create_empty_shape_1d(size))
f_shape_2d = epyccel(create_empty_shape_2d)
assert( f_shape_2d(size) == create_empty_shape_2d(size))
def test_empty_order():
@types('int','int')
def create_empty_shape_C(n,m):
from numpy import empty, shape
a = empty((n,m), order = 'C')
s = shape(a)
return len(s),s[0], s[1]
@types('int','int')
def create_empty_shape_F(n,m):
from numpy import empty, shape
a = empty((n,m), order = 'F')
s = shape(a)
return len(s),s[0], s[1]
size_1 = randint(10)
size_2 = randint(10)
f_shape_C = epyccel(create_empty_shape_C)
assert( f_shape_C(size_1,size_2) == create_empty_shape_C(size_1,size_2))
f_shape_F = epyccel(create_empty_shape_F)
assert( f_shape_F(size_1,size_2) == create_empty_shape_F(size_1,size_2))
def test_empty_dtype():
def create_empty_val_int():
from numpy import empty
a = empty(3,int)
return a[0]
def create_empty_val_float():
from numpy import empty
a = empty(3,float)
return a[0]
def create_empty_val_complex():
from numpy import empty
a = empty(3,complex)
return a[0]
def create_empty_val_int32():
from numpy import empty, int32
a = empty(3,int32)
return a[0]
def create_empty_val_float32():
from numpy import empty, float32
a = empty(3,float32)
return a[0]
def create_empty_val_float64():
from numpy import empty, float64
a = empty(3,float64)
return a[0]
def create_empty_val_complex64():
from numpy import empty, complex64
a = empty(3,complex64)
return a[0]
def create_empty_val_complex128():
from numpy import empty, complex128
a = empty(3,complex128)
return a[0]
f_int_int = epyccel(create_empty_val_int)
assert(type(f_int_int()) == type(create_empty_val_int().item())) # pylint: disable=unidiomatic-typecheck
f_int_float = epyccel(create_empty_val_float)
assert(type(f_int_float()) == type(create_empty_val_float().item())) # pylint: disable=unidiomatic-typecheck
f_int_complex = epyccel(create_empty_val_complex)
assert(type(f_int_complex()) == type(create_empty_val_complex().item())) # pylint: disable=unidiomatic-typecheck
f_real_int32 = epyccel(create_empty_val_int32)
assert(type(f_real_int32()) == type(create_empty_val_int32().item())) # pylint: disable=unidiomatic-typecheck
f_real_float32 = epyccel(create_empty_val_float32)
assert(type(f_real_float32()) == type(create_empty_val_float32().item())) # pylint: disable=unidiomatic-typecheck
f_real_float64 = epyccel(create_empty_val_float64)
assert(type(f_real_float64()) == type(create_empty_val_float64().item())) # pylint: disable=unidiomatic-typecheck
f_real_complex64 = epyccel(create_empty_val_complex64)
assert(type(f_real_complex64()) == type(create_empty_val_complex64().item())) # pylint: disable=unidiomatic-typecheck
f_real_complex128 = epyccel(create_empty_val_complex128)
assert(type(f_real_complex128()) == type(create_empty_val_complex128().item())) # pylint: disable=unidiomatic-typecheck
def test_empty_combined_args():
def create_empty_1_shape():
from numpy import empty, shape
a = empty((2,1),int,'F')
s = shape(a)
return len(s),s[0],s[1]
def create_empty_1_val():
from numpy import empty
a = empty((2,1),int,'F')
return a[0,0]
def create_empty_2_shape():
from numpy import empty, shape
a = empty((4,2),dtype=float)
s = shape(a)
return len(s),s[0],s[1]
def create_empty_2_val():
from numpy import empty
a = empty((4,2),dtype=float)
return a[0,0]
def create_empty_3_shape():
from numpy import empty, shape
a = empty(order = 'F', shape = (4,2),dtype=complex)
s = shape(a)
return len(s),s[0],s[1]
def create_empty_3_val():
from numpy import empty
a = empty(order = 'F', shape = (4,2),dtype=complex)
return a[0,0]
f1_shape = epyccel(create_empty_1_shape)
f1_val = epyccel(create_empty_1_val)
assert( f1_shape() == create_empty_1_shape() )
assert(type(f1_val()) == type(create_empty_1_val().item())) # pylint: disable=unidiomatic-typecheck