Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comparison operators for PwAff #115

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 54 additions & 4 deletions islpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,56 @@ def pw_get_aggregate_domain(self):

return result

def pw_le(self, other):
"""
:return: self <= other
"""
if isinstance(other, int):
return self.le_set(self * 0 + other)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There must be a better way to do this.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    zero = Aff.zero_on_domain(LocalSpace.from_space(space))
    result[0] = PwAff.from_aff(zero)

else:
assert isinstance(other, PwAff)
return self.le_set(other)

def pw_lt(self, other):
"""
:return: self < other
"""
if isinstance(other, int):
return self.lt_set(self * 0 + other)
else:
assert isinstance(other, PwAff)
return self.lt_set(other)

def pw_ge(self, other):
"""
:return: self >= other
"""
if isinstance(other, int):
return self.ge_set(self * 0 + other)
else:
assert isinstance(other, PwAff)
return self.ge_set(other)

def pw_gt(self, other):
"""
:return: self >= other
"""
if isinstance(other, int):
return self.gt_set(self * 0 + other)
else:
assert isinstance(other, PwAff)
return self.gt_set(other)

def pw_bool(self):
return NotImplementedError(
"Converting a PwAff to a boolean is nor supported.")

PwAff.__le__ = pw_le
PwAff.__lt__ = pw_lt
PwAff.__ge__ = pw_ge
PwAff.__gt__ = pw_gt
Comment on lines +734 to +737
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One issue with this is that it can't extend to == and !=. I wonder whether that invalidates the idea...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. This is a problem for eg: SymPy where == and != are structural equivalence and Eq gives an expression. Numpy however is consistent and implements == and != and returns a numpy array of bools.

PwAff.__bool__ = pw_bool
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to override __bool__ on Set.


PwAff.get_pieces = pwaff_get_pieces
PwAff.get_aggregate_domain = pw_get_aggregate_domain

Expand Down Expand Up @@ -1288,13 +1338,13 @@ def make_zero_and_vars(set_vars, params=(), ctx=None):
v = isl.make_zero_and_vars("i,j,k", "n")

myset = (
v[0].le_set(v["i"] + v["j"])
(0 <= v["i"] + v["j"])
&
(v["i"] + v["j"]).lt_set(v["n"])
(v["i"] + v["j"] < v["n"])
&
(v[0].le_set(v["i"]))
(0 <= v["i"])
&
(v["i"].le_set(13 + v["n"]))
(v["i"] <= 13 + v["n"])
)
"""
if ctx is None:
Expand Down
8 changes: 8 additions & 0 deletions test/test_isl.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,15 @@ def test_make_zero_and_vars():
& (v["i"].le_set(13 + v["n"]))
)

myset2 = (
(0 <= v["i"] + v["j"])
& (v["i"] + v["j"] < v["n"])
& (0 <= v["i"])
& (v["i"] <= 13 + v["n"])
)

print(myset)
assert myset == myset2


def test_affs_from_space():
Expand Down