github.com/gopherd/gonum@v0.0.4/lapack/gonum/dgeql2.go (about)

     1  // Copyright ©2016 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  // Dgeql2 computes the QL factorization of the m×n matrix A. That is, Dgeql2
    10  // computes Q and L such that
    11  //  A = Q * L
    12  // where Q is an m×m orthonormal matrix and L is a lower trapezoidal matrix.
    13  //
    14  // Q is represented as a product of elementary reflectors,
    15  //  Q = H_{k-1} * ... * H_1 * H_0
    16  // where k = min(m,n) and each H_i has the form
    17  //  H_i = I - tau[i] * v_i * v_iᵀ
    18  // Vector v_i has v[m-k+i+1:m] = 0, v[m-k+i] = 1, and v[:m-k+i+1] is stored on
    19  // exit in A[0:m-k+i-1, n-k+i].
    20  //
    21  // tau must have length at least min(m,n), and Dgeql2 will panic otherwise.
    22  //
    23  // work is temporary memory storage and must have length at least n.
    24  //
    25  // Dgeql2 is an internal routine. It is exported for testing purposes.
    26  func (impl Implementation) Dgeql2(m, n int, a []float64, lda int, tau, work []float64) {
    27  	switch {
    28  	case m < 0:
    29  		panic(mLT0)
    30  	case n < 0:
    31  		panic(nLT0)
    32  	case lda < max(1, n):
    33  		panic(badLdA)
    34  	}
    35  
    36  	// Quick return if possible.
    37  	k := min(m, n)
    38  	if k == 0 {
    39  		return
    40  	}
    41  
    42  	switch {
    43  	case len(a) < (m-1)*lda+n:
    44  		panic(shortA)
    45  	case len(tau) < k:
    46  		panic(shortTau)
    47  	case len(work) < n:
    48  		panic(shortWork)
    49  	}
    50  
    51  	var aii float64
    52  	for i := k - 1; i >= 0; i-- {
    53  		// Generate elementary reflector H_i to annihilate A[0:m-k+i-1, n-k+i].
    54  		aii, tau[i] = impl.Dlarfg(m-k+i+1, a[(m-k+i)*lda+n-k+i], a[n-k+i:], lda)
    55  
    56  		// Apply H_i to A[0:m-k+i, 0:n-k+i-1] from the left.
    57  		a[(m-k+i)*lda+n-k+i] = 1
    58  		impl.Dlarf(blas.Left, m-k+i+1, n-k+i, a[n-k+i:], lda, tau[i], a, lda, work)
    59  		a[(m-k+i)*lda+n-k+i] = aii
    60  	}
    61  }