Skip to content

Commit

Permalink
[Lang] Use ti.linalg.SparseMatrixBuilder as sparse matrix builder (ta…
Browse files Browse the repository at this point in the history
…ichi-dev#3152)

* move taichi/lang/linalg to taichi/lang/linalg_impl

* change ti.SparseMatrixBuilder to ti.linalg.SparseMatrixBuilder
  • Loading branch information
FantasyVR authored Oct 11, 2021
1 parent c3478c7 commit 9ef9c74
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 29 deletions.
18 changes: 9 additions & 9 deletions python/taichi/lang/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import time
from copy import deepcopy as _deepcopy

import taichi.lang.linalg
import taichi.lang.linalg_impl
import taichi.lang.meta
from taichi.core.util import locale_encode
from taichi.core.util import ti_core as _ti_core
Expand Down Expand Up @@ -515,7 +515,7 @@ def polar_decompose(A, dt=None):
"""Perform polar decomposition (A=UP) for arbitrary size matrix.
Mathematical concept refers to https://en.wikipedia.org/wiki/Polar_decomposition.
This is only a wrapper for :func:`taichi.lang.linalg.polar_decompose`.
This is only a wrapper for :func:`taichi.lang.linalg_impl.polar_decompose`.
Args:
A (ti.Matrix(n, n)): input nxn matrix `A`.
Expand All @@ -526,14 +526,14 @@ def polar_decompose(A, dt=None):
"""
if dt is None:
dt = impl.get_runtime().default_fp
return taichi.lang.linalg.polar_decompose(A, dt)
return taichi.lang.linalg_impl.polar_decompose(A, dt)


def svd(A, dt=None):
"""Perform singular value decomposition (A=USV^T) for arbitrary size matrix.
Mathematical concept refers to https://en.wikipedia.org/wiki/Singular_value_decomposition.
This is only a wrappers for :func:`taichi.lang.linalg.svd`.
This is only a wrappers for :func:`taichi.lang.linalg_impl.svd`.
Args:
A (ti.Matrix(n, n)): input nxn matrix `A`.
Expand All @@ -544,14 +544,14 @@ def svd(A, dt=None):
"""
if dt is None:
dt = impl.get_runtime().default_fp
return taichi.lang.linalg.svd(A, dt)
return taichi.lang.linalg_impl.svd(A, dt)


def eig(A, dt=None):
"""Compute the eigenvalues and right eigenvectors of a real matrix.
Mathematical concept refers to https://en.wikipedia.org/wiki/Eigendecomposition_of_a_matrix.
2D implementation refers to :func:`taichi.lang.linalg.eig2x2`.
2D implementation refers to :func:`taichi.lang.linalg_impl.eig2x2`.
Args:
A (ti.Matrix(n, n)): 2D Matrix for which the eigenvalues and right eigenvectors will be computed.
Expand All @@ -564,15 +564,15 @@ def eig(A, dt=None):
if dt is None:
dt = impl.get_runtime().default_fp
if A.n == 2:
return taichi.lang.linalg.eig2x2(A, dt)
return taichi.lang.linalg_impl.eig2x2(A, dt)
raise Exception("Eigen solver only supports 2D matrices.")


def sym_eig(A, dt=None):
"""Compute the eigenvalues and right eigenvectors of a real symmetric matrix.
Mathematical concept refers to https://en.wikipedia.org/wiki/Eigendecomposition_of_a_matrix.
2D implementation refers to :func:`taichi.lang.linalg.sym_eig2x2`.
2D implementation refers to :func:`taichi.lang.linalg_impl.sym_eig2x2`.
Args:
A (ti.Matrix(n, n)): Symmetric Matrix for which the eigenvalues and right eigenvectors will be computed.
Expand All @@ -586,7 +586,7 @@ def sym_eig(A, dt=None):
if dt is None:
dt = impl.get_runtime().default_fp
if A.n == 2:
return taichi.lang.linalg.sym_eig2x2(A, dt)
return taichi.lang.linalg_impl.sym_eig2x2(A, dt)
raise Exception("Symmetric eigen solver only supports 2D matrices.")


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ def svd(A, dt):
"""Perform singular value decomposition (A=USV^T) for arbitrary size matrix.
Mathematical concept refers to https://en.wikipedia.org/wiki/Singular_value_decomposition.
2D implementation refers to :func:`taichi.lang.linalg.svd2d`.
3D implementation refers to :func:`taichi.lang.linalg.svd3d`.
2D implementation refers to :func:`taichi.lang.linalg_impl.svd2d`.
3D implementation refers to :func:`taichi.lang.linalg_impl.svd3d`.
Args:
A (ti.Matrix(n, n)): input nxn matrix `A`.
Expand All @@ -242,8 +242,8 @@ def polar_decompose(A, dt):
"""Perform polar decomposition (A=UP) for arbitrary size matrix.
Mathematical concept refers to https://en.wikipedia.org/wiki/Polar_decomposition.
2D implementation refers to :func:`taichi.lang.linalg.polar_decompose2d`.
3D implementation refers to :func:`taichi.lang.linalg.polar_decompose3d`.
2D implementation refers to :func:`taichi.lang.linalg_impl.polar_decompose2d`.
3D implementation refers to :func:`taichi.lang.linalg_impl.polar_decompose3d`.
Args:
A (ti.Matrix(n, n)): input nxn matrix `A`.
Expand Down
2 changes: 1 addition & 1 deletion tests/python/test_sparse_linear_solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
@ti.test(arch=ti.cpu)
def test_sparse_LLT_solver(solver_type):
n = 4
Abuilder = ti.SparseMatrixBuilder(n, n, max_num_triplets=100)
Abuilder = ti.linalg.SparseMatrixBuilder(n, n, max_num_triplets=100)
b = ti.field(ti.f32, shape=n)

@ti.kernel
Expand Down
30 changes: 15 additions & 15 deletions tests/python/test_sparse_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
@ti.test(arch=ti.cpu)
def test_sparse_matrix_builder():
n = 8
Abuilder = ti.SparseMatrixBuilder(n, n, max_num_triplets=100)
Abuilder = ti.linalg.SparseMatrixBuilder(n, n, max_num_triplets=100)

@ti.kernel
def fill(Abuilder: ti.sparse_matrix_builder()):
Expand All @@ -21,7 +21,7 @@ def fill(Abuilder: ti.sparse_matrix_builder()):
@ti.test(arch=ti.cpu)
def test_sparse_matrix_element_access():
n = 8
Abuilder = ti.SparseMatrixBuilder(n, n, max_num_triplets=100)
Abuilder = ti.linalg.SparseMatrixBuilder(n, n, max_num_triplets=100)

@ti.kernel
def fill(Abuilder: ti.sparse_matrix_builder()):
Expand All @@ -37,7 +37,7 @@ def fill(Abuilder: ti.sparse_matrix_builder()):
@ti.test(arch=ti.cpu)
def test_sparse_matrix_element_modify():
n = 8
Abuilder = ti.SparseMatrixBuilder(n, n, max_num_triplets=100)
Abuilder = ti.linalg.SparseMatrixBuilder(n, n, max_num_triplets=100)

@ti.kernel
def fill(Abuilder: ti.sparse_matrix_builder()):
Expand All @@ -53,8 +53,8 @@ def fill(Abuilder: ti.sparse_matrix_builder()):
@ti.test(arch=ti.cpu)
def test_sparse_matrix_addition():
n = 8
Abuilder = ti.SparseMatrixBuilder(n, n, max_num_triplets=100)
Bbuilder = ti.SparseMatrixBuilder(n, n, max_num_triplets=100)
Abuilder = ti.linalg.SparseMatrixBuilder(n, n, max_num_triplets=100)
Bbuilder = ti.linalg.SparseMatrixBuilder(n, n, max_num_triplets=100)

@ti.kernel
def fill(Abuilder: ti.sparse_matrix_builder(),
Expand All @@ -75,8 +75,8 @@ def fill(Abuilder: ti.sparse_matrix_builder(),
@ti.test(arch=ti.cpu)
def test_sparse_matrix_subtraction():
n = 8
Abuilder = ti.SparseMatrixBuilder(n, n, max_num_triplets=100)
Bbuilder = ti.SparseMatrixBuilder(n, n, max_num_triplets=100)
Abuilder = ti.linalg.SparseMatrixBuilder(n, n, max_num_triplets=100)
Bbuilder = ti.linalg.SparseMatrixBuilder(n, n, max_num_triplets=100)

@ti.kernel
def fill(Abuilder: ti.sparse_matrix_builder(),
Expand All @@ -97,7 +97,7 @@ def fill(Abuilder: ti.sparse_matrix_builder(),
@ti.test(arch=ti.cpu)
def test_sparse_matrix_scalar_multiplication():
n = 8
Abuilder = ti.SparseMatrixBuilder(n, n, max_num_triplets=100)
Abuilder = ti.linalg.SparseMatrixBuilder(n, n, max_num_triplets=100)

@ti.kernel
def fill(Abuilder: ti.sparse_matrix_builder()):
Expand All @@ -115,7 +115,7 @@ def fill(Abuilder: ti.sparse_matrix_builder()):
@ti.test(arch=ti.cpu)
def test_sparse_matrix_transpose():
n = 8
Abuilder = ti.SparseMatrixBuilder(n, n, max_num_triplets=100)
Abuilder = ti.linalg.SparseMatrixBuilder(n, n, max_num_triplets=100)

@ti.kernel
def fill(Abuilder: ti.sparse_matrix_builder()):
Expand All @@ -133,8 +133,8 @@ def fill(Abuilder: ti.sparse_matrix_builder()):
@ti.test(arch=ti.cpu)
def test_sparse_matrix_elementwise_multiplication():
n = 8
Abuilder = ti.SparseMatrixBuilder(n, n, max_num_triplets=100)
Bbuilder = ti.SparseMatrixBuilder(n, n, max_num_triplets=100)
Abuilder = ti.linalg.SparseMatrixBuilder(n, n, max_num_triplets=100)
Bbuilder = ti.linalg.SparseMatrixBuilder(n, n, max_num_triplets=100)

@ti.kernel
def fill(Abuilder: ti.sparse_matrix_builder(),
Expand All @@ -155,8 +155,8 @@ def fill(Abuilder: ti.sparse_matrix_builder(),
@ti.test(arch=ti.cpu)
def test_sparse_matrix_multiplication():
n = 2
Abuilder = ti.SparseMatrixBuilder(n, n, max_num_triplets=100)
Bbuilder = ti.SparseMatrixBuilder(n, n, max_num_triplets=100)
Abuilder = ti.linalg.SparseMatrixBuilder(n, n, max_num_triplets=100)
Bbuilder = ti.linalg.SparseMatrixBuilder(n, n, max_num_triplets=100)

@ti.kernel
def fill(Abuilder: ti.sparse_matrix_builder(),
Expand All @@ -178,8 +178,8 @@ def fill(Abuilder: ti.sparse_matrix_builder(),
@ti.test(arch=ti.cpu)
def test_sparse_matrix_nonsymmetric_multiplication():
n, k, m = 2, 3, 4
Abuilder = ti.SparseMatrixBuilder(n, k, max_num_triplets=100)
Bbuilder = ti.SparseMatrixBuilder(k, m, max_num_triplets=100)
Abuilder = ti.linalg.SparseMatrixBuilder(n, k, max_num_triplets=100)
Bbuilder = ti.linalg.SparseMatrixBuilder(k, m, max_num_triplets=100)

@ti.kernel
def fill(Abuilder: ti.sparse_matrix_builder(),
Expand Down

0 comments on commit 9ef9c74

Please sign in to comment.