diff --git a/docs/arithmetics.rst b/docs/arithmetics.rst index cfdd31985eadf..fffe956f3ec17 100644 --- a/docs/arithmetics.rst +++ b/docs/arithmetics.rst @@ -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 diff --git a/docs/vector.rst b/docs/vector.rst index 17ddb844f8daf..d4881b98ac285 100644 --- a/docs/vector.rst +++ b/docs/vector.rst @@ -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``. @@ -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 diff --git a/python/taichi/lang/common_ops.py b/python/taichi/lang/common_ops.py index f5e5eb066630b..909eec1d5af75 100644 --- a/python/taichi/lang/common_ops.py +++ b/python/taichi/lang/common_ops.py @@ -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 diff --git a/python/taichi/lang/impl.py b/python/taichi/lang/impl.py index 9ffe22702526a..e3d0a034976f8 100644 --- a/python/taichi/lang/impl.py +++ b/python/taichi/lang/impl.py @@ -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) diff --git a/python/taichi/lang/transformer.py b/python/taichi/lang/transformer.py index 8db97e0f0eb7f..12b545bbc9656 100644 --- a/python/taichi/lang/transformer.py +++ b/python/taichi/lang/transformer.py @@ -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()