Skip to content
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

Fix nested broadcast of AbstractBlockTuple #14

Merged
merged 4 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "TensorAlgebra"
uuid = "68bd88dc-f39d-4e12-b2ca-f046b68fcc6a"
authors = ["ITensor developers <[email protected]> and contributors"]
version = "0.1.3"
version = "0.1.4"

[deps]
ArrayLayouts = "4c555306-a7a7-4459-81d9-ec55ddd5c99a"
Expand Down
12 changes: 12 additions & 0 deletions src/blockedtuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
function Base.getindex(bt::AbstractBlockTuple, bi::BlockIndexRange{1})
return bt[Block(bi)][only(bi.indices)]
end
# needed for nested broadcast in Julia < 1.11
Base.getindex(bt::AbstractBlockTuple, ci::CartesianIndex{1}) = bt[only(Tuple(ci))]

Base.iterate(bt::AbstractBlockTuple) = iterate(Tuple(bt))
Base.iterate(bt::AbstractBlockTuple, i::Int) = iterate(Tuple(bt), i)
Expand All @@ -53,6 +55,14 @@
return widened_constructorof(typeof(bt))(map(f, Tuple(bt)), Val(BL))
end

function Base.show(io::IO, bt::AbstractBlockTuple)
return print(io, nameof(typeof(bt)), blocks(bt))

Check warning on line 59 in src/blockedtuple.jl

View check run for this annotation

Codecov / codecov/patch

src/blockedtuple.jl#L58-L59

Added lines #L58 - L59 were not covered by tests
end
function Base.show(io::IO, ::MIME"text/plain", bt::AbstractBlockTuple)
println(io, typeof(bt))
return print(io, blocks(bt))

Check warning on line 63 in src/blockedtuple.jl

View check run for this annotation

Codecov / codecov/patch

src/blockedtuple.jl#L61-L63

Added lines #L61 - L63 were not covered by tests
end

# Broadcast interface
Base.broadcastable(bt::AbstractBlockTuple) = bt
struct AbstractBlockTupleBroadcastStyle{BlockLengths,BT} <: Broadcast.BroadcastStyle end
Expand All @@ -72,6 +82,8 @@
return widened_constructorof(BT)(bc.f.((Tuple.(bc.args))...), Val(BlockLengths))
end

Base.ndims(::Type{<:AbstractBlockTuple}) = 1 # needed in nested broadcast

# BlockArrays interface
BlockArrays.blockfirsts(::AbstractBlockTuple{0}) = ()
function BlockArrays.blockfirsts(bt::AbstractBlockTuple)
Expand Down
1 change: 1 addition & 0 deletions test/test_blockedtuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ using TensorAlgebra: BlockedTuple, blockeachindex, tuplemortar

bt = tuplemortar(((1:2, 1:2), (1:3,)))
@test length.(bt) == tuplemortar(((2, 2), (3,)))
@test length.(length.(bt)) == tuplemortar(((1, 1), (1,)))

# empty blocks
bt = tuplemortar(((1,), (), (5, 3)))
Expand Down
Loading