-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
sparse.jl
2298 lines (2044 loc) · 86.7 KB
/
sparse.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
module SparseTests
using Test
using SparseArrays
using LinearAlgebra
using Base.Printf: @printf
using Random
using Test: guardseed
@testset "issparse" begin
@test issparse(sparse(fill(1,5,5)))
@test !issparse(fill(1,5,5))
end
@testset "iszero specialization for SparseMatrixCSC" begin
@test !iszero(sparse(I, 3, 3)) # test failure
@test iszero(spzeros(3, 3)) # test success with no stored entries
S = sparse(I, 3, 3)
S[:] .= 0
@test iszero(S) # test success with stored zeros via broadcasting
S = sparse(I, 3, 3)
fill!(S, 0)
@test iszero(S) # test success with stored zeros via fill!
@test iszero(SparseMatrixCSC(2, 2, [1,2,3], [1,2], [0,0,1])) # test success with nonzeros beyond data range
end
@testset "isone specialization for SparseMatrixCSC" begin
@test isone(sparse(I, 3, 3)) # test success
@test !isone(sparse(I, 3, 4)) # test failure for non-square matrix
@test !isone(spzeros(3, 3)) # test failure for too few stored entries
@test !isone(sparse(2I, 3, 3)) # test failure for non-one diagonal entries
@test !isone(sparse(Bidiagonal(fill(1, 3), fill(1, 2), :U))) # test failure for non-zero off-diag entries
end
@testset "indtype" begin
@test SparseArrays.indtype(sparse(Int8[1,1],Int8[1,1],[1,1])) == Int8
end
@testset "sparse matrix construction" begin
@test (A = fill(1.0+im,5,5); isequal(Array(sparse(A)), A))
@test_throws ArgumentError sparse([1,2,3], [1,2], [1,2,3], 3, 3)
@test_throws ArgumentError sparse([1,2,3], [1,2,3], [1,2], 3, 3)
@test_throws ArgumentError sparse([1,2,3], [1,2,3], [1,2,3], 0, 1)
@test_throws ArgumentError sparse([1,2,3], [1,2,3], [1,2,3], 1, 0)
@test_throws ArgumentError sparse([1,2,4], [1,2,3], [1,2,3], 3, 3)
@test_throws ArgumentError sparse([1,2,3], [1,2,4], [1,2,3], 3, 3)
@test isequal(sparse(Int[], Int[], Int[], 0, 0), SparseMatrixCSC(0, 0, Int[1], Int[], Int[]))
@test sparse(Any[1,2,3], Any[1,2,3], Any[1,1,1]) == sparse([1,2,3], [1,2,3], [1,1,1])
@test sparse(Any[1,2,3], Any[1,2,3], Any[1,1,1], 5, 4) == sparse([1,2,3], [1,2,3], [1,1,1], 5, 4)
end
@testset "SparseMatrixCSC construction from UniformScaling" begin
@test_throws ArgumentError SparseMatrixCSC(I, -1, 3)
@test_throws ArgumentError SparseMatrixCSC(I, 3, -1)
@test SparseMatrixCSC(2I, 3, 3)::SparseMatrixCSC{Int,Int} == Matrix(2I, 3, 3)
@test SparseMatrixCSC(2I, 3, 4)::SparseMatrixCSC{Int,Int} == Matrix(2I, 3, 4)
@test SparseMatrixCSC(2I, 4, 3)::SparseMatrixCSC{Int,Int} == Matrix(2I, 4, 3)
@test SparseMatrixCSC(2.0I, 3, 3)::SparseMatrixCSC{Float64,Int} == Matrix(2I, 3, 3)
@test SparseMatrixCSC{Real}(2I, 3, 3)::SparseMatrixCSC{Real,Int} == Matrix(2I, 3, 3)
@test SparseMatrixCSC{Float64}(2I, 3, 3)::SparseMatrixCSC{Float64,Int} == Matrix(2I, 3, 3)
@test SparseMatrixCSC{Float64,Int32}(2I, 3, 3)::SparseMatrixCSC{Float64,Int32} == Matrix(2I, 3, 3)
@test SparseMatrixCSC{Float64,Int32}(0I, 3, 3)::SparseMatrixCSC{Float64,Int32} == Matrix(0I, 3, 3)
end
@testset "sparse(S::UniformScaling, shape...) convenience constructors" begin
# we exercise these methods only lightly as these methods call the SparseMatrixCSC
# constructor methods well-exercised by the immediately preceding testset
@test sparse(2I, 3, 4)::SparseMatrixCSC{Int,Int} == Matrix(2I, 3, 4)
@test sparse(2I, (3, 4))::SparseMatrixCSC{Int,Int} == Matrix(2I, 3, 4)
end
se33 = SparseMatrixCSC{Float64}(I, 3, 3)
do33 = fill(1.,3)
@testset "sparse binary operations" begin
@test isequal(se33 * se33, se33)
@test Array(se33 + convert(SparseMatrixCSC{Float32,Int32}, se33)) == Matrix(2I, 3, 3)
@test Array(se33 * convert(SparseMatrixCSC{Float32,Int32}, se33)) == Matrix(I, 3, 3)
@testset "shape checks for sparse elementwise binary operations equivalent to map" begin
sqrfloatmat, colfloatmat = sprand(4, 4, 0.5), sprand(4, 1, 0.5)
@test_throws DimensionMismatch (+)(sqrfloatmat, colfloatmat)
@test_throws DimensionMismatch (-)(sqrfloatmat, colfloatmat)
@test_throws DimensionMismatch map(min, sqrfloatmat, colfloatmat)
@test_throws DimensionMismatch map(max, sqrfloatmat, colfloatmat)
sqrboolmat, colboolmat = sprand(Bool, 4, 4, 0.5), sprand(Bool, 4, 1, 0.5)
@test_throws DimensionMismatch map(&, sqrboolmat, colboolmat)
@test_throws DimensionMismatch map(|, sqrboolmat, colboolmat)
@test_throws DimensionMismatch map(xor, sqrboolmat, colboolmat)
end
end
@testset "concatenation tests" begin
sp33 = sparse(1.0I, 3, 3)
@testset "horizontal concatenation" begin
@test [se33 se33] == [Array(se33) Array(se33)]
@test length(([sp33 0I]).nzval) == 3
end
@testset "vertical concatenation" begin
@test [se33; se33] == [Array(se33); Array(se33)]
se33_32bit = convert(SparseMatrixCSC{Float32,Int32}, se33)
@test [se33; se33_32bit] == [Array(se33); Array(se33_32bit)]
@test length(([sp33; 0I]).nzval) == 3
end
se44 = sparse(1.0I, 4, 4)
sz42 = spzeros(4, 2)
sz41 = spzeros(4, 1)
sz34 = spzeros(3, 4)
se77 = sparse(1.0I, 7, 7)
@testset "h+v concatenation" begin
@test all([se44 sz42 sz41; sz34 se33] == se77)
@test length(([sp33 0I; 1I 0I]).nzval) == 6
end
@testset "blockdiag concatenation" begin
@test blockdiag(se33, se33) == sparse(1:6,1:6,fill(1.,6))
@test blockdiag() == spzeros(0, 0)
@test nnz(blockdiag()) == 0
end
@testset "concatenation promotion" begin
sz41_f32 = spzeros(Float32, 4, 1)
se33_i32 = sparse(Int32(1)I, 3, 3)
@test all([se44 sz42 sz41_f32; sz34 se33_i32] == se77)
end
@testset "mixed sparse-dense concatenation" begin
sz33 = spzeros(3, 3)
de33 = Matrix(1.0I, 3, 3)
@test all([se33 de33; sz33 se33] == Array([se33 se33; sz33 se33 ]))
end
# check splicing + concatenation on random instances, with nested vcat and also side-checks sparse ref
@testset "splicing + concatenation on random instances" begin
for i = 1 : 10
a = sprand(5, 4, 0.5)
@test all([a[1:2,1:2] a[1:2,3:4]; a[3:5,1] [a[3:4,2:4]; a[5:5,2:4]]] == a)
end
end
end
let
a116 = copy(reshape(1:16, 4, 4))
s116 = sparse(a116)
@testset "sparse ref" begin
p = [4, 1, 2, 3, 2]
@test Array(s116[p,:]) == a116[p,:]
@test Array(s116[:,p]) == a116[:,p]
@test Array(s116[p,p]) == a116[p,p]
end
@testset "sparse assignment" begin
p = [4, 1, 3]
a116[p, p] .= -1
s116[p, p] .= -1
@test a116 == s116
p = [2, 1, 4]
a116[p, p] = reshape(1:9, 3, 3)
s116[p, p] = reshape(1:9, 3, 3)
@test a116 == s116
end
end
@testset "dropdims" begin
for i = 1:5
am = sprand(20, 1, 0.2)
av = dropdims(am, dims=2)
@test ndims(av) == 1
@test all(av.==am)
am = sprand(1, 20, 0.2)
av = dropdims(am, dims=1)
@test ndims(av) == 1
@test all(av' .== am)
end
end
@testset "matrix-vector multiplication (non-square)" begin
for i = 1:5
a = sprand(10, 5, 0.5)
b = rand(5)
@test maximum(abs.(a*b - Array(a)*b)) < 100*eps()
end
end
@testset "sparse matrix * BitArray" begin
A = sprand(5,5,0.2)
B = trues(5)
@test A*B ≈ Array(A)*B
B = trues(5,5)
@test A*B ≈ Array(A)*B
@test B*A ≈ B*Array(A)
end
@testset "complex matrix-vector multiplication and left-division" begin
if Base.USE_GPL_LIBS
for i = 1:5
a = I + 0.1*sprandn(5, 5, 0.2)
b = randn(5,3) + im*randn(5,3)
c = randn(5) + im*randn(5)
d = randn(5) + im*randn(5)
α = rand(ComplexF64)
β = rand(ComplexF64)
@test (maximum(abs.(a*b - Array(a)*b)) < 100*eps())
@test (maximum(abs.(mul!(similar(b), a, b) - Array(a)*b)) < 100*eps()) # for compatibility with present matmul API. Should go away eventually.
@test (maximum(abs.(mul!(similar(c), a, c) - Array(a)*c)) < 100*eps()) # for compatibility with present matmul API. Should go away eventually.
@test (maximum(abs.(mul!(similar(b), transpose(a), b) - transpose(Array(a))*b)) < 100*eps()) # for compatibility with present matmul API. Should go away eventually.
@test (maximum(abs.(mul!(similar(c), transpose(a), c) - transpose(Array(a))*c)) < 100*eps()) # for compatibility with present matmul API. Should go away eventually.
@test (maximum(abs.(a'b - Array(a)'b)) < 100*eps())
@test (maximum(abs.(transpose(a)*b - transpose(Array(a))*b)) < 100*eps())
@test (maximum(abs.(a\b - Array(a)\b)) < 1000*eps())
@test (maximum(abs.(a'\b - Array(a')\b)) < 1000*eps())
@test (maximum(abs.(transpose(a)\b - Array(transpose(a))\b)) < 1000*eps())
@test (maximum(abs.((a'*c + d) - (Array(a)'*c + d))) < 1000*eps())
@test (maximum(abs.((α*transpose(a)*c + β*d) - (α*transpose(Array(a))*c + β*d))) < 1000*eps())
@test (maximum(abs.((transpose(a)*c + d) - (transpose(Array(a))*c + d))) < 1000*eps())
c = randn(6) + im*randn(6)
@test_throws DimensionMismatch α*transpose(a)*c + β*c
@test_throws DimensionMismatch α*transpose(a)*fill(1.,5) + β*c
a = I + 0.1*sprandn(5, 5, 0.2) + 0.1*im*sprandn(5, 5, 0.2)
b = randn(5,3)
@test (maximum(abs.(a*b - Array(a)*b)) < 100*eps())
@test (maximum(abs.(a'b - Array(a)'b)) < 100*eps())
@test (maximum(abs.(transpose(a)*b - transpose(Array(a))*b)) < 100*eps())
@test (maximum(abs.(a\b - Array(a)\b)) < 1000*eps())
@test (maximum(abs.(a'\b - Array(a')\b)) < 1000*eps())
@test (maximum(abs.(transpose(a)\b - Array(transpose(a))\b)) < 1000*eps())
a = I + tril(0.1*sprandn(5, 5, 0.2))
b = randn(5,3) + im*randn(5,3)
@test (maximum(abs.(a*b - Array(a)*b)) < 100*eps())
@test (maximum(abs.(a'b - Array(a)'b)) < 100*eps())
@test (maximum(abs.(transpose(a)*b - transpose(Array(a))*b)) < 100*eps())
@test (maximum(abs.(a\b - Array(a)\b)) < 1000*eps())
@test (maximum(abs.(a'\b - Array(a')\b)) < 1000*eps())
@test (maximum(abs.(transpose(a)\b - Array(transpose(a))\b)) < 1000*eps())
a = I + tril(0.1*sprandn(5, 5, 0.2) + 0.1*im*sprandn(5, 5, 0.2))
b = randn(5,3)
@test (maximum(abs.(a*b - Array(a)*b)) < 100*eps())
@test (maximum(abs.(a'b - Array(a)'b)) < 100*eps())
@test (maximum(abs.(transpose(a)*b - transpose(Array(a))*b)) < 100*eps())
@test (maximum(abs.(a\b - Array(a)\b)) < 1000*eps())
@test (maximum(abs.(a'\b - Array(a')\b)) < 1000*eps())
@test (maximum(abs.(transpose(a)\b - Array(transpose(a))\b)) < 1000*eps())
a = I + triu(0.1*sprandn(5, 5, 0.2))
b = randn(5,3) + im*randn(5,3)
@test (maximum(abs.(a*b - Array(a)*b)) < 100*eps())
@test (maximum(abs.(a'b - Array(a)'b)) < 100*eps())
@test (maximum(abs.(transpose(a)*b - transpose(Array(a))*b)) < 100*eps())
@test (maximum(abs.(a\b - Array(a)\b)) < 1000*eps())
@test (maximum(abs.(a'\b - Array(a')\b)) < 1000*eps())
@test (maximum(abs.(transpose(a)\b - Array(transpose(a))\b)) < 1000*eps())
a = I + triu(0.1*sprandn(5, 5, 0.2) + 0.1*im*sprandn(5, 5, 0.2))
b = randn(5,3)
@test (maximum(abs.(a*b - Array(a)*b)) < 100*eps())
@test (maximum(abs.(a'b - Array(a)'b)) < 100*eps())
@test (maximum(abs.(transpose(a)*b - transpose(Array(a))*b)) < 100*eps())
@test (maximum(abs.(a\b - Array(a)\b)) < 1000*eps())
@test (maximum(abs.(a'\b - Array(a')\b)) < 1000*eps())
@test (maximum(abs.(transpose(a)\b - Array(transpose(a))\b)) < 1000*eps())
a = I + triu(0.1*sprandn(5, 5, 0.2))
b = randn(5,3) + im*randn(5,3)
@test (maximum(abs.(a*b - Array(a)*b)) < 100*eps())
@test (maximum(abs.(a'b - Array(a)'b)) < 100*eps())
@test (maximum(abs.(transpose(a)*b - transpose(Array(a))*b)) < 100*eps())
@test (maximum(abs.(a\b - Array(a)\b)) < 1000*eps())
@test (maximum(abs.(a'\b - Array(a')\b)) < 1000*eps())
@test (maximum(abs.(transpose(a)\b - Array(transpose(a))\b)) < 1000*eps())
# UpperTriangular/LowerTriangular solve
a = UpperTriangular(I + triu(0.1*sprandn(5, 5, 0.2)))
b = sprandn(5, 5, 0.2)
@test (maximum(abs.(a\b - Array(a)\Array(b))) < 1000*eps())
# test error throwing for bwdTrisolve
@test_throws DimensionMismatch a\Matrix{Float64}(I, 6, 6)
a = LowerTriangular(I + tril(0.1*sprandn(5, 5, 0.2)))
b = sprandn(5, 5, 0.2)
@test (maximum(abs.(a\b - Array(a)\Array(b))) < 1000*eps())
# test error throwing for fwdTrisolve
@test_throws DimensionMismatch a\Matrix{Float64}(I, 6, 6)
a = sparse(Diagonal(randn(5) + im*randn(5)))
b = randn(5,3)
@test (maximum(abs.(a*b - Array(a)*b)) < 100*eps())
@test (maximum(abs.(a'b - Array(a)'b)) < 100*eps())
@test (maximum(abs.(transpose(a)*b - transpose(Array(a))*b)) < 100*eps())
@test (maximum(abs.(a\b - Array(a)\b)) < 1000*eps())
@test (maximum(abs.(a'\b - Array(a')\b)) < 1000*eps())
@test (maximum(abs.(transpose(a)\b - Array(transpose(a))\b)) < 1000*eps())
b = randn(5,3) + im*randn(5,3)
@test (maximum(abs.(a*b - Array(a)*b)) < 100*eps())
@test (maximum(abs.(a'b - Array(a)'b)) < 100*eps())
@test (maximum(abs.(transpose(a)*b - transpose(Array(a))*b)) < 100*eps())
@test (maximum(abs.(a\b - Array(a)\b)) < 1000*eps())
@test (maximum(abs.(a'\b - Array(a')\b)) < 1000*eps())
@test (maximum(abs.(transpose(a)\b - Array(transpose(a))\b)) < 1000*eps())
end
end
end
@testset "matrix multiplication" begin
for i = 1:5
a = sprand(10, 5, 0.7)
b = sprand(5, 15, 0.3)
@test maximum(abs.(a*b - Array(a)*Array(b))) < 100*eps()
@test maximum(abs.(SparseArrays.spmatmul(a,b,sortindices=:sortcols) - Array(a)*Array(b))) < 100*eps()
@test maximum(abs.(SparseArrays.spmatmul(a,b,sortindices=:doubletranspose) - Array(a)*Array(b))) < 100*eps()
f = Diagonal(rand(5))
@test Array(a*f) == Array(a)*f
@test Array(f*b) == f*Array(b)
end
end
@testset "kronecker product" begin
for (m,n) in ((5,10), (13,8), (14,10))
a = sprand(m, 5, 0.4); a_d = Matrix(a)
b = sprand(n, 6, 0.3); b_d = Matrix(b)
x = sprand(m, 0.4); x_d = Vector(x)
y = sprand(n, 0.3); y_d = Vector(y)
# mat ⊗ mat
@test Array(kron(a, b)) == kron(a_d, b_d)
@test Array(kron(a_d, b)) == kron(a_d, b_d)
@test Array(kron(a, b_d)) == kron(a_d, b_d)
# vec ⊗ vec
@test Vector(kron(x, y)) == kron(x_d, y_d)
@test Vector(kron(x_d, y)) == kron(x_d, y_d)
@test Vector(kron(x, y_d)) == kron(x_d, y_d)
# mat ⊗ vec
@test Array(kron(a, y)) == kron(a_d, y_d)
@test Array(kron(a_d, y)) == kron(a_d, y_d)
@test Array(kron(a, y_d)) == kron(a_d, y_d)
# vec ⊗ mat
@test Array(kron(x, b)) == kron(x_d, b_d)
@test Array(kron(x_d, b)) == kron(x_d, b_d)
@test Array(kron(x, b_d)) == kron(x_d, b_d)
# test different types
z = convert(SparseVector{Float16, Int8}, y); z_d = Vector(z)
@test Vector(kron(x, z)) == kron(x_d, z_d)
@test Array(kron(a, z)) == kron(a_d, z_d)
@test Array(kron(z, b)) == kron(z_d, b_d)
end
end
@testset "sparse Frobenius dot/inner product" begin
for i = 1:5
A = sprand(ComplexF64,10,15,0.4)
B = sprand(ComplexF64,10,15,0.5)
@test dot(A,B) ≈ dot(Matrix(A),Matrix(B))
end
@test_throws DimensionMismatch dot(sprand(5,5,0.2),sprand(5,6,0.2))
end
sA = sprandn(3, 7, 0.5)
sC = similar(sA)
dA = Array(sA)
@testset "scaling with * and mul!, rmul!, and lmul!" begin
b = randn(7)
@test dA * Diagonal(b) == sA * Diagonal(b)
@test dA * Diagonal(b) == mul!(sC, sA, Diagonal(b))
@test dA * Diagonal(b) == rmul!(copy(sA), Diagonal(b))
b = randn(3)
@test Diagonal(b) * dA == Diagonal(b) * sA
@test Diagonal(b) * dA == mul!(sC, Diagonal(b), sA)
@test Diagonal(b) * dA == lmul!(Diagonal(b), copy(sA))
@test dA * 0.5 == sA * 0.5
@test dA * 0.5 == mul!(sC, sA, 0.5)
@test dA * 0.5 == rmul!(copy(sA), 0.5)
@test 0.5 * dA == 0.5 * sA
@test 0.5 * dA == mul!(sC, sA, 0.5)
@test 0.5 * dA == lmul!(0.5, copy(sA))
@test mul!(sC, 0.5, sA) == mul!(sC, sA, 0.5)
@testset "inverse scaling with mul!" begin
bi = inv.(b)
dAt = copy(transpose(dA))
sAt = copy(transpose(sA))
@test rmul!(copy(dAt), Diagonal(bi)) ≈ rdiv!(copy(sAt), Diagonal(b))
@test rmul!(copy(dAt), Diagonal(bi)) ≈ rdiv!(copy(sAt), transpose(Diagonal(b)))
@test rmul!(copy(dAt), Diagonal(conj(bi))) ≈ rdiv!(copy(sAt), adjoint(Diagonal(b)))
@test_throws DimensionMismatch rdiv!(copy(sAt), Diagonal(fill(1., length(b)+1)))
@test_throws LinearAlgebra.SingularException rdiv!(copy(sAt), Diagonal(zeros(length(b))))
end
end
@testset "copyto!" begin
A = sprand(5, 5, 0.2)
B = sprand(5, 5, 0.2)
copyto!(A, B)
@test A == B
@test pointer(A.nzval) != pointer(B.nzval)
@test pointer(A.rowval) != pointer(B.rowval)
@test pointer(A.colptr) != pointer(B.colptr)
# Test size(A) != size(B), but length(A) == length(B)
B = sprand(25, 1, 0.2)
copyto!(A, B)
@test A[:] == B[:]
# Test various size(A) / size(B) combinations
for mA in [5, 10, 20], nA in [5, 10, 20], mB in [5, 10, 20], nB in [5, 10, 20]
A = sprand(mA,nA,0.4)
Aorig = copy(A)
B = sprand(mB,nB,0.4)
if mA*nA >= mB*nB
copyto!(A,B)
@assert(A[1:length(B)] == B[:])
@assert(A[length(B)+1:end] == Aorig[length(B)+1:end])
else
@test_throws BoundsError copyto!(A,B)
end
end
# Test eltype(A) != eltype(B), size(A) != size(B)
A = sprand(5, 5, 0.2)
Aorig = copy(A)
B = sparse(rand(Float32, 3, 3))
copyto!(A, B)
@test A[1:9] == B[:]
@test A[10:end] == Aorig[10:end]
# Test eltype(A) != eltype(B), size(A) == size(B)
A = sparse(rand(Float64, 3, 3))
B = sparse(rand(Float32, 3, 3))
copyto!(A, B)
@test A == B
end
@testset "conj" begin
cA = sprandn(5,5,0.2) + im*sprandn(5,5,0.2)
@test Array(conj.(cA)) == conj(Array(cA))
@test Array(conj!(copy(cA))) == conj(Array(cA))
end
@testset "SparseMatrixCSC [c]transpose[!] and permute[!]" begin
smalldim = 5
largedim = 10
nzprob = 0.4
(m, n) = (smalldim, smalldim)
A = sprand(m, n, nzprob)
X = similar(A)
C = copy(transpose(A))
p = randperm(m)
q = randperm(n)
@testset "common error checking of [c]transpose! methods (ftranspose!)" begin
@test_throws DimensionMismatch transpose!(A[:, 1:(smalldim - 1)], A)
@test_throws DimensionMismatch transpose!(A[1:(smalldim - 1), 1], A)
@test_throws ArgumentError transpose!((B = similar(A); resize!(B.rowval, nnz(A) - 1); B), A)
@test_throws ArgumentError transpose!((B = similar(A); resize!(B.nzval, nnz(A) - 1); B), A)
end
@testset "common error checking of permute[!] methods / source-perm compat" begin
@test_throws DimensionMismatch permute(A, p[1:(end - 1)], q)
@test_throws DimensionMismatch permute(A, p, q[1:(end - 1)])
end
@testset "common error checking of permute[!] methods / source-dest compat" begin
@test_throws DimensionMismatch permute!(A[1:(m - 1), :], A, p, q)
@test_throws DimensionMismatch permute!(A[:, 1:(m - 1)], A, p, q)
@test_throws ArgumentError permute!((Y = copy(X); resize!(Y.rowval, nnz(A) - 1); Y), A, p, q)
@test_throws ArgumentError permute!((Y = copy(X); resize!(Y.nzval, nnz(A) - 1); Y), A, p, q)
end
@testset "common error checking of permute[!] methods / source-workmat compat" begin
@test_throws DimensionMismatch permute!(X, A, p, q, C[1:(m - 1), :])
@test_throws DimensionMismatch permute!(X, A, p, q, C[:, 1:(m - 1)])
@test_throws ArgumentError permute!(X, A, p, q, (D = copy(C); resize!(D.rowval, nnz(A) - 1); D))
@test_throws ArgumentError permute!(X, A, p, q, (D = copy(C); resize!(D.nzval, nnz(A) - 1); D))
end
@testset "common error checking of permute[!] methods / source-workcolptr compat" begin
@test_throws DimensionMismatch permute!(A, p, q, C, Vector{eltype(A.rowval)}(undef, length(A.colptr) - 1))
end
@testset "common error checking of permute[!] methods / permutation validity" begin
@test_throws ArgumentError permute!(A, (r = copy(p); r[2] = r[1]; r), q)
@test_throws ArgumentError permute!(A, (r = copy(p); r[2] = m + 1; r), q)
@test_throws ArgumentError permute!(A, p, (r = copy(q); r[2] = r[1]; r))
@test_throws ArgumentError permute!(A, p, (r = copy(q); r[2] = n + 1; r))
end
@testset "overall functionality of [c]transpose[!] and permute[!]" begin
for (m, n) in ((smalldim, smalldim), (smalldim, largedim), (largedim, smalldim))
A = sprand(m, n, nzprob)
At = copy(transpose(A))
# transpose[!]
fullAt = Array(transpose(A))
@test copy(transpose(A)) == fullAt
@test transpose!(similar(At), A) == fullAt
# adjoint[!]
C = A + im*A/2
fullCh = Array(C')
@test copy(C') == fullCh
@test adjoint!(similar(sparse(fullCh)), C) == fullCh
# permute[!]
p = randperm(m)
q = randperm(n)
fullPAQ = Array(A)[p,q]
@test permute(A, p, q) == sparse(Array(A[p,q]))
@test permute!(similar(A), A, p, q) == fullPAQ
@test permute!(similar(A), A, p, q, similar(At)) == fullPAQ
@test permute!(copy(A), p, q) == fullPAQ
@test permute!(copy(A), p, q, similar(At)) == fullPAQ
@test permute!(copy(A), p, q, similar(At), similar(A.colptr)) == fullPAQ
end
end
end
@testset "transpose of SubArrays" begin
A = view(sprandn(10, 10, 0.3), 1:4, 1:4)
@test copy(transpose(Array(A))) == Array(transpose(A))
@test copy(adjoint(Array(A))) == Array(adjoint(A))
end
@testset "exp" begin
A = sprandn(5,5,0.2)
@test ℯ.^A ≈ ℯ.^Array(A)
end
@testset "reductions" begin
pA = sparse(rand(3, 7))
p28227 = sparse(Real[0 0.5])
for arr in (se33, sA, pA, p28227)
for f in (sum, prod, minimum, maximum)
farr = Array(arr)
@test f(arr) ≈ f(farr)
@test f(arr, dims=1) ≈ f(farr, dims=1)
@test f(arr, dims=2) ≈ f(farr, dims=2)
@test f(arr, dims=(1, 2)) ≈ [f(farr)]
@test isequal(f(arr, dims=3), f(farr, dims=3))
end
end
for f in (sum, prod, minimum, maximum)
# Test with a map function that maps to non-zero
for arr in (se33, sA, pA)
@test f(x->x+1, arr) ≈ f(arr .+ 1)
end
# case where f(0) would throw
@test f(x->sqrt(x-1), pA .+ 1) ≈ f(sqrt.(pA))
# these actually throw due to #10533
# @test f(x->sqrt(x-1), pA .+ 1, dims=1) ≈ f(sqrt(pA), dims=1)
# @test f(x->sqrt(x-1), pA .+ 1, dims=2) ≈ f(sqrt(pA), dims=2)
# @test f(x->sqrt(x-1), pA .+ 1, dims=3) ≈ f(pA)
end
@testset "empty cases" begin
@test sum(sparse(Int[])) === 0
@test prod(sparse(Int[])) === 1
@test_throws ArgumentError minimum(sparse(Int[]))
@test_throws ArgumentError maximum(sparse(Int[]))
for f in (sum, prod)
@test isequal(f(spzeros(0, 1), dims=1), f(Matrix{Int}(I, 0, 1), dims=1))
@test isequal(f(spzeros(0, 1), dims=2), f(Matrix{Int}(I, 0, 1), dims=2))
@test isequal(f(spzeros(0, 1), dims=(1, 2)), f(Matrix{Int}(I, 0, 1), dims=(1, 2)))
@test isequal(f(spzeros(0, 1), dims=3), f(Matrix{Int}(I, 0, 1), dims=3))
end
for f in (minimum, maximum, findmin, findmax)
@test_throws ArgumentError f(spzeros(0, 1), dims=1)
@test isequal(f(spzeros(0, 1), dims=2), f(Matrix{Int}(I, 0, 1), dims=2))
@test_throws ArgumentError f(spzeros(0, 1), dims=(1, 2))
@test isequal(f(spzeros(0, 1), dims=3), f(Matrix{Int}(I, 0, 1), dims=3))
end
end
end
@testset "issue #5190" begin
@test_throws ArgumentError sparsevec([3,5,7],[0.1,0.0,3.2],4)
end
@testset "what used to be issue #5386" begin
K,J,V = findnz(SparseMatrixCSC(2,1,[1,3],[1,2],[1.0,0.0]))
@test length(K) == length(J) == length(V) == 2
end
@testset "findall" begin
# issue described in https://groups.google.com/d/msg/julia-users/Yq4dh8NOWBQ/GU57L90FZ3EJ
A = sparse(I, 5, 5)
@test findall(A) == findall(x -> x == true, A) == findall(Array(A))
# Non-stored entries are true
@test findall(x -> x == false, A) == findall(x -> x == false, Array(A))
# Not all stored entries are true
@test findall(sparse([true false])) == [CartesianIndex(1, 1)]
@test findall(x -> x > 1, sparse([1 2])) == [CartesianIndex(1, 2)]
end
@testset "issue #5824" begin
@test sprand(4,5,0.5).^0 == sparse(fill(1,4,5))
end
@testset "issue #5985" begin
@test sprand(Bool, 4, 5, 0.0) == sparse(zeros(Bool, 4, 5))
@test sprand(Bool, 4, 5, 1.00) == sparse(fill(true, 4, 5))
sprb45nnzs = zeros(5)
for i=1:5
sprb45 = sprand(Bool, 4, 5, 0.5)
@test length(sprb45) == 20
sprb45nnzs[i] = sum(sprb45)[1]
end
@test 4 <= sum(sprb45nnzs)/length(sprb45nnzs) <= 16
end
@testset "issue #5853, sparse diff" begin
for i=1:2, a=Any[[1 2 3], reshape([1, 2, 3],(3,1)), Matrix(1.0I, 3, 3)]
@test all(diff(sparse(a),dims=i) == diff(a,dims=i))
end
end
@testset "access to undefined error types that initially allocate elements as #undef" begin
@test all(sparse(1:2, 1:2, Number[1,2])^2 == sparse(1:2, 1:2, [1,4]))
sd1 = diff(sparse([1,1,1], [1,2,3], Number[1,2,3]), dims=1)
end
@testset "issue #6036" begin
P = spzeros(Float64, 3, 3)
for i = 1:3
P[i,i] = i
end
@test minimum(P) === 0.0
@test maximum(P) === 3.0
@test minimum(-P) === -3.0
@test maximum(-P) === 0.0
@test maximum(P, dims=(1,)) == [1.0 2.0 3.0]
@test maximum(P, dims=(2,)) == reshape([1.0,2.0,3.0],3,1)
@test maximum(P, dims=(1,2)) == reshape([3.0],1,1)
@test maximum(sparse(fill(-1,3,3))) == -1
@test minimum(sparse(fill(1,3,3))) == 1
end
@testset "unary functions" begin
A = sprand(5, 15, 0.5)
C = A + im*A
Afull = Array(A)
Cfull = Array(C)
# Test representatives of [unary functions that map zeros to zeros and may map nonzeros to zeros]
@test sin.(Afull) == Array(sin.(A))
@test tan.(Afull) == Array(tan.(A)) # should be redundant with sin test
@test ceil.(Afull) == Array(ceil.(A))
@test floor.(Afull) == Array(floor.(A)) # should be redundant with ceil test
@test real.(Afull) == Array(real.(A)) == Array(real(A))
@test imag.(Afull) == Array(imag.(A)) == Array(imag(A))
@test conj.(Afull) == Array(conj.(A)) == Array(conj(A))
@test real.(Cfull) == Array(real.(C)) == Array(real(C))
@test imag.(Cfull) == Array(imag.(C)) == Array(imag(C))
@test conj.(Cfull) == Array(conj.(C)) == Array(conj(C))
# Test representatives of [unary functions that map zeros to zeros and nonzeros to nonzeros]
@test expm1.(Afull) == Array(expm1.(A))
@test abs.(Afull) == Array(abs.(A))
@test abs2.(Afull) == Array(abs2.(A))
@test abs.(Cfull) == Array(abs.(C))
@test abs2.(Cfull) == Array(abs2.(C))
# Test representatives of [unary functions that map both zeros and nonzeros to nonzeros]
@test cos.(Afull) == Array(cos.(A))
# Test representatives of remaining vectorized-nonbroadcast unary functions
@test ceil.(Int, Afull) == Array(ceil.(Int, A))
@test floor.(Int, Afull) == Array(floor.(Int, A))
# Tests of real, imag, abs, and abs2 for SparseMatrixCSC{Int,X}s previously elsewhere
for T in (Int, Float16, Float32, Float64, BigInt, BigFloat)
R = rand(T[1:100;], 2, 2)
I = rand(T[1:100;], 2, 2)
D = R + I*im
S = sparse(D)
spR = sparse(R)
@test R == real.(S) == real(S)
@test I == imag.(S) == imag(S)
@test conj(Array(S)) == conj.(S) == conj(S)
@test real.(spR) == R
@test nnz(imag.(spR)) == nnz(imag(spR)) == 0
@test abs.(S) == abs.(D)
@test abs2.(S) == abs2.(D)
# test aliasing of real and conj of real valued matrix
@test real(spR) === spR
@test conj(spR) === spR
end
end
@testset "getindex" begin
ni = 23
nj = 32
a116 = reshape(1:(ni*nj), ni, nj)
s116 = sparse(a116)
ad116 = diagm(0 => diag(a116))
sd116 = sparse(ad116)
for (aa116, ss116) in [(a116, s116), (ad116, sd116)]
ij=11; i=3; j=2
@test ss116[ij] == aa116[ij]
@test ss116[(i,j)] == aa116[i,j]
@test ss116[i,j] == aa116[i,j]
@test ss116[i-1,j] == aa116[i-1,j]
ss116[i,j] = 0
@test ss116[i,j] == 0
ss116 = sparse(aa116)
@test ss116[:,:] == copy(ss116)
# range indexing
@test Array(ss116[i,:]) == aa116[i,:]
@test Array(ss116[:,j]) == aa116[:,j]
@test Array(ss116[i,1:2:end]) == aa116[i,1:2:end]
@test Array(ss116[1:2:end,j]) == aa116[1:2:end,j]
@test Array(ss116[i,end:-2:1]) == aa116[i,end:-2:1]
@test Array(ss116[end:-2:1,j]) == aa116[end:-2:1,j]
# float-range indexing is not supported
# sorted vector indexing
@test Array(ss116[i,[3:2:end-3;]]) == aa116[i,[3:2:end-3;]]
@test Array(ss116[[3:2:end-3;],j]) == aa116[[3:2:end-3;],j]
@test Array(ss116[i,[end-3:-2:1;]]) == aa116[i,[end-3:-2:1;]]
@test Array(ss116[[end-3:-2:1;],j]) == aa116[[end-3:-2:1;],j]
# unsorted vector indexing with repetition
p = [4, 1, 2, 3, 2, 6]
@test Array(ss116[p,:]) == aa116[p,:]
@test Array(ss116[:,p]) == aa116[:,p]
@test Array(ss116[p,p]) == aa116[p,p]
# bool indexing
li = bitrand(size(aa116,1))
lj = bitrand(size(aa116,2))
@test Array(ss116[li,j]) == aa116[li,j]
@test Array(ss116[li,:]) == aa116[li,:]
@test Array(ss116[i,lj]) == aa116[i,lj]
@test Array(ss116[:,lj]) == aa116[:,lj]
@test Array(ss116[li,lj]) == aa116[li,lj]
# empty indices
for empty in (1:0, Int[])
@test Array(ss116[empty,:]) == aa116[empty,:]
@test Array(ss116[:,empty]) == aa116[:,empty]
@test Array(ss116[empty,lj]) == aa116[empty,lj]
@test Array(ss116[li,empty]) == aa116[li,empty]
@test Array(ss116[empty,empty]) == aa116[empty,empty]
end
# out of bounds indexing
@test_throws BoundsError ss116[0, 1]
@test_throws BoundsError ss116[end+1, 1]
@test_throws BoundsError ss116[1, 0]
@test_throws BoundsError ss116[1, end+1]
for j in (1, 1:size(s116,2), 1:1, Int[1], trues(size(s116, 2)), 1:0, Int[])
@test_throws BoundsError ss116[0:1, j]
@test_throws BoundsError ss116[[0, 1], j]
@test_throws BoundsError ss116[end:end+1, j]
@test_throws BoundsError ss116[[end, end+1], j]
end
for i in (1, 1:size(s116,1), 1:1, Int[1], trues(size(s116, 1)), 1:0, Int[])
@test_throws BoundsError ss116[i, 0:1]
@test_throws BoundsError ss116[i, [0, 1]]
@test_throws BoundsError ss116[i, end:end+1]
@test_throws BoundsError ss116[i, [end, end+1]]
end
end
# workaround issue #7197: comment out let-block
#let S = SparseMatrixCSC(3, 3, UInt8[1,1,1,1], UInt8[], Int64[])
S1290 = SparseMatrixCSC(3, 3, UInt8[1,1,1,1], UInt8[], Int64[])
S1290[1,1] = 1
S1290[5] = 2
S1290[end] = 3
@test S1290[end] == (S1290[1] + S1290[2,2])
@test 6 == sum(diag(S1290))
@test Array(S1290)[[3,1],1] == Array(S1290[[3,1],1])
# check that indexing with an abstract array returns matrix
# with same colptr and rowval eltypes as input. Tests PR 24548
r1 = S1290[[5,9]]
r2 = S1290[[1 2;5 9]]
@test isa(r1, SparseVector{Int64,UInt8})
@test isa(r2, SparseMatrixCSC{Int64,UInt8})
# end
end
@testset "setindex" begin
a = spzeros(Int, 10, 10)
@test count(!iszero, a) == 0
a[1,:] .= 1
@test count(!iszero, a) == 10
@test a[1,:] == sparse(fill(1,10))
a[:,2] .= 2
@test count(!iszero, a) == 19
@test a[:,2] == sparse(fill(2,10))
b = copy(a)
# Zero-assignment behavior of setindex!(A, v, i, j)
a[1,3] = 0
@test nnz(a) == 19
@test count(!iszero, a) == 18
a[2,1] = 0
@test nnz(a) == 19
@test count(!iszero, a) == 18
# Zero-assignment behavior of setindex!(A, v, I, J)
a[1,:] .= 0
@test nnz(a) == 19
@test count(!iszero, a) == 9
a[2,:] .= 0
@test nnz(a) == 19
@test count(!iszero, a) == 8
a[:,1] .= 0
@test nnz(a) == 19
@test count(!iszero, a) == 8
a[:,2] .= 0
@test nnz(a) == 19
@test count(!iszero, a) == 0
a = copy(b)
a[:,:] .= 0
@test nnz(a) == 19
@test count(!iszero, a) == 0
# Zero-assignment behavior of setindex!(A, B::SparseMatrixCSC, I, J)
a = copy(b)
a[1:2,:] = spzeros(2, 10)
@test nnz(a) == 19
@test count(!iszero, a) == 8
a[1:2,1:3] = sparse([1 0 1; 0 0 1])
@test nnz(a) == 20
@test count(!iszero, a) == 11
a = copy(b)
a[1:2,:] = let c = sparse(fill(1,2,10)); fill!(c.nzval, 0); c; end
@test nnz(a) == 19
@test count(!iszero, a) == 8
a[1:2,1:3] = let c = sparse(fill(1,2,3)); c[1,2] = c[2,1] = c[2,2] = 0; c; end
@test nnz(a) == 20
@test count(!iszero, a) == 11
a[1,:] = 1:10
@test a[1,:] == sparse([1:10;])
a[:,2] = 1:10
@test a[:,2] == sparse([1:10;])
a[1,1:0] = []
@test a[1,:] == sparse([1; 1; 3:10])
a[1:0,2] = []
@test a[:,2] == sparse([1:10;])
a[1,1:0] .= 0
@test a[1,:] == sparse([1; 1; 3:10])
a[1:0,2] .= 0
@test a[:,2] == sparse([1:10;])
a[1,1:0] .= 1
@test a[1,:] == sparse([1; 1; 3:10])
a[1:0,2] .= 1
@test a[:,2] == sparse([1:10;])
@test_throws BoundsError a[:,11] = spzeros(10,1)
@test_throws BoundsError a[11,:] = spzeros(1,10)
@test_throws BoundsError a[:,-1] = spzeros(10,1)
@test_throws BoundsError a[-1,:] = spzeros(1,10)
@test_throws BoundsError a[0:9] = spzeros(1,10)
@test_throws BoundsError (a[:,11] .= 0; a)
@test_throws BoundsError (a[11,:] .= 0; a)
@test_throws BoundsError (a[:,-1] .= 0; a)
@test_throws BoundsError (a[-1,:] .= 0; a)
@test_throws BoundsError (a[0:9] .= 0; a)
@test_throws BoundsError (a[:,11] .= 1; a)
@test_throws BoundsError (a[11,:] .= 1; a)
@test_throws BoundsError (a[:,-1] .= 1; a)
@test_throws BoundsError (a[-1,:] .= 1; a)
@test_throws BoundsError (a[0:9] .= 1; a)
@test_throws DimensionMismatch a[1:2,1:2] = 1:3
@test_throws DimensionMismatch a[1:2,1] = 1:3
@test_throws DimensionMismatch a[1,1:2] = 1:3
@test_throws DimensionMismatch a[1:2] = 1:3
A = spzeros(Int, 10, 20)
A[1:5,1:10] .= 10
A[1:5,1:10] .= 10
@test count(!iszero, A) == 50
@test A[1:5,1:10] == fill(10, 5, 10)
A[6:10,11:20] .= 0
@test count(!iszero, A) == 50
A[6:10,11:20] .= 20
@test count(!iszero, A) == 100
@test A[6:10,11:20] == fill(20, 5, 10)
A[4:8,8:16] .= 15
@test count(!iszero, A) == 121
@test A[4:8,8:16] == fill(15, 5, 9)
ASZ = 1000
TSZ = 800
A = sprand(ASZ, 2*ASZ, 0.0001)
B = copy(A)
nA = count(!iszero, A)
x = A[1:TSZ, 1:(2*TSZ)]
nx = count(!iszero, x)
A[1:TSZ, 1:(2*TSZ)] .= 0
nB = count(!iszero, A)
@test nB == (nA - nx)
A[1:TSZ, 1:(2*TSZ)] = x
@test count(!iszero, A) == nA
@test A == B
A[1:TSZ, 1:(2*TSZ)] .= 10
@test count(!iszero, A) == nB + 2*TSZ*TSZ
A[1:TSZ, 1:(2*TSZ)] = x
@test count(!iszero, A) == nA
@test A == B
A = sparse(1I, 5, 5)
lininds = 1:10
X=reshape([trues(10); falses(15)],5,5)
@test A[lininds] == A[X] == [1,0,0,0,0,0,1,0,0,0]
A[lininds] = [1:10;]
@test A[lininds] == A[X] == 1:10
A[lininds] = zeros(Int, 10)
@test nnz(A) == 13
@test count(!iszero, A) == 3
@test A[lininds] == A[X] == zeros(Int, 10)
c = Vector(11:20); c[1] = c[3] = 0
A[lininds] = c
@test nnz(A) == 13
@test count(!iszero, A) == 11
@test A[lininds] == A[X] == c
A = sparse(1I, 5, 5)
A[lininds] = c
@test nnz(A) == 12
@test count(!iszero, A) == 11
@test A[lininds] == A[X] == c
let # prevent assignment to I from overwriting UniformSampling in enclosing scope
S = sprand(50, 30, 0.5, x -> round.(Int, rand(x) * 100))
I = sprand(Bool, 50, 30, 0.2)
FS = Array(S)
FI = Array(I)
@test sparse(FS[FI]) == S[I] == S[FI]
@test sum(S[FI]) + sum(S[.!FI]) == sum(S)
@test count(!iszero, I) == count(I)
sumS1 = sum(S)
sumFI = sum(S[FI])
nnzS1 = nnz(S)
S[FI] .= 0
sumS2 = sum(S)
cnzS2 = count(!iszero, S)
@test sum(S[FI]) == 0
@test nnz(S) == nnzS1
@test (sum(S) + sumFI) == sumS1
S[FI] .= 10
nnzS3 = nnz(S)
@test sum(S) == sumS2 + 10*sum(FI)
S[FI] .= 0
@test sum(S) == sumS2
@test nnz(S) == nnzS3
@test count(!iszero, S) == cnzS2
S[FI] .= [1:sum(FI);]
@test sum(S) == sumS2 + sum(1:sum(FI))
S = sprand(50, 30, 0.5, x -> round.(Int, rand(x) * 100))
N = length(S) >> 2
I = randperm(N) .* 4
J = randperm(N)
sumS1 = sum(S)
sumS2 = sum(S[I])
S[I] .= 0
@test sum(S) == (sumS1 - sumS2)
S[I] .= J
@test sum(S) == (sumS1 - sumS2 + sum(J))
end
end
@testset "dropstored!" begin
A = spzeros(Int, 10, 10)
# Introduce nonzeros in row and column two
A[1,:] .= 1
A[:,2] .= 2
@test nnz(A) == 19
# Test argument bounds checking for dropstored!(A, i, j)
@test_throws BoundsError SparseArrays.dropstored!(A, 0, 1)
@test_throws BoundsError SparseArrays.dropstored!(A, 1, 0)
@test_throws BoundsError SparseArrays.dropstored!(A, 1, 11)
@test_throws BoundsError SparseArrays.dropstored!(A, 11, 1)
# Test argument bounds checking for dropstored!(A, I, J)
@test_throws BoundsError SparseArrays.dropstored!(A, 0:1, 1:1)
@test_throws BoundsError SparseArrays.dropstored!(A, 1:1, 0:1)
@test_throws BoundsError SparseArrays.dropstored!(A, 10:11, 1:1)
@test_throws BoundsError SparseArrays.dropstored!(A, 1:1, 10:11)
# Test behavior of dropstored!(A, i, j)
# --> Test dropping a single stored entry
SparseArrays.dropstored!(A, 1, 2)
@test nnz(A) == 18
# --> Test dropping a single nonstored entry