solve2: Solve Systems of Equations

View source: R/solve.R

solve2R Documentation

Solve Systems of Equations

Description

Solve systems of equations Ax = b using an automatically chosen direct method (see solve_chol). Methods are chosen for speed at reasonable accuracy. Please choose a suitable method manually if numerical stability is the main consideration.

Usage

solve2(a, b, ...)

## Default S3 method:
solve2(a, b, ...)

## S3 method for class 'sanic_lu'
solve2(a, b, ...)

Arguments

a

Numeric matrix with the coefficients of the linear system, dense or sparse (see sparsify), or a sanic_lu factorization from factorize_lu.

b

Numeric vector or matrix at the right-hand side of the linear system. If missing, 'b' is set to an identity matrix and 'a' is inverted.

...

Dispatched to methods in the solvers.

Value

Solves for x and returns a numeric matrix with the results.

Examples

set.seed(42)
x <- rnorm(3)

# Solve using a general matrix
A <- matrix(rnorm(9), nrow = 3, ncol = 3)
b <- A %*% x
norm(solve2(A, b) - x)

# Solve using a symmetric matrix
A <- crossprod(matrix(rnorm(9), nrow = 3, ncol = 3))
b <- A %*% x
norm(solve2(A, b) - x)

# Solve using a square matrix
A <- matrix(rnorm(12), nrow = 4, ncol = 3)
b <- A %*% x
norm(solve2(A, b) - x)

# Solve reusing an LU factorization
A <- matrix(rnorm(9), nrow = 3, ncol = 3)
f <- factorize_lu(A)
norm(solve2(f, A %*% x) - x)


sanic documentation built on Oct. 25, 2026, 5:06 p.m.