1 // Copyright ©2015 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/lapack"
12 // Dgeqrf computes the QR factorization of the m×n matrix A using a blocked
13 // algorithm. See the documentation for Dgeqr2 for a description of the
14 // parameters at entry and exit.
16 // work is temporary storage, and lwork specifies the usable memory length.
17 // The length of work must be at least max(1, lwork) and lwork must be -1
18 // or at least n, otherwise this function will panic.
19 // Dgeqrf is a blocked QR factorization, but the block size is limited
20 // by the temporary space available. If lwork == -1, instead of performing Dgeqrf,
21 // the optimal work length will be stored into work[0].
23 // tau must have length at least min(m,n), and this function will panic otherwise.
24 func (impl Implementation) Dgeqrf(m, n int, a []float64, lda int, tau, work []float64, lwork int) {
25 if len(work) < max(1, lwork) {
28 // nb is the optimal blocksize, i.e. the number of columns transformed at a time.
29 nb := impl.Ilaenv(1, "DGEQRF", " ", m, n, -1, -1)
30 lworkopt := n * max(nb, 1)
31 lworkopt = max(n, lworkopt)
33 work[0] = float64(lworkopt)
36 checkMatrix(m, n, a, lda)
45 work[0] = float64(lworkopt)
48 nbmin := 2 // Minimal block size.
49 var nx int // Use unblocked (unless changed in the next for loop)
52 // Only consider blocked if the suggested block size is > 1 and the
53 // number of rows or columns is sufficiently large.
55 // nx is the block size at which the code switches from blocked
57 nx = max(0, impl.Ilaenv(3, "DGEQRF", " ", m, n, -1, -1))
61 // Not enough workspace to use the optimal block
62 // size. Get the minimum block size instead.
64 nbmin = max(2, impl.Ilaenv(2, "DGEQRF", " ", m, n, -1, -1))
71 // Compute QR using a blocked algorithm.
73 if nbmin <= nb && nb < k && nx < k {
74 for i = 0; i < k-nx; i += nb {
76 // Compute the QR factorization of the current block.
77 impl.Dgeqr2(m-i, ib, a[i*lda+i:], lda, tau[i:], work)
79 // Form the triangular factor of the block reflector and apply H^T
80 // In Dlarft, work becomes the T matrix.
81 impl.Dlarft(lapack.Forward, lapack.ColumnWise, m-i, ib,
85 impl.Dlarfb(blas.Left, blas.Trans, lapack.Forward, lapack.ColumnWise,
90 work[ib*ldwork:], ldwork)
94 // Call unblocked code on the remaining columns.
96 impl.Dgeqr2(m-i, n-i, a[i*lda+i:], lda, tau[i:], work)
98 work[0] = float64(lworkopt)