# [R] Solving Matrices

Richard A. O'Keefe ok at cs.otago.ac.nz
Fri Apr 16 02:08:51 CEST 2004

```Elizabeth (etb <lizzy at noradd.org>) wrote:
In execises 39-42, determine if the columns of the matrix span
R4:
4
Presumably that's R, 4-dimensional real space.

(or x <- matrix(data=c(7, -5, 6, -7, 2, -3, 10, 9, -5,
4, -2, 2, 8, -9, 7, 15), nrow=4, ncol=4)

That is the whole of the question and I suppose that the way to answer
this is by determining if:

1. For each b in Rm, the equation Ax = b has a solution, or
2. A has a pivot position in every row,

where A is an (m X n) matrix.

Another way to look at this is that it's a question about the rank of the
matrix.  The rank of an mxn matrix is at most min(m,n).  Unfortunately,
help("rank") doesn't tell you about matrix rank, but about something else.

Possibly the simplest method is to look at eign(x, only.values=TRUE)
and see how many of the eigenvalues are non-zero.

> eigen(x, only.values=TRUE)
\$values
  9.275635e+00+8.169494i  9.275635e+00-8.169494i -1.551270e+00+0.000000i
 -1.603246e-14+0.000000i

\$vectors
NULL

We see that this matrix has a pair of conjugate eigenvalues
9.28 +/- 8.17 i
and two real eigenvalues
-1.55
-1.6e-14

The smallest eigenvalue is pretty close to 0
Try again, this time asking for the eigenvectors:

vv <- eigen(x)\$vectors

Check what happens when you multiply the eigenvector corresponding to
the smallest eigenvalue by the matrix:

> print(v1 <- as.real(vv[,4]))
 -0.4172502  0.2377877 -0.8479600 -0.2243281
> print(v2 <- as.vector(x %*% v1))
  4.884981e-15 -2.664535e-15  8.437695e-15 -5.329071e-15

So x doesn't _precisely_ map that vector to 0, but it's close enough
for government work.

For another look at this, try the singular-value decomposition.
?svd

> svd(x)\$d
 2.436185e+01 1.376648e+01 6.163148e+00 9.241655e-16

Again, we see a pretty strong hint that the matrix is close to rank 3.

Finally, look at help(qr).  This is the most direct way of finding the
rank of a matrix.

> qr(x)\$rank
 3

So here are three different functions all saying much the same thing
about x:  it is numerically close to a matrix which does NOT span the
whole of R**4.

```