dpnp.scipy.sparse.linalg.cg

dpnp.scipy.sparse.linalg.cg(A, b, x0: dpnp_array | None = None, *, rtol: float = 1e-05, tol: float | None = None, maxiter: int | None = None, M=None, callback: Callable | None = None, atol=None) → tuple[dpnp_array, int][source]

Use Conjugate Gradient iteration to solve Ax = b for a Hermitian positive-definite A.

For full documentation refer to scipy.sparse.linalg.cg.

Parameters:
A{dpnp.ndarray, usm_ndarray, LinearOperator, csr_matrix}

The Hermitian positive-definite operator of shape (N, N).

b{dpnp.ndarray, usm_ndarray}

Right-hand side of the linear system, shape (N,) or (N, 1).

x0{None, dpnp.ndarray, usm_ndarray}, optional

Initial guess for the solution. Default: None (zeros).

rtolfloat, optional

Relative convergence tolerance. Default: 1e-5.

tol{None, float}, optional

Deprecated alias for rtol. Default: None.

maxiter{None, int}, optional

Maximum number of iterations. Default: 10 * N.

M{None, dpnp.ndarray, usm_ndarray, LinearOperator}, optional

Symmetric positive-definite preconditioner. Default: None.

callback{None, callable}, optional

Called as callback(xk) after each iteration. Default: None.

atol{None, float}, optional

Absolute convergence tolerance. Default: None.

Returns:
xdpnp.ndarray

The converged solution.

infoint

info follows the SciPy / CuPy contract:

  • info == 0 : converged successfully

  • info > 0 : did not converge; value is the iteration count at which the solver stopped (equals maxiter when the iteration budget was exhausted, or the iteration index when a numerical breakdown short-circuited the loop).

  • info < 0 : reserved for illegal-input errors; not produced by this implementation (illegal inputs raise ValueError instead).

Previous versions of this routine returned -1 for an rz/pAp breakdown, which violated the SciPy contract and broke user code that branched on info > 0.