Matrices¶
ti.Matrix
is for small matrices (e.g. 3x3) only. If you have 64x64 matrices, you should consider using a 2D tensor of scalars.ti.Vector
is the same asti.Matrix
, except that it has only one column.- Differentiate element-wise product
*
and matrix product@
. ti.Vector(n, dt=ti.f32)
orti.Matrix(n, m, dt=ti.f32)
to create tensors of vectors/matrices.A.transpose()
R, S = ti.polar_decompose(A, ti.f32)
U, sigma, V = ti.svd(A, ti.f32)
(Note thatsigma
is a3x3
diagonal matrix)any(A)
(Taichi-scope only)all(A)
(Taichi-scope only)
TODO: doc here better like Vector. WIP
A matrix in Taichi can have two forms:
- as a temporary local variable. An
n by m
matrix consists ofn * m
scalar values.- as a an element of a global tensor. In this case, the tensor is an N-dimensional array of
n by m
matrices.
Declaration¶
As global tensors of matrices¶
-
ti.Matrix.
var
(n, m, dt, shape = None, offset = None)¶ 参数: - n – (scalar) the number of rows in the matrix
- m – (scalar) the number of columns in the matrix
- dt – (DataType) data type of the components
- shape – (optional, scalar or tuple) shape the tensor of vectors, see 张量与矩阵
- offset – (optional, scalar or tuple) see Coordinate offsets
For example, this creates a 5x4 tensor of 3x3 matrices:
# Python-scope a = ti.Matrix.var(3, 3, dt=ti.f32, shape=(5, 4))
注解
In Python-scope, ti.var
declares Tensors of scalars, while ti.Matrix
declares tensors of matrices.
As a temporary local variable¶
-
ti.
Matrix
([[x, y, ...][, z, w, ...], ...])¶ 参数: - x – (scalar) the first component of the first row
- y – (scalar) the second component of the first row
- z – (scalar) the first component of the second row
- w – (scalar) the second component of the second row
For example, this creates a 2x3 matrix with components (2, 3, 4) in the first row and (5, 6, 7) in the second row:
# Taichi-scope a = ti.Matrix([[2, 3, 4], [5, 6, 7]])
-
ti.Matrix.
rows
([v0, v1, v2, ...])¶
-
ti.Matrix.
cols
([v0, v1, v2, ...])¶ 参数: - v0 – (vector) vector of elements forming first row (or column)
- v1 – (vector) vector of elements forming second row (or column)
- v2 – (vector) vector of elements forming third row (or column)
For example, this creates a 3x3 matrix by concactinating vectors into rows (or columns):
# Taichi-scope v0 = ti.Vector([1.0, 2.0, 3.0]) v1 = ti.Vector([4.0, 5.0, 6.0]) v2 = ti.Vector([7.0, 8.0, 9.0]) # to specify data in rows a = ti.Matrix.rows([v0, v1, v2]) # to specify data in columns instead a = ti.Matrix.cols([v0, v1, v2]) # lists can be used instead of vectors a = ti.Matrix.rows([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]])
Accessing components¶
As global tensors of vectors¶
-
a[p, q, ...][i, j]
参数: - a – (tensor of matrices) the tensor of matrices
- p – (scalar) index of the first tensor dimension
- q – (scalar) index of the second tensor dimension
- i – (scalar) row index of the matrix
- j – (scalar) column index of the matrix
This extracts the first element in matrix
a[6, 3]
:x = a[6, 3][0, 0] # or mat = a[6, 3] x = mat[0, 0]
注解
Always use two pair of square brackets to access scalar elements from tensors of matrices.
- The indices in the first pair of brackets locate the matrix inside the tensor of matrices;
- The indices in the second pair of brackets locate the scalar element inside the matrix.
For 0-D tensors of matrices, indices in the first pair of brackets should be [None]
.
As a temporary local variable¶
-
a[i, j]
参数: - a – (Matrix) the matrix
- i – (scalar) row index of the matrix
- j – (scalar) column index of the matrix
For example, this extracts the element in row 0 column 1 of matrix
a
:x = a[0, 1]
This sets the element in row 1 column 3 of
a
to 4:a[1, 3] = 4
Methods¶
-
a.
transpose
()¶ 参数: a – (Matrix) the matrix 返回: (Matrix) the transposed matrix of a
.For example:
a = ti.Matrix([[2, 3], [4, 5]]) b = a.transpose() # Now b = ti.Matrix([[2, 4], [3, 5]])
注解
a.transpose()
will not effect the data ina
, it just return the result.
-
a.
trace
()¶ 参数: a – (Matrix) the matrix 返回: (scalar) the trace of matrix a
.The return value can be computed as
a[0, 0] + a[1, 1] + ...
.
-
a.
determinant
()¶ 参数: a – (Matrix) the matrix 返回: (scalar) the determinant of matrix a
.注解
The matrix size of matrix must be 1x1, 2x2, 3x3 or 4x4 for now.
This function only works in Taichi-scope for now.
-
a.
inverse
()¶ 参数: a – (Matrix) the matrix 返回: (Matrix) the inverse of matrix a
.注解
The matrix size of matrix must be 1x1, 2x2, 3x3 or 4x4 for now.
This function only works in Taichi-scope for now.