github.com/gonum/lapack@v0.0.0-20181123203213-e4cdc5a0bff9/native/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 native
     6  
     7  import "github.com/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^T.
    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  	checkMatrix(m, n, a, lda)
    38  	if len(work) < n {
    39  		panic(badWork)
    40  	}
    41  	k := min(m, n)
    42  	if len(tau) < k {
    43  		panic(badTau)
    44  	}
    45  	for i := 0; i < k; i++ {
    46  		// Generate elementary reflector H_i.
    47  		a[i*lda+i], tau[i] = impl.Dlarfg(m-i, a[i*lda+i], a[min((i+1), m-1)*lda+i:], lda)
    48  		if i < n-1 {
    49  			aii := a[i*lda+i]
    50  			a[i*lda+i] = 1
    51  			impl.Dlarf(blas.Left, m-i, n-i-1,
    52  				a[i*lda+i:], lda,
    53  				tau[i],
    54  				a[i*lda+i+1:], lda,
    55  				work)
    56  			a[i*lda+i] = aii
    57  		}
    58  	}
    59  }