Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R(matrix(1, 1, 1)) breaks - bug or desired? #34

Open
JanaJarecki opened this issue May 3, 2020 · 12 comments
Open

R(matrix(1, 1, 1)) breaks - bug or desired? #34

JanaJarecki opened this issue May 3, 2020 · 12 comments

Comments

@JanaJarecki
Copy link

Hi!

Is it desired that a one-dimensional matrix breaks the R() function? If so, maybe an error msg could help?

R(matrix(1, 1, 1))
> Fehler in U[, j] : Indizierung ausserhalb der Grenzen

Kind regards
Jana

@friendly
Copy link
Owner

friendly commented May 4, 2020

Well, matrix(1,1,1) is a scalar, so R() should return 0 here.
matrix(1:3, 1,3) is a 1D matrix or vector, so R() should return 1.

Neither of these cases are presently trapped, so this is a reasonable TODO item

@john-d-fox
Copy link
Collaborator

john-d-fox commented May 4, 2020 via email

@gmonette
Copy link

gmonette commented May 4, 2020 via email

@friendly
Copy link
Owner

friendly commented May 4, 2020

> m <- matrix(1,1,1)
> qr(m)$rank
[1] 1
> qr(1:3)$rank
[1] 1

So, perhaps we should follow qr()

@john-d-fox
Copy link
Collaborator

john-d-fox commented May 4, 2020 via email

@john-d-fox
Copy link
Collaborator

john-d-fox commented May 4, 2020 via email

@gmonette
Copy link

gmonette commented May 4, 2020 via email

@john-d-fox
Copy link
Collaborator

john-d-fox commented May 4, 2020 via email

@friendly
Copy link
Owner

friendly commented May 5, 2020

R(X) is just short for QR(X)$rank. And recall that QR() was designed to make the computation visible to inspection, both at the code and computation levels.

A simple fix to QR() avoids this problem of R() generating an error when ncol{X} < 2.

  if (ncol(U)>1) {                 # trap potential error
    for (j in 2:ncol(U)){
      for (k in 1:(j - 1)){
        U[, j] <- U[, j] - as.vector(X[, j] %*% E[, k]) * E[, k]
      }
      len.U.j <- length(U[, j])
      if (len.U.j > tol) E[, j] <- U[, j]/len.U.j
    }
  }

But I don't think this is a general solution, at least if we want QR to match qr in such edge cases.

> QR(matrix(1:3,1,3))
$Q
     [,1] [,2] [,3]
[1,]   -1    0    0

$R
     [,1] [,2] [,3]
[1,]   -1   -2   -3
[2,]    0    0    0
[3,]    0    0    0

$rank
[1] 1

> qr(matrix(1:3,1,3))
$qr
     [,1] [,2] [,3]
[1,]    1    2    3

$rank
[1] 1

@john-d-fox
Copy link
Collaborator

john-d-fox commented May 5, 2020 via email

@gmonette
Copy link

gmonette commented May 5, 2020 via email

@john-d-fox
Copy link
Collaborator

john-d-fox commented May 5, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants