Skip to content

Commit dfa3646

Browse files
authored
[Compat] TensorKit v0.15.2 (#35)
* bump v0.3.1 * unitspace and zerospace * subblock * show(SumSpace) * fixup! bump v0.3.1 * show improvements * fix ambiguities
1 parent 5508f7c commit dfa3646

10 files changed

Lines changed: 87 additions & 61 deletions

File tree

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "BlockTensorKit"
22
uuid = "5f87ffc2-9cf1-4a46-8172-465d160bd8cd"
33
authors = ["Lukas Devos <ldevos98@gmail.com> and contributors"]
4-
version = "0.3.0"
4+
version = "0.3.1"
55

66
[deps]
77
BlockArrays = "8e7c35d0-a365-5155-bbbb-fb81a777f24e"
@@ -25,7 +25,7 @@ MatrixAlgebraKit = "0.5"
2525
Random = "1"
2626
SafeTestsets = "0.1"
2727
Strided = "2"
28-
TensorKit = "0.15"
28+
TensorKit = "0.15.2"
2929
TensorOperations = "5"
3030
Test = "1"
3131
TestExtras = "0.2, 0.3"

src/tensors/abstractblocktensor/abstractarray.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ end
142142
setindex!(parent(t), v, args...); t
143143
)
144144

145+
# ambiguity fix
146+
function Base.setindex!(::AbstractBlockTensorMap, ::AbstractTensorMap, sectors::Tuple{I, Vararg{I}}) where {I <: Sector}
147+
error("invalid indexing for blocktensormap")
148+
end
149+
function Base.setindex!(::AbstractBlockTensorMap, ::AbstractTensorMap, ::FusionTree, ::FusionTree)
150+
error("invalid indexing for blocktensormap")
151+
end
152+
145153
# setindex verifies structure is correct
146154
@inline function Base.setindex!(
147155
t::AbstractBlockTensorMap, v::AbstractTensorMap, indices::Vararg{SliceIndex}

src/tensors/abstractblocktensor/abstracttensormap.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@ end
1010

1111
eachspace(t::AbstractBlockTensorMap) = SumSpaceIndices(space(t))
1212

13-
# TODO: delete this method
14-
@inline function Base.getindex(t::AbstractBlockTensorMap, ::Nothing, ::Nothing)
15-
sectortype(t) === Trivial || throw(SectorMismatch())
16-
return mortar(map(x -> x[nothing, nothing], parent(t)))
17-
end
18-
@inline function Base.getindex(
19-
t::AbstractBlockTensorMap{E, S, N₁, N₂}, f₁::FusionTree{I, N₁}, f₂::FusionTree{I, N₂}
20-
) where {E, S, I, N₁, N₂}
21-
sectortype(S) === I || throw(SectorMismatch())
13+
@inline function TensorKit.subblock(
14+
t::AbstractBlockTensorMap, (f₁, f₂)::Tuple{FusionTree, FusionTree}
15+
)
16+
sectortype(t) === sectortype(f₁) === sectortype(f₂) ||
17+
throw(SectorMismatch("Not a valid sectortype for this tensor"))
18+
numout(t) == length(f₁) && numin(t) == length(f₂) ||
19+
throw(DimensionMismatch("Invalid number of fusiontree legs for this tensor"))
20+
2221
subblocks = map(eachspace(t), parent(t)) do V, x
2322
sz = (dims(codomain(V), f₁.uncoupled)..., dims(domain(V), f₂.uncoupled)...)
2423
if prod(sz) == 0
@@ -28,6 +27,7 @@ end
2827
return x[f₁, f₂]
2928
end
3029
end
30+
3131
return mortar(subblocks)
3232
end
3333
@inline function Base.setindex!(

src/tensors/abstractblocktensor/show.jl

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
# Show
22
# ----
3-
function Base.show(io::IO, t::AbstractBlockTensorMap)
4-
summary(io, t)
5-
get(io, :compact, false) && return nothing
6-
println(io, ":")
7-
for (c, b) in TensorKit.blocks(t)
8-
println(io, "* Block for sector $c:")
9-
show(io, b)
10-
end
3+
function Base.summary(io::IO, t::AbstractBlockTensorMap)
4+
V = space(t)
5+
sz = size(t)
6+
print(io, Base.dims2string(sz), "-blocked ", Base.dims2string(V), " ")
7+
Base.showarg(io, t, true)
118
return nothing
129
end
1310

1411
function Base.show(io::IO, ::MIME"text/plain", t::AbstractBlockTensorMap)
1512
# header:
1613
summary(io, t)
1714
nnz = nonzero_length(t)
18-
println(
19-
io, " with ", nnz, " stored entr", isone(nnz) ? "y" : "ies", iszero(nnz) ? "" : ":"
20-
)
15+
if issparse(t)
16+
println(
17+
io, " with ", nnz, " stored entr", isone(nnz) ? "y" : "ies", iszero(nnz) ? "" : ":"
18+
)
19+
end
20+
println(io, ":")
21+
println(io, " codomain: ", codomain(t))
22+
println(io, " domain: ", domain(t))
2123

2224
# body:
2325
compact = get(io, :compact, false)::Bool
@@ -44,7 +46,10 @@ function show_elements(io::IO, x::AbstractBlockTensorMap)
4446
nz_pairs = sort(vec(collect(nonzero_pairs(x))); by = first)
4547
for (k, (ind, val)) in enumerate(nz_pairs)
4648
if k < half_screen_rows || k > length(nzind) - half_screen_rows
47-
println(io, " ", '[', Base.join(lpad.(Tuple(ind), pads), ","), "] = ", val)
49+
print(io, " ", '[', Base.join(lpad.(Tuple(ind), pads), ","), "] = ")
50+
show(io, MIME"text/plain"(), val)
51+
println(io)
52+
4853
elseif k == half_screen_rows
4954
println(io, " ", Base.join(" " .^ pads, " "), " \u22ee")
5055
end

src/tensors/blocktensor.jl

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -175,22 +175,18 @@ Base.delete!(t::BlockTensorMap, I...) = (zerovector!(getindex(t, I...)); t)
175175

176176
# Show
177177
# ----
178-
function Base.summary(io::IO, t::BlockTensorMap)
179-
szstring = Base.dims2string(size(t))
180-
TT = eltype(t)
181-
typeinfo = get(io, :typeinfo, Any)
182-
if typeinfo <: typeof(t) || typeinfo <: TT
183-
typestring = ""
184-
else
185-
typestring = "{$TT}"
186-
end
187-
V = space(t)
188-
return print(io, "$szstring BlockTensorMap$typestring($V)")
178+
function Base.showarg(io::IO, t::BlockTensorMap, toplevel::Bool)
179+
!toplevel && print(io, "::")
180+
print(io, TK.type_repr(typeof(t)))
181+
return nothing
182+
end
183+
184+
function TK.type_repr(::Type{BlockTensorMap{T, E, S, N₁, N₂, N}}) where {T, E, S, N₁, N₂, N}
185+
return "BlockTensorMap{" * TK.type_repr(T) * ", …}"
189186
end
190187

191188
# Converters
192189
# ----------
193-
194190
function Base.promote_rule(
195191
::Type{<:BlockTensorMap{TT₁}}, ::Type{<:BlockTensorMap{TT₂}}
196192
) where {TT₁, TT₂}

src/tensors/sparseblocktensor.jl

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -181,15 +181,12 @@ end
181181

182182
# Show
183183
# ----
184-
function Base.summary(io::IO, t::SparseBlockTensorMap)
185-
szstring = Base.dims2string(size(t))
186-
TT = eltype(t)
187-
typeinfo = get(io, :typeinfo, Any)
188-
if typeinfo <: typeof(t) || typeinfo <: TT
189-
typestring = ""
190-
else
191-
typestring = "{$TT}"
192-
end
193-
V = space(t)
194-
return print(io, "$szstring SparseBlockTensorMap$typestring($V)")
184+
function Base.showarg(io::IO, t::SparseBlockTensorMap, toplevel::Bool)
185+
!toplevel && print(io, "::")
186+
print(io, TK.type_repr(typeof(t)))
187+
return nothing
188+
end
189+
190+
function TK.type_repr(::Type{SparseBlockTensorMap{T, E, S, N₁, N₂, N}}) where {T, E, S, N₁, N₂, N}
191+
return "SparseBlockTensorMap{" * join((TK.type_repr(T), E, TK.type_repr(S), N₁, N₂, N), ", ") * "}"
195192
end

src/vectorspaces/sumspace.jl

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,16 +161,15 @@ TensorKit.infimum(V::S, W::S) where {S <: SumSpace} = infimum(⊕(V), ⊕(W))
161161
TensorKit.supremum(V::S, W::S) where {S <: SumSpace} = supremum((V), (W))
162162
TensorKit.ominus(V::S, W::S) where {S <: SumSpace} = ominus((V), (W))
163163

164-
TensorKit.oplus(V::SumSpace{S}) where {S} = reduce(, V.spaces; init = isdual(V) ? zero(S)' : zero(S))
164+
TensorKit.oplus(V::SumSpace{S}) where {S} = reduce(, V.spaces; init = isdual(V) ? zerospace(S)' : zerospace(S))
165165
TensorKit.oplus(V1::SumSpace{S}, V2::SumSpace{S}...) where {S} = mapreduce(, , (V1, V2...))
166166

167167
function TensorKit.fuse(V1::S, V2::S) where {S <: SumSpace}
168168
return SumSpace(vec([fuse(v1, v2) for (v1, v2) in Base.product(V1.spaces, V2.spaces)]))
169169
end
170170

171-
Base.oneunit(S::Type{<:SumSpace}) = SumSpace(oneunit(eltype(S)))
172-
Base.zero(V::SumSpace{S}) where {S} = SumSpace{S}(; dual = isdual(V))
173-
Base.zero(::Type{SumSpace{S}}) where {S} = SumSpace{S}()
171+
TensorKit.unitspace(S::Type{<:SumSpace}) = SumSpace(TensorKit.unitspace(eltype(S)))
172+
TensorKit.zerospace(::Type{SumSpace{S}}) where {S} = SumSpace{S}()
174173

175174
# Promotion and conversion
176175
# ------------------------
@@ -228,19 +227,40 @@ function Base.show(io::IO, V::SumSpace)
228227
end
229228

230229
limited = get(io, :limited, true)
230+
ioc = IOContext(io, :compact => true)
231231
if limited && length(V) > SUMSPACE_SHOW_LIMIT[]
232232
ax = axes(V.spaces, 1)
233233
f, l = first(ax), last(ax)
234234
h = SUMSPACE_SHOW_LIMIT[] ÷ 2
235-
Base.show_delim_array(io, V.spaces, "(", "", "", false, f, f + h)
235+
Base.show_delim_array(ioc, V.spaces, "(", "", "", false, f, f + h)
236236
print(io, " ⊞ ⋯ ⊞ ")
237-
Base.show_delim_array(io, V.spaces, "", "", ")", false, l - h, l)
237+
Base.show_delim_array(ioc, V.spaces, "", "", ")", false, l - h, l)
238238
else
239-
Base.show_delim_array(io, V.spaces, "(", "", ")", false)
239+
Base.show_delim_array(ioc, V.spaces, "(", "", ")", false)
240240
end
241241
return nothing
242242
end
243243

244+
function Base.show(io::IO, ::MIME"text/plain", V::SumSpace)
245+
# print small summary, e.g.: l-element SumSpace(Vect[I](…)) of dim d
246+
l = length(V.spaces)
247+
d = dim(V)
248+
print(io, l, "-element ⊞(::", TK.type_repr(eltype(V)), "…)")
249+
isdual(V) && print(io, "'")
250+
print(io, " of dim ", d)
251+
252+
compact = get(io, :compact, false)::Bool
253+
(iszero(d) || compact) && return nothing
254+
255+
# print detailed space information - hijack Base.Vector printing
256+
print(io, ":\n")
257+
print_data = V.spaces
258+
ioc = IOContext(io, :typeinfo => eltype(print_data))
259+
Base.print_matrix(ioc, print_data)
260+
261+
return nothing
262+
end
263+
244264
# TensorMapSumSpace
245265
# -----------------
246266
# function TensorKit.fusionblockstructure(

test/abstracttensor/blocktensor.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ end
117117
@test i1 * i2 == @constinferred(id(storagetype(t), V1 V2))
118118
@test i2 * i1 == @constinferred(id(storagetype(t), V2 V1))
119119

120-
w = @constinferred(isometry(storagetype(t), V1 (oneunit(V1) oneunit(V1)), V1))
120+
w = @constinferred(isometry(storagetype(t), V1 (unitspace(V1) unitspace(V1)), V1))
121121
@test dim(w) == 2 * dim(V1 V1)
122122
@test w' * w == id(storagetype(t), V1)
123123
@test w * w' == (w * w')^2

test/abstracttensor/sparseblocktensor.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ end
123123
@test i1 * i2 == @constinferred(id(storagetype(t), V1 V2))
124124
@test i2 * i1 == @constinferred(id(storagetype(t), V2 V1))
125125

126-
w = @constinferred(isometry(storagetype(t), V1 (oneunit(V1) oneunit(V1)), V1))
126+
w = @constinferred(isometry(storagetype(t), V1 (unitspace(V1) unitspace(V1)), V1))
127127
@test dim(w) == 2 * dim(V1 V1)
128128
@test w' * w == id(storagetype(t), V1)
129129
@test w * w' == (w * w')^2

test/vectorspaces/sumspace.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ using TensorKit, BlockTensorKit
4040
@test (sectors(typeof(V)())...,) == ()
4141
@test @constinferred(axes(V)) == Base.OneTo(d)
4242
W = @constinferred SumSpace(ℝ^1)
43-
@test @constinferred(oneunit(V)) == W == oneunit(typeof(V))
43+
@test @constinferred(unitspace(V)) == W == unitspace(typeof(V))
4444
@test @constinferred((V, V)) == SumSpace(vcat(V.spaces, V.spaces))
45-
@test @constinferred((V, oneunit(V))) == SumSpace(vcat(V.spaces, ℝ^1))
45+
@test @constinferred((V, unitspace(V))) == SumSpace(vcat(V.spaces, ℝ^1))
4646
@test @constinferred((V, V, V, V)) == SumSpace(repeat(V.spaces, 4))
4747
@test @constinferred(fuse(V, V)) SumSpace(ℝ^(d^2))
4848
@test @constinferred(fuse(V, V', V, V')) SumSpace(ℝ^(d^4))
@@ -87,9 +87,9 @@ end
8787
@test (sectors(typeof(V)())...,) == ()
8888
@test @constinferred(axes(V)) == Base.OneTo(d)
8989
W = @constinferred SumSpace(ℂ^1)
90-
@test @constinferred(oneunit(V)) == W == oneunit(typeof(V))
90+
@test @constinferred(unitspace(V)) == W == unitspace(typeof(V))
9191
@test @constinferred((V, V)) == SumSpace(vcat(V.spaces, V.spaces))
92-
@test @constinferred((V, oneunit(V))) == SumSpace(vcat(V.spaces, ℂ^1))
92+
@test @constinferred((V, unitspace(V))) == SumSpace(vcat(V.spaces, ℂ^1))
9393
@test @constinferred((V, V, V, V)) == SumSpace(repeat(V.spaces, 4))
9494
@test @constinferred(fuse(V, V)) SumSpace(ℂ^(d^2))
9595
@test @constinferred(fuse(V, V', V, V')) SumSpace(ℂ^(d^4))
@@ -139,9 +139,9 @@ end
139139
@test (sectors(typeof(V)())...,) == ()
140140
@test @constinferred(axes(V)) == Base.OneTo(d)
141141
W = @constinferred SumSpace(U1Space(0 => 1))
142-
@test @constinferred(oneunit(V)) == W == @constinferred(oneunit(typeof(V)))
142+
@test @constinferred(unitspace(V)) == W == @constinferred(unitspace(typeof(V)))
143143
@test @constinferred((V, V)) == SumSpace(vcat(V.spaces, V.spaces))
144-
@test @constinferred((V, oneunit(V))) == SumSpace(vcat(V.spaces, oneunit(V1)))
144+
@test @constinferred((V, unitspace(V))) == SumSpace(vcat(V.spaces, unitspace(V1)))
145145
@test @constinferred((V, V, V, V)) == SumSpace(repeat(V.spaces, 4))
146146
@test @constinferred(fuse(V, V)) SumSpace(U1Space(0 => 9, 1 => 24, 2 => 16))
147147
@test @constinferred(fuse(V, V', V, V'))

0 commit comments

Comments
 (0)