solve_cg: Iteratively Solve a Linear System with Conjugate Gradient

View source: R/RcppExports.R

solve_cgR Documentation

Iteratively Solve a Linear System with Conjugate Gradient

Description

Solve the system l(x) = b where l(x) is a matrix-free representation of the linear operation Ax.

Usage

solve_cg(l, b, init, args)

Arguments

l

A linear transformation of x.

b

A vector.

init

Initial value of solution.

args

List of additional arguments from cg_args.

Value

A list with the form of a solve_cg_result described in section "Conjugate Gradient" of the package vignette.

Examples

set.seed(1234)

n = 8
idx_diag = cbind(1:n, 1:n)
idx_ldiag = cbind(2:n, 1:(n-1))
idx_udiag = cbind(1:(n-1), 2:n)
b = rep(1, n)

## Solution by explicit computation of solve(A, b)
A = matrix(0, n, n)
A[idx_diag] = 2
A[idx_ldiag] = 1
A[idx_udiag] = 1
solve(A, b)

## Solve iteratively with solve_cg
f = function(x) { A %*% x }
args = cg_args()
init = rep(0, n)
solve_cg(f, b, init, args)


fntl documentation built on April 4, 2025, 1:53 a.m.