gonum.org/v1/gonum@v0.14.0/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 "gonum.org/v1/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  //
    23  //	v[j] = 0           j < i
    24  //	v[j] = 1           j == i
    25  //	v[j] = a[j*lda+i]  j > i
    26  //
    27  // and computing H_i = I - tau[i] * v * vᵀ.
    28  //
    29  // The orthonormal matrix Q can be constructed from a product of these elementary
    30  // reflectors, Q = H_0 * H_1 * ... * H_{k-1}, where k = min(m,n).
    31  //
    32  // work is temporary storage of length at least n and this function will panic otherwise.
    33  //
    34  // Dgeqr2 is an internal routine. It is exported for testing purposes.
    35  func (impl Implementation) Dgeqr2(m, n int, a []float64, lda int, tau, work []float64) {
    36  	// TODO(btracey): This is oriented such that columns of a are eliminated.
    37  	// This likely could be re-arranged to take better advantage of row-major
    38  	// storage.
    39  
    40  	switch {
    41  	case m < 0:
    42  		panic(mLT0)
    43  	case n < 0:
    44  		panic(nLT0)
    45  	case lda < max(1, n):
    46  		panic(badLdA)
    47  	case len(work) < n:
    48  		panic(shortWork)
    49  	}
    50  
    51  	// Quick return if possible.
    52  	k := min(m, n)
    53  	if k == 0 {
    54  		return
    55  	}
    56  
    57  	switch {
    58  	case len(a) < (m-1)*lda+n:
    59  		panic(shortA)
    60  	case len(tau) < k:
    61  		panic(shortTau)
    62  	}
    63  
    64  	for i := 0; i < k; i++ {
    65  		// Generate elementary reflector H_i.
    66  		a[i*lda+i], tau[i] = impl.Dlarfg(m-i, a[i*lda+i], a[min((i+1), m-1)*lda+i:], lda)
    67  		if i < n-1 {
    68  			aii := a[i*lda+i]
    69  			a[i*lda+i] = 1
    70  			impl.Dlarf(blas.Left, m-i, n-i-1,
    71  				a[i*lda+i:], lda,
    72  				tau[i],
    73  				a[i*lda+i+1:], lda,
    74  				work)
    75  			a[i*lda+i] = aii
    76  		}
    77  	}
    78  }