1

I have a matrix A1 and I want to calculate it's null space.

A1=matrix(c(1,1,2,3,2,1,1,3,1,4),nrow=2,ncol=5,byrow =TRUE)



    [,1] [,2] [,3] [,4] [,5]
[1,]    1    1    2    3    2
[2,]    1    1    3    1    4

I have using the function 'nullspace' from the package 'pracma'.

nullspace(matrix(c(1,1,2,3,2,1,1,3,1,4),nrow=2,ncol=5,byrow =TRUE))

and the result I am getting is

            [,1]        [,2]       [,3]
[1,] -0.53530338 -0.53530338 -0.6117753
[2,]  0.23344278 -0.76655722  0.5525060
[3,]  0.68910778  0.02244112 -0.4505435
[4,] -0.07744944  0.25588390  0.1019625
[5,] -0.42200333  0.24466334  0.3272343

But when I am solving it manually, the result I am getting is

            [,1]        [,2]       [,3]
[1,]         -1          -7          2
[2,]          1           0          0
[3,]          0           2         -2
[4,]          0           1          0
[5,]          0           0          1

How can I get the second answer using R codes? I cant find any package for this.

Parush Arora
  • 11
  • 1
  • 2
  • Nullspace results would not be expected to be unique. Have you demonstrated that these two results define distinct spaces? I think not. – IRTFM Sep 14 '18 at 15:20
  • I saw it on this video https://www.khanacademy.org/math/linear-algebra/vectors-and-spaces/null-column-space/v/dimension-of-the-null-space-or-nullity – Parush Arora Sep 15 '18 at 16:47
  • No.. these results define the same null space. Just wanted to know if I can get the answer like I calculated manually or given in the video. – Parush Arora Sep 15 '18 at 16:50

2 Answers2

2

Have a close read on this Q & A: Solve homogenous system Ax = 0 for any m * n matrix A in R (find null space basis for A).

The NullSpace function in the answer does exactly what you are look for.

## Your matrix
A1 <- matrix(c(1,1,2,3,2,1,1,3,1,4),nrow=2,ncol=5,byrow =TRUE)

## get NullSpace from the linked Q & A yourself

## call NullSpace
X <- NullSpace(A1)

round(X, 15)
#     [,1] [,2] [,3]
#[1,]   -7    2   -1
#[2,]    0    0    1
#[3,]    2   -2    0
#[4,]    1    0    0
#[5,]    0    1    0
RandomUser
  • 21
  • 1
1

The representation of the nullspace as column vectors is not unique so the fact that they are not the same set of vectors does not mean they represent different spaces.

In fact, the two solutions represent the same vector space. Converting both to orthogonal projections we see that they are the same so they project onto the same space hence m1 and m2 span the same space.

library(pracma)

# manual
Lines <- "
     -1          -7          2
      1           0          0
      0           2         -2
      0           1          0
      0           0          1"
m1 <- as.matrix(read.table(text = Lines))
colnames(m1) <- NULL

# pracma
m2 <- nullspace(A1)

Projections

# projections
P1 <- m1 %*% solve(crossprod(m1)) %*% t(m1)
P2 <- m2 %*% solve(crossprod(m2)) %*% t(m2)

all.equal(P1, P2)
## [1] TRUE

We can also calculate the projection directly from A1 and verify that it is equal to these as well (using the fact that the perp of the space spanned by the range of the transpose of a matrix spans that matrix's nullspace):

P0 <- diag(5) - t(A1) %*% solve(crossprod(t(A1))) %*% A1
all.equal(P0, P1)
## [1] TRUE

Reduced Row Echelon Form

The reduced row echelon form (rref in pracma) of the transpose of m1 and m2 are also unique and the rowspace of each spans the nullspace of A1 since the rowspaces of t(m1) and t(m2) do.

all.equal(rref(t(m1)), rref(t(m2)), check.attributes = FALSE)
## [1] TRUE

A1 %*% t(rref(t(m1)))  # gives 0 
##              [,1]         [,2] [,3]
## [1,] 5.551115e-17 5.551115e-17    0
## [2,] 1.110223e-16 1.110223e-16    0

The common value can be computed via:

t(rref(t(m1)))

giving:

     [,1] [,2] [,3]
[1,]  1.0  0.0  0.0
[2,]  0.0  1.0  0.0
[3,]  0.0  0.0  1.0
[4,] -0.2 -0.2 -0.2
[5,] -0.2 -0.2 -0.7

Converting m2 to m1

In the comments it is asked to convert m2 into m1. It appears that m1 is the rref form but with rows and columns reversed so defining a function which reverses both and then transposes we have:

revt <- function(m) t(m[nrow(m):1, ncol(m):1])
all.equal(revt(rref(revt(m2))), m1, check.attributes = FALSE)
## [1] TRUE
G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • Thankyou for this elaborate answer. I understood quite a things from it. But I wanted to show my base variables in terms of free variables. Through m1, it is quite easy to show. So is there any way to convert m2 directly into m1? – Parush Arora Sep 15 '18 at 16:38
  • See added sections. – G. Grothendieck Sep 16 '18 at 04:32