Skip to content

Commit b8aa0cd

Browse files
committed
Various improvements
1 parent 760cf9c commit b8aa0cd

File tree

3 files changed

+54
-52
lines changed

3 files changed

+54
-52
lines changed

src/blocksparsearray/blockdiagonalarray.jl

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

1414
function BlockDiagonal(blocks::AbstractVector{<:AbstractMatrix})
15-
return sparsemortar(Diagonal(blocks), size.(blocks, 1), size.(blocks, 2))
15+
return _BlockSparseArray(Diagonal(blocks), size.(blocks, 1), size.(blocks, 2))
1616
end
1717

1818
function DiagonalArrays.diagonal(S::BlockSparseVector)

src/blocksparsearray/blocksparsearray.jl

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ using Dictionaries: Dictionary
1111
using SparseArraysBase: SparseArrayDOK
1212

1313
"""
14-
SparseArrayDOK{T}(::UndefBlocksInitializer, axes)
15-
SparseArrayDOK{T,N}(::UndefBlocksInitializer, axes)
14+
SparseArrayDOK{T}(undef_blocks, axes)
15+
SparseArrayDOK{T,N}(undef_blocks, axes)
1616
1717
Construct the block structure of an undefined BlockSparseArray that will have
1818
blocked axes `axes`.
19+
20+
See also: [`undef_blocks`](@ref BlockArrays.undef_blocks)`
1921
"""
2022
function SparseArraysBase.SparseArrayDOK{T,N}(
2123
::UndefBlocksInitializer, ax::Tuple{Vararg{AbstractUnitRange{<:Integer},N}}
@@ -28,14 +30,17 @@ function SparseArraysBase.SparseArrayDOK{T,N}(
2830
return SparseArrayDOK{T,N}(undef_blocks, ax)
2931
end
3032
function SparseArraysBase.SparseArrayDOK{T,N}(
31-
::UndefBlocksInitializer, dims::Tuple{Vararg{AbstractVector{<:Integer},N}}
33+
::UndefBlocksInitializer,
34+
dims::Tuple{AbstractVector{<:Integer},Vararg{AbstractVector{<:Integer}}},
3235
) where {T,N}
3336
return SparseArrayDOK{T,N}(undef_blocks, blockedrange.(dims))
3437
end
3538
function SparseArraysBase.SparseArrayDOK{T,N}(
36-
::UndefBlocksInitializer, dims::Vararg{AbstractVector{<:Integer},N}
39+
::UndefBlocksInitializer,
40+
dim1::AbstractVector{<:Integer},
41+
dim_rest::AbstractVector{<:Integer}...,
3742
) where {T,N}
38-
return SparseArrayDOK{T,N}(undef_blocks, blockedrange.(dims))
43+
return SparseArrayDOK{T,N}(undef_blocks, (dim1, dim_rest...))
3944
end
4045

4146
function SparseArraysBase.SparseArrayDOK{T}(
@@ -49,14 +54,17 @@ function SparseArraysBase.SparseArrayDOK{T}(
4954
return SparseArrayDOK{T,N}(undef_blocks, ax)
5055
end
5156
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))
57+
::UndefBlocksInitializer,
58+
dims::Tuple{AbstractVector{<:Integer},Vararg{AbstractVector{<:Integer}}},
59+
) where {T}
60+
return SparseArrayDOK{T}(undef_blocks, blockedrange.(dims))
5561
end
5662
function SparseArraysBase.SparseArrayDOK{T}(
57-
::UndefBlocksInitializer, dims::Vararg{AbstractVector{<:Integer},N}
58-
) where {T,N}
59-
return SparseArrayDOK{T,N}(undef_blocks, blockedrange.(dims))
63+
::UndefBlocksInitializer,
64+
dim1::AbstractVector{<:Integer},
65+
dim_rest::AbstractVector{<:Integer}...,
66+
) where {T}
67+
return SparseArrayDOK{T}(undef_blocks, (dim1, dim_rest...))
6068
end
6169

6270
function _BlockSparseArray end
@@ -110,14 +118,16 @@ function sparsemortar(
110118
end
111119
function sparsemortar(
112120
blocks::AbstractArray{<:AbstractArray{T,N},N},
113-
dims::Tuple{Vararg{AbstractVector{<:Integer},N}},
121+
dims::Tuple{AbstractVector{<:Integer},Vararg{AbstractVector{<:Integer}}},
114122
) where {T,N}
115123
return sparsemortar(blocks, blockedrange.(dims))
116124
end
117125
function sparsemortar(
118-
blocks::AbstractArray{<:AbstractArray{T,N},N}, dims::Vararg{AbstractVector{<:Integer},N}
126+
blocks::AbstractArray{<:AbstractArray{T,N},N},
127+
dim1::AbstractVector{<:Integer},
128+
dim_rest::AbstractVector{<:Integer}...,
119129
) where {T,N}
120-
return sparsemortar(blocks, dims)
130+
return sparsemortar(blocks, (dim1, dim_rest...))
121131
end
122132

123133
@doc """
@@ -142,22 +152,18 @@ function BlockSparseArray{T,N,A}(
142152
end
143153

144154
function BlockSparseArray{T,N,A}(
145-
::UndefInitializer, dims::Tuple{Vararg{AbstractVector{<:Integer},N}}
155+
::UndefInitializer,
156+
dims::Tuple{AbstractVector{<:Integer},Vararg{AbstractVector{<:Integer}}},
146157
) where {T,N,A<:AbstractArray{T,N}}
147158
return BlockSparseArray{T,N,A}(undef, blockedrange.(dims))
148159
end
149160

150-
# Fix ambiguity error.
151-
function BlockSparseArray{T,0,A}(
152-
::UndefInitializer, axes::Tuple{}
153-
) where {T,A<:AbstractArray{T,0}}
154-
return _BlockSparseArray(SparseArrayDOK{A}(undef_blocks, axes), axes)
155-
end
156-
157161
function BlockSparseArray{T,N,A}(
158-
::UndefInitializer, dims::Vararg{AbstractVector{<:Integer},N}
162+
::UndefInitializer,
163+
dim1::AbstractVector{<:Integer},
164+
dim_rest::AbstractVector{<:Integer}...,
159165
) where {T,N,A<:AbstractArray{T,N}}
160-
return BlockSparseArray{T,N,A}(undef, dims)
166+
return BlockSparseArray{T,N,A}(undef, (dim1, dim_rest...))
161167
end
162168

163169
function BlockSparseArray{T,N}(
@@ -172,24 +178,24 @@ function BlockSparseArray{T,N}(
172178
return BlockSparseArray{T,N}(undef, axes)
173179
end
174180

175-
function BlockSparseArray{T,0}(::UndefInitializer, axes::Tuple{}) where {T}
176-
return BlockSparseArray{T,0,Array{T,0}}(undef, axes)
177-
end
178-
179181
function BlockSparseArray{T,N}(
180-
::UndefInitializer, dims::Tuple{Vararg{AbstractVector{<:Integer},N}}
182+
::UndefInitializer,
183+
dims::Tuple{AbstractVector{<:Integer},Vararg{AbstractVector{<:Integer}}},
181184
) where {T,N}
182185
return BlockSparseArray{T,N}(undef, blockedrange.(dims))
183186
end
184187

185188
function BlockSparseArray{T,N}(
186-
::UndefInitializer, dims::Vararg{AbstractVector{<:Integer},N}
189+
::UndefInitializer,
190+
dim1::AbstractVector{<:Integer},
191+
dim_rest::AbstractVector{<:Integer}...,
187192
) where {T,N}
188-
return BlockSparseArray{T,N}(undef, dims)
193+
return BlockSparseArray{T,N}(undef, (dim1, dim_rest...))
189194
end
190195

191196
function BlockSparseArray{T}(
192-
::UndefInitializer, dims::Tuple{Vararg{AbstractVector{<:Integer}}}
197+
::UndefInitializer,
198+
dims::Tuple{AbstractVector{<:Integer},Vararg{AbstractVector{<:Integer}}},
193199
) where {T}
194200
return BlockSparseArray{T,length(dims)}(undef, dims)
195201
end
@@ -200,14 +206,12 @@ function BlockSparseArray{T}(
200206
return BlockSparseArray{T,length(axes)}(undef, axes)
201207
end
202208

203-
function BlockSparseArray{T}(::UndefInitializer, axes::Tuple{}) where {T}
204-
return BlockSparseArray{T,length(axes)}(undef, axes)
205-
end
206-
207209
function BlockSparseArray{T}(
208-
::UndefInitializer, dims::Vararg{AbstractVector{<:Integer}}
210+
::UndefInitializer,
211+
dim1::AbstractVector{<:Integer},
212+
dim_rest::AbstractVector{<:Integer}...,
209213
) where {T}
210-
return BlockSparseArray{T}(undef, dims)
214+
return BlockSparseArray{T}(undef, (dim1, dim_rest...))
211215
end
212216

213217
function BlockSparseArray{T}(
@@ -216,10 +220,6 @@ function BlockSparseArray{T}(
216220
return BlockSparseArray{T}(undef, axes)
217221
end
218222

219-
function BlockSparseArray{T}(::UndefInitializer) where {T}
220-
return BlockSparseArray{T}(undef, ())
221-
end
222-
223223
# Base `AbstractArray` interface
224224
Base.axes(a::BlockSparseArray) = a.axes
225225

src/blocksparsearrayinterface/map.jl

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
using BlockArrays: blocks, undef_blocks
21
using DerivableInterfaces: @interface, AbstractArrayInterface, interface
32
using GPUArraysCore: @allowscalar
4-
using SparseArraysBase: SparseArrayDOK, eachstoredindex
53

64
# TODO: Rewrite this so that it takes the blocking structure
75
# made by combining the blocking of the axes (i.e. the blocking that
@@ -101,12 +99,16 @@ end
10199
# specify the function preserves zeros, i.e.
102100
# `map(f, a; preserves_zeros=true)` or `@preserves_zeros map(f, a)`.
103101
function map_stored_blocks(f, a::AbstractArray)
104-
blocks_a = blocks(a)
105-
stored_indices = collect(eachstoredindex(blocks_a))
106-
stored_blocks = map(I -> f(blocks_a[I]), stored_indices)
107-
blocks_a′ = SparseArrayDOK{eltype(stored_blocks)}(undef_blocks, axes(a))
108-
for (I, b) in zip(stored_indices, stored_blocks)
109-
blocks_a′[I] = b
102+
block_stored_indices = collect(eachblockstoredindex(a))
103+
if isempty(block_stored_indices)
104+
blocktype_a′ = Base.promote_op(f, blocktype(a))
105+
return BlockSparseArray{eltype(blocktype_a′),ndims(a),blocktype_a′}(undef, axes(a))
110106
end
111-
return sparsemortar(blocks_a′, axes(a))
107+
stored_blocks = map(B -> f(@view!(a[B])), block_stored_indices)
108+
blocktype_a′ = eltype(stored_blocks)
109+
a′ = BlockSparseArray{eltype(blocktype_a′),ndims(a),blocktype_a′}(undef, axes(a))
110+
for (B, b) in zip(block_stored_indices, stored_blocks)
111+
a′[B] = b
112+
end
113+
return a′
112114
end

0 commit comments

Comments
 (0)