\HeaderA{solve}{Solve a System of Equations}{solve}
\methaliasA{solve.default}{solve}{solve.default}
\keyword{algebra}{solve}
\begin{Description}\relax
This generic function solves the equation \code{a \%*\% x = b} for \code{x},
where \code{b} can be either a vector or a matrix.
\end{Description}
\begin{Usage}
\begin{verbatim}
solve(a, b, ...)

## Default S3 method:
solve(a, b, tol, LINPACK = FALSE, ...)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{a}] a square numeric or complex matrix containing the coefficients of
the linear system.
\item[\code{b}] a numeric or complex vector or matrix giving the right-hand
side(s) of the linear system.  If missing, \code{b} is taken to be
an identity matrix and \code{solve} will return the inverse of \code{a}.
\item[\code{tol}] the tolerance for detecting linear dependencies in the
columns of \code{a}.  If \code{LINPACK} is \code{TRUE} the default
is \code{1e-7}, otherwise it is \code{.Machine\$double.eps}. Future
versions of R may use a tighter tolerance. Not presently used with
complex matrices \code{a}.
\item[\code{LINPACK}] logical. Should LINPACK be used (for compatibility with
\R{} < 1.7.0)?  Otherwise LAPACK is used.
\item[\code{...}] further arguments passed to or from other methods
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
\code{a} or \code{b} can be complex, but this uses double complex
arithmetic which might not be available on all platforms and LAPACK
will always be used.

The row and column names of the result are taken from the column
names of \code{a} and of \code{b} respectively.  As from \R{} 1.7.0
if \code{b} is missing the column names of the result are the row
names of \code{a}.  No check is made that the column names of \code{a}
and the row names of \code{b} are equal.

For back-compatibility \code{a} can be a (real) QR decomposition,
although \code{\LinkA{qr.solve}{qr.solve}} should be called in that case.
\code{\LinkA{qr.solve}{qr.solve}} can handle non-square systems.
\end{Details}
\begin{References}\relax
Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988)
\emph{The New S Language}.
Wadsworth \& Brooks/Cole.
\end{References}
\begin{SeeAlso}\relax
\code{\LinkA{solve.qr}{solve.qr}} for the \code{qr} method,
\code{\LinkA{chol2inv}{chol2inv}} for inverting from the Choleski factor
\code{\LinkA{backsolve}{backsolve}}, \code{\LinkA{qr.solve}{qr.solve}}.
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}
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))
\end{ExampleCode}
\end{Examples}

