Skip to main content


Taichi support module for sparse matrix operations.

class taichi.linalg.CG(A, b, x0=None, max_iter=50, atol=1e-06)#
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.

  • 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.


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


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

>>> 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]

Sparse Matrix transpose.


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.

  • 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 the triplets stored in the builder

class taichi.linalg.SparseSolver(dtype=f32, solver_type='LLT', ordering='AMD')#

Sparse linear system solver

Use this class to solve linear systems represented by sparse matrices.

  • solver_type (str) – The factorization type.

  • ordering (str) – The method for matrices re-ordering.

analyze_pattern(self, sparse_matrix)#

Reorder the nonzero elements of the matrix, such that the factorization step creates less fill-in.


sparse_matrix (SparseMatrix) – The sparse matrix to be analyzed.

compute(self, sparse_matrix)#

This method is equivalent to calling both analyze_pattern and then factorize.


sparse_matrix (SparseMatrix) – The sparse matrix to be computed.

factorize(self, sparse_matrix)#

Do the factorization step


sparse_matrix (SparseMatrix) – The sparse matrix to be factorized.


Check if the linear systems are solved successfully.


True if the solving process succeeded, False otherwise.

Return type:


solve(self, b)#

Computes the solution of the linear systems. :param b: The right-hand side of the linear systems. :type b: numpy.array or Field


The solution of linear systems.

Return type:


class taichi.linalg.sparse_matrix_builder#

Bases: taichi.types.annotations.sparse_matrix_builder