github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/lapack/gonum/dlabrd.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/blas/blas64"
    10  )
    11  
    12  // Dlabrd reduces the first NB rows and columns of a real general m×n matrix
    13  // A to upper or lower bidiagonal form by an orthogonal transformation
    14  //  Q**T * A * P
    15  // If m >= n, A is reduced to upper bidiagonal form and upon exit the elements
    16  // on and below the diagonal in the first nb columns represent the elementary
    17  // reflectors, and the elements above the diagonal in the first nb rows represent
    18  // the matrix P. If m < n, A is reduced to lower bidiagonal form and the elements
    19  // P is instead stored above the diagonal.
    20  //
    21  // The reduction to bidiagonal form is stored in d and e, where d are the diagonal
    22  // elements, and e are the off-diagonal elements.
    23  //
    24  // The matrices Q and P are products of elementary reflectors
    25  //  Q = H_0 * H_1 * ... * H_{nb-1}
    26  //  P = G_0 * G_1 * ... * G_{nb-1}
    27  // where
    28  //  H_i = I - tauQ[i] * v_i * v_iᵀ
    29  //  G_i = I - tauP[i] * u_i * u_iᵀ
    30  //
    31  // As an example, on exit the entries of A when m = 6, n = 5, and nb = 2
    32  //  [ 1   1  u1  u1  u1]
    33  //  [v1   1   1  u2  u2]
    34  //  [v1  v2   a   a   a]
    35  //  [v1  v2   a   a   a]
    36  //  [v1  v2   a   a   a]
    37  //  [v1  v2   a   a   a]
    38  // and when m = 5, n = 6, and nb = 2
    39  //  [ 1  u1  u1  u1  u1  u1]
    40  //  [ 1   1  u2  u2  u2  u2]
    41  //  [v1   1   a   a   a   a]
    42  //  [v1  v2   a   a   a   a]
    43  //  [v1  v2   a   a   a   a]
    44  //
    45  // Dlabrd also returns the matrices X and Y which are used with U and V to
    46  // apply the transformation to the unreduced part of the matrix
    47  //  A := A - V*Yᵀ - X*Uᵀ
    48  // and returns the matrices X and Y which are needed to apply the
    49  // transformation to the unreduced part of A.
    50  //
    51  // X is an m×nb matrix, Y is an n×nb matrix. d, e, taup, and tauq must all have
    52  // length at least nb. Dlabrd will panic if these size constraints are violated.
    53  //
    54  // Dlabrd is an internal routine. It is exported for testing purposes.
    55  func (impl Implementation) Dlabrd(m, n, nb int, a []float64, lda int, d, e, tauQ, tauP, x []float64, ldx int, y []float64, ldy int) {
    56  	switch {
    57  	case m < 0:
    58  		panic(mLT0)
    59  	case n < 0:
    60  		panic(nLT0)
    61  	case nb < 0:
    62  		panic(nbLT0)
    63  	case nb > n:
    64  		panic(nbGTN)
    65  	case nb > m:
    66  		panic(nbGTM)
    67  	case lda < max(1, n):
    68  		panic(badLdA)
    69  	case ldx < max(1, nb):
    70  		panic(badLdX)
    71  	case ldy < max(1, nb):
    72  		panic(badLdY)
    73  	}
    74  
    75  	if m == 0 || n == 0 || nb == 0 {
    76  		return
    77  	}
    78  
    79  	switch {
    80  	case len(a) < (m-1)*lda+n:
    81  		panic(shortA)
    82  	case len(d) < nb:
    83  		panic(shortD)
    84  	case len(e) < nb:
    85  		panic(shortE)
    86  	case len(tauQ) < nb:
    87  		panic(shortTauQ)
    88  	case len(tauP) < nb:
    89  		panic(shortTauP)
    90  	case len(x) < (m-1)*ldx+nb:
    91  		panic(shortX)
    92  	case len(y) < (n-1)*ldy+nb:
    93  		panic(shortY)
    94  	}
    95  
    96  	bi := blas64.Implementation()
    97  
    98  	if m >= n {
    99  		// Reduce to upper bidiagonal form.
   100  		for i := 0; i < nb; i++ {
   101  			bi.Dgemv(blas.NoTrans, m-i, i, -1, a[i*lda:], lda, y[i*ldy:], 1, 1, a[i*lda+i:], lda)
   102  			bi.Dgemv(blas.NoTrans, m-i, i, -1, x[i*ldx:], ldx, a[i:], lda, 1, a[i*lda+i:], lda)
   103  
   104  			a[i*lda+i], tauQ[i] = impl.Dlarfg(m-i, a[i*lda+i], a[min(i+1, m-1)*lda+i:], lda)
   105  			d[i] = a[i*lda+i]
   106  			if i < n-1 {
   107  				// Compute Y[i+1:n, i].
   108  				a[i*lda+i] = 1
   109  				bi.Dgemv(blas.Trans, m-i, n-i-1, 1, a[i*lda+i+1:], lda, a[i*lda+i:], lda, 0, y[(i+1)*ldy+i:], ldy)
   110  				bi.Dgemv(blas.Trans, m-i, i, 1, a[i*lda:], lda, a[i*lda+i:], lda, 0, y[i:], ldy)
   111  				bi.Dgemv(blas.NoTrans, n-i-1, i, -1, y[(i+1)*ldy:], ldy, y[i:], ldy, 1, y[(i+1)*ldy+i:], ldy)
   112  				bi.Dgemv(blas.Trans, m-i, i, 1, x[i*ldx:], ldx, a[i*lda+i:], lda, 0, y[i:], ldy)
   113  				bi.Dgemv(blas.Trans, i, n-i-1, -1, a[i+1:], lda, y[i:], ldy, 1, y[(i+1)*ldy+i:], ldy)
   114  				bi.Dscal(n-i-1, tauQ[i], y[(i+1)*ldy+i:], ldy)
   115  
   116  				// Update A[i, i+1:n].
   117  				bi.Dgemv(blas.NoTrans, n-i-1, i+1, -1, y[(i+1)*ldy:], ldy, a[i*lda:], 1, 1, a[i*lda+i+1:], 1)
   118  				bi.Dgemv(blas.Trans, i, n-i-1, -1, a[i+1:], lda, x[i*ldx:], 1, 1, a[i*lda+i+1:], 1)
   119  
   120  				// Generate reflection P[i] to annihilate A[i, i+2:n].
   121  				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)
   122  				e[i] = a[i*lda+i+1]
   123  				a[i*lda+i+1] = 1
   124  
   125  				// Compute X[i+1:m, i].
   126  				bi.Dgemv(blas.NoTrans, m-i-1, n-i-1, 1, a[(i+1)*lda+i+1:], lda, a[i*lda+i+1:], 1, 0, x[(i+1)*ldx+i:], ldx)
   127  				bi.Dgemv(blas.Trans, n-i-1, i+1, 1, y[(i+1)*ldy:], ldy, a[i*lda+i+1:], 1, 0, x[i:], ldx)
   128  				bi.Dgemv(blas.NoTrans, m-i-1, i+1, -1, a[(i+1)*lda:], lda, x[i:], ldx, 1, x[(i+1)*ldx+i:], ldx)
   129  				bi.Dgemv(blas.NoTrans, i, n-i-1, 1, a[i+1:], lda, a[i*lda+i+1:], 1, 0, x[i:], ldx)
   130  				bi.Dgemv(blas.NoTrans, m-i-1, i, -1, x[(i+1)*ldx:], ldx, x[i:], ldx, 1, x[(i+1)*ldx+i:], ldx)
   131  				bi.Dscal(m-i-1, tauP[i], x[(i+1)*ldx+i:], ldx)
   132  			}
   133  		}
   134  		return
   135  	}
   136  	// Reduce to lower bidiagonal form.
   137  	for i := 0; i < nb; i++ {
   138  		// Update A[i,i:n]
   139  		bi.Dgemv(blas.NoTrans, n-i, i, -1, y[i*ldy:], ldy, a[i*lda:], 1, 1, a[i*lda+i:], 1)
   140  		bi.Dgemv(blas.Trans, i, n-i, -1, a[i:], lda, x[i*ldx:], 1, 1, a[i*lda+i:], 1)
   141  
   142  		// Generate reflection P[i] to annihilate A[i, i+1:n]
   143  		a[i*lda+i], tauP[i] = impl.Dlarfg(n-i, a[i*lda+i], a[i*lda+min(i+1, n-1):], 1)
   144  		d[i] = a[i*lda+i]
   145  		if i < m-1 {
   146  			a[i*lda+i] = 1
   147  			// Compute X[i+1:m, i].
   148  			bi.Dgemv(blas.NoTrans, m-i-1, n-i, 1, a[(i+1)*lda+i:], lda, a[i*lda+i:], 1, 0, x[(i+1)*ldx+i:], ldx)
   149  			bi.Dgemv(blas.Trans, n-i, i, 1, y[i*ldy:], ldy, a[i*lda+i:], 1, 0, x[i:], ldx)
   150  			bi.Dgemv(blas.NoTrans, m-i-1, i, -1, a[(i+1)*lda:], lda, x[i:], ldx, 1, x[(i+1)*ldx+i:], ldx)
   151  			bi.Dgemv(blas.NoTrans, i, n-i, 1, a[i:], lda, a[i*lda+i:], 1, 0, x[i:], ldx)
   152  			bi.Dgemv(blas.NoTrans, m-i-1, i, -1, x[(i+1)*ldx:], ldx, x[i:], ldx, 1, x[(i+1)*ldx+i:], ldx)
   153  			bi.Dscal(m-i-1, tauP[i], x[(i+1)*ldx+i:], ldx)
   154  
   155  			// Update A[i+1:m, i].
   156  			bi.Dgemv(blas.NoTrans, m-i-1, i, -1, a[(i+1)*lda:], lda, y[i*ldy:], 1, 1, a[(i+1)*lda+i:], lda)
   157  			bi.Dgemv(blas.NoTrans, m-i-1, i+1, -1, x[(i+1)*ldx:], ldx, a[i:], lda, 1, a[(i+1)*lda+i:], lda)
   158  
   159  			// Generate reflection Q[i] to annihilate A[i+2:m, i].
   160  			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)
   161  			e[i] = a[(i+1)*lda+i]
   162  			a[(i+1)*lda+i] = 1
   163  
   164  			// Compute Y[i+1:n, i].
   165  			bi.Dgemv(blas.Trans, m-i-1, n-i-1, 1, a[(i+1)*lda+i+1:], lda, a[(i+1)*lda+i:], lda, 0, y[(i+1)*ldy+i:], ldy)
   166  			bi.Dgemv(blas.Trans, m-i-1, i, 1, a[(i+1)*lda:], lda, a[(i+1)*lda+i:], lda, 0, y[i:], ldy)
   167  			bi.Dgemv(blas.NoTrans, n-i-1, i, -1, y[(i+1)*ldy:], ldy, y[i:], ldy, 1, y[(i+1)*ldy+i:], ldy)
   168  			bi.Dgemv(blas.Trans, m-i-1, i+1, 1, x[(i+1)*ldx:], ldx, a[(i+1)*lda+i:], lda, 0, y[i:], ldy)
   169  			bi.Dgemv(blas.Trans, i+1, n-i-1, -1, a[i+1:], lda, y[i:], ldy, 1, y[(i+1)*ldy+i:], ldy)
   170  			bi.Dscal(n-i-1, tauQ[i], y[(i+1)*ldy+i:], ldy)
   171  		}
   172  	}
   173  }