gonum.org/v1/gonum@v0.14.0/lapack/gonum/dgerq2.go (about) 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. 4 5 package gonum 6 7 import "gonum.org/v1/gonum/blas" 8 9 // Dgerq2 computes an RQ factorization of the m×n matrix A, 10 // 11 // A = R * Q. 12 // 13 // On exit, if m <= n, the upper triangle of the subarray 14 // A[0:m, n-m:n] contains the m×m upper triangular matrix R. 15 // If m >= n, the elements on and above the (m-n)-th subdiagonal 16 // contain the m×n upper trapezoidal matrix R. 17 // The remaining elements, with tau, represent the 18 // orthogonal matrix Q as a product of min(m,n) elementary 19 // reflectors. 20 // 21 // The matrix Q is represented as a product of elementary reflectors 22 // 23 // Q = H_0 H_1 . . . H_{min(m,n)-1}. 24 // 25 // Each H(i) has the form 26 // 27 // H_i = I - tau_i * v * vᵀ 28 // 29 // where v is a vector with v[0:n-k+i-1] stored in A[m-k+i, 0:n-k+i-1], 30 // v[n-k+i:n] = 0 and v[n-k+i] = 1. 31 // 32 // tau must have length min(m,n) and work must have length m, otherwise 33 // Dgerq2 will panic. 34 // 35 // Dgerq2 is an internal routine. It is exported for testing purposes. 36 func (impl Implementation) Dgerq2(m, n int, a []float64, lda int, tau, work []float64) { 37 switch { 38 case m < 0: 39 panic(mLT0) 40 case n < 0: 41 panic(nLT0) 42 case lda < max(1, n): 43 panic(badLdA) 44 case len(work) < m: 45 panic(shortWork) 46 } 47 48 // Quick return if possible. 49 k := min(m, n) 50 if k == 0 { 51 return 52 } 53 54 switch { 55 case len(a) < (m-1)*lda+n: 56 panic(shortA) 57 case len(tau) < k: 58 panic(shortTau) 59 } 60 61 for i := k - 1; i >= 0; i-- { 62 // Generate elementary reflector H[i] to annihilate 63 // A[m-k+i, 0:n-k+i-1]. 64 mki := m - k + i 65 nki := n - k + i 66 var aii float64 67 aii, tau[i] = impl.Dlarfg(nki+1, a[mki*lda+nki], a[mki*lda:], 1) 68 69 // Apply H[i] to A[0:m-k+i-1, 0:n-k+i] from the right. 70 a[mki*lda+nki] = 1 71 impl.Dlarf(blas.Right, mki, nki+1, a[mki*lda:], 1, tau[i], a, lda, work) 72 a[mki*lda+nki] = aii 73 } 74 }