From 415c723f2ef3b75ebc65441ee6379dc4fc1f712c Mon Sep 17 00:00:00 2001 From: Bill Huang Date: Fri, 13 Oct 2023 10:47:53 +0800 Subject: [PATCH] fix: point_in_polygon fix: computation order --- src/evox/problems/numerical/maf.py | 4 ++-- tests/test_maf.py | 10 ++++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/evox/problems/numerical/maf.py b/src/evox/problems/numerical/maf.py index c6471c88a..c3313dcd0 100644 --- a/src/evox/problems/numerical/maf.py +++ b/src/evox/problems/numerical/maf.py @@ -11,8 +11,8 @@ @jit def inside(x, a, b): - """check if x is in [a, b) or (b, a]""" - return ((a <= x) & (x < b)) | ((b < x) & (x <= a)) + """check if x is in [a, b) or [b, a)""" + return (jnp.minimum(a, b) <= x) & (x < jnp.maximum(a, b)) @jit diff --git a/tests/test_maf.py b/tests/test_maf.py index 96db1f0a5..9961f41ac 100644 --- a/tests/test_maf.py +++ b/tests/test_maf.py @@ -17,8 +17,8 @@ def test_inside(): assert inside(8.5, 0.0, 1.0) == False assert inside(0.5, 0.0, 1.0) == True assert inside(0.5, 1.0, 0.0) == True - assert inside(1.0, 1.0, 0.0) == True - assert inside(0.0, 1.0, 0.0) == False + assert inside(1.0, 1.0, 0.0) == False + assert inside(0.0, 1.0, 0.0) == True assert inside(1.0, 0.0, 1.0) == False assert inside(0.0, 0.0, 1.0) == True @@ -27,9 +27,9 @@ def test_ray_intersect_segment(): assert ray_intersect_segment(point, jnp.array([1.0, 1.0]), jnp.array([1.0, 2.0])) == False assert ray_intersect_segment(point, jnp.array([1.0, 1.0]), jnp.array([-1.0, -1.0])) == True assert ray_intersect_segment(point, jnp.array([1.0, 1.0]), jnp.array([1.0, -1.0])) == True - assert ray_intersect_segment(point, jnp.array([1.0, 0.0]), jnp.array([1.0, -1.0])) == True + assert ray_intersect_segment(point, jnp.array([1.0, 0.0]), jnp.array([1.0, -1.0])) == False assert ray_intersect_segment(point, jnp.array([1.0, 0.0]), jnp.array([1.0, 1.0])) == True - assert ray_intersect_segment(point, jnp.array([1.0, 1.0]), jnp.array([1.0, 0.0])) == False + assert ray_intersect_segment(point, jnp.array([1.0, 1.0]), jnp.array([1.0, 0.0])) == True def test_point_in_polygon(): polygon = jnp.array([ @@ -43,6 +43,8 @@ def test_point_in_polygon(): assert point_in_polygon(polygon, point) == False point = jnp.array([0, 1.0]) assert point_in_polygon(polygon, point) == True + point = jnp.array([-1, 1.0]) + assert point_in_polygon(polygon, point) == False def test_maf1(): prob = MaF1(d=d, m=m)