Skip to content

Commit 5323bc4

Browse files
authored
Fix nested broadcast of AbstractBlockTuple (#14)
1 parent b876749 commit 5323bc4

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "TensorAlgebra"
22
uuid = "68bd88dc-f39d-4e12-b2ca-f046b68fcc6a"
33
authors = ["ITensor developers <[email protected]> and contributors"]
4-
version = "0.1.3"
4+
version = "0.1.4"
55

66
[deps]
77
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"

src/blockedtuple.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ end
3939
function Base.getindex(bt::AbstractBlockTuple, bi::BlockIndexRange{1})
4040
return bt[Block(bi)][only(bi.indices)]
4141
end
42+
# needed for nested broadcast in Julia < 1.11
43+
Base.getindex(bt::AbstractBlockTuple, ci::CartesianIndex{1}) = bt[only(Tuple(ci))]
4244

4345
Base.iterate(bt::AbstractBlockTuple) = iterate(Tuple(bt))
4446
Base.iterate(bt::AbstractBlockTuple, i::Int) = iterate(Tuple(bt), i)
@@ -53,6 +55,14 @@ function Base.map(f, bt::AbstractBlockTuple)
5355
return widened_constructorof(typeof(bt))(map(f, Tuple(bt)), Val(BL))
5456
end
5557

58+
function Base.show(io::IO, bt::AbstractBlockTuple)
59+
return print(io, nameof(typeof(bt)), blocks(bt))
60+
end
61+
function Base.show(io::IO, ::MIME"text/plain", bt::AbstractBlockTuple)
62+
println(io, typeof(bt))
63+
return print(io, blocks(bt))
64+
end
65+
5666
# Broadcast interface
5767
Base.broadcastable(bt::AbstractBlockTuple) = bt
5868
struct AbstractBlockTupleBroadcastStyle{BlockLengths,BT} <: Broadcast.BroadcastStyle end
@@ -72,6 +82,8 @@ function Base.copy(
7282
return widened_constructorof(BT)(bc.f.((Tuple.(bc.args))...), Val(BlockLengths))
7383
end
7484

85+
Base.ndims(::Type{<:AbstractBlockTuple}) = 1 # needed in nested broadcast
86+
7587
# BlockArrays interface
7688
BlockArrays.blockfirsts(::AbstractBlockTuple{0}) = ()
7789
function BlockArrays.blockfirsts(bt::AbstractBlockTuple)

test/test_blockedtuple.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ using TensorAlgebra: BlockedTuple, blockeachindex, tuplemortar
5858

5959
bt = tuplemortar(((1:2, 1:2), (1:3,)))
6060
@test length.(bt) == tuplemortar(((2, 2), (3,)))
61+
@test length.(length.(bt)) == tuplemortar(((1, 1), (1,)))
6162

6263
# empty blocks
6364
bt = tuplemortar(((1,), (), (5, 3)))

0 commit comments

Comments
 (0)