Skip to content

Commit 4e3ed86

Browse files
authored
Merge branch 'main' into compathelper/new_version/2025-03-01-01-57-17-916-00997056796
2 parents 30c7c38 + 302cdda commit 4e3ed86

File tree

5 files changed

+185
-79
lines changed

5 files changed

+185
-79
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "BlockSparseArrays"
22
uuid = "2c9a651f-6452-4ace-a6ac-809f4280fbb4"
33
authors = ["ITensor developers <[email protected]> and contributors"]
4-
version = "0.2.23"
4+
version = "0.2.25"
55

66
[deps]
77
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
@@ -45,7 +45,7 @@ MacroTools = "0.5.13"
4545
MapBroadcast = "0.1.5"
4646
SparseArraysBase = "0.2.10, 0.3"
4747
SplitApplyCombine = "1.2.3"
48-
TensorAlgebra = "0.1.0"
48+
TensorAlgebra = "0.1.0, 0.2"
4949
Test = "1.10"
5050
TypeParameterAccessors = "0.2.0, 0.3"
5151
julia = "1.10"

src/abstractblocksparsearray/abstractblocksparsearray.jl

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -92,27 +92,52 @@ function show_typeof_blocksparse(io::IO, a::AbstractBlockSparseArray)
9292
return nothing
9393
end
9494

95-
# Copied from `BlockArrays.jl`.
96-
block2string(b, s) = string(join(map(string, b), '×'), "-blocked ", Base.dims2string(s))
95+
# Copy of `Base.dims2string` defined in `show.jl`.
96+
function dims_to_string(d)
97+
isempty(d) && return "0-dimensional"
98+
length(d) == 1 && return "$(d[1])-element"
99+
return join(map(string, d), '×')
100+
end
97101

98-
function summary_blocksparse(io::IO, a::AbstractArray)
99-
print(io, block2string(blocksize(a), size(a)))
100-
print(io, ' ')
101-
show_typeof_blocksparse(io, a)
102-
return nothing
102+
# Copy of `BlockArrays.block2string` from `BlockArrays.jl`.
103+
block_to_string(b, s) = string(join(map(string, b), '×'), "-blocked ", dims_to_string(s))
104+
105+
using TypeParameterAccessors: type_parameters, unspecify_type_parameters
106+
function concretetype_to_string_truncated(type::Type; param_truncation_length=typemax(Int))
107+
isconcretetype(type) || throw(ArgumentError("Type must be concrete."))
108+
alias = Base.make_typealias(type)
109+
base_type, params = if isnothing(alias)
110+
unspecify_type_parameters(type), type_parameters(type)
111+
else
112+
base_type_globalref, params_svec = alias
113+
base_type_globalref.name, params_svec
114+
end
115+
str = string(base_type)
116+
if isempty(params)
117+
return str
118+
end
119+
str *= '{'
120+
param_strings = map(params) do param
121+
param_string = string(param)
122+
if length(param_string) > param_truncation_length
123+
return ""
124+
end
125+
return param_string
126+
end
127+
str *= join(param_strings, ", ")
128+
str *= '}'
129+
return str
103130
end
104131

105132
function Base.summary(io::IO, a::AbstractBlockSparseArray)
106-
summary_blocksparse(io, a)
133+
print(io, block_to_string(blocksize(a), size(a)))
134+
print(io, ' ')
135+
print(io, concretetype_to_string_truncated(typeof(a); param_truncation_length=40))
107136
return nothing
108137
end
109138

110139
function Base.showarg(io::IO, a::AbstractBlockSparseArray, toplevel::Bool)
111-
if toplevel
112-
show_typeof_blocksparse(io, a)
113-
else
114-
print(io, "::")
115-
show_typeof_blocksparse(io, a)
116-
end
140+
!toplevel && print(io, "::")
141+
print(io, concretetype_to_string_truncated(typeof(a); param_truncation_length=40))
117142
return nothing
118143
end

src/blocksparsearray/blocksparsearray.jl

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -208,31 +208,3 @@ TypeParameterAccessors.position(::Type{BlockSparseArray}, ::typeof(blocktype)) =
208208
function TypeParameterAccessors.position(::Type{BlockSparseArray}, ::typeof(blockstype))
209209
return Position(4)
210210
end
211-
212-
# TODO: Make this generic to `AbstractBlockSparseVector` using
213-
# TypeParameterAccessors.jl, for example using:
214-
# `set_ndims(unspecify_type_parameters(typeof(a)), 1)`.
215-
function show_typeof_blocksparse(io::IO, a::BlockSparseVector)
216-
print(io, "BlockSparseVector")
217-
print(io, '{')
218-
show(io, eltype(a))
219-
print(io, ", ")
220-
show(io, blocktype(a))
221-
print(io, ", …")
222-
print(io, '}')
223-
return nothing
224-
end
225-
226-
# TODO: Make this generic to `AbstractBlockSparseMatrix` using
227-
# TypeParameterAccessors.jl, for example using:
228-
# `set_ndims(unspecify_type_parameters(typeof(a)), 2)`.
229-
function show_typeof_blocksparse(io::IO, a::BlockSparseMatrix)
230-
print(io, "BlockSparseMatrix")
231-
print(io, '{')
232-
show(io, eltype(a))
233-
print(io, ", ")
234-
show(io, blocktype(a))
235-
print(io, ", …")
236-
print(io, '}')
237-
return nothing
238-
end

test/test_basics.jl

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1108,34 +1108,30 @@ arrayts = (Array, JLArray)
11081108
arrayt_elt = arrayt{elt,3}
11091109

11101110
a = BlockSparseVector{elt,arrayt{elt,1}}([2, 2])
1111+
res = sprint(summary, a)
1112+
function ref_vec(elt, arrayt, prefix="")
1113+
return "2-blocked 4-element $(prefix)BlockSparseVector{$(elt), $(arrayt), …, …}"
1114+
end
11111115
# Either option is possible depending on namespacing.
1112-
@test (
1113-
sprint(summary, a) ==
1114-
"2-blocked 4-element BlockSparseVector{$(elt), $(vectort_elt), …}"
1115-
) || (
1116-
sprint(summary, a) ==
1117-
"2-blocked 4-element BlockSparseArrays.BlockSparseVector{$(elt), $(vectort_elt), …}"
1118-
)
1116+
@test (res == ref_vec(elt, vectort_elt)) ||
1117+
(res == ref_vec(elt, vectort_elt, "BlockSparseArrays."))
11191118

11201119
a = BlockSparseMatrix{elt,arrayt{elt,2}}([2, 2], [2, 2])
1120+
res = sprint(summary, a)
1121+
function ref_mat(elt, arrayt, prefix="")
1122+
return "2×2-blocked 4×4 $(prefix)BlockSparseMatrix{$(elt), $(arrayt), …, …}"
1123+
end
11211124
# Either option is possible depending on namespacing.
1122-
@test (
1123-
sprint(summary, a) == "2×2-blocked 4×4 BlockSparseMatrix{$(elt), $(matrixt_elt), …}"
1124-
) || (
1125-
sprint(summary, a) ==
1126-
"2×2-blocked 4×4 BlockSparseArrays.BlockSparseMatrix{$(elt), $(matrixt_elt), …}"
1127-
)
1125+
@test (res == ref_mat(elt, matrixt_elt)) ||
1126+
(res == ref_mat(elt, matrixt_elt, "BlockSparseArrays."))
11281127

11291128
a = BlockSparseArray{elt,3,arrayt{elt,3}}([2, 2], [2, 2], [2, 2])
1130-
1131-
# Either option is possible depending on namespacing.
1132-
@test (
1133-
sprint(summary, a) ==
1134-
"2×2×2-blocked 4×4×4 BlockSparseArray{$(elt), 3, $(arrayt_elt), …}"
1135-
) || (
1136-
sprint(summary, a) ==
1137-
"2×2×2-blocked 4×4×4 BlockSparseArrays.BlockSparseArray{$(elt), 3, $(arrayt_elt), …}"
1138-
)
1129+
res = sprint(summary, a)
1130+
function ref_arr(elt, arrayt, prefix="")
1131+
return "2×2×2-blocked 4×4×4 $(prefix)BlockSparseArray{$(elt), 3, $(arrayt), …, …}"
1132+
end
1133+
@test (res == ref_arr(elt, arrayt_elt)) ||
1134+
(res == ref_arr(elt, arrayt_elt, "BlockSparseArrays."))
11391135

11401136
if elt === Float64
11411137
# Not testing other element types since they change the

test/test_tensoralgebraext.jl

Lines changed: 126 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,130 @@
1-
@eval module $(gensym())
2-
using Test: @test, @testset
1+
using Random: randn!
2+
using Test: @test, @test_broken, @testset
3+
4+
using BlockArrays: Block, BlockArray, BlockedArray, blockedrange, blocksize
5+
36
using BlockSparseArrays: BlockSparseArray
7+
using GradedUnitRanges: dual, gradedrange
8+
using SymmetrySectors: U1
49
using TensorAlgebra: contract
5-
using SparseArraysBase: densearray
6-
@testset "BlockSparseArraysTensorAlgebraExt (eltype=$elt)" for elt in (
7-
Float32, Float64, Complex{Float32}, Complex{Float64}
8-
)
9-
a1 = BlockSparseArray{elt}([1, 2], [2, 3], [3, 2])
10-
a2 = BlockSparseArray{elt}([2, 2], [3, 2], [2, 3])
11-
a_dest, dimnames_dest = contract(a1, (1, -1, -2), a2, (2, -2, -1))
12-
a_dest_dense, dimnames_dest_dense = contract(
13-
densearray(a1), (1, -1, -2), densearray(a2), (2, -2, -1)
14-
)
15-
@test a_dest a_dest_dense
10+
11+
function randn_blockdiagonal(elt::Type, axes::Tuple)
12+
a = BlockSparseArray{elt}(axes)
13+
blockdiaglength = minimum(blocksize(a))
14+
for i in 1:blockdiaglength
15+
b = Block(ntuple(Returns(i), ndims(a)))
16+
a[b] = randn!(a[b])
17+
end
18+
return a
1619
end
20+
21+
const elts = (Float32, Float64, Complex{Float32}, Complex{Float64})
22+
@testset "`contract` `BlockSparseArray` (eltype=$elt)" for elt in elts
23+
@testset "BlockedOneTo" begin
24+
d = blockedrange([2, 3])
25+
a1 = randn_blockdiagonal(elt, (d, d, d, d))
26+
a2 = randn_blockdiagonal(elt, (d, d, d, d))
27+
a3 = randn_blockdiagonal(elt, (d, d))
28+
a1_dense = convert(Array, a1)
29+
a2_dense = convert(Array, a2)
30+
a3_dense = convert(Array, a3)
31+
32+
# matrix matrix
33+
a_dest, dimnames_dest = contract(a1, (1, -1, 2, -2), a2, (2, -3, 1, -4))
34+
a_dest_dense, dimnames_dest_dense = contract(
35+
a1_dense, (1, -1, 2, -2), a2_dense, (2, -3, 1, -4)
36+
)
37+
@test dimnames_dest == dimnames_dest_dense
38+
@test size(a_dest) == size(a_dest_dense)
39+
@test a_dest isa BlockSparseArray
40+
@test a_dest a_dest_dense
41+
42+
# matrix vector
43+
@test_broken a_dest, dimnames_dest = contract(a1, (2, -1, -2, 1), a3, (1, 2))
44+
#=
45+
a_dest_dense, dimnames_dest_dense = contract(a1_dense, (2, -1, -2, 1), a3_dense, (1, 2))
46+
@test dimnames_dest == dimnames_dest_dense
47+
@test size(a_dest) == size(a_dest_dense)
48+
@test a_dest isa BlockSparseArray
49+
@test a_dest ≈ a_dest_dense
50+
=#
51+
52+
# vector matrix
53+
a_dest, dimnames_dest = contract(a3, (1, 2), a1, (2, -1, -2, 1))
54+
a_dest_dense, dimnames_dest_dense = contract(a3_dense, (1, 2), a1_dense, (2, -1, -2, 1))
55+
@test dimnames_dest == dimnames_dest_dense
56+
@test size(a_dest) == size(a_dest_dense)
57+
@test a_dest isa BlockSparseArray
58+
@test a_dest a_dest_dense
59+
60+
# vector vector
61+
a_dest_dense, dimnames_dest_dense = contract(a3_dense, (1, 2), a3_dense, (2, 1))
62+
a_dest, dimnames_dest = contract(a3, (1, 2), a3, (2, 1))
63+
@test dimnames_dest == dimnames_dest_dense
64+
@test size(a_dest) == size(a_dest_dense)
65+
@test a_dest isa BlockSparseArray{elt,0}
66+
@test a_dest a_dest_dense
67+
68+
# outer product
69+
a_dest_dense, dimnames_dest_dense = contract(a3_dense, (1, 2), a3_dense, (3, 4))
70+
a_dest, dimnames_dest = contract(a3, (1, 2), a3, (3, 4))
71+
@test dimnames_dest == dimnames_dest_dense
72+
@test size(a_dest) == size(a_dest_dense)
73+
@test a_dest isa BlockSparseArray
74+
@test a_dest a_dest_dense
75+
end
76+
77+
@testset "GradedOneTo with U(1)" begin
78+
d = gradedrange([U1(0) => 2, U1(1) => 3])
79+
a1 = randn_blockdiagonal(elt, (d, d, dual(d), dual(d)))
80+
a2 = randn_blockdiagonal(elt, (d, d, dual(d), dual(d)))
81+
a3 = randn_blockdiagonal(elt, (d, dual(d)))
82+
a1_dense = convert(Array, a1)
83+
a2_dense = convert(Array, a2)
84+
a3_dense = convert(Array, a3)
85+
86+
# matrix matrix
87+
a_dest, dimnames_dest = contract(a1, (1, -1, 2, -2), a2, (2, -3, 1, -4))
88+
a_dest_dense, dimnames_dest_dense = contract(
89+
a1_dense, (1, -1, 2, -2), a2_dense, (2, -3, 1, -4)
90+
)
91+
@test dimnames_dest == dimnames_dest_dense
92+
@test size(a_dest) == size(a_dest_dense)
93+
@test a_dest isa BlockSparseArray
94+
@test a_dest a_dest_dense
95+
96+
# matrix vector
97+
@test_broken a_dest, dimnames_dest = contract(a1, (2, -1, -2, 1), a3, (1, 2))
98+
#=
99+
a_dest_dense, dimnames_dest_dense = contract(a1_dense, (2, -1, -2, 1), a3_dense, (1, 2))
100+
@test dimnames_dest == dimnames_dest_dense
101+
@test size(a_dest) == size(a_dest_dense)
102+
@test a_dest isa BlockSparseArray
103+
@test a_dest ≈ a_dest_dense
104+
=#
105+
106+
# vector matrix
107+
a_dest, dimnames_dest = contract(a3, (1, 2), a1, (2, -1, -2, 1))
108+
a_dest_dense, dimnames_dest_dense = contract(a3_dense, (1, 2), a1_dense, (2, -1, -2, 1))
109+
@test dimnames_dest == dimnames_dest_dense
110+
@test size(a_dest) == size(a_dest_dense)
111+
@test a_dest isa BlockSparseArray
112+
@test a_dest a_dest_dense
113+
114+
# vector vector
115+
a_dest, dimnames_dest = contract(a3, (1, 2), a3, (2, 1))
116+
a_dest_dense, dimnames_dest_dense = contract(a3_dense, (1, 2), a3_dense, (2, 1))
117+
@test dimnames_dest == dimnames_dest_dense
118+
@test size(a_dest) == size(a_dest_dense)
119+
@test a_dest isa BlockSparseArray{elt,0}
120+
@test a_dest a_dest_dense
121+
122+
# outer product
123+
a_dest, dimnames_dest = contract(a3, (1, 2), a3, (3, 4))
124+
a_dest_dense, dimnames_dest_dense = contract(a3_dense, (1, 2), a3_dense, (3, 4))
125+
@test dimnames_dest == dimnames_dest_dense
126+
@test size(a_dest) == size(a_dest_dense)
127+
@test a_dest isa BlockSparseArray
128+
@test a_dest a_dest_dense
129+
end
17130
end

0 commit comments

Comments
 (0)