Skip to content

Commit 307437d

Browse files
committed
undef_blocks constructor for SparseArrayDOK, more sparsemortar constructors
1 parent a63764f commit 307437d

File tree

5 files changed

+117
-71
lines changed

5 files changed

+117
-71
lines changed

src/BlockSparseArrays.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ include("abstractblocksparsearray/cat.jl")
3939
include("abstractblocksparsearray/adapt.jl")
4040

4141
# functions specifically for BlockSparseArray
42-
include("blocksparsearray/defaults.jl")
4342
include("blocksparsearray/blocksparsearray.jl")
4443
include("blocksparsearray/blockdiagonalarray.jl")
4544

src/blocksparsearray/blockdiagonalarray.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ const BlockSparseDiagonal{T,A<:AbstractBlockSparseVector{T}} = Diagonal{T,A}
1212
end
1313

1414
function BlockDiagonal(blocks::AbstractVector{<:AbstractMatrix})
15-
return sparsemortar(
16-
Diagonal(blocks), (blockedrange(size.(blocks, 1)), blockedrange(size.(blocks, 2)))
17-
)
15+
return sparsemortar(Diagonal(blocks), size.(blocks, 1), size.(blocks, 2))
1816
end
1917

2018
function DiagonalArrays.diagonal(S::BlockSparseVector)

src/blocksparsearray/blocksparsearray.jl

Lines changed: 96 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,64 @@
1-
using BlockArrays: BlockArrays, Block, BlockedUnitRange, blockedrange, blocklength
1+
using BlockArrays:
2+
BlockArrays,
3+
Block,
4+
BlockedUnitRange,
5+
UndefBlocksInitializer,
6+
blockedrange,
7+
blocklength,
8+
undef_blocks
29
using DerivableInterfaces: @interface
310
using Dictionaries: Dictionary
411
using SparseArraysBase: SparseArrayDOK
512

13+
"""
14+
SparseArrayDOK{T}(::UndefBlocksInitializer, axes)
15+
SparseArrayDOK{T,N}(::UndefBlocksInitializer, axes)
16+
17+
Construct the block structure of an undefined BlockSparseArray that will have
18+
blocked axes `axes`.
19+
"""
20+
function SparseArraysBase.SparseArrayDOK{T,N}(
21+
::UndefBlocksInitializer, ax::Tuple{Vararg{AbstractUnitRange{<:Integer},N}}
22+
) where {T,N}
23+
return SparseArrayDOK{T,N}(undef, blocklength.(ax), GetUnstoredBlock(ax))
24+
end
25+
function SparseArraysBase.SparseArrayDOK{T,N}(
26+
::UndefBlocksInitializer, ax::Vararg{AbstractUnitRange{<:Integer},N}
27+
) where {T,N}
28+
return SparseArrayDOK{T,N}(undef_blocks, ax)
29+
end
30+
function SparseArraysBase.SparseArrayDOK{T,N}(
31+
::UndefBlocksInitializer, dims::Tuple{Vararg{AbstractVector{<:Integer},N}}
32+
) where {T,N}
33+
return SparseArrayDOK{T,N}(undef_blocks, blockedrange.(dims))
34+
end
35+
function SparseArraysBase.SparseArrayDOK{T,N}(
36+
::UndefBlocksInitializer, dims::Vararg{AbstractVector{<:Integer},N}
37+
) where {T,N}
38+
return SparseArrayDOK{T,N}(undef_blocks, blockedrange.(dims))
39+
end
40+
41+
function SparseArraysBase.SparseArrayDOK{T}(
42+
::UndefBlocksInitializer, ax::Tuple{Vararg{AbstractUnitRange{<:Integer},N}}
43+
) where {T,N}
44+
return SparseArrayDOK{T,N}(undef_blocks, ax)
45+
end
46+
function SparseArraysBase.SparseArrayDOK{T}(
47+
::UndefBlocksInitializer, ax::Vararg{AbstractUnitRange{<:Integer},N}
48+
) where {T,N}
49+
return SparseArrayDOK{T,N}(undef_blocks, ax)
50+
end
51+
function SparseArraysBase.SparseArrayDOK{T}(
52+
::UndefBlocksInitializer, dims::Tuple{Vararg{AbstractVector{<:Integer},N}}
53+
) where {T,N}
54+
return SparseArrayDOK{T,N}(undef_blocks, blockedrange.(dims))
55+
end
56+
function SparseArraysBase.SparseArrayDOK{T}(
57+
::UndefBlocksInitializer, dims::Vararg{AbstractVector{<:Integer},N}
58+
) where {T,N}
59+
return SparseArrayDOK{T,N}(undef_blocks, blockedrange.(dims))
60+
end
61+
662
function _BlockSparseArray end
763

864
struct BlockSparseArray{
@@ -46,12 +102,22 @@ function sparsemortar(
46102
) where {T,N}
47103
return _BlockSparseArray(blocks, axes)
48104
end
49-
50-
function BlockArrays.mortar(
51-
blocks::SparseArrayDOK{<:AbstractArray{T,N},N},
52-
axes::Tuple{Vararg{AbstractUnitRange{<:Integer},N}},
105+
function sparsemortar(
106+
blocks::AbstractArray{<:AbstractArray{T,N},N},
107+
axes::Vararg{AbstractUnitRange{<:Integer},N},
53108
) where {T,N}
54-
return _BlockSparseArray(blocks, axes)
109+
return sparsemortar(blocks, axes)
110+
end
111+
function sparsemortar(
112+
blocks::AbstractArray{<:AbstractArray{T,N},N},
113+
dims::Tuple{Vararg{AbstractVector{<:Integer},N}},
114+
) where {T,N}
115+
return sparsemortar(blocks, blockedrange.(dims))
116+
end
117+
function sparsemortar(
118+
blocks::AbstractArray{<:AbstractArray{T,N},N}, dims::Vararg{AbstractVector{<:Integer},N}
119+
) where {T,N}
120+
return sparsemortar(blocks, dims)
55121
end
56122

57123
@doc """
@@ -64,20 +130,19 @@ of block lengths in each dimension or a list of blocked ranges representing the
64130
""" BlockSparseArray
65131

66132
function BlockSparseArray{T,N,A}(
67-
::UndefInitializer, axes::Tuple{Vararg{AbstractUnitRange,N}}
133+
::UndefInitializer, axes::Tuple{Vararg{AbstractUnitRange{<:Integer},N}}
68134
) where {T,N,A<:AbstractArray{T,N}}
69-
blocks = default_blocks(A, axes)
70-
return _BlockSparseArray(blocks, axes)
135+
return _BlockSparseArray(SparseArrayDOK{A}(undef_blocks, axes), axes)
71136
end
72137

73138
function BlockSparseArray{T,N,A}(
74-
::UndefInitializer, axes::Vararg{AbstractUnitRange,N}
139+
::UndefInitializer, axes::Vararg{AbstractUnitRange{<:Integer},N}
75140
) where {T,N,A<:AbstractArray{T,N}}
76141
return BlockSparseArray{T,N,A}(undef, axes)
77142
end
78143

79144
function BlockSparseArray{T,N,A}(
80-
::UndefInitializer, dims::Tuple{Vararg{Vector{Int},N}}
145+
::UndefInitializer, dims::Tuple{Vararg{AbstractVector{<:Integer},N}}
81146
) where {T,N,A<:AbstractArray{T,N}}
82147
return BlockSparseArray{T,N,A}(undef, blockedrange.(dims))
83148
end
@@ -86,48 +151,51 @@ end
86151
function BlockSparseArray{T,0,A}(
87152
::UndefInitializer, axes::Tuple{}
88153
) where {T,A<:AbstractArray{T,0}}
89-
blocks = default_blocks(A, axes)
90-
return _BlockSparseArray(blocks, axes)
154+
return _BlockSparseArray(SparseArrayDOK{A}(undef_blocks, axes), axes)
91155
end
92156

93157
function BlockSparseArray{T,N,A}(
94-
::UndefInitializer, dims::Vararg{Vector{Int},N}
158+
::UndefInitializer, dims::Vararg{AbstractVector{<:Integer},N}
95159
) where {T,N,A<:AbstractArray{T,N}}
96160
return BlockSparseArray{T,N,A}(undef, dims)
97161
end
98162

99163
function BlockSparseArray{T,N}(
100-
::UndefInitializer, axes::Tuple{Vararg{AbstractUnitRange,N}}
164+
::UndefInitializer, axes::Tuple{Vararg{AbstractUnitRange{<:Integer},N}}
101165
) where {T,N}
102-
return BlockSparseArray{T,N,default_arraytype(T, axes)}(undef, axes)
166+
return BlockSparseArray{T,N,Array{T,N}}(undef, axes)
103167
end
104168

105169
function BlockSparseArray{T,N}(
106-
::UndefInitializer, axes::Vararg{AbstractUnitRange,N}
170+
::UndefInitializer, axes::Vararg{AbstractUnitRange{<:Integer},N}
107171
) where {T,N}
108172
return BlockSparseArray{T,N}(undef, axes)
109173
end
110174

111175
function BlockSparseArray{T,0}(::UndefInitializer, axes::Tuple{}) where {T}
112-
return BlockSparseArray{T,0,default_arraytype(T, axes)}(undef, axes)
176+
return BlockSparseArray{T,0,Array{T,0}}(undef, axes)
113177
end
114178

115179
function BlockSparseArray{T,N}(
116-
::UndefInitializer, dims::Tuple{Vararg{Vector{Int},N}}
180+
::UndefInitializer, dims::Tuple{Vararg{AbstractVector{<:Integer},N}}
117181
) where {T,N}
118182
return BlockSparseArray{T,N}(undef, blockedrange.(dims))
119183
end
120184

121-
function BlockSparseArray{T,N}(::UndefInitializer, dims::Vararg{Vector{Int},N}) where {T,N}
185+
function BlockSparseArray{T,N}(
186+
::UndefInitializer, dims::Vararg{AbstractVector{<:Integer},N}
187+
) where {T,N}
122188
return BlockSparseArray{T,N}(undef, dims)
123189
end
124190

125-
function BlockSparseArray{T}(::UndefInitializer, dims::Tuple{Vararg{Vector{Int}}}) where {T}
191+
function BlockSparseArray{T}(
192+
::UndefInitializer, dims::Tuple{Vararg{AbstractVector{<:Integer}}}
193+
) where {T}
126194
return BlockSparseArray{T,length(dims)}(undef, dims)
127195
end
128196

129197
function BlockSparseArray{T}(
130-
::UndefInitializer, axes::Tuple{Vararg{AbstractUnitRange}}
198+
::UndefInitializer, axes::Tuple{Vararg{AbstractUnitRange{<:Integer}}}
131199
) where {T}
132200
return BlockSparseArray{T,length(axes)}(undef, axes)
133201
end
@@ -136,11 +204,15 @@ function BlockSparseArray{T}(::UndefInitializer, axes::Tuple{}) where {T}
136204
return BlockSparseArray{T,length(axes)}(undef, axes)
137205
end
138206

139-
function BlockSparseArray{T}(::UndefInitializer, dims::Vararg{Vector{Int}}) where {T}
207+
function BlockSparseArray{T}(
208+
::UndefInitializer, dims::Vararg{AbstractVector{<:Integer}}
209+
) where {T}
140210
return BlockSparseArray{T}(undef, dims)
141211
end
142212

143-
function BlockSparseArray{T}(::UndefInitializer, axes::Vararg{AbstractUnitRange}) where {T}
213+
function BlockSparseArray{T}(
214+
::UndefInitializer, axes::Vararg{AbstractUnitRange{<:Integer}}
215+
) where {T}
144216
return BlockSparseArray{T}(undef, axes)
145217
end
146218

src/blocksparsearray/defaults.jl

Lines changed: 0 additions & 42 deletions
This file was deleted.

test/test_basics.jl

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ using BlockArrays:
1717
blocklengths,
1818
blocksize,
1919
blocksizes,
20-
mortar
20+
mortar,
21+
undef_blocks
2122
using BlockSparseArrays:
2223
@view!,
2324
BlockSparseArray,
@@ -158,6 +159,24 @@ arrayts = (Array, JLArray)
158159
@test (@constinferred blocktype(a)) <: SubArray{elt,2,arrayt{elt,2}}
159160
# TODO: This is difficult to determine just from type information.
160161
@test_broken blocktype(typeof(a)) <: SubArray{elt,2,arrayt{elt,2}}
162+
163+
# sparsemortar
164+
for ax in (
165+
([2, 3], [2, 3]),
166+
(([2, 3], [2, 3]),),
167+
blockedrange.(([2, 3], [2, 3])),
168+
(blockedrange.(([2, 3], [2, 3])),),
169+
)
170+
blocks = SparseArrayDOK{arrayt{elt,2}}(undef_blocks, ax...)
171+
blocks[2, 1] = arrayt(randn(elt, 3, 2))
172+
blocks[1, 2] = arrayt(randn(elt, 2, 3))
173+
a = sparsemortar(blocks, ax...)
174+
@test a isa BlockSparseArray{elt,2,arrayt{elt,2}}
175+
@test iszero(a[Block(1, 1)])
176+
@test a[Block(2, 1)] == blocks[2, 1]
177+
@test a[Block(1, 2)] == blocks[1, 2]
178+
@test iszero(a[Block(2, 2)])
179+
end
161180
end
162181
@testset "Basics" begin
163182
a = dev(BlockSparseArray{elt}(undef, [2, 3], [2, 3]))

0 commit comments

Comments
 (0)