[ RDocumentation ](/)

Moon  [ ](https://github.com/datacamp/rdocumentation-2.0) [ Learn R
](https://www.datacamp.com/learn/r)

Search all packages and functions

[ base  (version  3.6.2  ) ](/packages/base/versions/3.6.2)

#  solve: Solve a System of Equations

##  Description

This generic function solves the equation ` a %*% x = b ` for ` x ` , where `
b ` can be either a vector or a matrix.

##  Usage

    
    
    solve(a, b, …)
    
    # S3 method for default
    solve(a, b, tol, LINPACK = FALSE, …)

##  Arguments

a

a square numeric or complex matrix containing the coefficients of the linear
system. Logical matrices are coerced to numeric.

b

a numeric or complex vector or matrix giving the right-hand side(s) of the
linear system. If missing, ` b ` is taken to be an identity matrix and ` solve
` will return the inverse of ` a ` .

tol

the tolerance for detecting linear dependencies in the columns of ` a ` . The
default is ` .Machine$double.eps ` . Not currently used with complex matrices
` a ` .

LINPACK

logical. Defunct and ignored.

…

further arguments passed to or from other methods

##  Details

` a ` or ` b ` can be complex, but this uses double complex arithmetic which
might not be available on all platforms.

The row and column names of the result are taken from the column names of ` a
` and of ` b ` respectively. If ` b ` is missing the column names of the
result are the row names of ` a ` . No check is made that the column names of
` a ` and the row names of ` b ` are equal.

For back-compatibility ` a ` can be a (real) QR decomposition, although ` [
qr.solve ](/link/qr.solve?package=base&version=3.6.2) ` should be called in
that case. ` [ qr.solve ](/link/qr.solve?package=base&version=3.6.2) ` can
handle non-square systems.

Unsuccessful results from the underlying LAPACK code will result in an error
giving a positive error code: these can only be interpreted by detailed study
of the FORTRAN code.

##  References

Anderson. E. and ten others (1999) _LAPACK Users' Guide_ . Third Edition.
SIAM. Available on-line at [ http://www.netlib.org/lapack/lug/lapack_lug.html
](http://www.netlib.org/lapack/lug/lapack_lug.html) .

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) _The New S Language_ .
Wadsworth & Brooks/Cole.

##  See Also

` [ solve.qr ](/link/solve.qr?package=base&version=3.6.2) ` for the ` qr `
method, ` [ chol2inv ](/link/chol2inv?package=base&version=3.6.2) ` for
inverting from the Choleski factor ` [ backsolve
](/link/backsolve?package=base&version=3.6.2) ` , ` [ qr.solve
](/link/qr.solve?package=base&version=3.6.2) ` .

##  Examples

[ Run this code
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Fsolve&utm_source=r-docs&utm_medium=docs&utm_term=solve&utm_content=run_example_in_workspace)

    
    
    # NOT RUN {
    hilbert <- function(n) { i <- 1:n; 1 / outer(i - 1, i, "+") }
    h8 <- hilbert(8); h8
    sh8 <- solve(h8)
    round(sh8 %*% h8, 3)
    
    A <- hilbert(4)
    A[] <- as.complex(A)
    ## might not be supported on all platforms
    try(solve(A))
    # }
    

Run the code above in your browser using  [ DataCamp Workspace
](https://app.datacamp.com/workspace/preview?_tag=rdocs&rdocsPath=packages%2Fbase%2Fversions%2F3.6.2%2Ftopics%2Fsolve&utm_source=r-docs&utm_medium=docs&utm_term=solve&utm_content=run_example_in_workspace)

Powered by  [ DataCamp ](https://www.datacamp.com/)