From e19b521624596b4fc155c39484086925214713dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Gauth=C3=A9?= Date: Thu, 28 Nov 2024 15:55:36 +0100 Subject: [PATCH 1/4] define sector_type --- src/gradedunitrange.jl | 4 ++++ src/labelledunitrangedual.jl | 5 +++++ test/test_basics.jl | 9 ++++++++- test/test_dual.jl | 14 +++++++++----- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/gradedunitrange.jl b/src/gradedunitrange.jl index 37cda65..487552c 100644 --- a/src/gradedunitrange.jl +++ b/src/gradedunitrange.jl @@ -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)) diff --git a/src/labelledunitrangedual.jl b/src/labelledunitrangedual.jl index 319521e..d785eca 100644 --- a/src/labelledunitrangedual.jl +++ b/src/labelledunitrangedual.jl @@ -17,6 +17,11 @@ blocklabels(la::LabelledUnitRangeDual) = [label(la)] LabelledNumbers.label(a::LabelledUnitRangeDual) = dual(label(nondual(a))) LabelledNumbers.unlabel(a::LabelledUnitRangeDual) = unlabel(nondual(a)) LabelledNumbers.LabelledStyle(::LabelledUnitRangeDual) = IsLabelled() +function LabelledNumbers.label_type( + ::Type{<:LabelledUnitRangeDual{<:Any,NondualUnitRange}} +) where {NondualUnitRange} + return label_type(NondualUnitRange) +end for f in [:first, :getindex, :last, :length, :step] @eval Base.$f(a::LabelledUnitRangeDual, args...) = diff --git a/test/test_basics.jl b/test/test_basics.jl index 96e7dc6..cbd9bc4 100644 --- a/test/test_basics.jl +++ b/test/test_basics.jl @@ -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 @@ -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) diff --git a/test/test_dual.jl b/test/test_dual.jl index 8c4370e..de00871 100644 --- a/test/test_dual.jl +++ b/test/test_dual.jl @@ -14,8 +14,8 @@ using BlockArrays: mortar, combine_blockaxes using GradedUnitRanges: - GradedUnitRanges, AbstractGradedUnitRange, + GradedUnitRanges, GradedUnitRangeDual, LabelledUnitRangeDual, OneToOne, @@ -24,10 +24,11 @@ using GradedUnitRanges: blocksortperm, dual, flip, - space_isequal, gradedrange, isdual, - nondual + nondual, + space_isequal, + sector_type using LabelledNumbers: LabelledInteger, LabelledUnitRange, label, label_type, labelled, labelled_isequal, unlabel using Test: @test, @test_broken, @testset @@ -70,7 +71,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) @@ -90,7 +92,8 @@ 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 iterate(lad) == (1, 1) @test iterate(lad) == (1, 1) @@ -155,6 +158,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) From a270ec746f3d74b9b42b844f7086f100d99b44ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Gauth=C3=A9?= Date: Sun, 1 Dec 2024 21:36:35 +0100 Subject: [PATCH 2/4] define dual_type and nondual_type --- src/dual.jl | 5 +++++ src/gradedunitrangedual.jl | 17 +++++++++++++++++ src/labelledunitrangedual.jl | 15 +++++++++++++-- test/test_dual.jl | 12 ++++++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/dual.jl b/src/dual.jl index aadc58e..e39c5c6 100644 --- a/src/dual.jl +++ b/src/dual.jl @@ -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))) diff --git a/src/gradedunitrangedual.jl b/src/gradedunitrangedual.jl index 78cf48e..d1c8381 100644 --- a/src/gradedunitrangedual.jl +++ b/src/gradedunitrangedual.jl @@ -9,6 +9,23 @@ 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)) diff --git a/src/labelledunitrangedual.jl b/src/labelledunitrangedual.jl index d785eca..6dd91e5 100644 --- a/src/labelledunitrangedual.jl +++ b/src/labelledunitrangedual.jl @@ -14,13 +14,24 @@ 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{<:LabelledUnitRangeDual{<:Any,NondualUnitRange}} + type::Type{<:LabelledUnitRangeDual{<:Any,NondualUnitRange}} ) where {NondualUnitRange} - return label_type(NondualUnitRange) + # `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] diff --git a/test/test_dual.jl b/test/test_dual.jl index de00871..179e71e 100644 --- a/test/test_dual.jl +++ b/test/test_dual.jl @@ -23,10 +23,12 @@ using GradedUnitRanges: blockmergesortperm, blocksortperm, dual, + dual_type, flip, gradedrange, isdual, nondual, + nondual_type, space_isequal, sector_type using LabelledNumbers: @@ -95,6 +97,11 @@ end @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) @test iterate(lad, 1) == (2, 2) @@ -167,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)} From efbe15d4b93a7bf17b6dc424270db6a7a503bb9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Gauth=C3=A9?= Date: Sun, 1 Dec 2024 21:37:25 +0100 Subject: [PATCH 3/4] format --- src/gradedunitrangedual.jl | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/gradedunitrangedual.jl b/src/gradedunitrangedual.jl index d1c8381..2d26d91 100644 --- a/src/gradedunitrangedual.jl +++ b/src/gradedunitrangedual.jl @@ -19,12 +19,10 @@ 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 +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)) From 7b72aa789a1ecf8cc6f6c63bc343b9f35fca77b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Gauth=C3=A9?= Date: Mon, 2 Dec 2024 10:36:54 +0100 Subject: [PATCH 4/4] simplify label_type --- src/labelledunitrangedual.jl | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/labelledunitrangedual.jl b/src/labelledunitrangedual.jl index 6dd91e5..b2ca841 100644 --- a/src/labelledunitrangedual.jl +++ b/src/labelledunitrangedual.jl @@ -27,9 +27,7 @@ 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{<:Any,NondualUnitRange}} -) where {NondualUnitRange} +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