Skip to content

Commit

Permalink
Add docstring to SparseCG and MatrixFreeCG.
Browse files Browse the repository at this point in the history
  • Loading branch information
houkensjtu committed May 4, 2023
1 parent 3371aae commit 9ffb94d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
14 changes: 14 additions & 0 deletions python/taichi/linalg/matrixfree_cg.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,20 @@ def matvec(self, x, Ax):


def MatrixFreeCG(A, b, x, tol=1e-6, maxiter=5000, quiet=True):
"""Matrix-free conjugate-gradient solver.
Use conjugate-gradient method to solve the linear system Ax = b, where A is implicitly
represented as a LinearOperator.
Args:
A (LinearOperator): The coefficient matrix A of the linear system.
b (Field): The right-hand side of the linear system.
x (Field): The initial guess for the solution.
maxiter (int): Maximum number of iterations.
atol: Tolerance(absolute) for convergence.
quiet (bool): Switch to turn on/off iteration log.
"""

if b.dtype != x.dtype:
raise TaichiTypeError(f"Dtype mismatch b.dtype({b.dtype}) != x.dtype({x.dtype}).")
if str(b.dtype) == "f32":
Expand Down
12 changes: 12 additions & 0 deletions python/taichi/linalg/sparse_cg.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,18 @@


class SparseCG:
"""Conjugate-gradient solver built for SparseMatrix.
Use conjugate-gradient method to solve the linear system Ax = b, where A is SparseMatrix.
Args:
A (SparseMatrix): The coefficient matrix A of the linear system.
b (numpy ndarray, taichi Ndarray): The right-hand side of the linear system.
x0 (numpy ndarray, taichi Ndarray): The initial guess for the solution.
max_iter (int): Maximum number of iterations.
atol: Tolerance(absolute) for convergence.
"""

def __init__(self, A, b, x0=None, max_iter=50, atol=1e-6):
self.dtype = A.dtype
self.ti_arch = get_runtime().prog.config().arch
Expand Down

0 comments on commit 9ffb94d

Please sign in to comment.