-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
arrayops.jl
2652 lines (2359 loc) · 87.3 KB
/
arrayops.jl
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
# This file is a part of Julia. License is MIT: https://julialang.org/license
# Array test
isdefined(Main, :OffsetArrays) || @eval Main include("testhelpers/OffsetArrays.jl")
using .Main.OffsetArrays
using SparseArrays
using Random, LinearAlgebra
using Dates
@testset "basics" begin
@test length([1, 2, 3]) == 3
@test count(!iszero, [1, 2, 3]) == 3
let a = fill(1., 4), b = a+a, c = a-a, d = a+a+a
@test b[1] === 2. && b[2] === 2. && b[3] === 2. && b[4] === 2.
@test c[1] === 0. && c[2] === 0. && c[3] === 0. && c[4] === 0.
@test d[1] === 3. && d[2] === 3. && d[3] === 3. && d[4] === 3.
end
@test length((1,)) == 1
@test length((1,2)) == 2
@test isequal(1 .+ [1,2,3], [2,3,4])
@test isequal([1,2,3] .+ 1, [2,3,4])
@test isequal(1 .- [1,2,3], [0,-1,-2])
@test isequal([1,2,3] .- 1, [0,1,2])
@test isequal(5*[1,2,3], [5,10,15])
@test isequal([1,2,3]*5, [5,10,15])
@test isequal(1 ./ [1,2,5], [1.0,0.5,0.2])
@test isequal([1,2,3]/5, [0.2,0.4,0.6])
@test isequal(2 .% [1,2,3], [0,0,2])
@test isequal([1,2,3] .% 2, [1,0,1])
@test isequal(2 .÷ [1,2,3], [2,1,0])
@test isequal([1,2,3] .÷ 2, [0,1,1])
@test isequal(-2 .% [1,2,3], [0,0,-2])
@test isequal([-1,-2,-3].%2, [-1,0,-1])
@test isequal(-2 .÷ [1,2,3], [-2,-1,0])
@test isequal([-1,-2,-3] .÷ 2, [0,-1,-1])
@test isequal(1 .<< [1,2,5], [2,4,32])
@test isequal(128 .>> [1,2,5], [64,32,4])
@test isequal(2 .>> 1, 1)
@test isequal(1 .<< 1, 2)
@test isequal([1,2,5] .<< [1,2,5], [2,8,160])
@test isequal([10,20,50] .>> [1,2,5], [5,5,1])
a = fill(1.,2,2)
a[1,1] = 1
a[1,2] = 2
a[2,1] = 3
a[2,2] = 4
b = copy(a')
@test a[1,1] == 1. && a[1,2] == 2. && a[2,1] == 3. && a[2,2] == 4.
@test b[1,1] == 1. && b[2,1] == 2. && b[1,2] == 3. && b[2,2] == 4.
a[[1 2 3 4]] .= 0
@test a == zeros(2,2)
a[[1 2], [1 2]] .= 1
@test a == fill(1.,2,2)
a[[1 2], 1] .= 0
@test a[1,1] == 0. && a[1,2] == 1. && a[2,1] == 0. && a[2,2] == 1.
a[:, [1 2]] .= 2
@test a == fill(2.,2,2)
a = Array{Float64}(undef, 2, 2, 2, 2, 2)
a[1,1,1,1,1] = 10
a[1,2,1,1,2] = 20
a[1,1,2,2,1] = 30
@test a[1,1,1,1,1] == 10
@test a[1,2,1,1,2] == 20
@test a[1,1,2,2,1] == 30
b = reshape(a, (32,))
@test b[1] == 10
@test b[19] == 20
@test b[13] == 30
@test_throws DimensionMismatch reshape(b,(5,7))
@test_throws DimensionMismatch reshape(b,(35,))
@test_throws ArgumentError reinterpret(Any, b)
c = ["hello", "world"]
@test_throws ArgumentError reinterpret(Float32, c)
@test_throws ArgumentError resize!(Float64[], -2)
b = rand(32)
a = reshape(b, (2, 2, 2, 2, 2))
@test ndims(a) == 5
@test a[2,1,2,2,1] == b[14]
@test a[2,2,2,2,2] == b[end]
@test_throws DimensionMismatch reshape(1:3, 4)
# issue #23107
a = [1,2,3]
@test typeof(a)(a) !== a
@test Array(a) !== a
@test Array{eltype(a)}(a) !== a
@test Vector(a) !== a
end
@testset "reshaping SubArrays" begin
a = Array(reshape(1:5, 1, 5))
@testset "linearfast" begin
s = view(a, :, 2:4)
r = reshape(s, (length(s),))
@test length(r) == 3
@test r[1] == 2
@test r[3,1] == 4
@test r[Base.ReshapedIndex(CartesianIndex((1,2)))] == 3
@test parent(reshape(r, (1,3))) === r.parent === s
@test parentindices(r) == (1:1, 1:3)
@test reshape(r, (3,)) === r
@test convert(Array{Int,1}, r) == [2,3,4]
@test_throws MethodError convert(Array{Int,2}, r)
@test convert(Array{Int}, r) == [2,3,4]
@test Base.unsafe_convert(Ptr{Int}, r) == Base.unsafe_convert(Ptr{Int}, s)
@test isa(r, StridedArray) # issue #22411
end
@testset "linearslow" begin
s = view(a, :, [2,3,5])
r = reshape(s, length(s))
@test length(r) == 3
@test r[1] == 2
@test r[3,1] == 5
@test r[Base.ReshapedIndex(CartesianIndex((1,2)))] == 3
@test parent(reshape(r, (1,3))) === r.parent === s
@test parentindices(r) == (1:1, 1:3)
@test reshape(r, (3,)) === r
@test convert(Array{Int,1}, r) == [2,3,5]
@test_throws MethodError convert(Array{Int,2}, r)
@test convert(Array{Int}, r) == [2,3,5]
@test_throws ErrorException Base.unsafe_convert(Ptr{Int}, r)
r[2] = -1
@test a[3] == -1
a = zeros(0, 5) # an empty linearslow array
s = view(a, :, [2,3,5])
@test length(reshape(s, length(s))) == 0
end
end
@testset "reshape(a, Val(N))" begin
a = fill(1,3,3)
s = view(a, 1:2, 1:2)
for N in (1,3)
@test isa(reshape(a, Val(N)), Array{Int,N})
@test isa(reshape(s, Val(N)), Base.ReshapedArray{Int,N})
end
end
@testset "zero-dimensional reshapes" begin
@test reshape([1]) == fill(1)
@test reshape([1]) isa Array{Int,0}
@test reshape(1:1) == fill(1)
@test reshape(1:1) isa Base.ReshapedArray{Int, 0}
@test reshape([1], Val(0)) == fill(1)
@test reshape([1], Val(0)) isa Array{Int,0}
@test reshape(1:1, Val(0)) == fill(1)
@test reshape(1:1, Val(0)) isa Base.ReshapedArray{Int, 0}
@test_throws DimensionMismatch reshape([1,2])
@test_throws DimensionMismatch reshape([1,2], Val(0))
@test_throws DimensionMismatch reshape(1:2)
@test_throws DimensionMismatch reshape(1:2, Val(0))
end
@testset "sizehint!" begin
A = zeros(40)
resize!(A, 1)
sizehint!(A, 1)
@test length(A) == 1
A = zeros(40)
resize!(A, 20)
sizehint!(A, 1)
@test length(A) == 20
A = [1:10;]
sizehint!(A, 20)
@test length(A) == length([1:10;])
@test A == [1:10;]
A = [1:10;]
resize!(A, 5)
sizehint!(A, 5)
@test length(A) == 5
@test A == [1:5;]
end
@testset "reshape with colon" begin
# Reshape with an omitted dimension
let A = range(1, stop=60, length=60)
@test size(reshape(A, :)) == (60,)
@test size(reshape(A, :, 1)) == (60, 1)
@test size(reshape(A, (:, 2))) == (30, 2)
@test size(reshape(A, 3, :)) == (3, 20)
@test size(reshape(A, 2, 3, :)) == (2, 3, 10)
@test size(reshape(A, (2, :, 5))) == (2, 6, 5)
@test_throws DimensionMismatch reshape(A, 7, :)
@test_throws DimensionMismatch reshape(A, :, 2, 3, 4)
@test_throws DimensionMismatch reshape(A, (:, :))
B = rand(2,2,2,2)
@test size(reshape(B, :)) == (16,)
@test size(reshape(B, :, 4)) == (4, 4)
@test size(reshape(B, (2, 1, :))) == (2, 1, 8)
@test_throws DimensionMismatch reshape(B, 3, :)
@test_throws DimensionMismatch reshape(B, :, :, 2, 2)
end
end
struct Z26163 <: AbstractArray{Int,0}; end
Base.size(::Z26163) = ()
Base.getindex(::Z26163) = 0
struct V26163 <: AbstractArray{Int,1}; end
Base.size(::V26163) = (1,)
Base.getindex(::V26163, ::Int) = 0
@testset "reshape of custom zero- and one-dimensional arrays" begin
z = Z26163()
v = V26163()
@test z == reshape(v, ()) == fill(0, ())
@test reshape(z, 1) == v == [0]
@test reshape(z, 1, 1) == reshape(v, 1, 1) == fill(0, 1, 1)
@test occursin("1-element reshape", summary(reshape(z, 1)))
@test occursin("0-dimensional reshape", summary(reshape(v, ())))
end
@test reshape(1:5, (5,)) === 1:5
@test reshape(1:5, 5) === 1:5
@testset "setindex! on a reshaped range" begin
a = reshape(1:20, 5, 4)
for idx in ((3,), (2,2), (Base.ReshapedIndex(1),))
try
a[idx...] = 7
error("wrong error")
catch err
@test err.msg == "indexed assignment fails for a reshaped range; consider calling collect"
end
end
end
@testset "reshape isbitsunion Arrays (issue #28611)" begin
v = Union{Float64,Missing}[]
for i in 1:10 push!(v, i) end
v[5] = missing
a = @inferred(reshape(v, 2, 5))
for (I, i) in zip(CartesianIndices((2, 5)), 1:10)
@test a[I] === v[i]
end
ac = copy(a)
@test ac isa Array{Union{Float64, Missing}, 2}
b = @inferred(reshape(ac, 5, 2))
for (I, J) in zip(CartesianIndices((2, 5)), CartesianIndices((5, 2)))
@test ac[I] === b[J]
end
for T in (Any, Union{Float64,String})
a = Array{T}(undef, 4)
@test @inferred(reshape(a, (2, 2))) isa Array{T,2}
a = Array{T}(undef, 4, 1)
@test @inferred(reshape(a, (2, 2))) isa Array{T,2}
end
end
@testset "conversion from ReshapedArray to Array (#18262)" begin
a = Base.ReshapedArray(1:3, (3, 1), ())
@test convert(Array, a) == a
@test convert(Array{Int}, a) == a
@test convert(Array{Float64}, a) == a
@test convert(Matrix, a) == a
@test convert(Matrix{Int}, a) == a
@test convert(Matrix{Float64}, a) == a
b = Base.ReshapedArray(1:3, (3,), ())
@test convert(Array, b) == b
@test convert(Array{Int}, b) == b
@test convert(Array{Float64}, b) == b
@test convert(Vector, b) == b
@test convert(Vector{Int}, b) == b
@test convert(Vector{Float64}, b) == b
end
@testset "operations with IndexLinear ReshapedArray" begin
b = Vector(1:12)
a = Base.ReshapedArray(b, (4,3), ())
@test a[3,2] == 7
@test a[6] == 6
a[3,2] = -2
a[6] = -3
a[Base.ReshapedIndex(5)] = -4
@test b[5] == -4
@test b[6] == -3
@test b[7] == -2
b = reinterpret(Int, a)
b[1] = -1
@test vec(b) == vec(a)
a = rand(1, 1, 8, 8, 1)
@test @inferred(dropdims(a, dims=1)) == @inferred(dropdims(a, dims=(1,))) == reshape(a, (1, 8, 8, 1))
@test @inferred(dropdims(a, dims=(1, 5))) == dropdims(a, dims=(5, 1)) == reshape(a, (1, 8, 8))
@test @inferred(dropdims(a, dims=(1, 2, 5))) == dropdims(a, dims=(5, 2, 1)) == reshape(a, (8, 8))
@test_throws UndefKeywordError dropdims(a)
@test_throws ArgumentError dropdims(a, dims=0)
@test_throws ArgumentError dropdims(a, dims=(1, 1))
@test_throws ArgumentError dropdims(a, dims=(1, 2, 1))
@test_throws ArgumentError dropdims(a, dims=(1, 1, 2))
@test_throws ArgumentError dropdims(a, dims=3)
@test_throws ArgumentError dropdims(a, dims=4)
@test_throws ArgumentError dropdims(a, dims=6)
sz = (5,8,7)
A = reshape(1:prod(sz),sz...)
@test A[2:6] == [2:6;]
@test A[1:3,2,2:4] == cat(46:48,86:88,126:128; dims=2)
@test A[:,7:-3:1,5] == [191 176 161; 192 177 162; 193 178 163; 194 179 164; 195 180 165]
@test reshape(A, Val(2))[:,3:9] == reshape(11:45,5,7)
rng = (2,2:3,2:2:5)
tmp = zeros(Int,map(maximum,rng)...)
tmp[rng...] = A[rng...]
@test tmp == cat(zeros(Int,2,3),[0 0 0; 0 47 52],zeros(Int,2,3),[0 0 0; 0 127 132]; dims=3)
@test cat(1,2,3.,4.,5.; dims=[1,2]) == diagm(0 => [1,2,3.,4.,5.])
blk = [1 2;3 4]
tmp = cat(blk,blk; dims=[1,3])
@test tmp[1:2,1:2,1] == blk
@test tmp[1:2,1:2,2] == zero(blk)
@test tmp[3:4,1:2,1] == zero(blk)
@test tmp[3:4,1:2,2] == blk
x = rand(2,2)
b = x[1,:]
@test isequal(size(b), (2,))
b = x[:,1]
@test isequal(size(b), (2,))
x = rand(5,5)
b = x[2:3,2]
@test b[1] == x[2,2] && b[2] == x[3,2]
B = zeros(4,5)
B[:,3] = 1:4
@test B == [0 0 1 0 0; 0 0 2 0 0; 0 0 3 0 0; 0 0 4 0 0]
B[2,:] = 11:15
@test B == [0 0 1 0 0; 11 12 13 14 15; 0 0 3 0 0; 0 0 4 0 0]
B[[3,1],[2,4]] = [21 22; 23 24]
@test B == [0 23 1 24 0; 11 12 13 14 15; 0 21 3 22 0; 0 0 4 0 0]
B[4,[2,3]] .= 7
@test B == [0 23 1 24 0; 11 12 13 14 15; 0 21 3 22 0; 0 7 7 0 0]
@test isequal(reshape(reshape(1:27, 3, 3, 3), Val(2))[1,:], [1, 4, 7, 10, 13, 16, 19, 22, 25])
end
@testset "find(in(b), a)" begin
# unsorted inputs
a = [3, 5, -7, 6]
b = [4, 6, 2, -7, 1]
@test findall(in(b), a) == [3,4]
@test findall(in(Int[]), a) == Int[]
@test findall(in(a), Int[]) == Int[]
@test findall(in(b), reshape(a, 2, 2)) == [CartesianIndex(1, 2), CartesianIndex(2, 2)]
# sorted inputs
a = [1,2,3,4,5,10]
b = [2,3,4,6]
@test findall(in(b), a) == [2,3,4]
@test findall(in(a), b) == [1,2,3]
@test findall(in(Int[]), a) == Int[]
@test findall(in(a), Int[]) == Int[]
@test findall(in(b), reshape(a, 3, 2)) ==
[CartesianIndex(2, 1), CartesianIndex(3, 1), CartesianIndex(1, 2)]
@test findall(in(a), reshape(b, 2, 2)) ==
[CartesianIndex(1, 1), CartesianIndex(2, 1), CartesianIndex(1, 2)]
a = Vector(1:3:15)
b = Vector(2:4:10)
@test findall(in(b), a) == [4]
@test findall(in(b), [a[1:4]; a[4:end]]) == [4,5]
@test findall(in(NaN), [1.0, NaN, 2.0]) == [2]
@test findall(in(NaN), [1.0, 2.0, NaN]) == [3]
@testset "findall(in(x), b) for uncomparable element types" begin
a = [1 + 1im, 1 - 1im]
@test findall(in(1 + 1im), a) == [1]
@test findall(in(a), a) == [1,2]
end
@test findall(in([1, 2]), 2) == [1]
@test findall(in([1, 2]), 3) == []
@test sort(findall(Dict(1=>false, 2=>true, 3=>true))) == [2, 3]
@test findall(true) == [1]
@test findall(false) == Int[]
@test findall(isodd, 1) == [1]
@test findall(isodd, 2) == Int[]
end
@testset "setindex! return type" begin
rt = Base.return_types(setindex!, Tuple{Array{Int32, 3}, Vector{UInt8}, Vector{Int}, Int16, UnitRange{Int}})
@test length(rt) == 1 && rt[1] === Array{Int32, 3}
end
@testset "construction" begin
@test typeof(Vector{Int}(undef, 3)) == Vector{Int}
@test typeof(Vector{Int}()) == Vector{Int}
@test typeof(Vector(undef, 3)) == Vector{Any}
@test typeof(Vector()) == Vector{Any}
@test typeof(Matrix{Int}(undef, 2,3)) == Matrix{Int}
@test typeof(Matrix(undef, 2,3)) == Matrix{Any}
@test size(Vector{Int}(undef, 3)) == (3,)
@test size(Vector{Int}()) == (0,)
@test size(Vector(undef, 3)) == (3,)
@test size(Vector()) == (0,)
@test size(Matrix{Int}(undef, 2,3)) == (2,3)
@test size(Matrix(undef, 2,3)) == (2,3)
@test_throws MethodError Matrix()
@test_throws MethodError Matrix{Int}()
@test_throws MethodError Array{Int,3}()
end
@testset "get" begin
A = reshape(1:24, 3, 8)
x = get(A, 32, -12)
@test x == -12
x = get(A, 14, -12)
@test x == 14
x = get(A, (2,4), -12)
@test x == 11
x = get(A, (4,4), -12)
@test x == -12
X = get(A, -5:5, NaN32)
@test eltype(X) == Float32
@test Base.elsize(X) == sizeof(Float32)
@test !all(isinteger, X)
@test isnan.(X) == [trues(6);falses(5)]
@test X[7:11] == [1:5;]
X = get(A, (2:4, 9:-2:-13), 0)
Xv = zeros(Int, 3, 12)
Xv[1:2, 2:5] = A[2:3, 7:-2:1]
@test X == Xv
X2 = get(A, Vector{Int}[[2:4;], [9:-2:-13;]], 0)
@test X == X2
end
@testset "arrays as dequeues" begin
l = Any[1]
push!(l,2,3,8)
@test l[1]==1 && l[2]==2 && l[3]==3 && l[4]==8
v = pop!(l)
@test v == 8
v = pop!(l)
@test v == 3
@test length(l)==2
m = Any[]
@test_throws ArgumentError pop!(m)
@test_throws ArgumentError popfirst!(m)
pushfirst!(l,4,7,5)
@test l[1]==4 && l[2]==7 && l[3]==5 && l[4]==1 && l[5]==2
v = popfirst!(l)
@test v == 4
@test length(l)==4
v = [3, 7, 6]
@test_throws BoundsError insert!(v, 0, 5)
for i = 1:4
vc = copy(v)
@test insert!(vc, i, 5) === vc
@test vc == [v[1:(i-1)]; 5; v[i:end]]
end
@test_throws BoundsError insert!(v, 5, 5)
end
@testset "concatenation" begin
@test isequal([fill(1.,2,2) fill(2.,2,1)], [1. 1 2; 1 1 2])
@test isequal([fill(1.,2,2); fill(2.,1,2)], [1. 1; 1 1; 2 2])
end
@testset "typed array literals" begin
X = Float64[1 2 3]
Y = [1. 2. 3.]
@test size(X) == size(Y)
for i = 1:3 @test X[i] === Y[i] end
X = Float64[1;2;3]
Y = [1.,2.,3.]
@test size(X) == size(Y)
for i = 1:3 @test X[i] === Y[i] end
X = Float64[1 2 3; 4 5 6]
Y = [1. 2. 3.; 4. 5. 6.]
@test size(X) == size(Y)
for i = 1:length(X) @test X[i] === Y[i] end
_array_equiv(a,b) = eltype(a) == eltype(b) && a == b
@test _array_equiv(UInt8[1:3;4], [0x1,0x2,0x3,0x4])
@test_throws MethodError UInt8[1:3]
@test_throws MethodError UInt8[1:3,]
@test_throws MethodError UInt8[1:3,4:6]
a = Vector{UnitRange{Int}}(undef, 1); a[1] = 1:3
@test _array_equiv([1:3,], a)
a = Vector{UnitRange{Int}}(undef, 2); a[1] = 1:3; a[2] = 4:6
@test _array_equiv([1:3,4:6], a)
end
@testset "typed hvcat" begin
X = Float64[1 2 3; 4 5 6]
X32 = Float32[X X; X X]
@test eltype(X32) <: Float32
for i=[1,3], j=[1,4]
@test X32[i:(i+1), j:(j+2)] == X
end
end
@testset "fallback hvcat" begin
#Issue #23994
A23994 = [1 "two"; 3im 4.0; 5 6//1]
@test A23994[2] isa Complex{Int}
@test A23994[3] isa Int
@test A23994[5] isa Float64
@test A23994[6] isa Rational{Int}
end
@testset "cat issue #27326" begin
@test [Int; 1] == [Int, 1]
@test [Int 1] == reshape([Int, 1], 1, :)
@test [Int 1; String 2] == reshape([Int, String, 1, 2], 2, 2)
end
@testset "end" begin
X = [ i+2j for i=1:5, j=1:5 ]
@test X[end,end] == 15
@test X[end] == 15 # linear index
@test X[2, end] == 12
@test X[end, 2] == 9
@test X[end-1,2] == 8
Y = [2, 1, 4, 3]
@test X[Y[end],1] == 5
@test X[end,Y[end]] == 11
end
@testset "findall, findfirst, findnext, findlast, findprev" begin
a = [0,1,2,3,0,1,2,3]
@test findall(!iszero, a) == [2,3,4,6,7,8]
@test findall(a.==2) == [3,7]
@test findall(isodd,a) == [2,4,6,8]
@test findfirst(!iszero, a) == 2
@test findfirst(a.==0) == 1
@test findfirst(a.==5) == nothing
@test findfirst(Dict(1=>false, 2=>true)) == 2
@test findfirst(isequal(3), [1,2,4,1,2,3,4]) == 6
@test findfirst(!isequal(1), [1,2,4,1,2,3,4]) == 2
@test findfirst(isodd, [2,4,6,3,9,2,0]) == 4
@test findfirst(isodd, [2,4,6,2,0]) == nothing
@test findnext(!iszero,a,4) == 4
@test findnext(!iszero,a,5) == 6
@test findnext(!iszero,a,1) == 2
@test findnext(isequal(1),a,4) == 6
@test findnext(isequal(5),a,4) == nothing
@test findlast(!iszero, a) == 8
@test findlast(a.==0) == 5
@test findlast(a.==5) == nothing
@test findlast(isequal(3), [1,2,4,1,2,3,4]) == 6
@test findlast(isodd, [2,4,6,3,9,2,0]) == 5
@test findlast(isodd, [2,4,6,2,0]) == nothing
@test findprev(!iszero,a,4) == 4
@test findprev(!iszero,a,5) == 4
@test findprev(!iszero,a,1) == nothing
@test findprev(isequal(1),a,4) == 2
@test findprev(isequal(1),a,8) == 6
@test findprev(isodd, [2,4,5,3,9,2,0], 7) == 5
@test findprev(isodd, [2,4,5,3,9,2,0], 2) == nothing
@test findfirst(isequal(0x00), [0x01, 0x00]) == 2
@test findlast(isequal(0x00), [0x01, 0x00]) == 2
@test findnext(isequal(0x00), [0x00, 0x01, 0x00], 2) == 3
@test findprev(isequal(0x00), [0x00, 0x01, 0x00], 2) == 1
end
@testset "find with Matrix" begin
A = [1 2 0; 3 4 0]
@test findall(isodd, A) == [CartesianIndex(1, 1), CartesianIndex(2, 1)]
@test findall(!iszero, A) == [CartesianIndex(1, 1), CartesianIndex(2, 1),
CartesianIndex(1, 2), CartesianIndex(2, 2)]
@test findfirst(isodd, A) == CartesianIndex(1, 1)
@test findlast(isodd, A) == CartesianIndex(2, 1)
@test findnext(isodd, A, CartesianIndex(1, 1)) == CartesianIndex(1, 1)
@test findprev(isodd, A, CartesianIndex(2, 1)) == CartesianIndex(2, 1)
@test findnext(isodd, A, CartesianIndex(1, 2)) === nothing
@test findprev(iseven, A, CartesianIndex(2, 1)) === nothing
end
@testset "findmin findmax argmin argmax" begin
@test argmax([10,12,9,11]) == 2
@test argmin([10,12,9,11]) == 3
@test findmin([NaN,3.2,1.8]) === (NaN,1)
@test findmax([NaN,3.2,1.8]) === (NaN,1)
@test findmin([NaN,3.2,1.8,NaN]) === (NaN,1)
@test findmax([NaN,3.2,1.8,NaN]) === (NaN,1)
@test findmin([3.2,1.8,NaN,2.0]) === (NaN,3)
@test findmax([3.2,1.8,NaN,2.0]) === (NaN,3)
#14085
@test findmax(4:9) == (9,6)
@test argmax(4:9) == 6
@test findmin(4:9) == (4,1)
@test argmin(4:9) == 1
@test findmax(5:-2:1) == (5,1)
@test argmax(5:-2:1) == 1
@test findmin(5:-2:1) == (1,3)
@test argmin(5:-2:1) == 3
#23094
@test_throws MethodError findmax(Set(["abc"]))
@test findmin(["abc", "a"]) === ("a", 2)
@test_throws MethodError findmax([Set([1]), Set([2])])
@test findmin([0.0, -0.0]) === (-0.0, 2)
@test findmax([0.0, -0.0]) === (0.0, 1)
@test findmin([-0.0, 0.0]) === (-0.0, 1)
@test findmax([-0.0, 0.0]) === (0.0, 2)
@test isnan(findmin([NaN, NaN, 0.0/0.0])[1])
@test findmin([NaN, NaN, 0.0/0.0])[2] == 1
@test isnan(findmax([NaN, NaN, 0.0/0.0])[1])
@test findmax([NaN, NaN, 0.0/0.0])[2] == 1
# Check that cartesian indices are returned for matrices
@test argmax([10 12; 9 11]) === CartesianIndex(1, 2)
@test argmin([10 12; 9 11]) === CartesianIndex(2, 1)
@test findmax([10 12; 9 11]) === (12, CartesianIndex(1, 2))
@test findmin([10 12; 9 11]) === (9, CartesianIndex(2, 1))
end
@testset "permutedims" begin
# keeps the num of dim
p = randperm(5)
q = randperm(5)
a = rand(p...)
b = permutedims(a,q)
@test isequal(size(b), tuple(p[q]...))
# hand made case
y = zeros(1,2,3)
for i = 1:6
y[i]=i
end
z = zeros(3,1,2)
for i = 1:3
z[i] = i*2-1
z[i+3] = i*2
end
# permutes correctly
@test isequal(z,permutedims(y,[3,1,2]))
@test isequal(z,permutedims(y,(3,1,2)))
# of a subarray
a = rand(5,5)
s = view(a,2:3,2:3)
p = permutedims(s, [2,1])
@test p[1,1]==a[2,2] && p[1,2]==a[3,2]
@test p[2,1]==a[2,3] && p[2,2]==a[3,3]
# of a non-strided subarray
a = reshape(1:60, 3, 4, 5)
s = view(a,:,[1,2,4],[1,5])
c = convert(Array, s)
for p in ([1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1])
@test permutedims(s, p) == permutedims(c, p)
@test PermutedDimsArray(s, p) == permutedims(c, p)
end
@test_throws ArgumentError permutedims(a, (1,1,1))
@test_throws ArgumentError permutedims(s, (1,1,1))
@test_throws ArgumentError PermutedDimsArray(a, (1,1,1))
@test_throws ArgumentError PermutedDimsArray(s, (1,1,1))
cp = PermutedDimsArray(c, (3,2,1))
@test pointer(cp) == pointer(c)
@test_throws ArgumentError pointer(cp, 2)
@test strides(cp) == (9,3,1)
ap = PermutedDimsArray(Array(a), (2,1,3))
@test strides(ap) == (3,1,12)
for A in [rand(1,2,3,4),rand(2,2,2,2),rand(5,6,5,6),rand(1,1,1,1)]
perm = randperm(4)
@test isequal(A,permutedims(permutedims(A,perm),invperm(perm)))
@test isequal(A,permutedims(permutedims(A,invperm(perm)),perm))
end
m = [1 2; 3 4]
@test permutedims(m) == [1 3; 2 4]
v = [1,2,3]
@test permutedims(v) == [1 2 3]
end
@testset "circshift" begin
@test circshift(1:5, -1) == circshift(1:5, 4) == circshift(1:5, -6) == [2,3,4,5,1]
@test circshift(1:5, 1) == circshift(1:5, -4) == circshift(1:5, 6) == [5,1,2,3,4]
a = [1:5;]
@test_throws ArgumentError Base.circshift!(a, a, 1)
b = copy(a)
@test Base.circshift!(b, a, 1) == [5,1,2,3,4]
end
# unique across dim
# With hash collisions
struct HashCollision
x::Float64
end
Base.hash(::HashCollision, h::UInt) = h
# All rows and columns unique
let A, B, C, D
A = fill(1., 10, 10)
A[diagind(A)] = shuffle!([1:10;])
@test unique(A, dims=1) == A
@test unique(A, dims=2) == A
# 10 repeats of each row
B = A[shuffle!(repeat(1:10, 10)), :]
C = unique(B, dims=1)
@test sortslices(C, dims=1) == sortslices(A, dims=1)
@test unique(B, dims=2) == B
@test unique(B', dims=2)' == C
# Along third dimension
D = cat(B, B, dims=3)
@test unique(D, dims=1) == cat(C, C, dims=3)
@test unique(D, dims=3) == cat(B, dims=3)
# With hash collisions
@test map(x -> x.x, unique(map(HashCollision, B), dims=1)) == C
end
@testset "large matrices transpose" begin
for i = 1 : 3
a = rand(200, 300)
@test isequal(copy(a'), permutedims(a, [2, 1]))
end
end
@testset "repeat" begin
local A, A1, A2, A3, v, v2, cv, cv2, c, R, T
A = fill(1,2,3,4)
A1 = reshape(repeat([1,2],1,12),2,3,4)
A2 = reshape(repeat([1 2 3],2,4),2,3,4)
A3 = reshape(repeat([1 2 3 4],6,1),2,3,4)
@test isequal(cumsum(A,dims=1),A1)
@test isequal(cumsum(A,dims=2),A2)
@test isequal(cumsum(A,dims=3),A3)
# issue 20112
A3 = reshape(repeat([1 2 3 4],UInt32(6),UInt32(1)),2,3,4)
@test isequal(cumsum(A,dims=3),A3)
@test repeat([1,2,3,4], UInt32(1)) == [1,2,3,4]
@test repeat([1 2], UInt32(2)) == repeat([1 2], UInt32(2), UInt32(1))
# issue 20564
@test_throws MethodError repeat(1, 2, 3)
@test repeat([1, 2], 1, 2, 3) == repeat([1, 2], outer = (1, 2, 3))
# issue 29020
@test repeat(collect(5), outer=(2, 2)) == [5 5;5 5]
@test repeat(ones(Int64), inner=(1,2), outer=(2,2)) == [1 1 1 1;1 1 1 1]
# issue 29614
@test repeat(ones(2, 2), 1, 1, 1) == ones(2, 2, 1)
@test repeat(ones(2, 2), 2, 2, 2) == ones(4, 4, 2)
@test repeat(ones(2), 2, 2, 2) == ones(4, 2, 2)
@test repeat(ones(2, 2), inner=(1, 1, 1), outer=(2, 2, 2)) == ones(4, 4, 2)
R = repeat([1, 2])
@test R == [1, 2]
R = repeat([1, 2], inner=1)
@test R == [1, 2]
R = repeat([1, 2], outer=1)
@test R == [1, 2]
R = repeat([1, 2], inner=(1,))
@test R == [1, 2]
R = repeat([1, 2], outer=(1,))
@test R == [1, 2]
R = repeat([1, 2], inner=[1])
@test R == [1, 2]
R = repeat([1, 2], outer=[1])
@test R == [1, 2]
R = repeat([1, 2], inner=1, outer=1)
@test R == [1, 2]
R = repeat([1, 2], inner=(1,), outer=(1,))
@test R == [1, 2]
R = repeat([1, 2], inner=[1], outer=[1])
@test R == [1, 2]
R = repeat([1, 2], inner=2)
@test R == [1, 1, 2, 2]
R = repeat([1, 2], outer=2)
@test R == [1, 2, 1, 2]
R = repeat([1, 2], inner=(2,))
@test R == [1, 1, 2, 2]
R = repeat([1, 2], outer=(2,))
@test R == [1, 2, 1, 2]
R = repeat([1, 2], inner=[2])
@test R == [1, 1, 2, 2]
R = repeat([1, 2], outer=[2])
@test R == [1, 2, 1, 2]
R = repeat([1, 2], inner=2, outer=2)
@test R == [1, 1, 2, 2, 1, 1, 2, 2]
R = repeat([1, 2], inner=(2,), outer=(2,))
@test R == [1, 1, 2, 2, 1, 1, 2, 2]
R = repeat([1, 2], inner=[2], outer=[2])
@test R == [1, 1, 2, 2, 1, 1, 2, 2]
R = repeat([1, 2], inner = (1, 1), outer = (1, 1))
@test R == reshape([1, 2], (2,1))
R = repeat([1, 2], inner = (2, 1), outer = (1, 1))
@test R == reshape([1, 1, 2, 2], (4,1))
R = repeat([1, 2], inner = (1, 2), outer = (1, 1))
@test R == [1 1; 2 2]
R = repeat([1, 2], inner = (1, 1), outer = (2, 1))
@test R == reshape([1, 2, 1, 2], (4,1))
R = repeat([1, 2], inner = (1, 1), outer = (1, 2))
@test R == [1 1; 2 2]
R = repeat([1 2;
3 4], inner = (1, 1), outer = (1, 1))
@test R == [1 2;
3 4]
R = repeat([1 2;
3 4], inner = (1, 1), outer = (2, 1))
@test R == [1 2;
3 4;
1 2;
3 4]
R = repeat([1 2;
3 4], inner = (1, 1), outer = (1, 2))
@test R == [1 2 1 2;
3 4 3 4]
R = repeat([1 2;
3 4], inner = (1, 1), outer = (2, 2))
@test R == [1 2 1 2;
3 4 3 4;
1 2 1 2;
3 4 3 4]
R = repeat([1 2;
3 4], inner = (2, 1), outer = (1, 1))
@test R == [1 2;
1 2;
3 4;
3 4]
R = repeat([1 2;
3 4], inner = (2, 1), outer = (2, 1))
@test R == [1 2;
1 2;
3 4;
3 4;
1 2;
1 2;
3 4;
3 4]
R = repeat([1 2;
3 4], inner = (2, 1), outer = (1, 2))
@test R == [1 2 1 2;
1 2 1 2;
3 4 3 4;
3 4 3 4;]
R = repeat([1 2;
3 4], inner = (2, 1), outer = (2, 2))
@test R == [1 2 1 2;
1 2 1 2;
3 4 3 4;
3 4 3 4;
1 2 1 2;
1 2 1 2;
3 4 3 4;
3 4 3 4]
R = repeat([1 2;
3 4], inner = (1, 2), outer = (1, 1))
@test R == [1 1 2 2;
3 3 4 4]
R = repeat([1 2;
3 4], inner = (1, 2), outer = (2, 1))
@test R == [1 1 2 2;
3 3 4 4;
1 1 2 2;
3 3 4 4]
R = repeat([1 2;
3 4], inner = (1, 2), outer = (1, 2))
@test R == [1 1 2 2 1 1 2 2;
3 3 4 4 3 3 4 4]
R = repeat([1 2;
3 4], inner = (1, 2), outer = (2, 2))
@test R == [1 1 2 2 1 1 2 2;
3 3 4 4 3 3 4 4;
1 1 2 2 1 1 2 2;
3 3 4 4 3 3 4 4]
R = repeat([1 2;
3 4], inner = (2, 2), outer = [1, 1])
@test R == [1 1 2 2;
1 1 2 2;
3 3 4 4;
3 3 4 4]
R = repeat([1 2;
3 4], inner = (2, 2), outer = (2, 1))
@test R == [1 1 2 2;
1 1 2 2;
3 3 4 4;
3 3 4 4;
1 1 2 2;
1 1 2 2;
3 3 4 4;
3 3 4 4]
R = repeat([1 2;
3 4], inner = (2, 2), outer = (1, 2))
@test R == [1 1 2 2 1 1 2 2;
1 1 2 2 1 1 2 2;
3 3 4 4 3 3 4 4;
3 3 4 4 3 3 4 4]
R = repeat([1 2;
3 4], inner = (2, 2), outer = (2, 2))
@test R == [1 1 2 2 1 1 2 2;
1 1 2 2 1 1 2 2;
3 3 4 4 3 3 4 4;
3 3 4 4 3 3 4 4;
1 1 2 2 1 1 2 2;
1 1 2 2 1 1 2 2;
3 3 4 4 3 3 4 4;
3 3 4 4 3 3 4 4]
@test_throws ArgumentError repeat([1 2;
3 4], inner=2, outer=(2, 2))
@test_throws ArgumentError repeat([1 2;
3 4], inner=(2, 2), outer=2)
@test_throws ArgumentError repeat([1 2;
3 4], inner=(2,), outer=(2, 2))
@test_throws ArgumentError repeat([1 2;
3 4], inner=(2, 2), outer=(2,))
A = reshape(1:8, 2, 2, 2)
R = repeat(A, inner = (1, 1, 2), outer = (1, 1, 1))
T = reshape([1:4; 1:4; 5:8; 5:8], 2, 2, 4)
@test R == T
A = Array{Int}(undef, 2, 2, 2)
A[:, :, 1] = [1 2;
3 4]
A[:, :, 2] = [5 6;
7 8]
R = repeat(A, inner = (2, 2, 2), outer = (2, 2, 2))
@test R[1, 1, 1] == 1
@test R[2, 2, 2] == 1
@test R[3, 3, 3] == 8
@test R[4, 4, 4] == 8
@test R[5, 5, 5] == 1
@test R[6, 6, 6] == 1
@test R[7, 7, 7] == 8
@test R[8, 8, 8] == 8
R = repeat(1:2)
@test R == [1, 2]
R = repeat(1:2, inner=1)
@test R == [1, 2]
R = repeat(1:2, inner=2)
@test R == [1, 1, 2, 2]
R = repeat(1:2, outer=1)
@test R == [1, 2]
R = repeat(1:2, outer=2)
@test R == [1, 2, 1, 2]
R = repeat(1:2, inner=(3,), outer=(2,))
@test R == [1, 1, 1, 2, 2, 2, 1, 1, 1, 2, 2, 2]
# Arrays of arrays
@test repeat([[1], [2]], inner=2) == [[1], [1], [2], [2]]
@test repeat([[1], [2]], outer=2) == [[1], [2], [1], [2]]
@test repeat([[1], [2]], inner=2, outer=2) == [[1], [1], [2], [2], [1], [1], [2], [2]]
@test size(repeat([1], inner=(0,))) == (0,)
@test size(repeat([1], outer=(0,))) == (0,)
@test size(repeat([1 1], inner=(0, 1))) == (0, 2)
@test size(repeat([1 1], outer=(1, 0))) == (1, 0)
@test size(repeat([1 1], inner=(2, 0), outer=(2, 1))) == (4, 0)
@test size(repeat([1 1], inner=(2, 0), outer=(0, 1))) == (0, 0)
A = rand(4,4)
for s in Any[A[1:2:4, 1:2:4], view(A, 1:2:4, 1:2:4)]
c = cumsum(s, dims=1)
@test c[1,1] == A[1,1]
@test c[2,1] == A[1,1]+A[3,1]
@test c[1,2] == A[1,3]
@test c[2,2] == A[1,3]+A[3,3]
c = cumsum(s, dims=2)
@test c[1,1] == A[1,1]
@test c[2,1] == A[3,1]
@test c[1,2] == A[1,1]+A[1,3]
@test c[2,2] == A[3,1]+A[3,3]
end
@test repeat(BitMatrix(Matrix(I, 2, 2)), inner = (2,1), outer = (1,2)) ==
repeat(Matrix(I, 2, 2), inner = (2,1), outer = (1,2))
end
@testset "indexing with bools" begin
@test (1:5)[[true,false,true,false,true]] == [1,3,5]
@test [1:5;][[true,false,true,false,true]] == [1,3,5]
@test_throws BoundsError (1:5)[[true,false,true,false]]
@test_throws BoundsError (1:5)[[true,false,true,false,true,false]]
@test_throws BoundsError [1:5;][[true,false,true,false]]
@test_throws BoundsError [1:5;][[true,false,true,false,true,false]]
a = [1:5;]
a[[true,false,true,false,true]] .= 6
@test a == [6,2,6,4,6]
a[[true,false,true,false,true]] = [7,8,9]
@test a == [7,2,8,4,9]
@test_throws DimensionMismatch (a[[true,false,true,false,true]] = [7,8,9,10])
A = reshape(1:15, 3, 5)
@test A[[true, false, true], [false, false, true, true, false]] == [7 10; 9 12]