Taichi: [Lang] Comparison operators for ti.Vector

Created on 25 May 2020  路  5Comments  路  Source: taichi-dev/taichi

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!

feature request good first issue python welcome contribution

Most helpful comment

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 :)

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.

All 5 comments

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). any and all return a single boolean (or int)

Maybe we can take this chance to unify the comparison operators of Expr and those of Matrix.

https://github.com/taichi-dev/taichi/blob/1661f7a8d7216f54f260d071ea6be857566691e7/python/taichi/lang/expr.py#L77-L84

How about putting these operators to their common base class TaichiOperations? @archibate WDYT?

https://github.com/taichi-dev/taichi/blob/1661f7a8d7216f54f260d071ea6be857566691e7/python/taichi/lang/common_ops.py#L1-L4

(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-L477

If 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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

archibate picture archibate  路  4Comments

jackalcooper picture jackalcooper  路  4Comments

yuanming-hu picture yuanming-hu  路  4Comments

yuanming-hu picture yuanming-hu  路  3Comments

Xayahp picture Xayahp  路  3Comments