quadrants.linalg.sparse_matrix#

Classes#

SparseMatrix

Quadrants's Sparse Matrix class

SparseMatrixBuilder

A python wrap around sparse matrix builder.

Module Contents#

class quadrants.linalg.sparse_matrix.SparseMatrix(n=None, m=None, sm=None, dtype=f32, storage_format='col_major')[source]#

Quadrants’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.

dtype[source]#
transpose()[source]#

Sparse Matrix transpose.

Returns:

The transposed sparse mastrix.

property shape[source]#

The shape of the sparse matrix.

build_from_ndarray(ndarray)[source]#

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:

QuadrantsRuntimeError – 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(filename)[source]#

Writes the sparse matrix to Matrix Market file-like target.

Parameters:

filename (str) – the file name to write the sparse matrix to.

class quadrants.linalg.sparse_matrix.SparseMatrixBuilder(num_rows=None, num_cols=None, max_num_triplets=0, dtype=f32, storage_format='col_major')[source]#

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.

num_rows = None[source]#
num_cols = None[source]#
dtype[source]#
print_triplets()[source]#

Print the triplets stored in the builder

build(dtype=f32, _format='CSR')[source]#

Create a sparse matrix using the triplets