@@ -13,6 +13,7 @@ using BlockArrays:
13
13
blocksize,
14
14
blocksizes,
15
15
mortar
16
+ using Compat: @compat
16
17
using LinearAlgebra: mul!
17
18
using NDTensors. BlockSparseArrays:
18
19
@view! , BlockSparseArray, block_nstored, block_reshape, view!
@@ -23,22 +24,21 @@ include("TestBlockSparseArraysUtils.jl")
23
24
@testset " BlockSparseArrays (eltype=$elt )" for elt in
24
25
(Float32, Float64, ComplexF32, ComplexF64)
25
26
@testset " Broken" begin
26
- a = BlockSparseArray {elt} ([2 , 3 ], [3 , 4 ])
27
- @test_broken a[Block (1 , 2 )] .= 1
28
-
29
27
a = BlockSparseArray {elt} ([2 , 3 ], [3 , 4 ])
30
28
b = @view a[[Block (2 ), Block (1 )], [Block (2 ), Block (1 )]]
29
+ @test b isa SubArray{<: Any ,<: Any ,<: BlockSparseArray }
31
30
@test_broken b[2 : 4 , 2 : 4 ]
32
31
32
+ a = BlockSparseArray {elt} ([2 , 3 ], [3 , 4 ])
33
+ b = @views a[[Block (2 ), Block (1 )], [Block (2 ), Block (1 )]][Block (1 , 1 )]
34
+ @test_broken b isa SubArray{<: Any ,<: Any ,<: BlockSparseArray }
35
+
33
36
a = BlockSparseArray {elt} ([2 , 3 ], [3 , 4 ])
34
37
b = @views a[Block (1 , 1 )][1 : 2 , 1 : 1 ]
38
+ @test b isa SubArray{<: Any ,<: Any ,<: BlockSparseArray }
35
39
for i in parentindices (b)
36
40
@test_broken i isa BlockSlice{<: BlockIndexRange{1} }
37
41
end
38
-
39
- a = BlockSparseArray {elt} ([2 , 3 ], [3 , 4 ])
40
- b = @view a[[Block (2 ), Block (1 )], [Block (2 ), Block (1 )]]
41
- @test_broken b[Block (1 , 1 )] = randn (3 , 3 )
42
42
end
43
43
@testset " Basics" begin
44
44
a = BlockSparseArray {elt} ([2 , 3 ], [2 , 3 ])
@@ -82,6 +82,26 @@ include("TestBlockSparseArraysUtils.jl")
82
82
@test block_nstored (a) == 2
83
83
@test nstored (a) == 2 * 4 + 3 * 3
84
84
85
+ a = BlockSparseArray {elt} ([2 , 3 ], [3 , 4 ])
86
+ a[Block (1 , 2 )] .= 2
87
+ @test eltype (a) == elt
88
+ @test all (== (2 ), a[Block (1 , 2 )])
89
+ @test iszero (a[Block (1 , 1 )])
90
+ @test iszero (a[Block (2 , 1 )])
91
+ @test iszero (a[Block (2 , 2 )])
92
+ @test block_nstored (a) == 1
93
+ @test nstored (a) == 2 * 4
94
+
95
+ a = BlockSparseArray {elt} ([2 , 3 ], [3 , 4 ])
96
+ a[Block (1 , 2 )] .= 0
97
+ @test eltype (a) == elt
98
+ @test iszero (a[Block (1 , 1 )])
99
+ @test iszero (a[Block (2 , 1 )])
100
+ @test iszero (a[Block (1 , 2 )])
101
+ @test iszero (a[Block (2 , 2 )])
102
+ @test block_nstored (a) == 1
103
+ @test nstored (a) == 2 * 4
104
+
85
105
a = BlockSparseArray {elt} (undef, ([2 , 3 ], [3 , 4 ]))
86
106
@views for b in [Block (1 , 2 ), Block (2 , 1 )]
87
107
a[b] = randn (elt, size (a[b]))
@@ -490,13 +510,45 @@ include("TestBlockSparseArraysUtils.jl")
490
510
@test b[Block (2 , 2 )] == x
491
511
end
492
512
513
+ function f1 ()
514
+ a = BlockSparseArray {elt} ([2 , 3 ], [3 , 4 ])
515
+ b = @view a[[Block (2 ), Block (1 )], [Block (2 ), Block (1 )]]
516
+ x = randn (elt, 3 , 4 )
517
+ b[Block (1 , 1 )] .= x
518
+ return (; a, b, x)
519
+ end
520
+ function f2 ()
521
+ a = BlockSparseArray {elt} ([2 , 3 ], [3 , 4 ])
522
+ b = @view a[[Block (2 ), Block (1 )], [Block (2 ), Block (1 )]]
523
+ x = randn (elt, 3 , 4 )
524
+ b[Block (1 , 1 )] = x
525
+ return (; a, b, x)
526
+ end
527
+ for abx in (f1 (), f2 ())
528
+ @compat (; a, b, x) = abx
529
+ @test b isa SubArray{<: Any ,<: Any ,<: BlockSparseArray }
530
+ @test block_nstored (b) == 1
531
+ @test b[Block (1 , 1 )] == x
532
+ for blck in [Block (2 , 1 ), Block (1 , 2 ), Block (2 , 2 )]
533
+ @test iszero (b[blck])
534
+ end
535
+ @test block_nstored (a) == 1
536
+ @test a[Block (2 , 2 )] == x
537
+ for blck in [Block (1 , 1 ), Block (2 , 1 ), Block (1 , 2 )]
538
+ @test iszero (a[blck])
539
+ end
540
+ @test_throws DimensionMismatch b[Block (1 , 1 )] .= randn (2 , 3 )
541
+ end
542
+
493
543
a = BlockSparseArray {elt} ([2 , 3 ], [3 , 4 ])
494
- b = @view a[[Block (2 ), Block (1 )], [Block (2 ), Block (1 )]]
495
- x = randn (elt, 3 , 4 )
496
- b[Block (1 , 1 )] .= x
497
- @test b[Block (1 , 1 )] == x
498
- @test a[Block (2 , 2 )] == x
499
- @test_throws DimensionMismatch b[Block (1 , 1 )] .= randn (2 , 3 )
544
+ b = @views a[[Block (2 ), Block (1 )], [Block (2 ), Block (1 )]][Block (2 , 1 )]
545
+ @test iszero (b)
546
+ @test size (b) == (2 , 4 )
547
+ x = randn (elt, 2 , 4 )
548
+ b .= x
549
+ @test b == x
550
+ @test a[Block (1 , 2 )] == x
551
+ @test block_nstored (a) == 1
500
552
501
553
a = BlockSparseArray {elt} ([2 , 3 ], [3 , 4 ])
502
554
b = @view a[[Block (2 ), Block (1 )], [Block (2 ), Block (1 )]]
0 commit comments