github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/lapack/gonum/dgebd2.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 "github.com/jingcheng-WU/gonum/blas"
     8  
     9  // Dgebd2 reduces an m×n matrix A to upper or lower bidiagonal form by an orthogonal
    10  // transformation.
    11  //  Qᵀ * A * P = B
    12  // if m >= n, B is upper diagonal, otherwise B is lower bidiagonal.
    13  // d is the diagonal, len = min(m,n)
    14  // e is the off-diagonal len = min(m,n)-1
    15  //
    16  // Dgebd2 is an internal routine. It is exported for testing purposes.
    17  func (impl Implementation) Dgebd2(m, n int, a []float64, lda int, d, e, tauQ, tauP, work []float64) {
    18  	switch {
    19  	case m < 0:
    20  		panic(mLT0)
    21  	case n < 0:
    22  		panic(nLT0)
    23  	case lda < max(1, n):
    24  		panic(badLdA)
    25  	}
    26  
    27  	// Quick return if possible.
    28  	minmn := min(m, n)
    29  	if minmn == 0 {
    30  		return
    31  	}
    32  
    33  	switch {
    34  	case len(d) < minmn:
    35  		panic(shortD)
    36  	case len(e) < minmn-1:
    37  		panic(shortE)
    38  	case len(tauQ) < minmn:
    39  		panic(shortTauQ)
    40  	case len(tauP) < minmn:
    41  		panic(shortTauP)
    42  	case len(work) < max(m, n):
    43  		panic(shortWork)
    44  	}
    45  
    46  	if m >= n {
    47  		for i := 0; i < n; i++ {
    48  			a[i*lda+i], tauQ[i] = impl.Dlarfg(m-i, a[i*lda+i], a[min(i+1, m-1)*lda+i:], lda)
    49  			d[i] = a[i*lda+i]
    50  			a[i*lda+i] = 1
    51  			// Apply H_i to A[i:m, i+1:n] from the left.
    52  			if i < n-1 {
    53  				impl.Dlarf(blas.Left, m-i, n-i-1, a[i*lda+i:], lda, tauQ[i], a[i*lda+i+1:], lda, work)
    54  			}
    55  			a[i*lda+i] = d[i]
    56  			if i < n-1 {
    57  				a[i*lda+i+1], tauP[i] = impl.Dlarfg(n-i-1, a[i*lda+i+1], a[i*lda+min(i+2, n-1):], 1)
    58  				e[i] = a[i*lda+i+1]
    59  				a[i*lda+i+1] = 1
    60  				impl.Dlarf(blas.Right, m-i-1, n-i-1, a[i*lda+i+1:], 1, tauP[i], a[(i+1)*lda+i+1:], lda, work)
    61  				a[i*lda+i+1] = e[i]
    62  			} else {
    63  				tauP[i] = 0
    64  			}
    65  		}
    66  		return
    67  	}
    68  	for i := 0; i < m; i++ {
    69  		a[i*lda+i], tauP[i] = impl.Dlarfg(n-i, a[i*lda+i], a[i*lda+min(i+1, n-1):], 1)
    70  		d[i] = a[i*lda+i]
    71  		a[i*lda+i] = 1
    72  		if i < m-1 {
    73  			impl.Dlarf(blas.Right, m-i-1, n-i, a[i*lda+i:], 1, tauP[i], a[(i+1)*lda+i:], lda, work)
    74  		}
    75  		a[i*lda+i] = d[i]
    76  		if i < m-1 {
    77  			a[(i+1)*lda+i], tauQ[i] = impl.Dlarfg(m-i-1, a[(i+1)*lda+i], a[min(i+2, m-1)*lda+i:], lda)
    78  			e[i] = a[(i+1)*lda+i]
    79  			a[(i+1)*lda+i] = 1
    80  			impl.Dlarf(blas.Left, m-i-1, n-i-1, a[(i+1)*lda+i:], lda, tauQ[i], a[(i+1)*lda+i+1:], lda, work)
    81  			a[(i+1)*lda+i] = e[i]
    82  		} else {
    83  			tauQ[i] = 0
    84  		}
    85  	}
    86  }