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

[Lang] Make python scope comparison return 1 instead of -1 #5840

Merged
merged 2 commits into from
Aug 23, 2022
Merged
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
16 changes: 8 additions & 8 deletions python/taichi/lang/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -898,10 +898,10 @@ def any(self):
>>> v.any()
True
"""
ret = ops_mod.cmp_ne(self.entries[0], 0)
for i in range(1, len(self.entries)):
re-xyr marked this conversation as resolved.
Show resolved Hide resolved
ret = ret + ops_mod.cmp_ne(self.entries[i], 0)
return -ops_mod.cmp_lt(ret, 0)
ret = False
for entry in self.entries:
ret = ret | ops_mod.cmp_ne(entry, 0)
return ret & True

def all(self):
"""Test whether all element not equal zero.
Expand All @@ -915,10 +915,10 @@ def all(self):
>>> v.all()
False
"""
ret = ops_mod.cmp_ne(self.entries[0], 0)
for i in range(1, len(self.entries)):
ret = ret + ops_mod.cmp_ne(self.entries[i], 0)
return -ops_mod.cmp_eq(ret, -len(self.entries))
ret = True
for entry in self.entries:
ret = ret & ops_mod.cmp_ne(entry, 0)
return ret

@taichi_scope
def fill(self, val):
Expand Down
20 changes: 10 additions & 10 deletions python/taichi/lang/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,7 @@ def cmp_lt(a, b):
Union[:class:`~taichi.lang.expr.Expr`, bool]: True if LHS is strictly smaller than RHS, False otherwise

"""
return _binary_operation(_ti_core.expr_cmp_lt, lambda a, b: -int(a < b), a,
return _binary_operation(_ti_core.expr_cmp_lt, lambda a, b: int(a < b), a,
b)


Expand All @@ -933,8 +933,8 @@ def cmp_le(a, b):
Union[:class:`~taichi.lang.expr.Expr`, bool]: True if LHS is smaller than or equal to RHS, False otherwise

"""
return _binary_operation(_ti_core.expr_cmp_le, lambda a, b: -int(a <= b),
a, b)
return _binary_operation(_ti_core.expr_cmp_le, lambda a, b: int(a <= b), a,
b)


@binary
Expand All @@ -949,7 +949,7 @@ def cmp_gt(a, b):
Union[:class:`~taichi.lang.expr.Expr`, bool]: True if LHS is strictly larger than RHS, False otherwise

"""
return _binary_operation(_ti_core.expr_cmp_gt, lambda a, b: -int(a > b), a,
return _binary_operation(_ti_core.expr_cmp_gt, lambda a, b: int(a > b), a,
b)


Expand All @@ -965,8 +965,8 @@ def cmp_ge(a, b):
bool: True if LHS is greater than or equal to RHS, False otherwise

"""
return _binary_operation(_ti_core.expr_cmp_ge, lambda a, b: -int(a >= b),
a, b)
return _binary_operation(_ti_core.expr_cmp_ge, lambda a, b: int(a >= b), a,
b)


@binary
Expand All @@ -981,8 +981,8 @@ def cmp_eq(a, b):
Union[:class:`~taichi.lang.expr.Expr`, bool]: True if LHS is equal to RHS, False otherwise.

"""
return _binary_operation(_ti_core.expr_cmp_eq, lambda a, b: -int(a == b),
a, b)
return _binary_operation(_ti_core.expr_cmp_eq, lambda a, b: int(a == b), a,
b)


@binary
Expand All @@ -997,8 +997,8 @@ def cmp_ne(a, b):
Union[:class:`~taichi.lang.expr.Expr`, bool]: True if LHS is not equal to RHS, False otherwise

"""
return _binary_operation(_ti_core.expr_cmp_ne, lambda a, b: -int(a != b),
a, b)
return _binary_operation(_ti_core.expr_cmp_ne, lambda a, b: int(a != b), a,
b)


@binary
Expand Down
6 changes: 6 additions & 0 deletions tests/python/test_compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,9 @@ def foo():
i -= 1

foo()


@test_utils.test()
def test_python_scope_compare():
v = ti.math.vec3(0, 1, 2)
assert (v < 1)[0] == 1