-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Doc] Separate arithmetics.rst from syntax.rst and fill it with more …
…details (#1761) * [Doc] Separate arithmetics.rst from syntax.rst and fill it with more details * [skip ci] missing * [skip ci] Update docs/arithmetics.rst * [skip ci] Apply suggestions from code review
- Loading branch information
Showing
5 changed files
with
175 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
Scalar operations | ||
================= | ||
|
||
Operators | ||
--------- | ||
|
||
Arithmetic operators | ||
******************** | ||
|
||
- ``-a`` | ||
- ``a + b`` | ||
- ``a - b`` | ||
- ``a * b`` | ||
- ``a / b`` | ||
- ``a // b`` | ||
- ``a % b`` | ||
- ``a ** b`` | ||
|
||
.. note:: | ||
|
||
The ``%`` operator in Taichi follows the Python style instead of C style, e.g.: | ||
|
||
.. code-block:: python | ||
# no matter Taichi-scope or Python-scope: | ||
print(2 % 3) # 2 | ||
print(-2 % 3) # 1 | ||
For C-style mod, please use ``ti.raw_mod``: | ||
|
||
.. code-block:: python | ||
print(ti.raw_mod(2, 3)) # 2 | ||
print(ti.raw_mod(-2, 3)) # -2 | ||
.. note:: | ||
|
||
Python 3 distinguishes ``/`` (true division) and ``//`` (floor division). For example, ``1.0 / 2.0 = 0.5``, | ||
``1 / 2 = 0.5``, ``1 // 2 = 0``, ``4.2 // 2 = 2``. And Taichi follows the same design: | ||
|
||
- **true divisions** on integral types will first cast their operands to the default float point type. | ||
- **floor divisions** on float-point types will first cast their operands to the default integer type. | ||
|
||
To avoid such implicit casting, you can manually cast your operands to desired types, using ``ti.cast``. | ||
See :ref:`default_precisions` for more details on default numerical types. | ||
|
||
Logic operators | ||
*************** | ||
|
||
- ``~a`` | ||
- ``a == b`` | ||
- ``a != b`` | ||
- ``a > b`` | ||
- ``a < b`` | ||
- ``a >= b`` | ||
- ``a <= b`` | ||
- ``not a`` | ||
- ``a or b`` | ||
- ``a and b`` | ||
- ``a if cond else b`` | ||
|
||
Bitwise operators | ||
***************** | ||
|
||
- ``a & b`` | ||
- ``a ^ b`` | ||
- ``a | b`` | ||
|
||
Functions | ||
--------- | ||
|
||
Trigonometric functions | ||
*********************** | ||
|
||
.. function:: ti.sin(x) | ||
.. function:: ti.cos(x) | ||
.. function:: ti.tan(x) | ||
.. function:: ti.asin(x) | ||
.. function:: ti.acos(x) | ||
.. function:: ti.atan2(x, y) | ||
.. function:: ti.tanh(x) | ||
|
||
Other arithmetic functions | ||
************************** | ||
|
||
.. function:: ti.sqrt(x) | ||
.. function:: ti.rsqrt(x) | ||
|
||
A fast version for ``1 / ti.sqrt(x)``. | ||
|
||
.. function:: ti.exp(x) | ||
.. function:: ti.log(x) | ||
.. function:: ti.floor(x) | ||
.. function:: ti.ceil(x) | ||
|
||
Casting types | ||
************* | ||
|
||
.. function:: ti.cast(x, dtype) | ||
|
||
See :ref:`type` for more details. | ||
|
||
.. function:: int(x) | ||
|
||
A shortcut for ``ti.cast(x, int)``. | ||
|
||
.. function:: float(x) | ||
|
||
A shortcut for ``ti.cast(x, float)``. | ||
|
||
Builtin-alike functions | ||
*********************** | ||
|
||
.. function:: abs(x) | ||
.. function:: max(x, y, ...) | ||
.. function:: min(x, y, ...) | ||
.. function:: pow(x, y) | ||
|
||
Same as ``x ** y``. | ||
|
||
Random number generator | ||
*********************** | ||
|
||
.. function:: ti.random(dtype = float) | ||
|
||
|
||
Element-wise arithmetics for vectors and matrices | ||
------------------------------------------------- | ||
|
||
When these scalar functions are applied on :ref:`matrix` and :ref:`vector`, they are applied in an element-wise manner. | ||
For example: | ||
|
||
.. code-block:: python | ||
B = ti.Matrix([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]) | ||
C = ti.Matrix([[3.0, 4.0, 5.0], [6.0, 7.0, 8.0]]) | ||
A = ti.sin(B) | ||
# is equivalent to | ||
for i in ti.static(range(2)): | ||
for j in ti.static(range(3)): | ||
A[i, j] = ti.sin(B[i, j]) | ||
A = B ** 2 | ||
# is equivalent to | ||
for i in ti.static(range(2)): | ||
for j in ti.static(range(3)): | ||
A[i, j] = B[i, j] ** 2 | ||
A = B ** C | ||
# is equivalent to | ||
for i in ti.static(range(2)): | ||
for j in ti.static(range(3)): | ||
A[i, j] = B[i, j] ** C[i, j] | ||
A += 2 | ||
# is equivalent to | ||
for i in ti.static(range(2)): | ||
for j in ti.static(range(3)): | ||
A[i, j] += 2 | ||
A += B | ||
# is equivalent to | ||
for i in ti.static(range(2)): | ||
for j in ti.static(range(3)): | ||
A[i, j] += B[i, j] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
.. _type: | ||
|
||
Type system | ||
=========== | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters