github.com/gonum/lapack@v0.0.0-20181123203213-e4cdc5a0bff9/native/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 native
     6  
     7  import "github.com/gonum/blas"
     8  
     9  // Dgerq2 computes an RQ factorization of the m×n matrix A,
    10  //  A = R * Q.
    11  // On exit, if m <= n, the upper triangle of the subarray
    12  // A[0:m, n-m:n] contains the m×m upper triangular matrix R.
    13  // If m >= n, the elements on and above the (m-n)-th subdiagonal
    14  // contain the m×n upper trapezoidal matrix R.
    15  // The remaining elements, with tau, represent the
    16  // orthogonal matrix Q as a product of min(m,n) elementary
    17  // reflectors.
    18  //
    19  // The matrix Q is represented as a product of elementary reflectors
    20  //  Q = H_0 H_1 . . . H_{min(m,n)-1}.
    21  // Each H(i) has the form
    22  //  H_i = I - tau_i * v * v^T
    23  // where v is a vector with v[0:n-k+i-1] stored in A[m-k+i, 0:n-k+i-1],
    24  // v[n-k+i:n] = 0 and v[n-k+i] = 1.
    25  //
    26  // tau must have length min(m,n) and work must have length m, otherwise
    27  // Dgerq2 will panic.
    28  //
    29  // Dgerq2 is an internal routine. It is exported for testing purposes.
    30  func (impl Implementation) Dgerq2(m, n int, a []float64, lda int, tau, work []float64) {
    31  	checkMatrix(m, n, a, lda)
    32  	k := min(m, n)
    33  	if len(tau) < k {
    34  		panic(badTau)
    35  	}
    36  	if len(work) < m {
    37  		panic(badWork)
    38  	}
    39  
    40  	for i := k - 1; i >= 0; i-- {
    41  		// Generate elementary reflector H[i] to annihilate
    42  		// A[m-k+i, 0:n-k+i-1].
    43  		mki := m - k + i
    44  		nki := n - k + i
    45  		var aii float64
    46  		aii, tau[i] = impl.Dlarfg(nki+1, a[mki*lda+nki], a[mki*lda:], 1)
    47  
    48  		// Apply H[i] to A[0:m-k+i-1, 0:n-k+i] from the right.
    49  		a[mki*lda+nki] = 1
    50  		impl.Dlarf(blas.Right, mki, nki+1, a[mki*lda:], 1, tau[i], a, lda, work)
    51  		a[mki*lda+nki] = aii
    52  	}
    53  }