Mercurial > repos > public > sbplib_julia
changeset 1779:2a8a2b52a112 feature/grids/manifolds
Refactor Interval to its own type. This allows the user better control of the coordinate type of 1d grids
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Mon, 16 Sep 2024 08:33:43 +0200 |
parents | a93d2b668446 |
children | 8ecdc5bb46be |
files | src/Grids/equidistant_grid.jl src/Grids/manifolds.jl test/Grids/manifolds_test.jl |
diffstat | 3 files changed, 32 insertions(+), 11 deletions(-) [+] |
line wrap: on
line diff
--- a/src/Grids/equidistant_grid.jl Sun Sep 15 23:27:04 2024 +0200 +++ b/src/Grids/equidistant_grid.jl Mon Sep 16 08:33:43 2024 +0200 @@ -151,9 +151,8 @@ return EquidistantGrid(range(limit_lower, limit_upper, length=size)) # TBD: Should it use LinRange instead? end - -equidistant_grid(hb::HyperBox, dims::Vararg{Int}) = equidistant_grid(hb.a, hb.b, dims...) -# TODO: One dimensional grids shouldn't have vector eltype right?, Change here or in HyperBox? +equidistant_grid(d::Interval, size::Int) = equidistant_grid(limits(d)..., size) +equidistant_grid(hb::HyperBox, dims::Vararg{Int}) = equidistant_grid(limits(hb)..., dims...) function equidistant_grid(c::Chart, dims::Vararg{Int}) lg = equidistant_grid(parameterspace(c), dims...)
--- a/src/Grids/manifolds.jl Sun Sep 15 23:27:04 2024 +0200 +++ b/src/Grids/manifolds.jl Mon Sep 16 08:33:43 2024 +0200 @@ -21,6 +21,21 @@ Base.ndims(::ParameterSpace{D}) where D = D # TBD: Should implement domain_dim? +struct Interval{T} <: ParameterSpace{1} + a::T + b::T + + function Interval(a,b) + a, b = promote(a, b) + new{typeof(a)}(a,b) + end +end + +limits(i::Interval) = (i.a, i.b) + +unitinterval(T=Float64) = Interval(zero(T), one(T)) + + struct HyperBox{T,D} <: ParameterSpace{D} a::SVector{D,T} b::SVector{D,T} @@ -31,14 +46,12 @@ HyperBox(convert(T,a), convert(T,b)) end -Interval{T} = HyperBox{T,1} Rectangle{T} = HyperBox{T,2} Box{T} = HyperBox{T,3} limits(box::HyperBox, d) = (box.a[d], box.b[d]) limits(box::HyperBox) = (box.a, box.b) -unitinterval(T=Float64) = unithyperbox(T,1) unitsquare(T=Float64) = unithyperbox(T,2) unitcube(T=Float64) = unithyperbox(T,3) unithyperbox(T, D) = HyperBox((@SVector zeros(T,D)), (@SVector ones(T,D)))
--- a/test/Grids/manifolds_test.jl Sun Sep 15 23:27:04 2024 +0200 +++ b/test/Grids/manifolds_test.jl Mon Sep 16 08:33:43 2024 +0200 @@ -11,6 +11,21 @@ @test ndims(unittetrahedron()) == 3 end +@testset "Interval" begin + @test Interval <: ParameterSpace{1} + + @test Interval(0,1) isa Interval{Int} + @test Interval(0,1.) isa Interval{Float64} + + @test unitinterval() isa Interval{Float64} + @test unitinterval() == Interval(0.,1.) + @test limits(unitinterval()) == (0.,1.) + + @test unitinterval(Int) isa Interval{Int} + @test unitinterval(Int) == Interval(0,1) + @test limits(unitinterval(Int)) == (0,1) +end + @testset "HyperBox" begin @test HyperBox{<:Any, 2} <: ParameterSpace{2} @test HyperBox([1,1], [2,2]) isa HyperBox{Int, 2} @@ -19,12 +34,6 @@ @test limits(HyperBox([1,2], [3,4]), 1) == (1,3) @test limits(HyperBox([1,2], [3,4]), 2) == (2,4) - @test unitinterval() isa HyperBox{Float64,1} - @test limits(unitinterval()) == ([0], [1]) - - @test unitinterval(Int) isa HyperBox{Int,1} - @test limits(unitinterval(Int)) == ([0], [1]) - @test unitsquare() isa HyperBox{Float64,2} @test limits(unitsquare()) == ([0,0],[1,1])