github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/lapack/gonum/dorgqr.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 (
     8  	"github.com/jingcheng-WU/gonum/blas"
     9  	"github.com/jingcheng-WU/gonum/lapack"
    10  )
    11  
    12  // Dorgqr generates an m×n matrix Q with orthonormal columns defined by the
    13  // product of elementary reflectors
    14  //  Q = H_0 * H_1 * ... * H_{k-1}
    15  // as computed by Dgeqrf.
    16  // Dorgqr is the blocked version of Dorg2r that makes greater use of level-3 BLAS
    17  // routines.
    18  //
    19  // The length of tau must be at least k, and the length of work must be at least n.
    20  // It also must be that 0 <= k <= n and 0 <= n <= m.
    21  //
    22  // work is temporary storage, and lwork specifies the usable memory length. At
    23  // minimum, lwork >= n, and the amount of blocking is limited by the usable
    24  // length. If lwork == -1, instead of computing Dorgqr the optimal work length
    25  // is stored into work[0].
    26  //
    27  // Dorgqr will panic if the conditions on input values are not met.
    28  //
    29  // Dorgqr is an internal routine. It is exported for testing purposes.
    30  func (impl Implementation) Dorgqr(m, n, k int, a []float64, lda int, tau, work []float64, lwork int) {
    31  	switch {
    32  	case m < 0:
    33  		panic(mLT0)
    34  	case n < 0:
    35  		panic(nLT0)
    36  	case n > m:
    37  		panic(nGTM)
    38  	case k < 0:
    39  		panic(kLT0)
    40  	case k > n:
    41  		panic(kGTN)
    42  	case lda < max(1, n) && lwork != -1:
    43  		// Normally, we follow the reference and require the leading
    44  		// dimension to be always valid, even in case of workspace
    45  		// queries. However, if a caller provided a placeholder value
    46  		// for lda (and a) when doing a workspace query that didn't
    47  		// fulfill the condition here, it would cause a panic. This is
    48  		// exactly what Dgesvd does.
    49  		panic(badLdA)
    50  	case lwork < max(1, n) && lwork != -1:
    51  		panic(badLWork)
    52  	case len(work) < max(1, lwork):
    53  		panic(shortWork)
    54  	}
    55  
    56  	if n == 0 {
    57  		work[0] = 1
    58  		return
    59  	}
    60  
    61  	nb := impl.Ilaenv(1, "DORGQR", " ", m, n, k, -1)
    62  	// work is treated as an n×nb matrix
    63  	if lwork == -1 {
    64  		work[0] = float64(n * nb)
    65  		return
    66  	}
    67  
    68  	switch {
    69  	case len(a) < (m-1)*lda+n:
    70  		panic(shortA)
    71  	case len(tau) < k:
    72  		panic(shortTau)
    73  	}
    74  
    75  	nbmin := 2 // Minimum block size
    76  	var nx int // Crossover size from blocked to unbloked code
    77  	iws := n   // Length of work needed
    78  	var ldwork int
    79  	if 1 < nb && nb < k {
    80  		nx = max(0, impl.Ilaenv(3, "DORGQR", " ", m, n, k, -1))
    81  		if nx < k {
    82  			ldwork = nb
    83  			iws = n * ldwork
    84  			if lwork < iws {
    85  				nb = lwork / n
    86  				ldwork = nb
    87  				nbmin = max(2, impl.Ilaenv(2, "DORGQR", " ", m, n, k, -1))
    88  			}
    89  		}
    90  	}
    91  	var ki, kk int
    92  	if nbmin <= nb && nb < k && nx < k {
    93  		// The first kk columns are handled by the blocked method.
    94  		ki = ((k - nx - 1) / nb) * nb
    95  		kk = min(k, ki+nb)
    96  		for i := 0; i < kk; i++ {
    97  			for j := kk; j < n; j++ {
    98  				a[i*lda+j] = 0
    99  			}
   100  		}
   101  	}
   102  	if kk < n {
   103  		// Perform the operation on colums kk to the end.
   104  		impl.Dorg2r(m-kk, n-kk, k-kk, a[kk*lda+kk:], lda, tau[kk:], work)
   105  	}
   106  	if kk > 0 {
   107  		// Perform the operation on column-blocks.
   108  		for i := ki; i >= 0; i -= nb {
   109  			ib := min(nb, k-i)
   110  			if i+ib < n {
   111  				impl.Dlarft(lapack.Forward, lapack.ColumnWise,
   112  					m-i, ib,
   113  					a[i*lda+i:], lda,
   114  					tau[i:],
   115  					work, ldwork)
   116  
   117  				impl.Dlarfb(blas.Left, blas.NoTrans, lapack.Forward, lapack.ColumnWise,
   118  					m-i, n-i-ib, ib,
   119  					a[i*lda+i:], lda,
   120  					work, ldwork,
   121  					a[i*lda+i+ib:], lda,
   122  					work[ib*ldwork:], ldwork)
   123  			}
   124  			impl.Dorg2r(m-i, ib, ib, a[i*lda+i:], lda, tau[i:], work)
   125  			// Set rows 0:i-1 of current block to zero.
   126  			for j := i; j < i+ib; j++ {
   127  				for l := 0; l < i; l++ {
   128  					a[l*lda+j] = 0
   129  				}
   130  			}
   131  		}
   132  	}
   133  	work[0] = float64(iws)
   134  }