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]
- mmwrite(self, filename)#
Writes the sparse matrix to Matrix Market file-like target.
- Parameters:
filename (str) – the file name to write the sparse matrix to.
- 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.SparseSolver(dtype=f32, solver_type='LLT', ordering='AMD')#
Sparse linear system solver
Use this class to solve linear systems represented by sparse matrices.
- Parameters:
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.
- Parameters:
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.
- Parameters:
sparse_matrix (SparseMatrix) – The sparse matrix to be computed.
- factorize(self, sparse_matrix)#
Do the factorization step
- Parameters:
sparse_matrix (SparseMatrix) – The sparse matrix to be factorized.
- info(self)#
Check if the linear systems are solved successfully.
- Returns:
True if the solving process succeeded, False otherwise.
- Return type:
bool
- 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
- Returns:
The solution of linear systems.
- Return type:
numpy.array
- exception taichi.linalg.TaichiRuntimeError#
Bases:
RuntimeError
Thrown when the compiled program cannot be executed due to unspecified reasons.
- class args#
- with_traceback()#
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- exception taichi.linalg.TaichiTypeError#
Bases:
TaichiCompilationError
,TypeError
Thrown when a type mismatch is found during compilation.
- class args#
- with_traceback()#
Exception.with_traceback(tb) – set self.__traceback__ to tb and return self.
- class taichi.linalg.sparse_matrix_builder#
- taichi.linalg.taichi_cg_solver(A, b, x, tol=1e-06, maxiter=5000, quiet=True)#