Skip to main content

Kernel-Scope Built-in Functions

In this page, we list all the built-in functions available in taichi.js kernels.

Unary Functions

Common Math Functions

These can be applied to both scalars or vectors

  • ti.sin(x)
  • ti.cos(x)
  • ti.asin(x)
  • ti.acos(x)
  • ti.tan(x)
  • ti.tanh(x)
  • ti.exp(x)
  • ti.log(x)
  • ti.abs(x)
  • ti.sgn(x) (sign function)
  • ti.sqrt(x)
  • ti.rsqrt(x) (reciprocal of square root)

Vector/Matrix operations:

  • ti.len(v) (Amount of components of vector)
  • ti.sum(v) (Sum of all components)
  • ti.norm(v)
  • ti.normSqr(v) (square of ti.norm())
  • ti.normalized(v) (ti.normalized(x) == x / ti.norm(x))
  • ti.transpose(m) (transpose of matrix)

Casts

  • ti.i32(x)
  • ti.f32(x)
  • ti.bitcast_i32(x)
  • ti.bitcast_f32(x)

Syntax

Notice that, for each of these unary functions, there are two ways of writing it:

ti.sin(x);
x.sin();

These two ways are semantically equivalent.

Binary Functions

Common Math Functions

These can be applied to both scalars or vectors

  • ti.min(a, b)
  • ti.max(a, b)
  • ti.pow(a, b)
  • ti.atan2(a, b)

Vector/Matrix operations

  • ti.dot(v1, v2) (dot product, returns a scalar)
  • ti.cross(v1, v2) (cross product for 3D vectors, returns a 3D vector perpendicular to both v1 and v2)
  • ti.outerProduct(v1, v2) (outer product for 2 matrices of the same size, returns a matrix)
  • ti.matmul(m, v) (matrix-vector product)
  • ti.pow(a, b)
  • ti.atan2(a, b)

Struct operations

  • ti.mergeStructs(a, b)

    Example:

    let s0 = {i0: 0, f0: 0.0}
    let s1 = {i1: 1, f1: 1.0}
    let s = ti.mergeStructs(s0, s1) // {i0: 0, f0: 0.0, i1: 1, f1: 1.0}

Syntax

Notice that, for each of these unary functions, there are two ways of writing it:

ti.max(a, b);
a.max(b);

Random Generator

  • ti.random() returns a random value between 0.0 and 1.0

Javascript Built-ins

Functions available in taichi.js which are inherited from Javascript:

Atomic Built-ins

Atomic built-in functions are listed and described here.

Rendering Built-ins

There are also some built-in functions used specifically for rendering purposes. These are listed here.