gonum.org/v1/gonum@v0.14.0/lapack/gonum/dorg2l.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 (
     8  	"gonum.org/v1/gonum/blas"
     9  	"gonum.org/v1/gonum/blas/blas64"
    10  )
    11  
    12  // Dorg2l generates an m×n matrix Q with orthonormal columns which is defined
    13  // as the last n columns of a product of k elementary reflectors of order m.
    14  //
    15  //	Q = H_{k-1} * ... * H_1 * H_0
    16  //
    17  // See Dgelqf for more information. It must be that m >= n >= k.
    18  //
    19  // tau contains the scalar reflectors computed by Dgeqlf. tau must have length
    20  // at least k, and Dorg2l will panic otherwise.
    21  //
    22  // work contains temporary memory, and must have length at least n. Dorg2l will
    23  // panic otherwise.
    24  //
    25  // Dorg2l is an internal routine. It is exported for testing purposes.
    26  func (impl Implementation) Dorg2l(m, n, k 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 n > m:
    33  		panic(nGTM)
    34  	case k < 0:
    35  		panic(kLT0)
    36  	case k > n:
    37  		panic(kGTN)
    38  	case lda < max(1, n):
    39  		panic(badLdA)
    40  	}
    41  
    42  	if n == 0 {
    43  		return
    44  	}
    45  
    46  	switch {
    47  	case len(a) < (m-1)*lda+n:
    48  		panic(shortA)
    49  	case len(tau) < k:
    50  		panic(shortTau)
    51  	case len(work) < n:
    52  		panic(shortWork)
    53  	}
    54  
    55  	// Initialize columns 0:n-k to columns of the unit matrix.
    56  	for j := 0; j < n-k; j++ {
    57  		for l := 0; l < m; l++ {
    58  			a[l*lda+j] = 0
    59  		}
    60  		a[(m-n+j)*lda+j] = 1
    61  	}
    62  
    63  	bi := blas64.Implementation()
    64  	for i := 0; i < k; i++ {
    65  		ii := n - k + i
    66  
    67  		// Apply H_i to A[0:m-k+i, 0:n-k+i] from the left.
    68  		a[(m-n+ii)*lda+ii] = 1
    69  		impl.Dlarf(blas.Left, m-n+ii+1, ii, a[ii:], lda, tau[i], a, lda, work)
    70  		bi.Dscal(m-n+ii, -tau[i], a[ii:], lda)
    71  		a[(m-n+ii)*lda+ii] = 1 - tau[i]
    72  
    73  		// Set A[m-k+i:m, n-k+i+1] to zero.
    74  		for l := m - n + ii + 1; l < m; l++ {
    75  			a[l*lda+ii] = 0
    76  		}
    77  	}
    78  }