@@ -39,124 +39,14 @@ julia> Pkg.add("BlockSparseArrays")
3939
4040# ## Examples
4141
42- using BlockArrays: BlockArrays, BlockedVector, Block, blockedrange
42+ using BlockArrays: Block
4343using BlockSparseArrays: BlockSparseArray, blockstoredlength
44- using Test: @test , @test_broken
45-
46- function main ()
47- # # Block dimensions
48- i1 = [2 , 3 ]
49- i2 = [2 , 3 ]
50-
51- i_axes = (blockedrange (i1), blockedrange (i2))
52-
53- function block_size (axes, block)
54- return length .(getindex .(axes, Block .(block. n)))
55- end
56-
57- # # Data
58- nz_blocks = Block .([(1 , 1 ), (2 , 2 )])
59- nz_block_sizes = [block_size (i_axes, nz_block) for nz_block in nz_blocks]
60- nz_block_lengths = prod .(nz_block_sizes)
61-
62- # # Blocks with contiguous underlying data
63- d_data = BlockedVector (randn (sum (nz_block_lengths)), nz_block_lengths)
64- d_blocks = [
65- reshape (@view (d_data[Block (i)]), block_size (i_axes, nz_blocks[i])) for
66- i in 1 : length (nz_blocks)
67- ]
68- b = BlockSparseArray (nz_blocks, d_blocks, i_axes)
69-
70- @test blockstoredlength (b) == 2
71-
72- # # Blocks with discontiguous underlying data
73- d_blocks = randn .(nz_block_sizes)
74- b = BlockSparseArray (nz_blocks, d_blocks, i_axes)
75-
76- @test blockstoredlength (b) == 2
77-
78- # # Access a block
79- @test b[Block (1 , 1 )] == d_blocks[1 ]
80-
81- # # Access a zero block, returns a zero matrix
82- @test b[Block (1 , 2 )] == zeros (2 , 3 )
83-
84- # # Set a zero block
85- a₁₂ = randn (2 , 3 )
86- b[Block (1 , 2 )] = a₁₂
87- @test b[Block (1 , 2 )] == a₁₂
88-
89- # # Matrix multiplication
90- @test b * b ≈ Array (b) * Array (b)
91-
92- permuted_b = permutedims (b, (2 , 1 ))
93- @test permuted_b isa BlockSparseArray
94- @test permuted_b == permutedims (Array (b), (2 , 1 ))
95-
96- @test b + b ≈ Array (b) + Array (b)
97- @test b + b isa BlockSparseArray
98- # # TODO : Fix this, broken.
99- @test_broken blockstoredlength (b + b) == 2
100-
101- scaled_b = 2 b
102- @test scaled_b ≈ 2 Array (b)
103- @test scaled_b isa BlockSparseArray
104-
105- # # TODO : Fix this, broken.
106- @test_broken reshape (b, ([4 , 6 , 6 , 9 ],)) isa BlockSparseArray{<: Any ,1 }
107-
108- return nothing
109- end
110-
111- main ()
112-
113- # # BlockSparseArrays.jl and BlockArrays.jl interface
114-
115- using BlockArrays: BlockArrays, Block
116- using BlockSparseArrays: BlockSparseArray
117-
118- i1 = [2 , 3 ]
119- i2 = [2 , 3 ]
120- B = BlockSparseArray {Float64} (i1, i2)
121- B[Block (1 , 1 )] = randn (2 , 2 )
122- B[Block (2 , 2 )] = randn (3 , 3 )
123-
124- # # Minimal interface
125-
126- # # Specifies the block structure
127- @show collect .(BlockArrays. blockaxes (axes (B, 1 )))
128-
129- # # Index range of a block
130- @show axes (B, 1 )[Block (1 )]
131-
132- # # Last index of each block
133- @show BlockArrays. blocklasts (axes (B, 1 ))
134-
135- # # Find the block containing the index
136- @show BlockArrays. findblock (axes (B, 1 ), 3 )
137-
138- # # Retrieve a block
139- @show B[Block (1 , 1 )]
140- @show BlockArrays. viewblock (B, Block (1 , 1 ))
141-
142- # # Check block bounds
143- @show BlockArrays. blockcheckbounds (B, 2 , 2 )
144- @show BlockArrays. blockcheckbounds (B, Block (2 , 2 ))
145-
146- # # Derived interface
147-
148- # # Specifies the block structure
149- @show collect (Iterators. product (BlockArrays. blockaxes (B)... ))
150-
151- # # Iterate over block views
152- @show sum .(BlockArrays. eachblock (B))
153-
154- # # Reshape into 1-d
155- # # TODO : Fix this, broken.
156- # # @show BlockArrays.blockvec(B)[Block(1)]
157-
158- # # Array-of-array view
159- @show BlockArrays. blocks (B)[1 , 1 ] == B[Block (1 , 1 )]
160-
161- # # Access an index within a block
162- @show B[Block (1 , 1 )[1 , 1 ]] == B[1 , 1 ]
44+ using Test: @test
45+
46+ a = BlockSparseArray {Float64} (undef, [2 , 3 ], [2 , 3 ])
47+ a[Block (1 , 2 )] = randn (2 , 3 )
48+ a[Block (2 , 1 )] = randn (3 , 2 )
49+ @test blockstoredlength (a) == 2
50+ b = a .+ 2 .* a'
51+ @test Array (b) ≈ Array (a) + 2 * Array (a' )
52+ @test blockstoredlength (b) == 2
0 commit comments