Concisely describe the proposed feature
Comparison operators for vectors would be very useful for programming dimensionally indpendent simulators. Combined with any and all functions would lead to very nice syntax for if statements. For example,
for I in ti.grouped(x):
if (1 <= I < N-1).all():
# do operation
could be used for looping over only the interior cells, excluding those in a band around the boundary.
Describe the solution you'd like (if any)
I think it should just require some new functions in ti.Matrix. ie. __lt__, __gt__, __le__, __ge__, __eq__, __eq__ that do the comparisons element wise and return a vector of booleans (or ints). any and all return a single boolean (or int)
Let me know if you have any ideas!
I agree that this will be very useful! Given all and any are actually Python built-in functions, maybe we can use all(1 <= I < N-1) instead of (1 <= I < N-1).all().
I think it should just require some new functions in ti.Matrix. ie.
__lt__, __gt__, __le__, __ge__, __eq__, __eq__that do the comparisons element wise and return a vector of booleans (or ints).anyandallreturn a single boolean (or int)
Maybe we can take this chance to unify the comparison operators of Expr and those of Matrix.
How about putting these operators to their common base class TaichiOperations? @archibate WDYT?
(Btw do we have a better name for TaichiOperations?)
We should return i32 for now since we don't have bool yet...
I agree, all(1 <= I < N-1) does look better. The implementation should be similar to int(), correct?
Maybe we can take this chance to unify the comparison operators of Expr and those of Matrix.
Ahh, yes. Unifying them definitely looks like the easiest way to implement this.
(Btw do we have a better name for TaichiOperations?)
Imo TaichiOperators or just Operators sounds a little better ?
How about putting these operators to their common base class TaichiOperations? @archibate WDYT?
+1! I will do this refactor.
A very similar function you could use as a base for all():
https://github.com/taichi-dev/taichi/blob/1661f7a8d7216f54f260d071ea6be857566691e7/python/taichi/lang/matrix.py#L473-L477
If someone feel interested, please go ahead :)
How about putting these operators to their common base class TaichiOperations? @archibate WDYT?
+1! I will do this refactor.
A very similar function you could use as a base for
all():
https://github.com/taichi-dev/taichi/blob/1661f7a8d7216f54f260d071ea6be857566691e7/python/taichi/lang/matrix.py#L473-L477If someone feel interested, please go ahead :)
Thank you! Note that we may also need a translation in transformer.py so that all(vector) becomes something like ti_all(vector), which we have the freedom to dispatch.
Yep! I can handle all() and any() once we pull #1054 in.
Most helpful comment
Thank you! Note that we may also need a translation in
transformer.pyso thatall(vector)becomes something liketi_all(vector), which we have the freedom to dispatch.