Skip to content

Commit

Permalink
Define sector_type (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
ogauthe authored Dec 2, 2024
1 parent 6640b77 commit ddb527e
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 6 deletions.
5 changes: 5 additions & 0 deletions src/dual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ dual(x) = x
nondual(r::AbstractUnitRange) = r
isdual(::AbstractUnitRange) = false

dual_type(x) = dual_type(typeof(x))
dual_type(T::Type) = T
nondual_type(x) = nondual_type(typeof(x))
nondual_type(T::Type) = T

using LabelledNumbers: LabelledStyle, IsLabelled, NotLabelled, label, labelled, unlabel

dual(i::LabelledInteger) = labelled(unlabel(i), dual(label(i)))
Expand Down
4 changes: 4 additions & 0 deletions src/gradedunitrange.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ Base.eltype(::Type{<:GradedUnitRange{T}}) where {T} = T
LabelledNumbers.label_type(g::AbstractGradedUnitRange) = label_type(typeof(g))
LabelledNumbers.label_type(T::Type{<:AbstractGradedUnitRange}) = label_type(eltype(T))

sector_type(x) = sector_type(typeof(x))
sector_type(T::Type) = error("Not implemented")
sector_type(T::Type{<:AbstractUnitRange}) = label_type(T)

function gradedrange(lblocklengths::AbstractVector{<:LabelledInteger})
brange = blockedrange(unlabel.(lblocklengths))
lblocklasts = labelled.(blocklasts(brange), label.(lblocklengths))
Expand Down
15 changes: 15 additions & 0 deletions src/gradedunitrangedual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ nondual(a::GradedUnitRangeDual) = a.nondual_unitrange
dual(a::GradedUnitRangeDual) = nondual(a)
flip(a::GradedUnitRangeDual) = dual(flip(nondual(a)))
isdual(::GradedUnitRangeDual) = true

function nondual_type(
::Type{<:GradedUnitRangeDual{<:Any,<:Any,NondualUnitRange}}
) where {NondualUnitRange}
return NondualUnitRange
end
dual_type(T::Type{<:GradedUnitRangeDual}) = nondual_type(T)
function dual_type(type::Type{<:AbstractGradedUnitRange{T,BlockLasts}}) where {T,BlockLasts}
return GradedUnitRangeDual{T,BlockLasts,type}
end
function LabelledNumbers.label_type(type::Type{<:GradedUnitRangeDual})
# `dual_type` right now doesn't do anything but anticipates defining `SectorDual`.
return dual_type(label_type(nondual_type(type)))
end

## TODO: Define this to instantiate a dual unit range.
## materialize_dual(a::GradedUnitRangeDual) = materialize_dual(nondual(a))

Expand Down
14 changes: 14 additions & 0 deletions src/labelledunitrangedual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,23 @@ label_dual(::IsLabelled, a::LabelledUnitRangeDual) = dual(label_dual(nondual(a))
isdual(::LabelledUnitRangeDual) = true
blocklabels(la::LabelledUnitRangeDual) = [label(la)]

function nondual_type(
::Type{<:LabelledUnitRangeDual{<:Any,NondualUnitRange}}
) where {NondualUnitRange}
return NondualUnitRange
end
dual_type(T::Type{<:LabelledUnitRangeDual}) = nondual_type(T)
function dual_type(T::Type{<:LabelledUnitRange})
return LabelledUnitRangeDual{eltype(T),T}
end

LabelledNumbers.label(a::LabelledUnitRangeDual) = dual(label(nondual(a)))
LabelledNumbers.unlabel(a::LabelledUnitRangeDual) = unlabel(nondual(a))
LabelledNumbers.LabelledStyle(::LabelledUnitRangeDual) = IsLabelled()
function LabelledNumbers.label_type(type::Type{<:LabelledUnitRangeDual})
# `dual_type` right now doesn't do anything but anticipates defining `SectorDual`.
return dual_type(label_type(nondual_type(type)))
end

for f in [:first, :getindex, :last, :length, :step]
@eval Base.$f(a::LabelledUnitRangeDual, args...) =
Expand Down
9 changes: 8 additions & 1 deletion test/test_basics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ using BlockArrays:
combine_blockaxes,
mortar
using GradedUnitRanges:
GradedOneTo, GradedUnitRange, OneToOne, blocklabels, gradedrange, space_isequal
GradedOneTo,
GradedUnitRange,
OneToOne,
blocklabels,
gradedrange,
sector_type,
space_isequal
using LabelledNumbers:
LabelledUnitRange, islabelled, label, labelled, labelled_isequal, unlabel
using Test: @test, @test_broken, @testset
Expand Down Expand Up @@ -41,6 +47,7 @@ end
gradedrange(["x" => 2, "y" => 3]),
)
@test a isa GradedOneTo
@test sector_type(a) === String
@test labelled_isequal(a, a)
@test !labelled_isequal(a0, a)
@test !labelled_isequal(a, a0)
Expand Down
26 changes: 21 additions & 5 deletions test/test_dual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,23 @@ using BlockArrays:
mortar,
combine_blockaxes
using GradedUnitRanges:
GradedUnitRanges,
AbstractGradedUnitRange,
GradedUnitRanges,
GradedUnitRangeDual,
LabelledUnitRangeDual,
OneToOne,
blocklabels,
blockmergesortperm,
blocksortperm,
dual,
dual_type,
flip,
space_isequal,
gradedrange,
isdual,
nondual
nondual,
nondual_type,
space_isequal,
sector_type
using LabelledNumbers:
LabelledInteger, LabelledUnitRange, label, label_type, labelled, labelled_isequal, unlabel
using Test: @test, @test_broken, @testset
Expand Down Expand Up @@ -70,7 +73,8 @@ end
@test !isdual(la)
@test labelled_isequal(la, la)
@test space_isequal(la, la)
@test label_type(la) == U1
@test label_type(la) === U1
@test sector_type(la) === U1

@test iterate(la) == (1, 1)
@test iterate(la) == (1, 1)
Expand All @@ -90,7 +94,13 @@ end
@test isdual(lad)
@test nondual(lad) === la
@test dual(lad) === la
@test label_type(lad) == U1
@test label_type(lad) === U1
@test sector_type(lad) === U1

@test dual_type(la) === typeof(lad)
@test dual_type(lad) === typeof(la)
@test nondual_type(lad) === typeof(la)
@test nondual_type(la) === typeof(la)

@test iterate(lad) == (1, 1)
@test iterate(lad) == (1, 1)
Expand Down Expand Up @@ -155,6 +165,7 @@ end
@test eltype(ad) == LabelledInteger{Int,U1}
@test blocklengths(ad) isa Vector
@test eltype(blocklengths(ad)) == eltype(blocklengths(a))
@test sector_type(a) === U1

@test space_isequal(dual(ad), a)
@test space_isequal(nondual(ad), a)
Expand All @@ -163,6 +174,11 @@ end
@test !space_isequal(a, ad)
@test !space_isequal(ad, a)

@test dual_type(a) === typeof(ad)
@test dual_type(ad) === typeof(a)
@test nondual_type(ad) === typeof(a)
@test nondual_type(a) === typeof(a)

@test isdual(ad)
@test !isdual(a)
@test axes(Base.Slice(a)) isa Tuple{typeof(a)}
Expand Down

0 comments on commit ddb527e

Please sign in to comment.