github.com/gopherd/gonum@v0.0.4/lapack/gonum/dorgtr.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  // Dorgtr generates a real orthogonal matrix Q which is defined as the product
    10  // of n-1 elementary reflectors of order n as returned by Dsytrd.
    11  //
    12  // The construction of Q depends on the value of uplo:
    13  //  Q = H_{n-1} * ... * H_1 * H_0  if uplo == blas.Upper
    14  //  Q = H_0 * H_1 * ... * H_{n-1}  if uplo == blas.Lower
    15  // where H_i is constructed from the elementary reflectors as computed by Dsytrd.
    16  // See the documentation for Dsytrd for more information.
    17  //
    18  // tau must have length at least n-1, and Dorgtr will panic otherwise.
    19  //
    20  // work is temporary storage, and lwork specifies the usable memory length. At
    21  // minimum, lwork >= max(1,n-1), and Dorgtr will panic otherwise. The amount of blocking
    22  // is limited by the usable length.
    23  // If lwork == -1, instead of computing Dorgtr the optimal work length is stored
    24  // into work[0].
    25  //
    26  // Dorgtr is an internal routine. It is exported for testing purposes.
    27  func (impl Implementation) Dorgtr(uplo blas.Uplo, n int, a []float64, lda int, tau, work []float64, lwork int) {
    28  	switch {
    29  	case uplo != blas.Upper && uplo != blas.Lower:
    30  		panic(badUplo)
    31  	case n < 0:
    32  		panic(nLT0)
    33  	case lda < max(1, n):
    34  		panic(badLdA)
    35  	case lwork < max(1, n-1) && lwork != -1:
    36  		panic(badLWork)
    37  	case len(work) < max(1, lwork):
    38  		panic(shortWork)
    39  	}
    40  
    41  	if n == 0 {
    42  		work[0] = 1
    43  		return
    44  	}
    45  
    46  	var nb int
    47  	if uplo == blas.Upper {
    48  		nb = impl.Ilaenv(1, "DORGQL", " ", n-1, n-1, n-1, -1)
    49  	} else {
    50  		nb = impl.Ilaenv(1, "DORGQR", " ", n-1, n-1, n-1, -1)
    51  	}
    52  	lworkopt := max(1, n-1) * nb
    53  	if lwork == -1 {
    54  		work[0] = float64(lworkopt)
    55  		return
    56  	}
    57  
    58  	switch {
    59  	case len(a) < (n-1)*lda+n:
    60  		panic(shortA)
    61  	case len(tau) < n-1:
    62  		panic(shortTau)
    63  	}
    64  
    65  	if uplo == blas.Upper {
    66  		// Q was determined by a call to Dsytrd with uplo == blas.Upper.
    67  		// Shift the vectors which define the elementary reflectors one column
    68  		// to the left, and set the last row and column of Q to those of the unit
    69  		// matrix.
    70  		for j := 0; j < n-1; j++ {
    71  			for i := 0; i < j; i++ {
    72  				a[i*lda+j] = a[i*lda+j+1]
    73  			}
    74  			a[(n-1)*lda+j] = 0
    75  		}
    76  		for i := 0; i < n-1; i++ {
    77  			a[i*lda+n-1] = 0
    78  		}
    79  		a[(n-1)*lda+n-1] = 1
    80  
    81  		// Generate Q[0:n-1, 0:n-1].
    82  		impl.Dorgql(n-1, n-1, n-1, a, lda, tau, work, lwork)
    83  	} else {
    84  		// Q was determined by a call to Dsytrd with uplo == blas.Upper.
    85  		// Shift the vectors which define the elementary reflectors one column
    86  		// to the right, and set the first row and column of Q to those of the unit
    87  		// matrix.
    88  		for j := n - 1; j > 0; j-- {
    89  			a[j] = 0
    90  			for i := j + 1; i < n; i++ {
    91  				a[i*lda+j] = a[i*lda+j-1]
    92  			}
    93  		}
    94  		a[0] = 1
    95  		for i := 1; i < n; i++ {
    96  			a[i*lda] = 0
    97  		}
    98  		if n > 1 {
    99  			// Generate Q[1:n, 1:n].
   100  			impl.Dorgqr(n-1, n-1, n-1, a[lda+1:], lda, tau, work, lwork)
   101  		}
   102  	}
   103  	work[0] = float64(lworkopt)
   104  }