gonum.org/v1/gonum@v0.14.0/lapack/gonum/dorghr.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  // Dorghr generates an n×n orthogonal matrix Q which is defined as the product
     8  // of ihi-ilo elementary reflectors:
     9  //
    10  //	Q = H_{ilo} H_{ilo+1} ... H_{ihi-1}.
    11  //
    12  // a and lda represent an n×n matrix that contains the elementary reflectors, as
    13  // returned by Dgehrd. On return, a is overwritten by the n×n orthogonal matrix
    14  // Q. Q will be equal to the identity matrix except in the submatrix
    15  // Q[ilo+1:ihi+1,ilo+1:ihi+1].
    16  //
    17  // ilo and ihi must have the same values as in the previous call of Dgehrd. It
    18  // must hold that
    19  //
    20  //	0 <= ilo <= ihi < n  if n > 0,
    21  //	ilo = 0, ihi = -1    if n == 0.
    22  //
    23  // tau contains the scalar factors of the elementary reflectors, as returned by
    24  // Dgehrd. tau must have length n-1.
    25  //
    26  // work must have length at least max(1,lwork) and lwork must be at least
    27  // ihi-ilo. For optimum performance lwork must be at least (ihi-ilo)*nb where nb
    28  // is the optimal blocksize. On return, work[0] will contain the optimal value
    29  // of lwork.
    30  //
    31  // If lwork == -1, instead of performing Dorghr, only the optimal value of lwork
    32  // will be stored into work[0].
    33  //
    34  // If any requirement on input sizes is not met, Dorghr will panic.
    35  //
    36  // Dorghr is an internal routine. It is exported for testing purposes.
    37  func (impl Implementation) Dorghr(n, ilo, ihi int, a []float64, lda int, tau, work []float64, lwork int) {
    38  	nh := ihi - ilo
    39  	switch {
    40  	case ilo < 0 || max(1, n) <= ilo:
    41  		panic(badIlo)
    42  	case ihi < min(ilo, n-1) || n <= ihi:
    43  		panic(badIhi)
    44  	case lda < max(1, n):
    45  		panic(badLdA)
    46  	case lwork < max(1, nh) && lwork != -1:
    47  		panic(badLWork)
    48  	case len(work) < max(1, lwork):
    49  		panic(shortWork)
    50  	}
    51  
    52  	// Quick return if possible.
    53  	if n == 0 {
    54  		work[0] = 1
    55  		return
    56  	}
    57  
    58  	lwkopt := max(1, nh) * impl.Ilaenv(1, "DORGQR", " ", nh, nh, nh, -1)
    59  	if lwork == -1 {
    60  		work[0] = float64(lwkopt)
    61  		return
    62  	}
    63  
    64  	switch {
    65  	case len(a) < (n-1)*lda+n:
    66  		panic(shortA)
    67  	case len(tau) < n-1:
    68  		panic(shortTau)
    69  	}
    70  
    71  	// Shift the vectors which define the elementary reflectors one column
    72  	// to the right.
    73  	for i := ilo + 2; i < ihi+1; i++ {
    74  		copy(a[i*lda+ilo+1:i*lda+i], a[i*lda+ilo:i*lda+i-1])
    75  	}
    76  	// Set the first ilo+1 and the last n-ihi-1 rows and columns to those of
    77  	// the identity matrix.
    78  	for i := 0; i < ilo+1; i++ {
    79  		for j := 0; j < n; j++ {
    80  			a[i*lda+j] = 0
    81  		}
    82  		a[i*lda+i] = 1
    83  	}
    84  	for i := ilo + 1; i < ihi+1; i++ {
    85  		for j := 0; j <= ilo; j++ {
    86  			a[i*lda+j] = 0
    87  		}
    88  		for j := i; j < n; j++ {
    89  			a[i*lda+j] = 0
    90  		}
    91  	}
    92  	for i := ihi + 1; i < n; i++ {
    93  		for j := 0; j < n; j++ {
    94  			a[i*lda+j] = 0
    95  		}
    96  		a[i*lda+i] = 1
    97  	}
    98  	if nh > 0 {
    99  		// Generate Q[ilo+1:ihi+1,ilo+1:ihi+1].
   100  		impl.Dorgqr(nh, nh, nh, a[(ilo+1)*lda+ilo+1:], lda, tau[ilo:ihi], work, lwork)
   101  	}
   102  	work[0] = float64(lwkopt)
   103  }