Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Doc] Update math module #6203

Merged
merged 20 commits into from
Sep 30, 2022
Merged
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@ sidebar_position: 1
Taichi provides a built-in `math` module that supports frequently used mathematical functions and utility functions, including:

+ Commonly-used mathematical functions that are analogous to those in Python's built-in `math` module.
+ Small vector and matrix types that are analogous to those in the [OpenGL shading language](https://www.khronos.org/opengl/wiki/OpenGL_Shading_Language)(GLSL).
+ Small vector and matrix types that are analogous to those in the [OpenGL shading language](https://www.khronos.org/opengl/wiki/OpenGL_Shading_Language) (GLSL).
+ Some GLSL-standard functions.
+ Complex number operations of 2D vectors.



## Mathematical functions

Taichi's `math` module has a large overlap with Python's built-in `math` module. You need to call the functions in the Taichi scope. For example:
You should call the mathematical functions provided by Taichi's `math` module from within the Taichi scope. For example:
Olinaaaloompa marked this conversation as resolved.
Show resolved Hide resolved

```python
import taichi as ti
Expand All @@ -33,7 +31,7 @@ def test():
...
```

These functions also accept vectors and matrices as arguments, and they apply to them element-wise:
These functions also accept vectors and matrices as arguments and conduct element-wise operations:
Olinaaaloompa marked this conversation as resolved.
Show resolved Hide resolved

```python
@ti.kernel
Expand All @@ -49,34 +47,33 @@ def test():

:::note

Difference between Taichi's math module and Python's built-in math module:
Taichi's math module has a large overlap with Python's built-in math module. But you should follow a few extra rules when using Taichi's math module:
Olinaaaloompa marked this conversation as resolved.
Show resolved Hide resolved

- Functions in Taichi's math module must be called from within the Taichi scope.
- Functions in Taichi's math module also accept vectors/matrices as arguments.
- The precision of a function in Taichi's math module depends on the value of `default_fp` and backend specified in the `ti.init()` method call.
+ Functions in Taichi's math module *must* be called from within the Taichi scope.
Olinaaaloompa marked this conversation as resolved.
Show resolved Hide resolved
+ Functions in Taichi's math module also accept vectors or matrices as arguments.
Olinaaaloompa marked this conversation as resolved.
Show resolved Hide resolved
+ The precision of a function in Taichi's math module depends on the value of `default_fp` and the backend specified in the `ti.init()` method call.
Olinaaaloompa marked this conversation as resolved.
Show resolved Hide resolved

:::

## Small vector and matrix types

Taichi's math module supplies a few small vector and matrix types:
Taichi's math module provides a few small vector and matrix types:


+ `vec2/vec3/vec4` for 2D/3D/4D floating-point vector types.
+ `ivec2/ivec3/ivec4` for 2D/3D/4D integer vector types.
+ `uvec2/uvec3/uvec4` for 2D/3D/4D unsigned integer vector types.
+ `mat2/mat3/mat4` for 2D/3D/4D floating-point square matrix types.


These vector/matrix types are created with the two template functions `ti.types.vector()` and `ti.types.matrix()`. For example, `vec2` is defined in the following way:
To create one of the vector/matrix types above, use the template `ti.types.vector()` or `ti.types.matrix()`. For example, `vec2` is defined in the following way:
Olinaaaloompa marked this conversation as resolved.
Show resolved Hide resolved

```python
vec2 = ti.types.vector(2, float)
```

The number of precision bits of such a type is determined by `default_fp` or `default_ip` in the `ti.init()` method call. For example, if `ti.init(default_fp=ti.f64)` is called, then `vec2/vec3/vec4` and `mat2/mat3/mat4` will all have 64-bit floating-point precision in the Taichi scope.
The number of precision bits of such a type is determined by `default_fp` or `default_ip` in the `ti.init()` method call. For example, if `ti.init(default_fp=ti.f64)` is called, then `vec2/vec3/vec4` and `mat2/mat3/mat4` defined in the Taichi scope all have 64-bit floating-point precision.
Olinaaaloompa marked this conversation as resolved.
Show resolved Hide resolved

These types can be used to instantiate vectors and matrices or annotate the data types of function arguments and struct members. See [type system](../type_system/type.md) for more information. Here we emphasize that they have very flexible initialization routines:
You can use these types to instantiate vectors/matrices or annotate the data types of function arguments and struct members. See the [Type System](../type_system/type.md) for more information. Here we emphasize that they have very flexible initialization routines:
Olinaaaloompa marked this conversation as resolved.
Show resolved Hide resolved

```python
mat2 = ti.math.mat2
Expand Down Expand Up @@ -104,17 +101,18 @@ u = v.wzyx # vec4(4, 3, 2, 1)
u = v.rraa # vec4(1, 1, 2, 2)
```

### Relationship between `ti.Vector`, `ti.types.vector` and `ti.math.vec3`
### Relations between `ti.Vector`, `ti.types.vector` and `ti.math.vec3`

+ `ti.Vector` is a function that accepts a 1D array and returns a matrix instance that has only one column. For example, `ti.Vector([1, 2, 3, 4, 5])`.
+ `ti.types.vector` is a function that accepts an integer and a primitive type, and returns a vector type. For example: `vec5f = ti.types.vector(5, float)`. `vec5f` can then be used to instantiate 5D vectors or annotate data types in function arguments and struct members:
+ `ti.types.vector` is a function that accepts an integer and a primitive type and returns a vector type. For example: `vec5f = ti.types.vector(5, float)`. `vec5f` can then be used to instantiate 5D vectors or annotate data types of function arguments and struct members:
Olinaaaloompa marked this conversation as resolved.
Show resolved Hide resolved

```python
@ti.kernel
def test(v: vec5f):
print(v.xyz)
```
Unlike `ti.Vector`, whose input data must be a 1D array, vector types created by `ti.types.vector()` have more flexible ways to initialize, as explained above.
+ `ti.math.vec3` is simply created by `vec3 = ti.types.vector(3, float)`.
+ `ti.math.vec3` is created by `vec3 = ti.types.vector(3, float)`.


## GLSL-standard functions
Expand Down Expand Up @@ -143,9 +141,9 @@ Texture support in Taichi is implemented in the `ti.types.texture_types` module.

## Complex number operations

Taichi's math module also supports basic complex arithmetic in the form of 2D vectors.
Taichi's math module also supports basic complex number operations via 2D vectors.
Olinaaaloompa marked this conversation as resolved.
Show resolved Hide resolved

You can use a 2D vector of type `ti.math.vec2` to represent a complex number, the addtion/subtraction of complex numbers are just addtion/subtraction of 2D vectors, and multiplication and division can be performed by calling `ti.math.cmul` and `ti.math.cdiv`:
You can use a 2D vector of type `ti.math.vec2` to represent a complex number. In this way, the addition and subtraction of complex numbers are equivalent to the addtion and subtraction of 2D vectors, and the multiplication and division of complex numbers can be performed by calling `ti.math.cmul` and `ti.math.cdiv`:
Olinaaaloompa marked this conversation as resolved.
Show resolved Hide resolved

```python
import taichi as ti
Expand All @@ -160,7 +158,7 @@ def test():
w = tm.cdiv(x, y) # vec2(2, 0) = 2+0j
```

You can also compute the power, logarithm and exponetial of a complex number:
You can also compute the power, logarithm and exponential of a complex number:
Olinaaaloompa marked this conversation as resolved.
Show resolved Hide resolved

```python

Expand Down