From ffd17b5ebcb8dcd603d85b905a87e6532c9469d2 Mon Sep 17 00:00:00 2001 From: Yi Xu Date: Tue, 13 Sep 2022 15:26:15 +0800 Subject: [PATCH 1/5] [Doc] Update docstring of pow() --- python/taichi/lang/ops.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/python/taichi/lang/ops.py b/python/taichi/lang/ops.py index 25564bad52f6b..cd860c8949669 100644 --- a/python/taichi/lang/ops.py +++ b/python/taichi/lang/ops.py @@ -732,10 +732,18 @@ def expr_python_mod(a, b): def pow(x, a): # pylint: disable=W0622 """First array elements raised to powers from second array :math:`x^a`, element-wise. - Negative values raised to a non-integral value will return `nan`. - A zero value raised to a negative value will return `inf`. - If debug mode or optimization passes are on, an exception will be raised - when an integral value is raised to a negative value; otherwise 1 will be returned. + The result type of two scalar operands is determined as follows: + - If the exponent is an integral value, then the result type takes the type of the base. + - Otherwise, the result type follows + [Implicit type casting in binary operations](https://docs.taichi-lang.org/docs/type#implicit-type-casting-in-binary-operations). + + With the above rules, an integral value raised to a negative integral value cannot have a + feasible type. Therefore, an exception will be raised if debug mode or optimization passes + are on; otherwise 1 will be returned. + + In the following situations, the result is undefined: + - A negative value raised to a non-integral value. + - A zero value raised to a non-positive value. Args: x (Union[:mod:`~taichi.types.primitive_types`, :class:`~taichi.Matrix`]): \ @@ -744,8 +752,8 @@ def pow(x, a): # pylint: disable=W0622 The exponents. Returns: - The bases in `x1` raised to the exponents in `x2`. This is a scalar if both \ - `x1` and `x2` are scalars. + The bases in `x` raised to the exponents in `a`. This is a scalar if both \ + `x` and `a` are scalars. Example:: From ecfc126ea137bee51a0b2091ff57447374e47fca Mon Sep 17 00:00:00 2001 From: Yi Xu Date: Tue, 13 Sep 2022 15:43:16 +0800 Subject: [PATCH 2/5] Fix example --- python/taichi/lang/ops.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/python/taichi/lang/ops.py b/python/taichi/lang/ops.py index cd860c8949669..8c7e044293388 100644 --- a/python/taichi/lang/ops.py +++ b/python/taichi/lang/ops.py @@ -759,13 +759,13 @@ def pow(x, a): # pylint: disable=W0622 >>> @ti.kernel >>> def test(): - >>> x = ti.Matrix([-2.0, 0.0, 2.0]) - >>> y = -2.2 + >>> x = ti.Matrix([-2.0, 2.0]) + >>> y = -3 >>> z = ti.pow(x, y) >>> print(z) >>> >>> test() - [-nan, inf, 0.217638] + [-0.125000, 0.125000] """ return _binary_operation(_ti_core.expr_pow, _bt_ops_mod.pow, x, a) From c2945ae8b94735dbf71256899c3ff90d714a17d1 Mon Sep 17 00:00:00 2001 From: Yi Xu Date: Tue, 13 Sep 2022 16:58:51 +0800 Subject: [PATCH 3/5] Rename x and a --- python/taichi/lang/ops.py | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/python/taichi/lang/ops.py b/python/taichi/lang/ops.py index 8c7e044293388..4977029f6e9d0 100644 --- a/python/taichi/lang/ops.py +++ b/python/taichi/lang/ops.py @@ -729,8 +729,8 @@ def expr_python_mod(a, b): @binary -def pow(x, a): # pylint: disable=W0622 - """First array elements raised to powers from second array :math:`x^a`, element-wise. +def pow(base, exp): # pylint: disable=W0622 + """First array elements raised to second array elements :math:`{base}^{exp}`, element-wise. The result type of two scalar operands is determined as follows: - If the exponent is an integral value, then the result type takes the type of the base. @@ -746,14 +746,13 @@ def pow(x, a): # pylint: disable=W0622 - A zero value raised to a non-positive value. Args: - x (Union[:mod:`~taichi.types.primitive_types`, :class:`~taichi.Matrix`]): \ + base (Union[:mod:`~taichi.types.primitive_types`, :class:`~taichi.Matrix`]): \ The bases. - a (Union[:mod:`~taichi.types.primitive_types`, :class:`~taichi.Matrix`]): \ + exp (Union[:mod:`~taichi.types.primitive_types`, :class:`~taichi.Matrix`]): \ The exponents. Returns: - The bases in `x` raised to the exponents in `a`. This is a scalar if both \ - `x` and `a` are scalars. + `base` raised to `exp`. This is a scalar if both `base` and `exp` are scalars. Example:: @@ -767,7 +766,7 @@ def pow(x, a): # pylint: disable=W0622 >>> test() [-0.125000, 0.125000] """ - return _binary_operation(_ti_core.expr_pow, _bt_ops_mod.pow, x, a) + return _binary_operation(_ti_core.expr_pow, _bt_ops_mod.pow, base, exp) @binary From d7ddc89e90d9dbca410385a4cd4b102b953a560c Mon Sep 17 00:00:00 2001 From: Yi Xu Date: Tue, 13 Sep 2022 17:51:42 +0800 Subject: [PATCH 4/5] Rename --- python/taichi/lang/ops.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/python/taichi/lang/ops.py b/python/taichi/lang/ops.py index 4977029f6e9d0..da36d7a1295cb 100644 --- a/python/taichi/lang/ops.py +++ b/python/taichi/lang/ops.py @@ -729,8 +729,8 @@ def expr_python_mod(a, b): @binary -def pow(base, exp): # pylint: disable=W0622 - """First array elements raised to second array elements :math:`{base}^{exp}`, element-wise. +def pow(base, exponent): # pylint: disable=W0622 + """First array elements raised to second array elements :math:`{base}^{exponent}`, element-wise. The result type of two scalar operands is determined as follows: - If the exponent is an integral value, then the result type takes the type of the base. @@ -748,11 +748,11 @@ def pow(base, exp): # pylint: disable=W0622 Args: base (Union[:mod:`~taichi.types.primitive_types`, :class:`~taichi.Matrix`]): \ The bases. - exp (Union[:mod:`~taichi.types.primitive_types`, :class:`~taichi.Matrix`]): \ + exponent (Union[:mod:`~taichi.types.primitive_types`, :class:`~taichi.Matrix`]): \ The exponents. Returns: - `base` raised to `exp`. This is a scalar if both `base` and `exp` are scalars. + `base` raised to `exponent`. This is a scalar if both `base` and `exponent` are scalars. Example:: @@ -766,7 +766,7 @@ def pow(base, exp): # pylint: disable=W0622 >>> test() [-0.125000, 0.125000] """ - return _binary_operation(_ti_core.expr_pow, _bt_ops_mod.pow, base, exp) + return _binary_operation(_ti_core.expr_pow, _bt_ops_mod.pow, base, exponent) @binary From 5de38551ec72caafaf20f498467903103e47dd4a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 13 Sep 2022 09:53:37 +0000 Subject: [PATCH 5/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- python/taichi/lang/ops.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/python/taichi/lang/ops.py b/python/taichi/lang/ops.py index da36d7a1295cb..ecb4b1310003d 100644 --- a/python/taichi/lang/ops.py +++ b/python/taichi/lang/ops.py @@ -766,7 +766,8 @@ def pow(base, exponent): # pylint: disable=W0622 >>> test() [-0.125000, 0.125000] """ - return _binary_operation(_ti_core.expr_pow, _bt_ops_mod.pow, base, exponent) + return _binary_operation(_ti_core.expr_pow, _bt_ops_mod.pow, base, + exponent) @binary