From cf68133d070b89496f20e373ccb2db89fbe8c0df Mon Sep 17 00:00:00 2001 From: Oliver Schulz Date: Tue, 30 May 2023 17:26:21 +0200 Subject: [PATCH 1/3] Use StaticInteger instead of StaticInt --- src/MeasureBase.jl | 1 + src/density-core.jl | 2 +- src/standard/stdmeasure.jl | 2 +- src/static.jl | 8 ++++---- src/transport.jl | 8 ++++---- src/utils.jl | 2 +- 6 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/MeasureBase.jl b/src/MeasureBase.jl index d8336689..e29c4ae9 100644 --- a/src/MeasureBase.jl +++ b/src/MeasureBase.jl @@ -34,6 +34,7 @@ const Pretty = PrettyPrinting using ChainRulesCore import FillArrays using Static +using Static: StaticInteger using FunctionChains export ≪ diff --git a/src/density-core.jl b/src/density-core.jl index 3fac8cfc..c8c861ee 100644 --- a/src/density-core.jl +++ b/src/density-core.jl @@ -143,7 +143,7 @@ end @generated function _logdensity_rel( μs::Tμ, νs::Tν, - ::Tuple{StaticInt{M},StaticInt{N}}, + ::Tuple{<:StaticInteger{M},<:StaticInteger{N}}, x::X, ) where {Tμ,Tν,M,N,X} sμ = schema(Tμ) diff --git a/src/standard/stdmeasure.jl b/src/standard/stdmeasure.jl index 1806575e..833f280e 100644 --- a/src/standard/stdmeasure.jl +++ b/src/standard/stdmeasure.jl @@ -34,7 +34,7 @@ end # Implement transport_to(NU::Type{<:StdMeasure}, μ) and transport_to(ν, MU::Type{<:StdMeasure}): -_std_measure(::Type{M}, ::StaticInt{1}) where {M<:StdMeasure} = M() +_std_measure(::Type{M}, ::StaticInteger{1}) where {M<:StdMeasure} = M() _std_measure(::Type{M}, dof::IntegerLike) where {M<:StdMeasure} = M()^dof _std_measure_for(::Type{M}, μ::Any) where {M<:StdMeasure} = _std_measure(M, getdof(μ)) diff --git a/src/static.jl b/src/static.jl index b3a09dc5..81c528ba 100644 --- a/src/static.jl +++ b/src/static.jl @@ -1,9 +1,9 @@ """ MeasureBase.IntegerLike -Equivalent to `Union{Integer,Static.StaticInt}`. +Equivalent to `Union{Integer,Static.StaticInteger}`. """ -const IntegerLike = Union{Integer,Static.StaticInt} +const IntegerLike = Union{Integer,Static.StaticInteger} """ MeasureBase.one_to(n::IntegerLike) @@ -14,7 +14,7 @@ Returns an instance of `Base.OneTo` or `Static.SOneTo`, depending on the type of `n`. """ @inline one_to(n::Integer) = Base.OneTo(n) -@inline one_to(::Static.StaticInt{N}) where {N} = Static.SOneTo{N}() +@inline one_to(::Static.StaticInteger{N}) where {N} = Static.SOneTo{N}() _dynamic(x::Number) = dynamic(x) _dynamic(::Static.SOneTo{N}) where {N} = Base.OneTo(N) @@ -49,7 +49,7 @@ Returns the length of `x` as a dynamic or static integer. """ maybestatic_length(x) = length(x) maybestatic_length(x::AbstractUnitRange) = length(x) -function maybestatic_length(::Static.OptionallyStaticUnitRange{StaticInt{A},StaticInt{B}}) where {A,B} +function maybestatic_length(::Static.OptionallyStaticUnitRange{<:StaticInteger{A},<:StaticInteger{B}}) where {A,B} StaticInt{B - A + 1}() end diff --git a/src/transport.jl b/src/transport.jl index cc73c07d..ce8ce1fd 100644 --- a/src/transport.jl +++ b/src/transport.jl @@ -139,14 +139,14 @@ _origin_depth_pullback(ΔΩ) = NoTangent(), NoTangent() ChainRulesCore.rrule(::typeof(_origin_depth), ν) = _origin_depth(ν), _origin_depth_pullback # If both both measures have no origin: -function _transport_between_origins(ν, ::StaticInt{0}, ::StaticInt{0}, μ, x) +function _transport_between_origins(ν, ::StaticInteger{0}, ::StaticInteger{0}, μ, x) _transport_with_intermediate(ν, _transport_intermediate(ν, μ), μ, x) end @generated function _transport_between_origins( ν, - ::StaticInt{n_ν}, - ::StaticInt{n_μ}, + ::StaticInteger{n_ν}, + ::StaticInteger{n_μ}, μ, x, ) where {n_ν,n_μ} @@ -188,7 +188,7 @@ end @inline _transport_intermediate(ν, μ) = _transport_intermediate(getdof(ν), getdof(μ)) @inline _transport_intermediate(::Integer, n_μ::Integer) = StdUniform()^n_μ -@inline _transport_intermediate(::StaticInt{1}, ::StaticInt{1}) = StdUniform() +@inline _transport_intermediate(::StaticInteger{1}, ::StaticInteger{1}) = StdUniform() _call_transport_def(ν, μ, x) = transport_def(ν, μ, x) _call_transport_def(::Any, ::Any, x::NoTransportOrigin) = x diff --git a/src/utils.jl b/src/utils.jl index 861d1448..4a0c79a6 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -33,7 +33,7 @@ repeatedly until there's no change. That's what this does. _rootmeasure(μ, static(n)) end -@generated function _rootmeasure(μ, ::StaticInt{n}) where {n} +@generated function _rootmeasure(μ, ::StaticInteger{n}) where {n} q = quote end foreach(1:n) do _ push!(q.args, :(μ = basemeasure(μ))) From b1544406050a339fb42c1094ef8f20850c5978f5 Mon Sep 17 00:00:00 2001 From: Oliver Schulz Date: Tue, 30 May 2023 18:19:19 +0200 Subject: [PATCH 2/3] Add tests for static integer and range functions --- test/Project.toml | 1 + test/runtests.jl | 2 ++ test/static.jl | 31 +++++++++++++++++++++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 test/static.jl diff --git a/test/Project.toml b/test/Project.toml index 3b769743..be6a8723 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -3,6 +3,7 @@ Aqua = "4c88cf16-eb10-579e-8560-4a9242c79595" ChainRulesTestUtils = "cdddcdb0-9152-4a09-a978-84456f9df70a" ChangesOfVariables = "9e997f8a-9a97-42d5-a9f1-ce6bfc15e2c0" DensityInterface = "b429d917-457f-4dbc-8f4c-0cc954292b1d" +FillArrays = "1a297f60-69ca-5386-bcde-b61e274b549b" InverseFunctions = "3587e190-3f89-42d0-90ee-14403ec27112" IrrationalConstants = "92d709cd-6900-40b7-9082-c6be49f344b6" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" diff --git a/test/runtests.jl b/test/runtests.jl index f10bde45..8a2deaa4 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -10,6 +10,8 @@ using MeasureBase: test_interface, test_smf using Aqua Aqua.test_all(MeasureBase; ambiguities = false) +include("static.jl") + # Aqua._test_ambiguities( # Aqua.aspkgids(MeasureBase); # exclude = [LogarithmicNumbers.Logarithmic], diff --git a/test/static.jl b/test/static.jl new file mode 100644 index 00000000..a6c50db2 --- /dev/null +++ b/test/static.jl @@ -0,0 +1,31 @@ +using Test + +import MeasureBase + +import Static +using Static: static +import FillArrays + +@testset "static" begin + @test 2 isa MeasureBase.IntegerLike + @test static(2) isa MeasureBase.IntegerLike + @test true isa MeasureBase.IntegerLike + @test static(true) isa MeasureBase.IntegerLike + + @test @inferred(MeasureBase.one_to(7)) isa Base.OneTo + @test @inferred(MeasureBase.one_to(7)) == 1:7 + @test @inferred(MeasureBase.one_to(static(7))) isa Static.SOneTo + @test @inferred(MeasureBase.one_to(static(7))) == static(1):static(7) + + @test @inferred(MeasureBase.fill_with(4.2, (7,))) == FillArrays.Fill(4.2, 7) + @test @inferred(MeasureBase.fill_with(4.2, (static(7),))) == FillArrays.Fill(4.2, 7) + @test @inferred(MeasureBase.fill_with(4.2, (3, static(7)))) == FillArrays.Fill(4.2, 3, 7) + @test @inferred(MeasureBase.fill_with(4.2, (3:7,))) == FillArrays.Fill(4.2, (3:7,)) + @test @inferred(MeasureBase.fill_with(4.2, (static(3):static(7),))) == FillArrays.Fill(4.2, (3:7,)) + @test @inferred(MeasureBase.fill_with(4.2, (3:7, static(2):static(5)))) == FillArrays.Fill(4.2, (3:7, 2:5)) + + @test MeasureBase.maybestatic_length(MeasureBase.one_to(7)) isa Int + @test MeasureBase.maybestatic_length(MeasureBase.one_to(7)) == 7 + @test MeasureBase.maybestatic_length(MeasureBase.one_to(static(7))) isa Static.StaticInt + @test MeasureBase.maybestatic_length(MeasureBase.one_to(static(7))) == static(7) +end From 4dac4fcc411d52fc9dec04061ce686cdf6acda98 Mon Sep 17 00:00:00 2001 From: Oliver Schulz Date: Wed, 31 May 2023 10:34:21 +0200 Subject: [PATCH 3/3] Increase package version to 0.14.7 --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index d7a2d510..366abc2f 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "MeasureBase" uuid = "fa1605e6-acd5-459c-a1e6-7e635759db14" authors = ["Chad Scherrer and contributors"] -version = "0.14.6" +version = "0.14.7" [deps] ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"