Skip to content

Commit

Permalink
fix flip
Browse files Browse the repository at this point in the history
  • Loading branch information
ogauthe committed Nov 7, 2024
1 parent 17d457d commit 0d51b19
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
1 change: 1 addition & 0 deletions NDTensors/src/lib/GradedAxes/src/dual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ label_dual(x) = label_dual(LabelledStyle(x), x)
label_dual(::NotLabelled, x) = x
label_dual(::IsLabelled, x) = labelled(unlabel(x), dual(label(x)))

flip(a::AbstractUnitRange) = dual(label_dual(a))
flip(g::AbstractGradedUnitRange) = dual(gradedrange(label_dual.(blocklengths(g))))
3 changes: 2 additions & 1 deletion NDTensors/src/lib/GradedAxes/src/labelledunitrangedual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ end
dual(a::LabelledUnitRange) = LabelledUnitRangeDual(a)
nondual(a::LabelledUnitRangeDual) = a.nondual_unitrange
dual(a::LabelledUnitRangeDual) = nondual(a)
flip(a::LabelledUnitRangeDual) = dual(flip(nondual(a)))
label_dual(::IsLabelled, a::LabelledUnitRangeDual) = dual(label_dual(nondual(a)))
isdual(::LabelledUnitRangeDual) = true
blocklabels(la::LabelledUnitRangeDual) = [label(la)]

LabelledNumbers.label(a::LabelledUnitRangeDual) = dual(label(nondual(a)))
LabelledNumbers.unlabel(a::LabelledUnitRangeDual) = unlabel(nondual(a))
LabelledNumbers.LabelledStyle(::LabelledUnitRangeDual) = IsLabelled()

for f in [:first, :getindex, :last, :length, :step]
@eval Base.$f(a::LabelledUnitRangeDual, args...) =
Expand Down
29 changes: 28 additions & 1 deletion NDTensors/src/lib/GradedAxes/test/test_dual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ using NDTensors.GradedAxes:
isdual,
nondual
using NDTensors.LabelledNumbers:
LabelledInteger, LabelledUnitRange, label, labelled, labelled_isequal, unlabel
LabelledInteger, LabelledUnitRange, label, label_type, labelled, labelled_isequal, unlabel
using Test: @test, @test_broken, @testset
struct U1
n::Int
Expand Down Expand Up @@ -70,6 +70,7 @@ end
@test !isdual(la)
@test labelled_isequal(la, la)
@test space_isequal(la, la)
@test label_type(la) == U1

lad = dual(la)
@test lad isa LabelledUnitRangeDual
Expand All @@ -84,6 +85,22 @@ end
@test isdual(lad)
@test nondual(lad) === la
@test dual(lad) === la
@test label_type(lad) == U1

laf = flip(la)
@test laf isa LabelledUnitRangeDual
@test label(laf) == U1(1)
@test unlabel(laf) == 1:2

ladf = flip(dual(la))
@test ladf isa LabelledUnitRange
@test label(ladf) == U1(-1)
@test unlabel(ladf) == 1:2

lafd = dual(flip(la))
@test lafd isa LabelledUnitRange
@test label(lafd) == U1(-1)
@test unlabel(lafd) == 1:2

# check default behavior for objects without dual
la = labelled(1:2, 'x')
Expand All @@ -100,6 +117,16 @@ end
@test isdual(lad)
@test nondual(lad) === la
@test dual(lad) === la

laf = flip(la)
@test laf isa LabelledUnitRangeDual
@test label(laf) == 'x'
@test unlabel(laf) == 1:2

ladf = flip(lad)
@test ladf isa LabelledUnitRange
@test label(ladf) == 'x'
@test unlabel(ladf) == 1:2
end

@testset "GradedUnitRangeDual" begin
Expand Down

0 comments on commit 0d51b19

Please sign in to comment.