Skip to content

Commit 0abdf6a

Browse files
committed
src/sage/geometry/cone.py: optimize cone containment check
Rewrite the conditionals inside the main loop of _contains() to avoid one equality test. Currently, in the worst case (where we do not return False and proceed to the next constraint), we test, * c.is_equality() * pr != 0 * pr < 0 * need_strict * pr == 0 (redundant!) After rewriting, we test * pr > 0 * pr < 0 * need_strict * c.is_equality() For this to matter, you need to check an absurd number of conditions, but %timeit does show a small improvement after many repeated runs.
1 parent b2bf630 commit 0abdf6a

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/sage/geometry/cone.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,11 +1686,14 @@ def _contains(self, point, region='whole cone'):
16861686
M = self.dual_lattice()
16871687
for c in self._PPL_cone().minimized_constraints():
16881688
pr = M(c.coefficients()) * point
1689-
if c.is_equality():
1690-
if pr != 0:
1691-
return False
1692-
elif pr < 0 or need_strict and pr == 0:
1689+
if pr < 0:
16931690
return False
1691+
elif pr > 0:
1692+
if c.is_equality():
1693+
return False
1694+
else:
1695+
if need_strict and not c.is_equality():
1696+
return False
16941697
return True
16951698

16961699
def interior_contains(self, *args):

0 commit comments

Comments
 (0)