Vectors¶
A vector in Taichi can have two forms:
 as a temporary local variable. An
n
component vector consists ofn
scalar values. as an element of a global tensor. In this case, the tensor is an Ndimensional array of
n
component vectors
Declaration¶
As global tensors of vectors¶

ti.
Vector
(n, dt=type, shape=shape)¶ Parameters:  n – (scalar) the number of components in the vector
 type – (DataType) data type of the components
 shape – (scalar or tuple) shape the tensor of vectors, see Tensors and matrices
For example, this creates a 5x4 tensor of 3 component vectors:
# Pythonscope a = ti.Vector(3, dt=ti.f32, shape=(5, 4))
Note
In Pythonscope, ti.var
declares Tensors of scalars, while ti.Vector
declares tensors of vectors.
As a temporary local variable¶

ti.
Vector
([x, y, ...]) Parameters:  x – (scalar) the first component of the vector
 y – (scalar) the second component of the vector
For example, this creates a 3D vector with components (2, 3, 4):
# Taichiscope a = ti.Vector([2, 3, 4])
Accessing components¶
As global tensors of vectors¶

a[p, q, ...][i]
Parameters:  a – (Vector) the vector
 p – (scalar) index of the first tensor dimension
 q – (scalar) index of the second tensor dimension
 i – (scalar) index of the vector component
This extracts the first component of vector
a[6, 3]
:x = a[6, 3][0] # or vec = a[6, 3] x = vec[0]
Note
Always use two pair of square brackets to access scalar elements from tensors of vectors.
 The indices in the first pair of brackets locate the vector inside the tensor of vectors;
 The indices in the second pair of brackets locate the scalar element inside the vector.
For 0D tensors of vectors, indices in the first pair of brackets should be [None]
.
As a temporary local variable¶

a[i]
Parameters:  a – (Vector) the vector
 i – (scalar) index of the component
For example, this extracts the first component of vector
a
:x = a[0]
This sets the second component of
a
to 4:a[1] = 4
TODO: add descriptions about
a(i, j)
Methods¶

a.
norm
(eps = 0)¶ Parameters:  a – (Vector)
 eps – (optional, scalar) a safeguard value for
sqrt
, usually 0. See the note below.
Returns: (scalar) the magnitude / length / norm of vector
For example,
a = ti.Vector([3, 4]) a.norm() # sqrt(3*3 + 4*4 + 0) = 5
a.norm(eps)
is equivalent toti.sqrt(a.dot(a) + eps)
Note
Set eps = 1e5
for example, to safe guard the operator’s gradient on zero vectors during differentiable programming.

a.
dot
(b)¶ Parameters:  a – (Vector)
 b – (Vector)
Returns: (scalar) the dot (inner) product of
a
andb
E.g.,
a = ti.Vector([1, 3]) b = ti.Vector([2, 4]) a.dot(b) # 1*2 + 3*4 = 14

ti.
cross
(a, b)¶ Parameters:  a – (Vector, 3 component)
 b – (Vector, 3 component)
Returns: (Vector, 3D) the cross product of
a
andb
We use righthanded coordinate system, E.g.,
a = ti.Vector([1, 2, 3]) b = ti.Vector([4, 5, 6]) c = ti.cross(a, b) # [2*6  5*3, 4*3  1*6, 1*5  4*2]

ti.
outer_product
(a, b)¶ Parameters:  a – (Vector)
 b – (Vector)
Returns: (Matrix) the outer product of
a
andb
E.g.,
a = ti.Vector([1, 2, 3]) b = ti.Vector([4, 5, 6]) c = ti.outer_product(a, b) # NOTE: c[i, j] = a[i] * b[j] # c = [[1*4, 1*5, 1*6], [2*4, 2*5, 2*6], [3*4, 3*5, 3*6]]
Note
This is not the same as ti.cross. a
and b
do not have to be 3 component vectors.

a.
cast
(dt)¶ Parameters:  a – (Vector)
 dt – (DataType)
Returns: (Vector) vector with all components of
a
casted into typedt
E.g.,
# Taichiscope a = ti.Vector([1.6, 2.3]) a.cast(ti.i32) # [2, 3]
Note
Vectors are special matrices with only 1 column. In fact, ti.Vector
is just an alias of ti.Matrix
.