amgcl_solve: Solve a Sparse Linear System

View source: R/amgcl.R

amgcl_solveR Documentation

Solve a Sparse Linear System

Description

Solve Ax = b with AMG-preconditioned conjugate gradients or BiCGStab, either by constructing a solver for one call or reusing an existing solver.

Usage

amgcl_solve(A, b, solver = c("cg", "bicgstab"), tol = 1e-8,
            maxiter = 1000L, threads = 1L)

Arguments

A

A nonempty, square Matrix::dgCMatrix or symmetric Matrix::dsCMatrix with finite entries and a nonzero diagonal entry in every row, or a reusable amgcl_solver created by amgcl_setup.

b

A finite real numeric right-hand-side vector, including an integer vector, of length equal to the matrix dimension. A one-column base numeric matrix or one-column double-precision Matrix object with the same number of rows is also accepted and converted to a vector. Multiple columns, arrays with more than two dimensions, and logical or complex inputs are rejected.

solver

"cg" or "bicgstab". Must be omitted when A is a reusable solver.

tol

Finite positive tolerance for AMGCL's relative residual estimate. Must be omitted when A is a reusable solver.

maxiter

Positive integer iteration limit. Must be omitted when A is a reusable solver.

threads

Positive integer requested thread count for this call, defaulting to one. Capped by the capacity reported by amgcl_info; one thread is used without OpenMP.

Details

Every solve starts from a zero initial guess. Inspect converged and residual before using a result. CG requires a symmetric positive definite matrix; convergence does not validate that assumption. See amgcl_setup for matrix requirements, the direct coarse LU preconditioner used for systems with at most 64 rows, and setup errors.

When A is a matrix, threads controls both setup and solving. When it is a reusable solver, threads controls this solve only; the setup thread count is not retained. OpenMP settings are restored after each operation. See amgcl_setup for thread and object-lifetime details.

Value

A list with components:

x

Numeric solution vector.

iterations

Number of iterations reported by AMGCL.

error

AMGCL's final relative residual estimate.

residual

Independently computed Euclidean residual norm divided by the Euclidean norm of b. For a zero right-hand side, the absolute residual norm is returned.

converged

Logical; whether the independently computed residual is at most tol.

threads

Effective requested thread cap after applying build and capacity limits. This is not a measurement of the worker count used inside each parallel region.

See Also

amgcl_setup, amgcl_info

Examples

A <- methods::as(Matrix::bandSparse(10, k = c(-1, 0, 1),
    diagonals = list(rep(-1, 9), rep(3, 10), rep(-1, 9))), "dgCMatrix")
result <- amgcl_solve(A, rep(1, 10))
result$x
result$residual

# Symmetric sparse storage and a one-column Matrix right-hand side.
S <- Matrix::bandSparse(10, k = c(0, 1),
    diagonals = list(rep(3, 10), rep(-1, 9)), symmetric = TRUE)
rhs <- S %*% rep(1, 10)
amgcl_solve(S, rhs)$x

RcppAMGCL documentation built on Nov. 26, 2026, 5:08 p.m.