-
Notifications
You must be signed in to change notification settings - Fork 2
Define BlockedTuple
#9
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
200f88d
define BlockedTuple
ogauthe b5dfa02
full code coverage
ogauthe a82f21c
BlockLengths as type parameter
ogauthe 05fc957
define AbstractBlockTuple
ogauthe 2342979
reorder file
ogauthe f33dbb7
test type stability
ogauthe 18003d0
generic BlockedTuple
ogauthe 1c9aae7
generic getindex
ogauthe 0914944
generic copy
ogauthe eabbf50
use Base.front
ogauthe df2aad7
remove copy
ogauthe 831c906
define tuplemortar
ogauthe 2b5922a
generic broadcast
ogauthe 3b9352d
generic map
ogauthe bfc6554
fix version conflict
ogauthe a2d212f
tuplemortar with nested tuple input
ogauthe f554927
version 0.1.1
ogauthe File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# This file defines BlockedTuple, a Tuple of heterogeneous Tuple with a BlockArrays.jl | ||
# like interface | ||
|
||
using BlockArrays: Block, BlockArrays, BlockIndexRange, BlockRange, blockedrange | ||
|
||
struct BlockedTuple{Divs,Flat} | ||
flat::Flat | ||
|
||
function BlockedTuple{Divs}(flat::Tuple) where {Divs} | ||
length(flat) != sum(Divs) && throw(DimensionMismatch("Invalid total length")) | ||
return new{Divs,typeof(flat)}(flat) | ||
end | ||
end | ||
|
||
# TensorAlgebra Interface | ||
BlockedTuple(tt::Vararg{Tuple}) = BlockedTuple{length.(tt)}(flatten_tuples(tt)) | ||
ogauthe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
BlockedTuple(bt::BlockedTuple) = bt | ||
ogauthe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
flatten_tuples(bt::BlockedTuple) = Tuple(bt) | ||
ogauthe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Base interface | ||
Base.Tuple(bt::BlockedTuple) = bt.flat | ||
|
||
Base.axes(bt::BlockedTuple) = (blockedrange([blocklengths(bt)...]),) | ||
|
||
Base.broadcastable(bt::BlockedTuple) = bt | ||
struct BlockedTupleBroadcastStyle{Divs} <: Broadcast.BroadcastStyle end | ||
function Base.BroadcastStyle(::Type{<:BlockedTuple{Divs}}) where {Divs} | ||
return BlockedTupleBroadcastStyle{Divs}() | ||
end | ||
ogauthe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
function Base.BroadcastStyle(::BlockedTupleBroadcastStyle, ::BlockedTupleBroadcastStyle) | ||
throw(DimensionMismatch("Incompatible blocks")) | ||
mtfishman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
# BroadcastStyle is not called for two identical styles | ||
ogauthe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
function Base.copy(bc::Broadcast.Broadcasted{BlockedTupleBroadcastStyle{Divs}}) where {Divs} | ||
return BlockedTuple{Divs}(bc.f.((Tuple.(bc.args))...)) | ||
end | ||
|
||
Base.copy(bt::BlockedTuple) = BlockedTuple{blocklengths(bt)}(copy.(Tuple(bt))) | ||
|
||
Base.deepcopy(bt::BlockedTuple) = BlockedTuple{blocklengths(bt)}(deepcopy.(Tuple(bt))) | ||
ogauthe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Base.firstindex(::BlockedTuple) = 1 | ||
|
||
Base.getindex(bt::BlockedTuple, i::Integer) = Tuple(bt)[i] | ||
ogauthe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Base.getindex(bt::BlockedTuple, r::AbstractUnitRange) = Tuple(bt)[r] | ||
ogauthe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Base.getindex(bt::BlockedTuple, b::Block{1}) = blocks(bt)[Int(b)] | ||
ogauthe marked this conversation as resolved.
Show resolved
Hide resolved
ogauthe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Base.getindex(bt::BlockedTuple, br::BlockRange{1}) = blocks(bt)[Int.(br)] | ||
mtfishman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Base.getindex(bt::BlockedTuple, bi::BlockIndexRange{1}) = bt[Block(bi)][only(bi.indices)] | ||
ogauthe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Base.iterate(bt::BlockedTuple) = iterate(Tuple(bt)) | ||
Base.iterate(bt::BlockedTuple, i::Int) = iterate(Tuple(bt), i) | ||
ogauthe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Base.lastindex(bt::BlockedTuple) = length(bt) | ||
|
||
Base.length(bt::BlockedTuple) = length(Tuple(bt)) | ||
ogauthe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Base.map(f, bt::BlockedTuple) = BlockedTuple{blocklengths(bt)}(map(f, Tuple(bt))) | ||
|
||
# BlockArrays interface | ||
function BlockArrays.blockfirsts(bt::BlockedTuple) | ||
return (0, cumsum(blocklengths(bt)[begin:(end - 1)])...) .+ 1 | ||
ogauthe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
function BlockArrays.blocklasts(bt::BlockedTuple) | ||
return cumsum(blocklengths(bt)[begin:end]) | ||
end | ||
|
||
BlockArrays.blocklength(::BlockedTuple{Divs}) where {Divs} = length(Divs) | ||
ogauthe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
BlockArrays.blocklengths(::BlockedTuple{Divs}) where {Divs} = Divs | ||
|
||
function BlockArrays.blocks(bt::BlockedTuple) | ||
bf = blockfirsts(bt) | ||
bl = blocklasts(bt) | ||
return ntuple(i -> Tuple(bt)[bf[i]:bl[i]], blocklength(bt)) | ||
end | ||
mtfishman marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
using Test: @test, @test_throws | ||
|
||
using BlockArrays: Block, blocklength, blocklengths, blockedrange, blockisequal, blocks | ||
|
||
using TensorAlgebra: BlockedTuple, flatten_tuples | ||
|
||
@testset "BlockedTuple" begin | ||
flat = (1, 'a', 2, 'b', 3) | ||
divs = (1, 2, 2) | ||
|
||
bt = BlockedTuple{divs}(flat) | ||
|
||
@test Tuple(bt) == flat | ||
@test flatten_tuples(bt) == flat | ||
@test bt == BlockedTuple((1,), ('a', 2), ('b', 3)) | ||
@test BlockedTuple(bt) == bt | ||
@test blocklength(bt) == 3 | ||
@test blocklengths(bt) == (1, 2, 2) | ||
@test blocks(bt) == ((1,), ('a', 2), ('b', 3)) | ||
|
||
@test bt[1] == 1 | ||
@test bt[2] == 'a' | ||
@test bt[Block(1)] == blocks(bt)[1] | ||
@test bt[Block(2)] == blocks(bt)[2] | ||
@test bt[Block(1):Block(2)] == blocks(bt)[1:2] | ||
@test bt[Block(2)[1:2]] == ('a', 2) | ||
@test bt[2:4] == ('a', 2, 'b') | ||
|
||
@test firstindex(bt) == 1 | ||
@test lastindex(bt) == 5 | ||
@test length(bt) == 5 | ||
|
||
@test iterate(bt) == (1, 2) | ||
@test iterate(bt, 2) == ('a', 3) | ||
@test blockisequal(only(axes(bt)), blockedrange([1, 2, 2])) | ||
|
||
@test_throws DimensionMismatch BlockedTuple{(1, 2, 3)}(flat) | ||
|
||
bt = BlockedTuple((1,), (4, 2), (5, 3)) | ||
@test Tuple(bt) == (1, 4, 2, 5, 3) | ||
@test blocklengths(bt) == (1, 2, 2) | ||
@test copy(bt) == bt | ||
@test deepcopy(bt) == bt | ||
|
||
@test map(n -> n + 1, bt) == BlockedTuple{blocklengths(bt)}(Tuple(bt) .+ 1) | ||
@test bt .+ BlockedTuple((1,), (1, 1), (1, 1)) == | ||
BlockedTuple{blocklengths(bt)}(Tuple(bt) .+ 1) | ||
@test_throws DimensionMismatch bt .+ BlockedTuple((1, 1), (1, 1), (1,)) | ||
|
||
bt = BlockedTuple((1:2, 1:2), (1:3,)) | ||
@test length.(bt) == BlockedTuple((2, 2), (3,)) | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.