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