[R] What solve() does?

Paul Johnson
Sat, 04 Sep 2010 18:23:05 -0000

On Wed, Sep 1, 2010 at 5:36 AM, Petar Milin <pmilin at ff.uns.ac.rs>
wrote:
> Hello!
> Can anyone explain me what solve() function does: Gaussian elimination or
> iterative, numeric solve? In addition, I would need both the Gaussian
> elimination and iterative solution for the course. Are the two built in R?
>
> Thanks!

> PM

Hello, Petar:

I think you are assuming that solve uses an elementary linear algebra
"paper and pencil" procedure, but I don't think it does.  In a
digital
computer, those things are not precise, and I think the folks here
will even say you shouldn't use solve to get an inverse, but I can't
remember all of the details.

To see how solve works ...

Let me show you a trick I just learned. Read

?solve

notice it is a "generic method", meaning it does not actually do the
calculations for you. Rather, there are specific implementations for
different types of cases. To find the implementations, run

methods(solve)

I get:

> methods(solve)
[1] solve.default solve.qr

Then if you want to read HOW solve does what it does (which I think
was your question), run this:

> solve.default

or

> solve.qr

In that code, you will see the chosen procedure depends on the linear
algebra libraries you make available.  I'm no expert on the details,
but it appears QR decomposition is the preferred method.  You can read
about that online or in numerical algebra books.



--
Paul E. Johnson
Professor, Political Science
1541 Lilac Lane, Room 504
University of Kansas