Solvers

pymatting.cg

🔗

Signature

cg( A, b, x0=None, atol=0.0, rtol=1e-7, maxiter=10000, callback=None, M=None, reorthogonalize=False)

Function Description

Solves a system of linear equations \(Ax=b\) using conjugate gradient descent [HS52]

Parameters

  • A (scipy.sparse.csr_matrix)
      Square matrix
  • b (numpy.ndarray)
      Vector describing the right-hand side of the system
  • x0 (numpy.ndarray)
      Initialization, if None then x=np.zeros_like(b)
  • atol (float)
      Absolute tolerance. The loop terminates if the \(||r||\) is smaller than atol, where \(r\) denotes the residual of the current iterate.
  • rtol (float)
      Relative tolerance. The loop terminates if \({||r||}/{||b||}\) is smaller than rtol, where \(r\) denotes the residual of the current iterate.
  • callback (function)
      Function callback(A, x, b, norm_b, r, norm_r) called after each iteration, defaults to None
  • M (function or scipy.sparse.csr_matrix)
      Function that applies the preconditioner to a vector. Alternatively, M can be a matrix describing the precondioner.
  • reorthogonalize (boolean)
      Whether to apply reorthogonalization of the residuals after each update, defaults to False

Returns

  • x (numpy.ndarray)
      Solution of the system

Example

>>> from pymatting import * >>> import numpy as np >>> A = np.array([[3.0, 1.0], [1.0, 2.0]]) >>> M = jacobi(A) >>> b = np.array([4.0, 3.0]) >>> cg(A, b, M=M) array([1., 1.])