Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3696379

Browse filesBrowse files
committed
implement method 'Domain.encloses' for finding points inside a domain
1 parent 929a9fd commit 3696379
Copy full SHA for 3696379

File tree

2 files changed

+51
-1
lines changed
Filter options

2 files changed

+51
-1
lines changed

‎adaptive/domain.py

Copy file name to clipboardExpand all lines: adaptive/domain.py
+30Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,21 @@ def which_subdomains(self, x):
8787
ValueError : if x is outside of the domain
8888
"""
8989

90+
@abc.abstractmethod
91+
def encloses(self, points):
92+
"""Return whether the domain encloses the points
93+
94+
Parameters
95+
----------
96+
points : a point or sequence of points
97+
98+
Returns
99+
-------
100+
Boolean (if a single point was provided) or an array of booleans
101+
if a sequence of points was provided) that is True when
102+
the domain encloses the point.
103+
"""
104+
90105
@abc.abstractmethod
91106
def vertices(self):
92107
"""Returns the vertices of the domain."""
@@ -276,6 +291,14 @@ def contains_subdomain(self, subdomain):
276291
def vertices(self):
277292
return self.points
278293

294+
def encloses(self, points):
295+
a, b = self.bounds
296+
points = np.asarray(points)
297+
if points.shape == (): # single point
298+
return a <= points <= b
299+
else:
300+
return np.logical_and(a <= points, points <= b)
301+
279302
def neighbors(self, subdomain, n=1):
280303
a, b = subdomain
281304
p = self.points
@@ -679,6 +702,13 @@ def neighbors(self, subdomain, n=1):
679702
def subdomains(self):
680703
return self.triangulation.simplices
681704

705+
def encloses(self, points):
706+
points = np.asarray(points).T
707+
A, b = self.bounds.equations[:, :-1], self.bounds.equations[:, -1:]
708+
if len(points.shape) == 1:
709+
points = points[:, None]
710+
return np.all(A @ points + b <= 0, axis=0)
711+
682712
def vertices(self):
683713
return self.triangulation.vertices
684714

‎adaptive/tests/test_domain.py

Copy file name to clipboardExpand all lines: adaptive/tests/test_domain.py
+21-1Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
a_few_points_inside,
77
make_hypercube_domain,
88
point_inside,
9-
point_outside,
109
point_on_shared_face,
10+
point_outside,
11+
points_inside,
12+
points_outside,
1113
)
1214
from hypothesis import given, settings
1315

@@ -66,6 +68,24 @@ def test_insert_points_outside_domain_raises(data, ndim):
6668
domain.insert(x)
6769

6870

71+
@pytest.mark.parametrize("ndim", [1, 2, 3])
72+
@given(data=st.data())
73+
def test_encloses(data, ndim):
74+
domain = data.draw(make_hypercube_domain(ndim))
75+
76+
xin = data.draw(point_inside(domain))
77+
assert domain.encloses(xin)
78+
79+
xout = data.draw(point_outside(domain))
80+
assert not domain.encloses(xout)
81+
82+
xins = data.draw(points_inside(domain, 20))
83+
assert np.all(domain.encloses(xins))
84+
85+
xouts = data.draw(points_outside(domain, 20))
86+
assert not np.any(domain.encloses(xouts))
87+
88+
6989
@pytest.mark.parametrize("ndim", [1, 2, 3])
7090
@given(data=st.data())
7191
def test_split_at_point_outside_domain_raises(data, ndim):

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.