Skip to content

Commit acef25f

Browse files
committed
Fix more tests
1 parent ec8c96e commit acef25f

File tree

7 files changed

+55
-53
lines changed

7 files changed

+55
-53
lines changed

ext/BlockSparseArraysGradedUnitRangesExt/BlockSparseArraysGradedUnitRangesExt.jl

Lines changed: 1 addition & 1 deletion
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/blocksparsearray/blocksparsearray.jl

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -24,61 +24,62 @@ const BlockSparseVector{T,A<:AbstractVector{T},Blocks<:AbstractVector{A},Axes<:T
2424
T,1,A,Blocks,Axes
2525
}
2626

27-
# TODO: Rename to `sparsemortar`.
28-
function BlockSparseArray(
27+
"""
28+
sparsemortar(blocks::AbstractArray{<:AbstractArray{T,N},N}, axes) -> ::BlockSparseArray{T,N}
29+
30+
Construct a block sparse array from a sparse array of arrays and specified blocked axes.
31+
The block sizes must be commensurate with the blocks of the axes.
32+
"""
33+
function sparsemortar(
34+
blocks::AbstractArray{<:AbstractArray{T,N},N}, axes::Tuple{Vararg{AbstractUnitRange,N}}
35+
) where {T,N}
36+
return BlockSparseArray{T,N,eltype(blocks),typeof(blocks),typeof(axes)}(blocks, axes)
37+
end
38+
39+
"""
40+
sparsemortar(blocks::Dictionary{<:Block{N},<:AbstractArray{T,N}}, axes) -> ::BlockSparseArray{T,N}
41+
42+
Construct a block sparse array from a dictionary mapping the locations of the stored/nonzero
43+
blocks to the data of those blocks, along with a set of blocked axes.
44+
The block sizes must be commensurate with the blocks of the specified axes.
45+
"""
46+
function sparsemortar(
2947
block_data::Dictionary{<:Block{N},<:AbstractArray{<:Any,N}},
3048
axes::Tuple{Vararg{AbstractUnitRange,N}},
3149
) where {N}
3250
blocks = default_blocks(block_data, axes)
33-
# TODO: Rename to `sparsemortar`.
34-
return BlockSparseArray(blocks, axes)
51+
return sparsemortar(blocks, axes)
3552
end
3653

37-
# TODO: Rename to `sparsemortar`.
38-
function BlockSparseArray(
54+
"""
55+
sparsemortar(block_indices::Vector{<:Block{N}}, block_data::Vector{<:AbstractArray{T,N}}, axes) -> ::BlockSparseArray{T,N}
56+
57+
Construct a block sparse array from a list of locations of the stored/nonzero blocks,
58+
a corresponding list of the data of those blocks, along with a set of blocked axes.
59+
The block sizes must be commensurate with the blocks of the specified axes.
60+
"""
61+
function sparsemortar(
3962
block_indices::Vector{<:Block{N}},
4063
block_data::Vector{<:AbstractArray{<:Any,N}},
4164
axes::Tuple{Vararg{AbstractUnitRange,N}},
4265
) where {N}
43-
# TODO: Rename to `sparsemortar`.
44-
return BlockSparseArray(Dictionary(block_indices, block_data), axes)
66+
return sparsemortar(Dictionary(block_indices, block_data), axes)
4567
end
4668

47-
# TODO: Rename to `sparsemortar`.
48-
function BlockSparseArray{T,N,A,Blocks}(
49-
blocks::AbstractArray{<:AbstractArray{T,N},N}, axes::Tuple{Vararg{AbstractUnitRange,N}}
50-
) where {T,N,A<:AbstractArray{T,N},Blocks<:AbstractArray{A,N}}
51-
return BlockSparseArray{T,N,A,Blocks,typeof(axes)}(blocks, axes)
52-
end
69+
@doc """
70+
BlockSparseArray{T}(undef, dims)
71+
BlockSparseArray{T,N}(undef, dims)
72+
BlockSparseArray{T,N,A}(undef, dims)
5373
54-
# TODO: Rename to `sparsemortar`.
55-
function BlockSparseArray{T,N,A}(
56-
blocks::AbstractArray{<:AbstractArray{T,N},N}, axes::Tuple{Vararg{AbstractUnitRange,N}}
57-
) where {T,N,A<:AbstractArray{T,N}}
58-
return BlockSparseArray{T,N,A,typeof(blocks)}(blocks, axes)
59-
end
60-
61-
# TODO: Rename to `sparsemortar`.
62-
function BlockSparseArray{T,N}(
63-
blocks::AbstractArray{<:AbstractArray{T,N},N}, axes::Tuple{Vararg{AbstractUnitRange,N}}
64-
) where {T,N}
65-
return BlockSparseArray{T,N,eltype(blocks),typeof(blocks),typeof(axes)}(blocks, axes)
66-
end
67-
68-
# TODO: Rename to `sparsemortar`.
69-
function BlockSparseArray{T,N}(
70-
block_data::Dictionary{Block{N,Int},<:AbstractArray{T,N}},
71-
axes::Tuple{Vararg{AbstractUnitRange,N}},
72-
) where {T,N}
73-
blocks = default_blocks(block_data, axes)
74-
return BlockSparseArray{T,N}(blocks, axes)
75-
end
74+
Construct an uninitialized N-dimensional BlockSparseArray containing elements of type T. `dims` should be a list
75+
of block lengths in each dimension or a list of blocked ranges representing the axes.
76+
""" BlockSparseArray
7677

7778
function BlockSparseArray{T,N,A}(
7879
::UndefInitializer, axes::Tuple{Vararg{AbstractUnitRange,N}}
7980
) where {T,N,A<:AbstractArray{T,N}}
8081
blocks = default_blocks(A, axes)
81-
return BlockSparseArray{T,N,A}(blocks, axes)
82+
return sparsemortar(blocks, axes)
8283
end
8384

8485
function BlockSparseArray{T,N,A}(
@@ -98,7 +99,7 @@ function BlockSparseArray{T,0,A}(
9899
::UndefInitializer, axes::Tuple{}
99100
) where {T,A<:AbstractArray{T,0}}
100101
blocks = default_blocks(A, axes)
101-
return BlockSparseArray{T,0,A}(blocks, axes)
102+
return sparsemortar(blocks, axes)
102103
end
103104

104105
function BlockSparseArray{T,N,A}(

src/blocksparsearrayinterface/map.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,5 +112,5 @@ function map_stored_blocks(f, a::AbstractArray)
112112
# since they can't necessarily by `Diagonal` if there are rectangular blocks.
113113
mapped_blocks = Dictionary{eltype(bs),eltype(ds)}(bs, ds)
114114
# TODO: Use `similartype(typeof(a), eltype(eltype(mapped_blocks)))(...)`.
115-
return BlockSparseArray(mapped_blocks, axes(a))
115+
return sparsemortar(mapped_blocks, axes(a))
116116
end

test/test_basics.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ using BlockSparseArrays:
3030
eachstoredblock,
3131
blockstype,
3232
blocktype,
33+
sparsemortar,
3334
view!
3435
using GPUArraysCore: @allowscalar
3536
using JLArrays: JLArray

test/test_gradedunitrangesext.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ using TensorAlgebra: fusedims, splitdims
2020
using LinearAlgebra: adjoint
2121
using Random: randn!
2222
function randn_blockdiagonal(elt::Type, axes::Tuple)
23-
a = BlockSparseArray{elt}(axes)
23+
a = BlockSparseArray{elt}(undef, axes)
2424
blockdiaglength = minimum(blocksize(a))
2525
for i in 1:blockdiaglength
2626
b = Block(ntuple(Returns(i), ndims(a)))
@@ -135,7 +135,7 @@ const elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
135135
@testset "dual axes" begin
136136
r = gradedrange([U1(0) => 2, U1(1) => 2])
137137
for ax in ((r, r), (dual(r), r), (r, dual(r)), (dual(r), dual(r)))
138-
a = BlockSparseArray{elt}(ax...)
138+
a = BlockSparseArray{elt}(undef, ax...)
139139
@views for b in [Block(1, 1), Block(2, 2)]
140140
a[b] = randn(elt, size(a[b]))
141141
end
@@ -178,7 +178,7 @@ const elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
178178

179179
@testset "GradedOneTo" begin
180180
r = gradedrange([U1(0) => 2, U1(1) => 2])
181-
a = BlockSparseArray{elt}(r, r)
181+
a = BlockSparseArray{elt}(undef, r, r)
182182
@views for i in [Block(1, 1), Block(2, 2)]
183183
a[i] = randn(elt, size(a[i]))
184184
end
@@ -199,7 +199,7 @@ const elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
199199

200200
@testset "GradedUnitRange" begin
201201
r = gradedrange([U1(0) => 2, U1(1) => 2])[1:3]
202-
a = BlockSparseArray{elt}(r, r)
202+
a = BlockSparseArray{elt}(undef, r, r)
203203
@views for i in [Block(1, 1), Block(2, 2)]
204204
a[i] = randn(elt, size(a[i]))
205205
end
@@ -226,7 +226,7 @@ const elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
226226
# Test case when all axes are dual.
227227
@testset "dual GradedOneTo" begin
228228
r = gradedrange([U1(-1) => 2, U1(1) => 2])
229-
a = BlockSparseArray{elt}(dual(r), dual(r))
229+
a = BlockSparseArray{elt}(undef, dual(r), dual(r))
230230
@views for i in [Block(1, 1), Block(2, 2)]
231231
a[i] = randn(elt, size(a[i]))
232232
end
@@ -251,7 +251,7 @@ const elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
251251

252252
@testset "dual GradedUnitRange" begin
253253
r = gradedrange([U1(0) => 2, U1(1) => 2])[1:3]
254-
a = BlockSparseArray{elt}(dual(r), dual(r))
254+
a = BlockSparseArray{elt}(undef, dual(r), dual(r))
255255
@views for i in [Block(1, 1), Block(2, 2)]
256256
a[i] = randn(elt, size(a[i]))
257257
end
@@ -277,7 +277,7 @@ const elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
277277

278278
@testset "dual BlockedUnitRange" begin # self dual
279279
r = blockedrange([2, 2])
280-
a = BlockSparseArray{elt}(dual(r), dual(r))
280+
a = BlockSparseArray{elt}(undef, dual(r), dual(r))
281281
@views for i in [Block(1, 1), Block(2, 2)]
282282
a[i] = randn(elt, size(a[i]))
283283
end
@@ -302,7 +302,7 @@ const elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
302302
gradedrange([U1(0) => 2, U1(1) => 2]),
303303
gradedrange([U1(0) => 2, U1(1) => 2])[begin:end],
304304
)
305-
a = BlockSparseArray{elt}(r, r)
305+
a = BlockSparseArray{elt}(undef, r, r)
306306
@views for i in [Block(1, 1), Block(2, 2)]
307307
a[i] = randn(elt, size(a[i]))
308308
end
@@ -330,16 +330,16 @@ const elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
330330
end
331331
@testset "Matrix multiplication" begin
332332
r = gradedrange([U1(0) => 2, U1(1) => 3])
333-
a1 = BlockSparseArray{elt}(dual(r), r)
333+
a1 = BlockSparseArray{elt}(undef, dual(r), r)
334334
a1[Block(1, 2)] = randn(elt, size(@view(a1[Block(1, 2)])))
335335
a1[Block(2, 1)] = randn(elt, size(@view(a1[Block(2, 1)])))
336-
a2 = BlockSparseArray{elt}(dual(r), r)
336+
a2 = BlockSparseArray{elt}(undef, dual(r), r)
337337
a2[Block(1, 2)] = randn(elt, size(@view(a2[Block(1, 2)])))
338338
a2[Block(2, 1)] = randn(elt, size(@view(a2[Block(2, 1)])))
339339
@test Array(a1 * a2) Array(a1) * Array(a2)
340340
@test Array(a1' * a2') Array(a1') * Array(a2')
341341

342-
a2 = BlockSparseArray{elt}(r, dual(r))
342+
a2 = BlockSparseArray{elt}(undef, r, dual(r))
343343
a2[Block(1, 2)] = randn(elt, size(@view(a2[Block(1, 2)])))
344344
a2[Block(2, 1)] = randn(elt, size(@view(a2[Block(2, 1)])))
345345
@test Array(a1' * a2) Array(a1') * Array(a2)

test/test_svd.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ end
4747
# -----------
4848
@testset "($m, $n) BlockSparseMatrix{$T}" for ((m, n), T) in
4949
Iterators.product(blockszs, eltypes)
50-
a = BlockSparseArray{T}(m, n)
50+
a = BlockSparseArray{T}(undef, m, n)
5151

5252
# test empty matrix
5353
usv_empty = svd(a)

test/test_tensoralgebraext.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ using SymmetrySectors: U1
99
using TensorAlgebra: contract
1010

1111
function randn_blockdiagonal(elt::Type, axes::Tuple)
12-
a = BlockSparseArray{elt}(axes)
12+
a = BlockSparseArray{elt}(undef, axes)
1313
blockdiaglength = minimum(blocksize(a))
1414
for i in 1:blockdiaglength
1515
b = Block(ntuple(Returns(i), ndims(a)))

0 commit comments

Comments
 (0)