Skip to content

Commit d1fadaf

Browse files
committed
Improve type specificity in map_blocks
1 parent 07205d3 commit d1fadaf

File tree

1 file changed

+7
-2
lines changed
  • NDTensors/src/lib/BlockSparseArrays/src/blocksparsearrayinterface

1 file changed

+7
-2
lines changed

NDTensors/src/lib/BlockSparseArrays/src/blocksparsearrayinterface/map.jl

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ function map_stored_blocks(f, a::AbstractArray)
77
# TODO: `block_stored_indices` should output `Indices` storing
88
# the stored Blocks, not a `Dictionary` from cartesian indices
99
# to Blocks.
10-
bs = block_stored_indices(a)
11-
mapped_blocks = Dictionary(bs, map(b -> f(@view(a[b])), bs))
10+
bs = collect(block_stored_indices(a))
11+
ds = map(b -> f(@view(a[b])), bs)
12+
# We manually specify the block type using `Base.promote_op`
13+
# since `a[b]` may not be inferrable. For example, if `blocktype(a)`
14+
# is `Diagonal{Float64,Vector{Float64}}`, the non-stored blocks are `Matrix{Float64}`
15+
# since they can't necessarily by `Diagonal` if there are rectangular blocks.
16+
mapped_blocks = Dictionary{eltype(bs),eltype(ds)}(bs, ds)
1217
# TODO: Use `similartype(typeof(a), eltype(eltype(mapped_blocks)))(...)`.
1318
return BlockSparseArray(mapped_blocks, axes(a))
1419
end

0 commit comments

Comments
 (0)