Skip to main content

taichi.linalg#

Taichi support module for sparse matrix operations.

class taichi.linalg.SparseMatrix(n=None, m=None, sm=None, dtype=f32, storage_format='col_major')#

Taichi’s Sparse Matrix class

A sparse matrix allows the programmer to solve a large linear system.

Parameters:
  • n (int) – the first dimension of a sparse matrix.

  • m (int) – the second dimension of a sparse matrix.

  • sm (SparseMatrix) – another sparse matrix that will be built from.

build_from_ndarray(self, ndarray)#

Build the sparse matrix from a ndarray.

Parameters:

ndarray (Union[ti.ndarray, ti.Vector.ndarray, ti.Matrix.ndarray]) – the ndarray to build the sparse matrix from.

Raises:

TaichiRuntimeError – If the input is not a ndarray or the length is not divisible by 3.

Example::
>>> N = 5
>>> triplets = ti.Vector.ndarray(n=3, dtype=ti.f32, shape=10, layout=ti.Layout.AOS)
>>> @ti.kernel
>>> def fill(triplets: ti.types.ndarray()):
>>>     for i in range(N):
>>>        triplets[i] = ti.Vector([i, (i + 1) % N, i+1], dt=ti.f32)
>>> fill(triplets)
>>> A = ti.linalg.SparseMatrix(n=N, m=N, dtype=ti.f32)
>>> A.build_from_ndarray(triplets)
>>> print(A)
[0, 1, 0, 0, 0]
[0, 0, 2, 0, 0]
[0, 0, 0, 3, 0]
[0, 0, 0, 0, 4]
[5, 0, 0, 0, 0]
property shape(self)#

The shape of the sparse matrix.

transpose(self)#

Sparse Matrix transpose.

Returns:

The transposed sparse mastrix.

class taichi.linalg.SparseMatrixBuilder(num_rows=None, num_cols=None, max_num_triplets=0, dtype=f32, storage_format='col_major')#

A python wrap around sparse matrix builder.

Use this builder to fill the sparse matrix.

Parameters:
  • num_rows (int) – the first dimension of a sparse matrix.

  • num_cols (int) – the second dimension of a sparse matrix.

  • max_num_triplets (int) – the maximum number of triplets.

  • dtype (ti.dtype) – the data type of the sparse matrix.

  • storage_format (str) – the storage format of the sparse matrix.

build(self, dtype=f32, _format='CSR')#

Create a sparse matrix using the triplets

print_triplets(self)#

Print the triplets stored in the builder

class taichi.linalg.sparse_matrix_builder#

Bases: taichi.types.annotations.sparse_matrix_builder