1 // Copyright ©2017 The Gonum Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
8 "gonum.org/v1/gonum/blas"
9 "gonum.org/v1/gonum/blas/blas64"
12 // Dgeqp3 computes a QR factorization with column pivoting of the
13 // m×n matrix A: A*P = Q*R using Level 3 BLAS.
15 // The matrix Q is represented as a product of elementary reflectors
16 // Q = H_0 H_1 . . . H_{k-1}, where k = min(m,n).
17 // Each H_i has the form
18 // H_i = I - tau * v * v^T
19 // where tau and v are real vectors with v[0:i-1] = 0 and v[i] = 1;
20 // v[i:m] is stored on exit in A[i:m, i], and tau in tau[i].
22 // jpvt specifies a column pivot to be applied to A. If
23 // jpvt[j] is at least zero, the jth column of A is permuted
24 // to the front of A*P (a leading column), if jpvt[j] is -1
25 // the jth column of A is a free column. If jpvt[j] < -1, Dgeqp3
26 // will panic. On return, jpvt holds the permutation that was
27 // applied; the jth column of A*P was the jpvt[j] column of A.
28 // jpvt must have length n or Dgeqp3 will panic.
30 // tau holds the scalar factors of the elementary reflectors.
31 // It must have length min(m, n), otherwise Dgeqp3 will panic.
33 // work must have length at least max(1,lwork), and lwork must be at least
34 // 3*n+1, otherwise Dgeqp3 will panic. For optimal performance lwork must
35 // be at least 2*n+(n+1)*nb, where nb is the optimal blocksize. On return,
36 // work[0] will contain the optimal value of lwork.
38 // If lwork == -1, instead of performing Dgeqp3, only the optimal value of lwork
39 // will be stored in work[0].
41 // Dgeqp3 is an internal routine. It is exported for testing purposes.
42 func (impl Implementation) Dgeqp3(m, n int, a []float64, lda int, jpvt []int, tau, work []float64, lwork int) {
48 checkMatrix(m, n, a, lda)
53 for _, v := range jpvt {
55 panic("lapack: jpvt element out of range")
59 if len(work) < max(1, lwork) {
63 var iws, lwkopt, nb int
69 nb = impl.Ilaenv(inb, "DGEQRF", " ", m, n, -1, -1)
70 lwkopt = 2*n + (n+1)*nb
72 work[0] = float64(lwkopt)
82 bi := blas64.Implementation()
84 // Move initial columns up front.
86 for j := 0; j < n; j++ {
92 bi.Dswap(m, a[j:], lda, a[nfxd:], lda)
93 jpvt[j], jpvt[nfxd] = jpvt[nfxd], j
100 // Factorize nfxd columns.
102 // Compute the QR factorization of nfxd columns and update remaining columns.
105 impl.Dgeqrf(m, na, a, lda, tau, work, lwork)
106 iws = max(iws, int(work[0]))
108 impl.Dormqr(blas.Left, blas.Trans, m, n-na, na, a, lda, tau[:na], a[na:], lda,
110 iws = max(iws, int(work[0]))
115 work[0] = float64(iws)
119 // Factorize free columns.
122 sminmn := minmn - nfxd
124 // Determine the block size.
125 nb = impl.Ilaenv(inb, "DGEQRF", " ", sm, sn, -1, -1)
129 if 1 < nb && nb < sminmn {
130 // Determine when to cross over from blocked to unblocked code.
131 nx = max(0, impl.Ilaenv(ixover, "DGEQRF", " ", sm, sn, -1, -1))
134 // Determine if workspace is large enough for blocked code.
135 minws := 2*sn + (sn+1)*nb
136 iws = max(iws, minws)
138 // Not enough workspace to use optimal nb. Reduce
139 // nb and determine the minimum value of nb.
140 nb = (lwork - 2*sn) / (sn + 1)
141 nbmin = max(2, impl.Ilaenv(inbmin, "DGEQRF", " ", sm, sn, -1, -1))
146 // Initialize partial column norms.
147 // The first n elements of work store the exact column norms.
148 for j := nfxd; j < n; j++ {
149 work[j] = bi.Dnrm2(sm, a[nfxd*lda+j:], lda)
153 if nbmin <= nb && nb < sminmn && nx < sminmn {
154 // Use blocked code initially.
156 // Compute factorization.
158 for topbmn := minmn - nx; j < topbmn; j += fjb {
159 jb := min(nb, topbmn-j)
161 // Factorize jb columns among columns j:n.
162 fjb = impl.Dlaqps(m, n-j, j, jb, a[j:], lda, jpvt[j:], tau[j:],
163 work[j:n], work[j+n:2*n], work[2*n:2*n+jb], work[2*n+jb:], jb)
167 // Use unblocked code to factor the last or only block.
169 impl.Dlaqp2(m, n-j, j, a[j:], lda, jpvt[j:], tau[j:],
170 work[j:n], work[j+n:2*n], work[2*n:])
173 work[0] = float64(iws)