R independent columns in the matrix

I am trying to find independent columns to solve a system of linear equations. Here is my simplified example:

> mat = matrix(c(1,0,0,0,-1,1,0,0,0,-1,1,0,0,0,-1,0,-1,0,0,1,0,0,1,-1), nrow=4, ncol=6, dimnames=list(c("A", "B", "C", "D"), paste("v", 1:6, sep="")))
> mat
  v1 v2 v3 v4 v5 v6
A  1 -1  0  0 -1  0
B  0  1 -1  0  0  0
C  0  0  1 -1  0  1
D  0  0  0  0  1 -1

The matrix has a full rank:

qr(mat)$rank

gives me 4, and since there are 6 columns, there should be 6-4 = 2 independent columns from which I can calculate the rest. I know that v4 and v6 columns are independent ... My first question is: how can I find these columns (possibly with qr (mat) $ pivot)?

Rearranging linear equations on paper, I see that [v1, v2, v3, v4, v5, v6] = [v4, v4-v6, v4-v6, v4, v4, v6, v6]

and, therefore, I can find from arbitrary values ​​for v4 and v6 a vector lying in zero space by multiplying v4 and v6 by the vectors below:

v4 * [1,1,1,1,0,0] + v6 * [0,-1,-1,0,1,1]

My second question: how to find these vectors, which means how to solve the matrix for v4 and v6? for instance

qr.solve(mat, cbind(c(0,0,0,0), c(0,0,0,0)))

gives me two vectors of length 6 with zeros.

, !

-H -

0
2

, :

q <- qr(mat)

mmat <- mat[,q$pivot[seq(q$rank)]]

mmat
##   v1 v2 v3 v5
## A  1 -1  0 -1
## B  0  1 -1  0
## C  0  0  1  0
## D  0  0  0  1

qr(mmat)$rank
## [1] 4

? pivot QR.Auxiliaries {base} ?qr.Q. :

qr.R returns R. This may be pivoted, e.g., if a <- qr(x) then x[, a$pivot] = QR.
The number of rows of R is either nrow(X) or ncol(X) (and may depend on whether
complete is TRUE or FALSE).

. , 0 , q$rank q$pivot ( , Q 4x4).

QR.Auxiliaries {base} :

pivI <- sort.list(a$pivot) # the inverse permutation
stopifnot(
 all.equal(x[, a$pivot], qr.Q(a) %*% qr.R(a)),          # TRUE
 all.equal(x           , qr.Q(a) %*% qr.R(a)[, pivI]))  # TRUE too!
+3

v4 v6, 2 inrows 1 2, v1 v2 v3. , .

> qr(mat[, c(1,2,4,6)])$rank
[1] 4
> qr(mat[, c(1,2,3,5)])$rank
[1] 4
> qr(mat[, c(1,3,4,6)])$rank
[1] 4

, " " . , , , , , .

, :

> qr(mat[, c(1,2,3,4)])$rank
[1] 3
0

Source: https://habr.com/ru/post/1661749/


All Articles