github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/lapack/gonum/dgeqr2.go (about) 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. 4 5 package gonum 6 7 import "github.com/jingcheng-WU/gonum/blas" 8 9 // Dgeqr2 computes a QR factorization of the m×n matrix A. 10 // 11 // In a QR factorization, Q is an m×m orthonormal matrix, and R is an 12 // upper triangular m×n matrix. 13 // 14 // A is modified to contain the information to construct Q and R. 15 // The upper triangle of a contains the matrix R. The lower triangular elements 16 // (not including the diagonal) contain the elementary reflectors. tau is modified 17 // to contain the reflector scales. tau must have length at least min(m,n), and 18 // this function will panic otherwise. 19 // 20 // The ith elementary reflector can be explicitly constructed by first extracting 21 // the 22 // v[j] = 0 j < i 23 // v[j] = 1 j == i 24 // v[j] = a[j*lda+i] j > i 25 // and computing H_i = I - tau[i] * v * vᵀ. 26 // 27 // The orthonormal matrix Q can be constructed from a product of these elementary 28 // reflectors, Q = H_0 * H_1 * ... * H_{k-1}, where k = min(m,n). 29 // 30 // work is temporary storage of length at least n and this function will panic otherwise. 31 // 32 // Dgeqr2 is an internal routine. It is exported for testing purposes. 33 func (impl Implementation) Dgeqr2(m, n int, a []float64, lda int, tau, work []float64) { 34 // TODO(btracey): This is oriented such that columns of a are eliminated. 35 // This likely could be re-arranged to take better advantage of row-major 36 // storage. 37 38 switch { 39 case m < 0: 40 panic(mLT0) 41 case n < 0: 42 panic(nLT0) 43 case lda < max(1, n): 44 panic(badLdA) 45 case len(work) < n: 46 panic(shortWork) 47 } 48 49 // Quick return if possible. 50 k := min(m, n) 51 if k == 0 { 52 return 53 } 54 55 switch { 56 case len(a) < (m-1)*lda+n: 57 panic(shortA) 58 case len(tau) < k: 59 panic(shortTau) 60 } 61 62 for i := 0; i < k; i++ { 63 // Generate elementary reflector H_i. 64 a[i*lda+i], tau[i] = impl.Dlarfg(m-i, a[i*lda+i], a[min((i+1), m-1)*lda+i:], lda) 65 if i < n-1 { 66 aii := a[i*lda+i] 67 a[i*lda+i] = 1 68 impl.Dlarf(blas.Left, m-i, n-i-1, 69 a[i*lda+i:], lda, 70 tau[i], 71 a[i*lda+i+1:], lda, 72 work) 73 a[i*lda+i] = aii 74 } 75 } 76 }