Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h1>Introduction</h1> <p>R uses the <a href="http://www.netlib.org/linpack/" rel="noreferrer">LINPACK</a> <a href="http://www.netlib.org/linpack/dqrdc.f" rel="noreferrer"><code>dqrdc</code></a> routine, by default, or the <a href="http://www.netlib.org/lapack/" rel="noreferrer">LAPACK</a> <a href="http://www.netlib.org/lapack/double/dgeqp3.f" rel="noreferrer"><code>DGEQP3</code></a> routine, when specified, for computing the QR decomposition. Both routines compute the decomposition using Householder reflections. An m x n matrix A is decomposed into an m x n economy-size orthogonal matrix (Q) and an n x n upper triangular matrix (R) as A = QR, where Q can be computed by the product of t Householder reflection matrices, with t being the lesser of m-1 and n: Q = H<sub>1</sub>H<sub>2</sub>...H<sub>t</sub>.</p> <p>Each reflection matrix H<sub>i</sub> can be represented by a length-(m-i+1) vector. For example, H<sub>1</sub> requires a length-m vector for compact storage. All but one entry of this vector is placed in the first column of the lower triangle of the input matrix (the diagonal is used by the R factor). Therefore, each reflection needs one more scalar of storage, and this is provided by an auxiliary vector (called <code>$qraux</code> in the result from R's <code>qr</code>).</p> <p><em>The compact representation used is different between the LINPACK and LAPACK routines.</em></p> <h2>The LINPACK Way</h2> <p>A Householder reflection is computed as H<sub>i</sub> = I - v<sub>i</sub>v<sub>i</sub><sup>T</sup>/p<sub>i</sub>, where I is the identity matrix, p<sub>i</sub> is the corresponding entry in <code>$qraux</code>, and v<sub>i</sub> is as follows:</p> <ul> <li>v<sub>i</sub>[1..i-1] = 0,</li> <li>v<sub>i</sub>[i] = p<sub>i</sub></li> <li>v<sub>i</sub>[i+1:m] = A[i+1..m, i] (i.e., a column of the lower triangle of A after calling <code>qr</code>)</li> </ul> <h2>LINPACK Example</h2> <p>Let's work through the <a href="http://en.wikipedia.org/wiki/QR_decomposition#Example_2" rel="noreferrer">example from the QR decomposition article</a> at Wikipedia in R.</p> <p>The matrix being decomposed is</p> <pre><code>&gt; A &lt;- matrix(c(12, 6, -4, -51, 167, 24, 4, -68, -41), nrow=3) &gt; A [,1] [,2] [,3] [1,] 12 -51 4 [2,] 6 167 -68 [3,] -4 24 -41 </code></pre> <p>We do the decomposition, and the most relevant portions of the result is shown below:</p> <pre><code>&gt; Aqr = qr(A) &gt; Aqr $qr [,1] [,2] [,3] [1,] -14.0000000 -21.0000000 14 [2,] 0.4285714 -175.0000000 70 [3,] -0.2857143 0.1107692 -35 [snip...] $qraux [1] 1.857143 1.993846 35.000000 [snip...] </code></pre> <p>This decomposition was done (under the covers) by computing two Householder reflections and multiplying them by A to get R. We will now recreate the reflections from the information in <code>$qr</code>.</p> <pre><code>&gt; p = Aqr$qraux # for convenience &gt; v1 &lt;- matrix(c(p[1], Aqr$qr[2:3,1])) &gt; v1 [,1] [1,] 1.8571429 [2,] 0.4285714 [3,] -0.2857143 &gt; v2 &lt;- matrix(c(0, p[2], Aqr$qr[3,2])) &gt; v2 [,1] [1,] 0.0000000 [2,] 1.9938462 [3,] 0.1107692 &gt; I = diag(3) # identity matrix &gt; H1 = I - v1 %*% t(v1)/p[1] # I - v1*v1^T/p[1] &gt; H2 = I - v2 %*% t(v2)/p[2] # I - v2*v2^T/p[2] &gt; Q = H1 %*% H2 &gt; Q [,1] [,2] [,3] [1,] -0.8571429 0.3942857 0.33142857 [2,] -0.4285714 -0.9028571 -0.03428571 [3,] 0.2857143 -0.1714286 0.94285714 </code></pre> <p>Now let's verify the Q computed above is correct:</p> <pre><code>&gt; qr.Q(Aqr) [,1] [,2] [,3] [1,] -0.8571429 0.3942857 0.33142857 [2,] -0.4285714 -0.9028571 -0.03428571 [3,] 0.2857143 -0.1714286 0.94285714 </code></pre> <p>Looks good! We can also verify QR is equal to A.</p> <pre><code>&gt; R = qr.R(Aqr) # extract R from Aqr$qr &gt; Q %*% R [,1] [,2] [,3] [1,] 12 -51 4 [2,] 6 167 -68 [3,] -4 24 -41 </code></pre> <h2>The LAPACK Way</h2> <p>A Householder reflection is computed as H<sub>i</sub> = I - p<sub>i</sub>v<sub>i</sub>v<sub>i</sub><sup>T</sup>, where I is the identity matrix, p<sub>i</sub> is the corresponding entry in <code>$qraux</code>, and v<sub>i</sub> is as follows:</p> <ul> <li>v<sub>i</sub>[1..i-1] = 0,</li> <li>v<sub>i</sub>[i] = 1</li> <li>v<sub>i</sub>[i+1:m] = A[i+1..m, i] (i.e., a column of the lower triangle of A after calling <code>qr</code>)</li> </ul> <p>There is another twist when using the LAPACK routine in R: column pivoting is used, so the decomposition is solving a different, related problem: AP = QR, where P is a <a href="http://en.wikipedia.org/wiki/Permutation_matrix" rel="noreferrer">permutation matrix</a>.</p> <h2>LAPACK Example</h2> <p>This section does the same example as before.</p> <pre><code>&gt; A &lt;- matrix(c(12, 6, -4, -51, 167, 24, 4, -68, -41), nrow=3) &gt; Bqr = qr(A, LAPACK=TRUE) &gt; Bqr $qr [,1] [,2] [,3] [1,] 176.2554964 -71.1694118 1.668033 [2,] -0.7348557 35.4388886 -2.180855 [3,] -0.1056080 0.6859203 -13.728129 [snip...] $qraux [1] 1.289353 1.360094 0.000000 $pivot [1] 2 3 1 attr(,"useLAPACK") [1] TRUE [snip...] </code></pre> <p>Notice the <code>$pivot</code> field; we will come back to that. Now we generate Q from the information the <code>Aqr</code>.</p> <pre><code>&gt; p = Bqr$qraux # for convenience &gt; v1 = matrix(c(1, Bqr$qr[2:3,1])) &gt; v1 [,1] [1,] 1.0000000 [2,] -0.7348557 [3,] -0.1056080 &gt; v2 = matrix(c(0, 1, Bqr$qr[3,2])) &gt; v2 [,1] [1,] 0.0000000 [2,] 1.0000000 [3,] 0.6859203 &gt; H1 = I - p[1]*v1 %*% t(v1) # I - p[1]*v1*v1^T &gt; H2 = I - p[2]*v2 %*% t(v2) # I - p[2]*v2*v2^T &gt; Q = H1 %*% H2 [,1] [,2] [,3] [1,] -0.2893527 -0.46821615 -0.8348944 [2,] 0.9474882 -0.01602261 -0.3193891 [3,] 0.1361660 -0.88346868 0.4482655 </code></pre> <p>Once again, the Q computed above agrees with the R-provided Q.</p> <pre><code>&gt; qr.Q(Bqr) [,1] [,2] [,3] [1,] -0.2893527 -0.46821615 -0.8348944 [2,] 0.9474882 -0.01602261 -0.3193891 [3,] 0.1361660 -0.88346868 0.4482655 </code></pre> <p>Finally, let's compute QR.</p> <pre><code>&gt; R = qr.R(Bqr) &gt; Q %*% R [,1] [,2] [,3] [1,] -51 4 12 [2,] 167 -68 6 [3,] 24 -41 -4 </code></pre> <p>Notice the difference? QR is A with its columns permuted given the order in <code>Bqr$pivot</code> above.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload