From 5e216c135d087ed41e981ca47104a5a52a7b63fe Mon Sep 17 00:00:00 2001 From: Gabriel Vainer <67750020+vai9er@users.noreply.github.com> Date: Sun, 11 Dec 2022 17:31:46 -0500 Subject: [PATCH 01/12] [doc] field.md --- docs/lang/articles/basic/field.md | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/docs/lang/articles/basic/field.md b/docs/lang/articles/basic/field.md index ae437ca03788e..ca6e45be5483c 100644 --- a/docs/lang/articles/basic/field.md +++ b/docs/lang/articles/basic/field.md @@ -9,15 +9,11 @@ The term _field_ is borrowed from mathematics and physics. If you already know [ Fields in Taichi are the _global_ data containers, which can be accessed from both the Python scope and the Taichi scope. Just like an ndarray in NumPy or a tensor in PyTorch, a field in Taichi is defined as a multi-dimensional array of elements, and elements in a field can be a scalar, a vector, a matrix, or a struct. -:::note -A 0D (zero-dimensional) field contains *only one* element. -::: - ## Scalar fields Scalar fields refer to the fields that store scalars and are the most basic fields. -- A 0D scalar field is a single scalar. +- A 0D scalar field is a single scalar. - A 1D scalar field is a 1D array of scalars. - A 2D scalar field is a 2D array of scalars, and so on. From 2a2b3e5d00ff531a48b14b0272e614506819d18f Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 11 Dec 2022 22:33:07 +0000 Subject: [PATCH 02/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- docs/lang/articles/basic/field.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/lang/articles/basic/field.md b/docs/lang/articles/basic/field.md index ca6e45be5483c..f93c8ba5075e3 100644 --- a/docs/lang/articles/basic/field.md +++ b/docs/lang/articles/basic/field.md @@ -13,7 +13,7 @@ Fields in Taichi are the _global_ data containers, which can be accessed from bo Scalar fields refer to the fields that store scalars and are the most basic fields. -- A 0D scalar field is a single scalar. +- A 0D scalar field is a single scalar. - A 1D scalar field is a 1D array of scalars. - A 2D scalar field is a 2D array of scalars, and so on. From 16b8711908ddfab8c199792d19535654887de326 Mon Sep 17 00:00:00 2001 From: Gabriel Vainer <67750020+vai9er@users.noreply.github.com> Date: Sun, 11 Dec 2022 22:51:01 +0000 Subject: [PATCH 03/12] Cleanup --- docs/lang/articles/basic/field.md | 51 +++++++++++++++---------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/docs/lang/articles/basic/field.md b/docs/lang/articles/basic/field.md index ca6e45be5483c..e08fc1a7b8f65 100644 --- a/docs/lang/articles/basic/field.md +++ b/docs/lang/articles/basic/field.md @@ -7,9 +7,9 @@ sidebar_position: 1 The term _field_ is borrowed from mathematics and physics. If you already know [scalar field](https://en.wikipedia.org/wiki/Scalar_field) (for example heat field), or vector field (for example [gravitational field](https://en.wikipedia.org/wiki/Gravitational_field)), then it is easy for you to understand fields in Taichi. -Fields in Taichi are the _global_ data containers, which can be accessed from both the Python scope and the Taichi scope. Just like an ndarray in NumPy or a tensor in PyTorch, a field in Taichi is defined as a multi-dimensional array of elements, and elements in a field can be a scalar, a vector, a matrix, or a struct. +Fields in Taichi are the _global_ data containers, which can be accessed from both the Python scope and the Taichi scope. Just like an ndarray in NumPy or a tensor in PyTorch, a field in Taichi is defined as a multi-dimensional array of elements, and elements in a field can be a Scalar, a Vector, a Matrix, or a Struct. -## Scalar fields +## Scalar Fields Scalar fields refer to the fields that store scalars and are the most basic fields. @@ -44,7 +44,7 @@ The simplest way to declare a scalar field is to call `ti.field(dtype, shape)`, ```python f_1d = ti.field(ti.i32, shape=9) # A 1D field of length 9 ``` - + Over here we are defining a An illustration of `f_1d`: ``` @@ -58,7 +58,7 @@ The simplest way to declare a scalar field is to call `ti.field(dtype, shape)`, There is little difference between a 0D field and a 1D field of length 1 except for their indexing rules. You *must* use `None` as the index to access a 0D field and `0` as the index to access a 1D field of length 1: ```python - # f1 and f2 are basically interchangeable + f1 = ti.field(int, shape=()) f2 = ti.field(int, shape=1) @@ -66,13 +66,13 @@ The simplest way to declare a scalar field is to call `ti.field(dtype, shape)`, f2[0] = 1 # Use 0 to access a 1D field of length 1 ``` -- When declaring a 2D scalar field, you need to set its two dimensions (numbers of rows and columns) respectively. For example, the following code snippet defines a 2D scalar field in the shape (3, 6) (three rows and six columns): +- When declaring a 2D scalar field, you need to set its two dimensions (numbers of rows and columns) respectively. For example, the following code snippet defines a 2D scalar field with the shape (3, 6) (three rows and six columns): ```python f_2d = ti.field(int, shape=(3, 6)) # A 2D field in the shape (3, 6) ``` - An illustration of `f_2d`: + Here is an illustration of `f_2d`: ``` f_2d.shape[1] @@ -88,7 +88,7 @@ The simplest way to declare a scalar field is to call `ti.field(dtype, shape)`, └ └───┴───┴───┴───┴───┴───┘ ┘ ``` -Scalar fields of higher dimensions can be similarily defined. +Scalar Fields of higher dimensions can be similarily defined. :::caution WARNING @@ -97,9 +97,9 @@ Taichi only supports fields of dimensions ≤ 8. ::: -### Access elements in a scalar field +### Accessing Elements in a Scalar Field -Once a field is declared, Taichi automatically initializes its elements with the value zero. +Once a field is declared, Taichi automatically initializes its elements to zero. To access an element in a scalar field, you need to explicitly specify the element's index. @@ -123,7 +123,7 @@ When accessing a 0D field `x`, use `x[None] = 0`, *not* `x = 0`. f_0d.shape=() ``` -- To access an element in a 1D field, use index `i`, which is an integer in the range `[0, f_1d.shape[0])`: +- To access an element in a 1D field, use index `i` to get the ith element of our defined field. ```python for i in range(9): @@ -138,10 +138,7 @@ When accessing a 0D field `x`, use `x[None] = 0`, *not* `x = 0`. └───┴───┴───┴───┴───┴───┴───┴───┴───┘ ``` -- To access an element in a 2D field, use index `(i, j)`, which is an integer pair. - - - `i` in the range `[0, f_2d.shape[0])` - - `j` in the range `[0, f_2d.shape[1])`: +- To access an element in a 2D field, use index `(i, j)`, which is an integer pair to get the ith jth element of our defined field. ```python for i, j in f_2d: @@ -188,7 +185,7 @@ while gui.running: :::caution WARNING -Taichi fields do not support slicing. Neither of the following usage is correct: +Taichi fields do not support slicing. Neither of the following usages are correct: ```python for x in f_2d[0]: # Error! You tried to access its first row,but it is not supported @@ -202,7 +199,7 @@ f_2d[0][3:] = [4, 5, 6] # Error! You tried to access a slice of the first row, ::: -### Fill a scalar field with a given value +### Fill a Scalar Field with a Given Value To set all elements in a scalar field to a given value, call `field.fill()`: @@ -224,7 +221,7 @@ f_1d.shape # (9,) f_3d.dtype # f32 ``` -## Vector fields +## Vector Fields As the name suggests, vector fields are the fields whose elements are vectors. What a vector represents depends on the scenario of your program. For example, a vector may stand for the (R, G, B) triple of a pixel, the position of a particle, or the gravitational field in space. @@ -259,11 +256,12 @@ The following code snippet declares a `300x300x300` vector field `volumetric_fie ```python box_size = (300, 300, 300) # A 300x300x300 grid in a 3D space + # Declares a 300x300x300 vector field, whose vector dimension is n=3 volumetric_field = ti.Vector.field(n=3, dtype=ti.f32, shape=box_size) ``` -### Access elements in a vector field +### Accessing Elements in a Vector Field Accessing a vector field is similar to accessing a multi-dimensional array: You use an index operator `[]` to access an element in the field. The only difference is that, to access a specific component of an element (vector in this case), you need an *extra* index operator `[]`: @@ -301,6 +299,7 @@ vec_field = ti.Vector.field(n, dtype=float, shape=(w,h)) def fill_vector(): for i,j in vec_field: for k in ti.static(range(n)): + #ti.static unrolls the inner loops vec_field[i,j][k] = ti.random() @@ -314,9 +313,9 @@ To access the `p`-th component of the 0D vector field `x = ti.Vector.field(n=3, `x[None][p]` (0 ≤ p < n). ::: -## Matrix fields +## Matrix Fields -As the name suggests, matrix fields are the fields whose elements are matrices. In continuum mechanics, at each infinitesimal point in a 3D material exists a strain and stress tensor. The strain and stress tensor is a 3 x 2 matrix. Then, you can use a matrix field to represent such a tensor field. +As the name suggests, matrix fields are the fields whose elements are matrices. In continuum mechanics, at each infinitesimal point in a 3D material exists a `strain` and `stress` tensor. The strain and stress tensor is a 3 x 2 matrix. We can use a Matrix Field to represent this tensor. ### Declaration @@ -327,11 +326,11 @@ The following code snippet declares a tensor field: tensor_field = ti.Matrix.field(n=3, m=2, dtype=ti.f32, shape=(300, 400, 500)) ``` -### Access elements in a matrix field +### Accessing Elements in a Matrix Field Accessing a matrix field is similar to accessing a vector field: You use an index operator `[]` for field indexing and a second index operator `[]` for matrix indexing. -- To access the `i, j` element of the matrix field `tensor_field`: +- To access the `ith, jth` element of the matrix field `tensor_field`: `mat = tensor_field[i, j]` @@ -368,21 +367,21 @@ def test(): # a[i][1, 2] = 1 ``` -Operating on larger matrices (for example `32x128`) can lead to longer compilation time and poorer performance. For performance reasons, it is recommended that you keep your matrices small: +Operating on larger matrices (for example `32x128`) can lead to longer compilation time and poor performance. For performance reasons, it is recommended that you keep your matrices small: - `2x1`, `3x3`, and `4x4` matrices work fine. - `32x6` is a bit too large. **Workaround:** -When declaring a matrix field, leave large dimensions to the fields, rather than to the matrices. If you have a `3x2` field of `64x32` matrices: +When declaring a matrix field, leave large dimensions to the Fields, rather than to the matrices. If you have a `3x2` field of `64x32` matrices: - Not recommended: `ti.Matrix.field(64, 32, dtype=ti.f32, shape=(3, 2))` - Recommended: `ti.Matrix.field(3, 2, dtype=ti.f32, shape=(64, 32))` -## Struct fields +## Struct Fields Struct fields are fields that store user-defined structs. Members of a struct element can be: @@ -421,7 +420,7 @@ particle = ti.types.struct( particle_field = particle.field(shape=(n,)) ``` -### Access elements in a struct field +### Access Elements in a Struct Field You can access a member of an element in a struct field in either of the following ways: index-first or name-first. From 1c52665257793c3bdcf8b35e362de984d8af2e81 Mon Sep 17 00:00:00 2001 From: Gabriel Vainer <67750020+vai9er@users.noreply.github.com> Date: Sun, 11 Dec 2022 17:31:46 -0500 Subject: [PATCH 04/12] [doc] field.md --- docs/lang/articles/basic/field.md | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/docs/lang/articles/basic/field.md b/docs/lang/articles/basic/field.md index ae437ca03788e..ca6e45be5483c 100644 --- a/docs/lang/articles/basic/field.md +++ b/docs/lang/articles/basic/field.md @@ -9,15 +9,11 @@ The term _field_ is borrowed from mathematics and physics. If you already know [ Fields in Taichi are the _global_ data containers, which can be accessed from both the Python scope and the Taichi scope. Just like an ndarray in NumPy or a tensor in PyTorch, a field in Taichi is defined as a multi-dimensional array of elements, and elements in a field can be a scalar, a vector, a matrix, or a struct. -:::note -A 0D (zero-dimensional) field contains *only one* element. -::: - ## Scalar fields Scalar fields refer to the fields that store scalars and are the most basic fields. -- A 0D scalar field is a single scalar. +- A 0D scalar field is a single scalar. - A 1D scalar field is a 1D array of scalars. - A 2D scalar field is a 2D array of scalars, and so on. From 8dbd80f83a8bd8c1a29f21eba466b2709cda77db Mon Sep 17 00:00:00 2001 From: Gabriel Vainer <67750020+vai9er@users.noreply.github.com> Date: Sun, 11 Dec 2022 22:51:01 +0000 Subject: [PATCH 05/12] Cleanup --- docs/lang/articles/basic/field.md | 51 +++++++++++++++---------------- 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/docs/lang/articles/basic/field.md b/docs/lang/articles/basic/field.md index ca6e45be5483c..e08fc1a7b8f65 100644 --- a/docs/lang/articles/basic/field.md +++ b/docs/lang/articles/basic/field.md @@ -7,9 +7,9 @@ sidebar_position: 1 The term _field_ is borrowed from mathematics and physics. If you already know [scalar field](https://en.wikipedia.org/wiki/Scalar_field) (for example heat field), or vector field (for example [gravitational field](https://en.wikipedia.org/wiki/Gravitational_field)), then it is easy for you to understand fields in Taichi. -Fields in Taichi are the _global_ data containers, which can be accessed from both the Python scope and the Taichi scope. Just like an ndarray in NumPy or a tensor in PyTorch, a field in Taichi is defined as a multi-dimensional array of elements, and elements in a field can be a scalar, a vector, a matrix, or a struct. +Fields in Taichi are the _global_ data containers, which can be accessed from both the Python scope and the Taichi scope. Just like an ndarray in NumPy or a tensor in PyTorch, a field in Taichi is defined as a multi-dimensional array of elements, and elements in a field can be a Scalar, a Vector, a Matrix, or a Struct. -## Scalar fields +## Scalar Fields Scalar fields refer to the fields that store scalars and are the most basic fields. @@ -44,7 +44,7 @@ The simplest way to declare a scalar field is to call `ti.field(dtype, shape)`, ```python f_1d = ti.field(ti.i32, shape=9) # A 1D field of length 9 ``` - + Over here we are defining a An illustration of `f_1d`: ``` @@ -58,7 +58,7 @@ The simplest way to declare a scalar field is to call `ti.field(dtype, shape)`, There is little difference between a 0D field and a 1D field of length 1 except for their indexing rules. You *must* use `None` as the index to access a 0D field and `0` as the index to access a 1D field of length 1: ```python - # f1 and f2 are basically interchangeable + f1 = ti.field(int, shape=()) f2 = ti.field(int, shape=1) @@ -66,13 +66,13 @@ The simplest way to declare a scalar field is to call `ti.field(dtype, shape)`, f2[0] = 1 # Use 0 to access a 1D field of length 1 ``` -- When declaring a 2D scalar field, you need to set its two dimensions (numbers of rows and columns) respectively. For example, the following code snippet defines a 2D scalar field in the shape (3, 6) (three rows and six columns): +- When declaring a 2D scalar field, you need to set its two dimensions (numbers of rows and columns) respectively. For example, the following code snippet defines a 2D scalar field with the shape (3, 6) (three rows and six columns): ```python f_2d = ti.field(int, shape=(3, 6)) # A 2D field in the shape (3, 6) ``` - An illustration of `f_2d`: + Here is an illustration of `f_2d`: ``` f_2d.shape[1] @@ -88,7 +88,7 @@ The simplest way to declare a scalar field is to call `ti.field(dtype, shape)`, └ └───┴───┴───┴───┴───┴───┘ ┘ ``` -Scalar fields of higher dimensions can be similarily defined. +Scalar Fields of higher dimensions can be similarily defined. :::caution WARNING @@ -97,9 +97,9 @@ Taichi only supports fields of dimensions ≤ 8. ::: -### Access elements in a scalar field +### Accessing Elements in a Scalar Field -Once a field is declared, Taichi automatically initializes its elements with the value zero. +Once a field is declared, Taichi automatically initializes its elements to zero. To access an element in a scalar field, you need to explicitly specify the element's index. @@ -123,7 +123,7 @@ When accessing a 0D field `x`, use `x[None] = 0`, *not* `x = 0`. f_0d.shape=() ``` -- To access an element in a 1D field, use index `i`, which is an integer in the range `[0, f_1d.shape[0])`: +- To access an element in a 1D field, use index `i` to get the ith element of our defined field. ```python for i in range(9): @@ -138,10 +138,7 @@ When accessing a 0D field `x`, use `x[None] = 0`, *not* `x = 0`. └───┴───┴───┴───┴───┴───┴───┴───┴───┘ ``` -- To access an element in a 2D field, use index `(i, j)`, which is an integer pair. - - - `i` in the range `[0, f_2d.shape[0])` - - `j` in the range `[0, f_2d.shape[1])`: +- To access an element in a 2D field, use index `(i, j)`, which is an integer pair to get the ith jth element of our defined field. ```python for i, j in f_2d: @@ -188,7 +185,7 @@ while gui.running: :::caution WARNING -Taichi fields do not support slicing. Neither of the following usage is correct: +Taichi fields do not support slicing. Neither of the following usages are correct: ```python for x in f_2d[0]: # Error! You tried to access its first row,but it is not supported @@ -202,7 +199,7 @@ f_2d[0][3:] = [4, 5, 6] # Error! You tried to access a slice of the first row, ::: -### Fill a scalar field with a given value +### Fill a Scalar Field with a Given Value To set all elements in a scalar field to a given value, call `field.fill()`: @@ -224,7 +221,7 @@ f_1d.shape # (9,) f_3d.dtype # f32 ``` -## Vector fields +## Vector Fields As the name suggests, vector fields are the fields whose elements are vectors. What a vector represents depends on the scenario of your program. For example, a vector may stand for the (R, G, B) triple of a pixel, the position of a particle, or the gravitational field in space. @@ -259,11 +256,12 @@ The following code snippet declares a `300x300x300` vector field `volumetric_fie ```python box_size = (300, 300, 300) # A 300x300x300 grid in a 3D space + # Declares a 300x300x300 vector field, whose vector dimension is n=3 volumetric_field = ti.Vector.field(n=3, dtype=ti.f32, shape=box_size) ``` -### Access elements in a vector field +### Accessing Elements in a Vector Field Accessing a vector field is similar to accessing a multi-dimensional array: You use an index operator `[]` to access an element in the field. The only difference is that, to access a specific component of an element (vector in this case), you need an *extra* index operator `[]`: @@ -301,6 +299,7 @@ vec_field = ti.Vector.field(n, dtype=float, shape=(w,h)) def fill_vector(): for i,j in vec_field: for k in ti.static(range(n)): + #ti.static unrolls the inner loops vec_field[i,j][k] = ti.random() @@ -314,9 +313,9 @@ To access the `p`-th component of the 0D vector field `x = ti.Vector.field(n=3, `x[None][p]` (0 ≤ p < n). ::: -## Matrix fields +## Matrix Fields -As the name suggests, matrix fields are the fields whose elements are matrices. In continuum mechanics, at each infinitesimal point in a 3D material exists a strain and stress tensor. The strain and stress tensor is a 3 x 2 matrix. Then, you can use a matrix field to represent such a tensor field. +As the name suggests, matrix fields are the fields whose elements are matrices. In continuum mechanics, at each infinitesimal point in a 3D material exists a `strain` and `stress` tensor. The strain and stress tensor is a 3 x 2 matrix. We can use a Matrix Field to represent this tensor. ### Declaration @@ -327,11 +326,11 @@ The following code snippet declares a tensor field: tensor_field = ti.Matrix.field(n=3, m=2, dtype=ti.f32, shape=(300, 400, 500)) ``` -### Access elements in a matrix field +### Accessing Elements in a Matrix Field Accessing a matrix field is similar to accessing a vector field: You use an index operator `[]` for field indexing and a second index operator `[]` for matrix indexing. -- To access the `i, j` element of the matrix field `tensor_field`: +- To access the `ith, jth` element of the matrix field `tensor_field`: `mat = tensor_field[i, j]` @@ -368,21 +367,21 @@ def test(): # a[i][1, 2] = 1 ``` -Operating on larger matrices (for example `32x128`) can lead to longer compilation time and poorer performance. For performance reasons, it is recommended that you keep your matrices small: +Operating on larger matrices (for example `32x128`) can lead to longer compilation time and poor performance. For performance reasons, it is recommended that you keep your matrices small: - `2x1`, `3x3`, and `4x4` matrices work fine. - `32x6` is a bit too large. **Workaround:** -When declaring a matrix field, leave large dimensions to the fields, rather than to the matrices. If you have a `3x2` field of `64x32` matrices: +When declaring a matrix field, leave large dimensions to the Fields, rather than to the matrices. If you have a `3x2` field of `64x32` matrices: - Not recommended: `ti.Matrix.field(64, 32, dtype=ti.f32, shape=(3, 2))` - Recommended: `ti.Matrix.field(3, 2, dtype=ti.f32, shape=(64, 32))` -## Struct fields +## Struct Fields Struct fields are fields that store user-defined structs. Members of a struct element can be: @@ -421,7 +420,7 @@ particle = ti.types.struct( particle_field = particle.field(shape=(n,)) ``` -### Access elements in a struct field +### Access Elements in a Struct Field You can access a member of an element in a struct field in either of the following ways: index-first or name-first. From 9755328a0b1da2e9d40c14dc3c38b4484aee63ce Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 11 Dec 2022 22:33:07 +0000 Subject: [PATCH 06/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- docs/lang/articles/basic/field.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/lang/articles/basic/field.md b/docs/lang/articles/basic/field.md index e08fc1a7b8f65..8c1306c18059e 100644 --- a/docs/lang/articles/basic/field.md +++ b/docs/lang/articles/basic/field.md @@ -13,7 +13,7 @@ Fields in Taichi are the _global_ data containers, which can be accessed from bo Scalar fields refer to the fields that store scalars and are the most basic fields. -- A 0D scalar field is a single scalar. +- A 0D scalar field is a single scalar. - A 1D scalar field is a 1D array of scalars. - A 2D scalar field is a 2D array of scalars, and so on. From da7bac2b5be484ec2648fe058e191497f5803384 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sun, 11 Dec 2022 22:52:36 +0000 Subject: [PATCH 07/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- docs/lang/articles/basic/field.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/lang/articles/basic/field.md b/docs/lang/articles/basic/field.md index 8c1306c18059e..b9c35d72b1fed 100644 --- a/docs/lang/articles/basic/field.md +++ b/docs/lang/articles/basic/field.md @@ -44,7 +44,7 @@ The simplest way to declare a scalar field is to call `ti.field(dtype, shape)`, ```python f_1d = ti.field(ti.i32, shape=9) # A 1D field of length 9 ``` - Over here we are defining a + Over here we are defining a An illustration of `f_1d`: ``` @@ -58,7 +58,7 @@ The simplest way to declare a scalar field is to call `ti.field(dtype, shape)`, There is little difference between a 0D field and a 1D field of length 1 except for their indexing rules. You *must* use `None` as the index to access a 0D field and `0` as the index to access a 1D field of length 1: ```python - + f1 = ti.field(int, shape=()) f2 = ti.field(int, shape=1) From 34b2084fdea241b477ebcc87557d6067d42bf1e4 Mon Sep 17 00:00:00 2001 From: Gabriel Vainer <67750020+vai9er@users.noreply.github.com> Date: Tue, 13 Dec 2022 03:43:40 +0000 Subject: [PATCH 08/12] resolve --- docs/lang/articles/basic/field.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/lang/articles/basic/field.md b/docs/lang/articles/basic/field.md index 8c1306c18059e..6cf9533ca00cf 100644 --- a/docs/lang/articles/basic/field.md +++ b/docs/lang/articles/basic/field.md @@ -44,7 +44,6 @@ The simplest way to declare a scalar field is to call `ti.field(dtype, shape)`, ```python f_1d = ti.field(ti.i32, shape=9) # A 1D field of length 9 ``` - Over here we are defining a An illustration of `f_1d`: ``` @@ -344,7 +343,7 @@ To access the 0D matrix field `x = ti.Matrix.field(n=3, m=4, dtype=ti.f32, shape `x[None][p, q]` (0 ≤ p < n, 0 ≤ q < m) ::: -### Considerations: Matrix size +### Considerations: Matrix Size Matrix operations are unrolled at compile time. Take a look at the following example: From 27c2a61176d326efc8c15a1771338bb803ea7e53 Mon Sep 17 00:00:00 2001 From: Gabriel Vainer <67750020+vai9er@users.noreply.github.com> Date: Tue, 13 Dec 2022 03:44:12 +0000 Subject: [PATCH 09/12] resolve --- docs/lang/articles/basic/field.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/lang/articles/basic/field.md b/docs/lang/articles/basic/field.md index a49566e529896..6a5e7a146e051 100644 --- a/docs/lang/articles/basic/field.md +++ b/docs/lang/articles/basic/field.md @@ -46,7 +46,7 @@ The simplest way to declare a scalar field is to call `ti.field(dtype, shape)`, ``` <<<<<<< HEAD ======= - Over here we are defining a + >>>>>>> da7bac2b5be484ec2648fe058e191497f5803384 An illustration of `f_1d`: From 8fbe99a5e26c855d8f398f029117d3d50bee9793 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 13 Dec 2022 03:45:09 +0000 Subject: [PATCH 10/12] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- docs/lang/articles/basic/field.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/lang/articles/basic/field.md b/docs/lang/articles/basic/field.md index 6a5e7a146e051..a6df42889ae69 100644 --- a/docs/lang/articles/basic/field.md +++ b/docs/lang/articles/basic/field.md @@ -62,7 +62,7 @@ The simplest way to declare a scalar field is to call `ti.field(dtype, shape)`, ```python <<<<<<< HEAD - + ======= >>>>>>> da7bac2b5be484ec2648fe058e191497f5803384 From f9c9e0f96109c25e90cafe11ac0d31a5d355788d Mon Sep 17 00:00:00 2001 From: Olinaaaloompa <106292061+Olinaaaloompa@users.noreply.github.com> Date: Wed, 14 Dec 2022 12:04:19 +0800 Subject: [PATCH 11/12] Apply suggestions from code review --- docs/lang/articles/basic/field.md | 32 +++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/docs/lang/articles/basic/field.md b/docs/lang/articles/basic/field.md index a6df42889ae69..fb8b1df940300 100644 --- a/docs/lang/articles/basic/field.md +++ b/docs/lang/articles/basic/field.md @@ -9,7 +9,7 @@ The term _field_ is borrowed from mathematics and physics. If you already know [ Fields in Taichi are the _global_ data containers, which can be accessed from both the Python scope and the Taichi scope. Just like an ndarray in NumPy or a tensor in PyTorch, a field in Taichi is defined as a multi-dimensional array of elements, and elements in a field can be a Scalar, a Vector, a Matrix, or a Struct. -## Scalar Fields +## Scalar fields Scalar fields refer to the fields that store scalars and are the most basic fields. @@ -95,7 +95,7 @@ The simplest way to declare a scalar field is to call `ti.field(dtype, shape)`, └ └───┴───┴───┴───┴───┴───┘ ┘ ``` -Scalar Fields of higher dimensions can be similarily defined. +Scalar fields of higher dimensions can be similarly defined. :::caution WARNING @@ -104,7 +104,7 @@ Taichi only supports fields of dimensions ≤ 8. ::: -### Accessing Elements in a Scalar Field +### Accessing elements in a scalar field Once a field is declared, Taichi automatically initializes its elements to zero. @@ -130,7 +130,7 @@ When accessing a 0D field `x`, use `x[None] = 0`, *not* `x = 0`. f_0d.shape=() ``` -- To access an element in a 1D field, use index `i` to get the ith element of our defined field. +- To access an element in a 1D field, use index `i` to get the `i`-th element of our defined field. ```python for i in range(9): @@ -145,7 +145,7 @@ When accessing a 0D field `x`, use `x[None] = 0`, *not* `x = 0`. └───┴───┴───┴───┴───┴───┴───┴───┴───┘ ``` -- To access an element in a 2D field, use index `(i, j)`, which is an integer pair to get the ith jth element of our defined field. +- To access an element in a 2D field, use index `(i, j)`, which is an integer pair to get the `i-th, j-th` element of our defined field. ```python for i, j in f_2d: @@ -206,7 +206,7 @@ f_2d[0][3:] = [4, 5, 6] # Error! You tried to access a slice of the first row, ::: -### Fill a Scalar Field with a Given Value +### Fill a scalar field with a given value To set all elements in a scalar field to a given value, call `field.fill()`: @@ -228,7 +228,7 @@ f_1d.shape # (9,) f_3d.dtype # f32 ``` -## Vector Fields +## Vector fields As the name suggests, vector fields are the fields whose elements are vectors. What a vector represents depends on the scenario of your program. For example, a vector may stand for the (R, G, B) triple of a pixel, the position of a particle, or the gravitational field in space. @@ -268,7 +268,7 @@ box_size = (300, 300, 300) # A 300x300x300 grid in a 3D space volumetric_field = ti.Vector.field(n=3, dtype=ti.f32, shape=box_size) ``` -### Accessing Elements in a Vector Field +### Accessing elements in a vector field Accessing a vector field is similar to accessing a multi-dimensional array: You use an index operator `[]` to access an element in the field. The only difference is that, to access a specific component of an element (vector in this case), you need an *extra* index operator `[]`: @@ -320,9 +320,9 @@ To access the `p`-th component of the 0D vector field `x = ti.Vector.field(n=3, `x[None][p]` (0 ≤ p < n). ::: -## Matrix Fields +## Matrix fields -As the name suggests, matrix fields are the fields whose elements are matrices. In continuum mechanics, at each infinitesimal point in a 3D material exists a `strain` and `stress` tensor. The strain and stress tensor is a 3 x 2 matrix. We can use a Matrix Field to represent this tensor. +As the name suggests, matrix fields are the fields whose elements are matrices. In continuum mechanics, at each infinitesimal point in a 3D material exists a strain and stress tensor, which is a 3 x 2 matrix. We can use a matrix field to represent this tensor. ### Declaration @@ -333,11 +333,11 @@ The following code snippet declares a tensor field: tensor_field = ti.Matrix.field(n=3, m=2, dtype=ti.f32, shape=(300, 400, 500)) ``` -### Accessing Elements in a Matrix Field +### Accessing elements in a matrix field Accessing a matrix field is similar to accessing a vector field: You use an index operator `[]` for field indexing and a second index operator `[]` for matrix indexing. -- To access the `ith, jth` element of the matrix field `tensor_field`: +- To access the `i-th, j-th` element of the matrix field `tensor_field`: `mat = tensor_field[i, j]` @@ -351,7 +351,7 @@ To access the 0D matrix field `x = ti.Matrix.field(n=3, m=4, dtype=ti.f32, shape `x[None][p, q]` (0 ≤ p < n, 0 ≤ q < m) ::: -### Considerations: Matrix Size +### Considerations: Matrix size Matrix operations are unrolled at compile time. Take a look at the following example: @@ -381,14 +381,14 @@ Operating on larger matrices (for example `32x128`) can lead to longer compilati **Workaround:** -When declaring a matrix field, leave large dimensions to the Fields, rather than to the matrices. If you have a `3x2` field of `64x32` matrices: +When declaring a matrix field, leave large dimensions to the *fields*, rather than to the matrices. If you have a `3x2` field of `64x32` matrices: - Not recommended: `ti.Matrix.field(64, 32, dtype=ti.f32, shape=(3, 2))` - Recommended: `ti.Matrix.field(3, 2, dtype=ti.f32, shape=(64, 32))` -## Struct Fields +## Struct fields Struct fields are fields that store user-defined structs. Members of a struct element can be: @@ -427,7 +427,7 @@ particle = ti.types.struct( particle_field = particle.field(shape=(n,)) ``` -### Access Elements in a Struct Field +### Accessing elements in a struct field You can access a member of an element in a struct field in either of the following ways: index-first or name-first. From 7282008b140c0e340f348399a95dce22b616dfe1 Mon Sep 17 00:00:00 2001 From: Gabriel Vainer <67750020+vai9er@users.noreply.github.com> Date: Thu, 15 Dec 2022 00:47:42 +0000 Subject: [PATCH 12/12] resolve --- docs/lang/articles/basic/field.md | 8 -------- 1 file changed, 8 deletions(-) diff --git a/docs/lang/articles/basic/field.md b/docs/lang/articles/basic/field.md index fb8b1df940300..f06d4e17f49bf 100644 --- a/docs/lang/articles/basic/field.md +++ b/docs/lang/articles/basic/field.md @@ -44,10 +44,7 @@ The simplest way to declare a scalar field is to call `ti.field(dtype, shape)`, ```python f_1d = ti.field(ti.i32, shape=9) # A 1D field of length 9 ``` -<<<<<<< HEAD -======= ->>>>>>> da7bac2b5be484ec2648fe058e191497f5803384 An illustration of `f_1d`: ``` @@ -61,11 +58,6 @@ The simplest way to declare a scalar field is to call `ti.field(dtype, shape)`, There is little difference between a 0D field and a 1D field of length 1 except for their indexing rules. You *must* use `None` as the index to access a 0D field and `0` as the index to access a 1D field of length 1: ```python -<<<<<<< HEAD - -======= - ->>>>>>> da7bac2b5be484ec2648fe058e191497f5803384 f1 = ti.field(int, shape=()) f2 = ti.field(int, shape=1)