Skip to content

Commit 2b4ab96

Browse files
authored
Require undef in BlockSparseArray constructors (#68)
1 parent 5c72fbd commit 2b4ab96

18 files changed

+299
-494
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "BlockSparseArrays"
22
uuid = "2c9a651f-6452-4ace-a6ac-809f4280fbb4"
33
authors = ["ITensor developers <[email protected]> and contributors"]
4-
version = "0.2.28"
4+
version = "0.3.0"
55

66
[deps]
77
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"

README.md

+10-122
Original file line numberDiff line numberDiff line change
@@ -34,129 +34,17 @@ julia> Pkg.add("BlockSparseArrays")
3434
## Examples
3535

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

16250
---

docs/Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"
66

77
[compat]
88
BlockArrays = "1"
9-
BlockSparseArrays = "0.2"
9+
BlockSparseArrays = "0.3"
1010
Documenter = "1"
1111
Literate = "2"

examples/Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
55

66
[compat]
77
BlockArrays = "1"
8-
BlockSparseArrays = "0.2"
8+
BlockSparseArrays = "0.3"
99
Test = "1"

examples/README.jl

+10-120
Original file line numberDiff line numberDiff line change
@@ -39,124 +39,14 @@ julia> Pkg.add("BlockSparseArrays")
3939

4040
# ## Examples
4141

42-
using BlockArrays: BlockArrays, BlockedVector, Block, blockedrange
42+
using BlockArrays: Block
4343
using 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 = 2b
102-
@test scaled_b 2Array(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

ext/BlockSparseArraysGradedUnitRangesExt/BlockSparseArraysGradedUnitRangesExt.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function similar_blocksparse(
1717
return BlockSparseArray{
1818
elt,length(axes),similartype(unwrap_array_type(blocktype(a)), elt, axes)
1919
}(
20-
axes
20+
undef, axes
2121
)
2222
end
2323

src/BlockSparseArrays.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ export BlockSparseArray,
55
BlockSparseVector,
66
blockstoredlength,
77
eachblockstoredindex,
8-
eachstoredblock
8+
eachstoredblock,
9+
sparsemortar
910

1011
# factorizations
1112
include("factorizations/svd.jl")
@@ -38,7 +39,6 @@ include("abstractblocksparsearray/cat.jl")
3839
include("abstractblocksparsearray/adapt.jl")
3940

4041
# functions specifically for BlockSparseArray
41-
include("blocksparsearray/defaults.jl")
4242
include("blocksparsearray/blocksparsearray.jl")
4343
include("blocksparsearray/blockdiagonalarray.jl")
4444

src/abstractblocksparsearray/arraylayouts.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ function ArrayLayouts.sub_materialize(layout::BlockLayout{<:SparseLayout}, a, ax
3838
# TODO: Define `blocktype`/`blockstype` for `SubArray` wrapping `BlockSparseArray`.
3939
# TODO: Use `similar`?
4040
blocktype_a = blocktype(parent(a))
41-
a_dest = BlockSparseArray{eltype(a),length(axes),blocktype_a}(axes)
41+
a_dest = BlockSparseArray{eltype(a),length(axes),blocktype_a}(undef, axes)
4242
a_dest .= a
4343
return a_dest
4444
end

src/blocksparsearray/blockdiagonalarray.jl

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
# type alias for block-diagonal
2-
using LinearAlgebra: Diagonal
1+
using BlockArrays: blockedrange
32
using DiagonalArrays: DiagonalArrays, diagonal
3+
using LinearAlgebra: Diagonal
44

5+
# type alias for block-diagonal
56
const BlockDiagonal{T,A,Axes,V<:AbstractVector{A}} = BlockSparseMatrix{
67
T,A,Diagonal{A,V},Axes
78
}
@@ -12,8 +13,8 @@ const BlockSparseDiagonal{T,A<:AbstractBlockSparseVector{T}} = Diagonal{T,A}
1213
end
1314

1415
function BlockDiagonal(blocks::AbstractVector{<:AbstractMatrix})
15-
return BlockSparseArray(
16-
Diagonal(blocks), (blockedrange(size.(blocks, 1)), blockedrange(size.(blocks, 2)))
16+
return _BlockSparseArray(
17+
Diagonal(blocks), blockedrange.((size.(blocks, 1), size.(blocks, 2)))
1718
)
1819
end
1920

0 commit comments

Comments
 (0)