changeset 1990:8cf1764ba124 feature/grids/parameter_spaces/in

Implement Base.in(x, ::HyperBox)
author Jonatan Werpers <jonatan@werpers.com>
date Wed, 23 Apr 2025 15:42:38 +0200
parents 77ff0a2acbe5
children 7ead7a87af18
files src/Grids/parameter_space.jl test/Grids/parameter_space_test.jl
diffstat 2 files changed, 13 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/src/Grids/parameter_space.jl	Wed Apr 23 15:27:42 2025 +0200
+++ b/src/Grids/parameter_space.jl	Wed Apr 23 15:42:38 2025 +0200
@@ -104,6 +104,11 @@
     end
 end
 
+function Base.in(x, box::HyperBox)
+    return all(eachindex(x)) do i
+        box.a[i] <= x[i] <= box.b[i]
+    end
+end
 
 """
     unitsquare(T=Float64)
--- a/test/Grids/parameter_space_test.jl	Wed Apr 23 15:27:42 2025 +0200
+++ b/test/Grids/parameter_space_test.jl	Wed Apr 23 15:42:38 2025 +0200
@@ -1,6 +1,7 @@
 using Test
 
 using Diffinitive.Grids
+using StaticArrays
 
 @testset "ParameterSpace" begin
     @test ndims(HyperBox([1,1], [2,2])) == 2
@@ -66,6 +67,13 @@
         CartesianBoundary{3,LowerBoundary}(),
         CartesianBoundary{3,UpperBoundary}(),
     ]
+
+    @test @SVector[1.5, 3.5] ∈ HyperBox([1,2], [3,4])
+    @test @SVector[1, 2] ∈ HyperBox([1,2], [3,4])
+    @test @SVector[3, 4] ∈ HyperBox([1,2], [3,4])
+
+    @test @SVector[0.5, 3.5] ∉ HyperBox([1,2], [3,4])
+    @test @SVector[1.5, 4.5] ∉ HyperBox([1,2], [3,4])
 end
 
 @testset "Simplex" begin