Skip to content

Require undef in BlockSparseArray constructors #68

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Mar 5, 2025
Merged
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "BlockSparseArrays"
uuid = "2c9a651f-6452-4ace-a6ac-809f4280fbb4"
authors = ["ITensor developers <[email protected]> and contributors"]
version = "0.2.28"
version = "0.3.0"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ julia> Pkg.add("BlockSparseArrays")

````julia
using BlockArrays: BlockArrays, BlockedVector, Block, blockedrange
using BlockSparseArrays: BlockSparseArray, blockstoredlength
using BlockSparseArrays: BlockSparseArray, blockstoredlength, sparsemortar
using Test: @test, @test_broken

function main()
Expand All @@ -60,13 +60,13 @@ function main()
reshape(@view(d_data[Block(i)]), block_size(i_axes, nz_blocks[i])) for
i in 1:length(nz_blocks)
]
b = BlockSparseArray(nz_blocks, d_blocks, i_axes)
b = sparsemortar(nz_blocks, d_blocks, i_axes)

@test blockstoredlength(b) == 2

# Blocks with discontiguous underlying data
d_blocks = randn.(nz_block_sizes)
b = BlockSparseArray(nz_blocks, d_blocks, i_axes)
b = sparsemortar(nz_blocks, d_blocks, i_axes)

@test blockstoredlength(b) == 2

Expand Down Expand Up @@ -114,7 +114,7 @@ using BlockSparseArrays: BlockSparseArray

i1 = [2, 3]
i2 = [2, 3]
B = BlockSparseArray{Float64}(i1, i2)
B = BlockSparseArray{Float64}(undef, i1, i2)
B[Block(1, 1)] = randn(2, 2)
B[Block(2, 2)] = randn(3, 3)

Expand Down
2 changes: 1 addition & 1 deletion docs/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306"

[compat]
BlockArrays = "1"
BlockSparseArrays = "0.2"
BlockSparseArrays = "0.3"
Documenter = "1"
Literate = "2"
2 changes: 1 addition & 1 deletion examples/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

[compat]
BlockArrays = "1"
BlockSparseArrays = "0.2"
BlockSparseArrays = "0.3"
Test = "1"
8 changes: 4 additions & 4 deletions examples/README.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ julia> Pkg.add("BlockSparseArrays")
# ## Examples

using BlockArrays: BlockArrays, BlockedVector, Block, blockedrange
using BlockSparseArrays: BlockSparseArray, blockstoredlength
using BlockSparseArrays: BlockSparseArray, blockstoredlength, sparsemortar
using Test: @test, @test_broken

function main()
Expand All @@ -65,13 +65,13 @@ function main()
reshape(@view(d_data[Block(i)]), block_size(i_axes, nz_blocks[i])) for
i in 1:length(nz_blocks)
]
b = BlockSparseArray(nz_blocks, d_blocks, i_axes)
b = sparsemortar(nz_blocks, d_blocks, i_axes)

@test blockstoredlength(b) == 2

## Blocks with discontiguous underlying data
d_blocks = randn.(nz_block_sizes)
b = BlockSparseArray(nz_blocks, d_blocks, i_axes)
b = sparsemortar(nz_blocks, d_blocks, i_axes)

@test blockstoredlength(b) == 2

Expand Down Expand Up @@ -117,7 +117,7 @@ using BlockSparseArrays: BlockSparseArray

i1 = [2, 3]
i2 = [2, 3]
B = BlockSparseArray{Float64}(i1, i2)
B = BlockSparseArray{Float64}(undef, i1, i2)
B[Block(1, 1)] = randn(2, 2)
B[Block(2, 2)] = randn(3, 3)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function similar_blocksparse(
return BlockSparseArray{
elt,length(axes),similartype(unwrap_array_type(blocktype(a)), elt, axes)
}(
axes
undef, axes
)
end

Expand Down
3 changes: 2 additions & 1 deletion src/BlockSparseArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ export BlockSparseArray,
BlockSparseVector,
blockstoredlength,
eachblockstoredindex,
eachstoredblock
eachstoredblock,
sparsemortar

# factorizations
include("factorizations/svd.jl")
Expand Down
2 changes: 1 addition & 1 deletion src/abstractblocksparsearray/arraylayouts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
# TODO: Define `blocktype`/`blockstype` for `SubArray` wrapping `BlockSparseArray`.
# TODO: Use `similar`?
blocktype_a = blocktype(parent(a))
a_dest = BlockSparseArray{eltype(a),length(axes),blocktype_a}(axes)
a_dest = BlockSparseArray{eltype(a),length(axes),blocktype_a}(undef, axes)

Check warning on line 41 in src/abstractblocksparsearray/arraylayouts.jl

View check run for this annotation

Codecov / codecov/patch

src/abstractblocksparsearray/arraylayouts.jl#L41

Added line #L41 was not covered by tests
a_dest .= a
return a_dest
end
Expand Down
163 changes: 79 additions & 84 deletions src/blocksparsearray/blocksparsearray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
using Dictionaries: Dictionary
using SparseArraysBase: SparseArrayDOK

# TODO: Delete this.
## using BlockArrays: blocks

struct BlockSparseArray{
T,
N,
Expand All @@ -27,142 +24,140 @@
T,1,A,Blocks,Axes
}

function BlockSparseArray(
"""
sparsemortar(blocks::AbstractArray{<:AbstractArray{T,N},N}, axes) -> ::BlockSparseArray{T,N}

Construct a block sparse array from a sparse array of arrays and specified blocked axes.
The block sizes must be commensurate with the blocks of the axes.
"""
function sparsemortar(
blocks::AbstractArray{<:AbstractArray{T,N},N}, axes::Tuple{Vararg{AbstractUnitRange,N}}
) where {T,N}
return BlockSparseArray{T,N,eltype(blocks),typeof(blocks),typeof(axes)}(blocks, axes)
end

"""
sparsemortar(blocks::Dictionary{<:Block{N},<:AbstractArray{T,N}}, axes) -> ::BlockSparseArray{T,N}

Construct a block sparse array from a dictionary mapping the locations of the stored/nonzero
blocks to the data of those blocks, along with a set of blocked axes.
The block sizes must be commensurate with the blocks of the specified axes.
"""
function sparsemortar(
block_data::Dictionary{<:Block{N},<:AbstractArray{<:Any,N}},
axes::Tuple{Vararg{AbstractUnitRange,N}},
) where {N}
blocks = default_blocks(block_data, axes)
return BlockSparseArray(blocks, axes)
return sparsemortar(blocks, axes)
end

function BlockSparseArray(
"""
sparsemortar(block_indices::Vector{<:Block{N}}, block_data::Vector{<:AbstractArray{T,N}}, axes) -> ::BlockSparseArray{T,N}

Construct a block sparse array from a list of locations of the stored/nonzero blocks,
a corresponding list of the data of those blocks, along with a set of blocked axes.
The block sizes must be commensurate with the blocks of the specified axes.
"""
function sparsemortar(
block_indices::Vector{<:Block{N}},
block_data::Vector{<:AbstractArray{<:Any,N}},
axes::Tuple{Vararg{AbstractUnitRange,N}},
) where {N}
return BlockSparseArray(Dictionary(block_indices, block_data), axes)
return sparsemortar(Dictionary(block_indices, block_data), axes)
end

function BlockSparseArray{T,N,A,Blocks}(
blocks::AbstractArray{<:AbstractArray{T,N},N}, axes::Tuple{Vararg{AbstractUnitRange,N}}
) where {T,N,A<:AbstractArray{T,N},Blocks<:AbstractArray{A,N}}
return BlockSparseArray{T,N,A,Blocks,typeof(axes)}(blocks, axes)
end
@doc """
BlockSparseArray{T}(undef, dims)
BlockSparseArray{T,N}(undef, dims)
BlockSparseArray{T,N,A}(undef, dims)

function BlockSparseArray{T,N,A}(
blocks::AbstractArray{<:AbstractArray{T,N},N}, axes::Tuple{Vararg{AbstractUnitRange,N}}
) where {T,N,A<:AbstractArray{T,N}}
return BlockSparseArray{T,N,A,typeof(blocks)}(blocks, axes)
end

function BlockSparseArray{T,N}(
blocks::AbstractArray{<:AbstractArray{T,N},N}, axes::Tuple{Vararg{AbstractUnitRange,N}}
) where {T,N}
return BlockSparseArray{T,N,eltype(blocks),typeof(blocks),typeof(axes)}(blocks, axes)
end

function BlockSparseArray{T,N}(
block_data::Dictionary{Block{N,Int},<:AbstractArray{T,N}},
axes::Tuple{Vararg{AbstractUnitRange,N}},
) where {T,N}
blocks = default_blocks(block_data, axes)
return BlockSparseArray{T,N}(blocks, axes)
end
Construct an uninitialized N-dimensional BlockSparseArray containing elements of type T. `dims` should be a list
of block lengths in each dimension or a list of blocked ranges representing the axes.
""" BlockSparseArray

function BlockSparseArray{T,N,A}(
axes::Tuple{Vararg{AbstractUnitRange,N}}
::UndefInitializer, axes::Tuple{Vararg{AbstractUnitRange,N}}
) where {T,N,A<:AbstractArray{T,N}}
blocks = default_blocks(A, axes)
return BlockSparseArray{T,N,A}(blocks, axes)
return sparsemortar(blocks, axes)
end

function BlockSparseArray{T,N,A}(
axes::Vararg{AbstractUnitRange,N}
::UndefInitializer, axes::Vararg{AbstractUnitRange,N}
) where {T,N,A<:AbstractArray{T,N}}
return BlockSparseArray{T,N,A}(axes)
return BlockSparseArray{T,N,A}(undef, axes)

Check warning on line 88 in src/blocksparsearray/blocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/blocksparsearray/blocksparsearray.jl#L88

Added line #L88 was not covered by tests
end

function BlockSparseArray{T,N,A}(
dims::Tuple{Vararg{Vector{Int},N}}
::UndefInitializer, dims::Tuple{Vararg{Vector{Int},N}}
) where {T,N,A<:AbstractArray{T,N}}
return BlockSparseArray{T,N,A}(blockedrange.(dims))
return BlockSparseArray{T,N,A}(undef, blockedrange.(dims))

Check warning on line 94 in src/blocksparsearray/blocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/blocksparsearray/blocksparsearray.jl#L94

Added line #L94 was not covered by tests
end

# Fix ambiguity error.
function BlockSparseArray{T,0,A}(axes::Tuple{}) where {T,A<:AbstractArray{T,0}}
function BlockSparseArray{T,0,A}(

Check warning on line 98 in src/blocksparsearray/blocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/blocksparsearray/blocksparsearray.jl#L98

Added line #L98 was not covered by tests
::UndefInitializer, axes::Tuple{}
) where {T,A<:AbstractArray{T,0}}
blocks = default_blocks(A, axes)
return BlockSparseArray{T,0,A}(blocks, axes)
return sparsemortar(blocks, axes)

Check warning on line 102 in src/blocksparsearray/blocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/blocksparsearray/blocksparsearray.jl#L102

Added line #L102 was not covered by tests
end

function BlockSparseArray{T,N,A}(
dims::Vararg{Vector{Int},N}
::UndefInitializer, dims::Vararg{Vector{Int},N}
) where {T,N,A<:AbstractArray{T,N}}
return BlockSparseArray{T,N,A}(dims)
end

function BlockSparseArray{T,N}(axes::Tuple{Vararg{AbstractUnitRange,N}}) where {T,N}
return BlockSparseArray{T,N,default_arraytype(T, axes)}(axes)
end

function BlockSparseArray{T,N}(axes::Vararg{AbstractUnitRange,N}) where {T,N}
return BlockSparseArray{T,N}(axes)
end

function BlockSparseArray{T,0}(axes::Tuple{}) where {T}
return BlockSparseArray{T,0,default_arraytype(T, axes)}(axes)
return BlockSparseArray{T,N,A}(undef, dims)

Check warning on line 108 in src/blocksparsearray/blocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/blocksparsearray/blocksparsearray.jl#L108

Added line #L108 was not covered by tests
end

function BlockSparseArray{T,N}(dims::Tuple{Vararg{Vector{Int},N}}) where {T,N}
return BlockSparseArray{T,N}(blockedrange.(dims))
end

function BlockSparseArray{T,N}(dims::Vararg{Vector{Int},N}) where {T,N}
return BlockSparseArray{T,N}(dims)
function BlockSparseArray{T,N}(
::UndefInitializer, axes::Tuple{Vararg{AbstractUnitRange,N}}
) where {T,N}
return BlockSparseArray{T,N,default_arraytype(T, axes)}(undef, axes)
end

function BlockSparseArray{T}(dims::Tuple{Vararg{Vector{Int}}}) where {T}
return BlockSparseArray{T,length(dims)}(dims)
function BlockSparseArray{T,N}(

Check warning on line 117 in src/blocksparsearray/blocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/blocksparsearray/blocksparsearray.jl#L117

Added line #L117 was not covered by tests
::UndefInitializer, axes::Vararg{AbstractUnitRange,N}
) where {T,N}
return BlockSparseArray{T,N}(undef, axes)

Check warning on line 120 in src/blocksparsearray/blocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/blocksparsearray/blocksparsearray.jl#L120

Added line #L120 was not covered by tests
end

function BlockSparseArray{T}(axes::Tuple{Vararg{AbstractUnitRange}}) where {T}
return BlockSparseArray{T,length(axes)}(axes)
function BlockSparseArray{T,0}(::UndefInitializer, axes::Tuple{}) where {T}
return BlockSparseArray{T,0,default_arraytype(T, axes)}(undef, axes)

Check warning on line 124 in src/blocksparsearray/blocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/blocksparsearray/blocksparsearray.jl#L123-L124

Added lines #L123 - L124 were not covered by tests
end

function BlockSparseArray{T}(axes::Tuple{}) where {T}
return BlockSparseArray{T,length(axes)}(axes)
function BlockSparseArray{T,N}(
::UndefInitializer, dims::Tuple{Vararg{Vector{Int},N}}
) where {T,N}
return BlockSparseArray{T,N}(undef, blockedrange.(dims))
end

function BlockSparseArray{T}(dims::Vararg{Vector{Int}}) where {T}
return BlockSparseArray{T}(dims)
function BlockSparseArray{T,N}(::UndefInitializer, dims::Vararg{Vector{Int},N}) where {T,N}
return BlockSparseArray{T,N}(undef, dims)

Check warning on line 134 in src/blocksparsearray/blocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/blocksparsearray/blocksparsearray.jl#L133-L134

Added lines #L133 - L134 were not covered by tests
end

function BlockSparseArray{T}(axes::Vararg{AbstractUnitRange}) where {T}
return BlockSparseArray{T}(axes)
function BlockSparseArray{T}(::UndefInitializer, dims::Tuple{Vararg{Vector{Int}}}) where {T}
return BlockSparseArray{T,length(dims)}(undef, dims)
end

function BlockSparseArray{T}() where {T}
return BlockSparseArray{T}(())
function BlockSparseArray{T}(

Check warning on line 141 in src/blocksparsearray/blocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/blocksparsearray/blocksparsearray.jl#L141

Added line #L141 was not covered by tests
::UndefInitializer, axes::Tuple{Vararg{AbstractUnitRange}}
) where {T}
return BlockSparseArray{T,length(axes)}(undef, axes)

Check warning on line 144 in src/blocksparsearray/blocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/blocksparsearray/blocksparsearray.jl#L144

Added line #L144 was not covered by tests
end

# undef
function BlockSparseArray{T,N,A,Blocks}(
::UndefInitializer, args...
) where {T,N,A<:AbstractArray{T,N},Blocks<:AbstractArray{A,N}}
return BlockSparseArray{T,N,A,Blocks}(args...)
function BlockSparseArray{T}(::UndefInitializer, axes::Tuple{}) where {T}
return BlockSparseArray{T,length(axes)}(undef, axes)

Check warning on line 148 in src/blocksparsearray/blocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/blocksparsearray/blocksparsearray.jl#L147-L148

Added lines #L147 - L148 were not covered by tests
end

function BlockSparseArray{T,N,A}(
::UndefInitializer, args...
) where {T,N,A<:AbstractArray{T,N}}
return BlockSparseArray{T,N,A}(args...)
function BlockSparseArray{T}(::UndefInitializer, dims::Vararg{Vector{Int}}) where {T}
return BlockSparseArray{T}(undef, dims)
end

function BlockSparseArray{T,N}(::UndefInitializer, args...) where {T,N}
return BlockSparseArray{T,N}(args...)
function BlockSparseArray{T}(::UndefInitializer, axes::Vararg{AbstractUnitRange}) where {T}
return BlockSparseArray{T}(undef, axes)

Check warning on line 156 in src/blocksparsearray/blocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/blocksparsearray/blocksparsearray.jl#L155-L156

Added lines #L155 - L156 were not covered by tests
end

function BlockSparseArray{T}(::UndefInitializer, args...) where {T}
return BlockSparseArray{T}(args...)
function BlockSparseArray{T}(::UndefInitializer) where {T}
return BlockSparseArray{T}(undef, ())

Check warning on line 160 in src/blocksparsearray/blocksparsearray.jl

View check run for this annotation

Codecov / codecov/patch

src/blocksparsearray/blocksparsearray.jl#L159-L160

Added lines #L159 - L160 were not covered by tests
end

# Base `AbstractArray` interface
Expand Down
2 changes: 1 addition & 1 deletion src/blocksparsearrayinterface/map.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,5 +112,5 @@
# since they can't necessarily by `Diagonal` if there are rectangular blocks.
mapped_blocks = Dictionary{eltype(bs),eltype(ds)}(bs, ds)
# TODO: Use `similartype(typeof(a), eltype(eltype(mapped_blocks)))(...)`.
return BlockSparseArray(mapped_blocks, axes(a))
return sparsemortar(mapped_blocks, axes(a))

Check warning on line 115 in src/blocksparsearrayinterface/map.jl

View check run for this annotation

Codecov / codecov/patch

src/blocksparsearrayinterface/map.jl#L115

Added line #L115 was not covered by tests
end
2 changes: 1 addition & 1 deletion test/Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Adapt = "4"
Aqua = "0.8"
ArrayLayouts = "1"
BlockArrays = "1"
BlockSparseArrays = "0.2"
BlockSparseArrays = "0.3"
DiagonalArrays = "0.2"
GPUArraysCore = "0.2"
GradedUnitRanges = "0.1"
Expand Down
Loading
Loading