github.com/gopherd/gonum@v0.0.4/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 "github.com/gopherd/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ᵀ
    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  	switch {
    32  	case m < 0:
    33  		panic(mLT0)
    34  	case n < 0:
    35  		panic(nLT0)
    36  	case lda < max(1, n):
    37  		panic(badLdA)
    38  	case len(work) < m:
    39  		panic(shortWork)
    40  	}
    41  
    42  	// Quick return if possible.
    43  	k := min(m, n)
    44  	if k == 0 {
    45  		return
    46  	}
    47  
    48  	switch {
    49  	case len(a) < (m-1)*lda+n:
    50  		panic(shortA)
    51  	case len(tau) < k:
    52  		panic(shortTau)
    53  	}
    54  
    55  	for i := k - 1; i >= 0; i-- {
    56  		// Generate elementary reflector H[i] to annihilate
    57  		// A[m-k+i, 0:n-k+i-1].
    58  		mki := m - k + i
    59  		nki := n - k + i
    60  		var aii float64
    61  		aii, tau[i] = impl.Dlarfg(nki+1, a[mki*lda+nki], a[mki*lda:], 1)
    62  
    63  		// Apply H[i] to A[0:m-k+i-1, 0:n-k+i] from the right.
    64  		a[mki*lda+nki] = 1
    65  		impl.Dlarf(blas.Right, mki, nki+1, a[mki*lda:], 1, tau[i], a, lda, work)
    66  		a[mki*lda+nki] = aii
    67  	}
    68  }