Skip to main content


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.

  • 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_coo(self, row_coo, col_coo, value_coo)#

Build a CSR format sparse matrix from COO format inputs.

  • row_indices (ti.ndarray) – the row indices of the matrix entries.

  • col_indices (ti.ndarray) – the column indices of the matrix entries.

  • data (ti.ndarray) – the entries of the matrix.


TaichiRuntimeError – If the inputs are not ti.ndarray or the datatypes of the ndarray are not correct.

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]
property shape(self)#

The shape of the sparse matrix.

spmv(self, x, y)#

Sparse matrix-vector multiplication using cuSparse.

  • x (ti.ndarray) – the vector to be multiplied.

  • y (ti.ndarray) – the result of matrix-vector multiplication.

>>> x = ti.ndarray(shape=4, dtype=val_dt)
>>> y = ti.ndarray(shape=4, dtype=val_dt)
>>> A = ti.linalg.SparseMatrix(n=4, m=4, dtype=ti.f32)
>>> A.build_from_ndarray_cusparse(row_csr, col_csr, value_csr)
>>> A.spmv(x, y)

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:


solve_cu(self, sparse_matrix, b, x)#
class taichi.linalg.sparse_matrix_builder#

Bases: taichi.types.annotations.sparse_matrix_builder