Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8382273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:53:37+00:00 2026-06-09T16:53:37+00:00

I want to do a QR decomposition with the Matrix:::qr() function on a Matrix

  • 0

I want to do a QR decomposition with the Matrix:::qr() function on a Matrix that I created with B<-as(A, "sparseMatrix"). I know that I can get the R matrix with Matrix:::qr.R(). However, I also need to the Q Matrix. There seems to be no qr.Q() function in the Matrix package. How do I get the Q matrix?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T16:53:38+00:00Added an answer on June 9, 2026 at 4:53 pm

    The Q matrix is actually stored in the V slot. It seems that the current R Matrix version contains a bug — it just mysteriously adds zero rows into the matrix a before doing the qr decomposition. I wish the developers could come and explain it. Therefore the following codes help you recover both R and Q:

    gx.qr.Q <- function(a){
      if(is(a, "qr")){
        return(qr.Q(a, complete = T))
      }else if(is(a, "sparseQR")){
        Q <- diag(nrow = a@V@Dim[1], ncol = a@V@Dim[1])
        for(i in rev(1:a@V@Dim[2])){
          Q <- Q - (a@V[ ,i] * a@beta[i]) %*% (a@V[ ,i] %*% Q)
        }
        return(Q[order(a@p), ][1:a@Dim[1], 1:a@Dim[1]])
      }else{
        stop(gettextf("gx.qr.Q() fails on class '%s'", class(a)[1]))
      }
    }
    
    gx.qr.R <- function(a){
      if(is(a, "qr")){
        return(qr.R(a, complete = T)[ ,order(a$pivot)])
      }else if(is(a, "sparseQR")){
        if(length(a@q) == 0){
          return(a@R[1:a@Dim[1], ])
        }else{
          return(a@R[1:a@Dim[1] ,order(a@q)])
        }
      }else{
        stop(gettextf("gx.qr.R() fails on class '%s'", class(a)[1]))
      }
    }
    

    I have tested by randomly set the matrix size and sparsity and they work smoothly. However this is of the style “just make it work without knowing why”, and is posted here only for discussion. Because I have not hacked into the implementation details of the package “Matrix”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Want to know if there is a difference between the both. Explanation would be
Want to know if it is possible to get different URL paths to redirect
want to ask about void type, so I could know that it works or
Want to run javascript function from parent window in child window Example I have
want to know why String behaves like value type while using ==. String s1
want to have a Hyperlink-Button in a gridView in which I can display a
Want the function to sort the table by HP but if duplicate HPs then
want to ask user to input something but not want to wait forever. There
for example, there is a matrix: import numpy as np A = np.array([[ 8.
want to create a temporary table that has an auto_increment field plus a field

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.