Skip to content

Commit

Permalink
[error] Better error message when vector used as if's condition varia…
Browse files Browse the repository at this point in the history
…ble (#1804)

* [error] Better error message when vector used as if's condition variable

* [skip ci] enforce code format

Co-authored-by: Taichi Gardener <[email protected]>
  • Loading branch information
archibate and taichi-gardener authored Aug 29, 2020
1 parent fa4b2da commit e784339
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/arithmetics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Arithmetic operators
print(2 % 3) # 2
print(-2 % 3) # 1
For C-style mod, please use ``ti.raw_mod``:
For C style mod, please use ``ti.raw_mod``:

.. code-block:: python
Expand Down
5 changes: 5 additions & 0 deletions docs/vector.rst
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,10 @@ Methods
a = ti.Vector([1.6, 2.3])
a.cast(ti.i32) # [2, 3]

See :ref:`type` for more details.

.. note::

Vectors are special matrices with only 1 column. In fact, ``ti.Vector`` is just an alias of ``ti.Matrix``.


Expand All @@ -240,4 +243,6 @@ Metadata
a = ti.Vector.field(3, dtype=ti.f32, shape=())
a.n # 3

See :ref:`meta` for more details.

TODO: add element wise operations docs
5 changes: 0 additions & 5 deletions python/taichi/lang/common_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@ def __neg__(self):
_taichi_skip_traceback = 1
return ti.neg(self)

def __abs__(self):
import taichi as ti
_taichi_skip_traceback = 1
return ti.abs(self)

def __add__(self, other):
import taichi as ti
_taichi_skip_traceback = 1
Expand Down
11 changes: 11 additions & 0 deletions python/taichi/lang/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@ def begin_frontend_struct_for(group, loop_range):
taichi_lang_core.begin_frontend_struct_for(group, loop_range.ptr)


def begin_frontend_if(cond):
if is_taichi_class(cond):
raise ValueError(
'The truth value of vectors / matrices is ambiguous.\n'
'Consider using `any` or `all` when comparing vectors:\n'
' if all(x == y):\n'
'or\n'
' if any(x != y):\n')
taichi_lang_core.begin_frontend_if(Expr(cond).ptr)


def wrap_scalar(x):
if type(x) in [int, float]:
return Expr(x)
Expand Down
2 changes: 1 addition & 1 deletion python/taichi/lang/transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ def visit_If(self, node):
template = '''
if 1:
__cond = 0
ti.core.begin_frontend_if(ti.Expr(__cond).ptr)
ti.begin_frontend_if(__cond)
ti.core.begin_frontend_if_true()
ti.core.pop_scope()
ti.core.begin_frontend_if_false()
Expand Down

0 comments on commit e784339

Please sign in to comment.