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/lapack"
12 // Dgerqf computes an RQ factorization of the m×n matrix A,
14 // On exit, if m <= n, the upper triangle of the subarray
15 // A[0:m, n-m:n] contains the m×m upper triangular matrix R.
16 // If m >= n, the elements on and above the (m-n)-th subdiagonal
17 // contain the m×n upper trapezoidal matrix R.
18 // The remaining elements, with tau, represent the
19 // orthogonal matrix Q as a product of min(m,n) elementary
22 // The matrix Q is represented as a product of elementary reflectors
23 // Q = H_0 H_1 . . . H_{min(m,n)-1}.
24 // Each H(i) has the form
25 // H_i = I - tau_i * v * v^T
26 // where v is a vector with v[0:n-k+i-1] stored in A[m-k+i, 0:n-k+i-1],
27 // v[n-k+i:n] = 0 and v[n-k+i] = 1.
29 // tau must have length min(m,n), work must have length max(1, lwork),
30 // and lwork must be -1 or at least max(1, m), otherwise Dgerqf will panic.
31 // On exit, work[0] will contain the optimal length for work.
33 // Dgerqf is an internal routine. It is exported for testing purposes.
34 func (impl Implementation) Dgerqf(m, n int, a []float64, lda int, tau, work []float64, lwork int) {
35 checkMatrix(m, n, a, lda)
37 if len(work) < max(1, lwork) {
40 if lwork != -1 && lwork < max(1, m) {
53 nb = impl.Ilaenv(1, "DGERQF", " ", m, n, -1, -1)
56 work[0] = float64(lwkopt)
62 // Return quickly if possible.
72 // Determine when to cross over from blocked to unblocked code.
73 nx = max(0, impl.Ilaenv(3, "DGERQF", " ", m, n, -1, -1))
75 // Determine whether workspace is large enough for blocked code.
78 // Not enough workspace to use optimal nb. Reduce
79 // nb and determine the minimum value of nb.
81 nbmin = max(2, impl.Ilaenv(2, "DGERQF", " ", m, n, -1, -1))
88 if nbmin <= nb && nb < k && nx < k {
89 // Use blocked code initially.
90 // The last kk rows are handled by the block method.
91 ki := ((k - nx - 1) / nb) * nb
95 for i = k - kk + ki; i >= k-kk; i -= nb {
98 // Compute the RQ factorization of the current block
99 // A[m-k+i:m-k+i+ib-1, 0:n-k+i+ib-1].
100 impl.Dgerq2(ib, n-k+i+ib, a[(m-k+i)*lda:], lda, tau[i:], work)
102 // Form the triangular factor of the block reflector
103 // H = H_{i+ib-1} . . . H_{i+1} H_i.
104 impl.Dlarft(lapack.Backward, lapack.RowWise,
105 n-k+i+ib, ib, a[(m-k+i)*lda:], lda, tau[i:],
108 // Apply H to A[0:m-k+i-1, 0:n-k+i+ib-1] from the right.
109 impl.Dlarfb(blas.Right, blas.NoTrans, lapack.Backward, lapack.RowWise,
110 m-k+i, n-k+i+ib, ib, a[(m-k+i)*lda:], lda,
113 work[ib*ldwork:], ldwork)
123 // Use unblocked code to factor the last or only block.
124 if mu > 0 && nu > 0 {
125 impl.Dgerq2(mu, nu, a, lda, tau, work)
127 work[0] = float64(iws)