github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/lapack/gonum/dlatrd.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 (
     8  	"github.com/jingcheng-WU/gonum/blas"
     9  	"github.com/jingcheng-WU/gonum/blas/blas64"
    10  )
    11  
    12  // Dlatrd reduces nb rows and columns of a real n×n symmetric matrix A to symmetric
    13  // tridiagonal form. It computes the orthonormal similarity transformation
    14  //  Qᵀ * A * Q
    15  // and returns the matrices V and W to apply to the unreduced part of A. If
    16  // uplo == blas.Upper, the upper triangle is supplied and the last nb rows are
    17  // reduced. If uplo == blas.Lower, the lower triangle is supplied and the first
    18  // nb rows are reduced.
    19  //
    20  // a contains the symmetric matrix on entry with active triangular half specified
    21  // by uplo. On exit, the nb columns have been reduced to tridiagonal form. The
    22  // diagonal contains the diagonal of the reduced matrix, the off-diagonal is
    23  // set to 1, and the remaining elements contain the data to construct Q.
    24  //
    25  // If uplo == blas.Upper, with n = 5 and nb = 2 on exit a is
    26  //  [ a   a   a  v4  v5]
    27  //  [     a   a  v4  v5]
    28  //  [         a   1  v5]
    29  //  [             d   1]
    30  //  [                 d]
    31  //
    32  // If uplo == blas.Lower, with n = 5 and nb = 2, on exit a is
    33  //  [ d                ]
    34  //  [ 1   d            ]
    35  //  [v1   1   a        ]
    36  //  [v1  v2   a   a    ]
    37  //  [v1  v2   a   a   a]
    38  //
    39  // e contains the superdiagonal elements of the reduced matrix. If uplo == blas.Upper,
    40  // e[n-nb:n-1] contains the last nb columns of the reduced matrix, while if
    41  // uplo == blas.Lower, e[:nb] contains the first nb columns of the reduced matrix.
    42  // e must have length at least n-1, and Dlatrd will panic otherwise.
    43  //
    44  // tau contains the scalar factors of the elementary reflectors needed to construct Q.
    45  // The reflectors are stored in tau[n-nb:n-1] if uplo == blas.Upper, and in
    46  // tau[:nb] if uplo == blas.Lower. tau must have length n-1, and Dlatrd will panic
    47  // otherwise.
    48  //
    49  // w is an n×nb matrix. On exit it contains the data to update the unreduced part
    50  // of A.
    51  //
    52  // The matrix Q is represented as a product of elementary reflectors. Each reflector
    53  // H has the form
    54  //  I - tau * v * vᵀ
    55  // If uplo == blas.Upper,
    56  //  Q = H_{n-1} * H_{n-2} * ... * H_{n-nb}
    57  // where v[:i-1] is stored in A[:i-1,i], v[i-1] = 1, and v[i:n] = 0.
    58  //
    59  // If uplo == blas.Lower,
    60  //  Q = H_0 * H_1 * ... * H_{nb-1}
    61  // where v[:i+1] = 0, v[i+1] = 1, and v[i+2:n] is stored in A[i+2:n,i].
    62  //
    63  // The vectors v form the n×nb matrix V which is used with W to apply a
    64  // symmetric rank-2 update to the unreduced part of A
    65  //  A = A - V * Wᵀ - W * Vᵀ
    66  //
    67  // Dlatrd is an internal routine. It is exported for testing purposes.
    68  func (impl Implementation) Dlatrd(uplo blas.Uplo, n, nb int, a []float64, lda int, e, tau, w []float64, ldw int) {
    69  	switch {
    70  	case uplo != blas.Upper && uplo != blas.Lower:
    71  		panic(badUplo)
    72  	case n < 0:
    73  		panic(nLT0)
    74  	case nb < 0:
    75  		panic(nbLT0)
    76  	case nb > n:
    77  		panic(nbGTN)
    78  	case lda < max(1, n):
    79  		panic(badLdA)
    80  	case ldw < max(1, nb):
    81  		panic(badLdW)
    82  	}
    83  
    84  	if n == 0 {
    85  		return
    86  	}
    87  
    88  	switch {
    89  	case len(a) < (n-1)*lda+n:
    90  		panic(shortA)
    91  	case len(w) < (n-1)*ldw+nb:
    92  		panic(shortW)
    93  	case len(e) < n-1:
    94  		panic(shortE)
    95  	case len(tau) < n-1:
    96  		panic(shortTau)
    97  	}
    98  
    99  	bi := blas64.Implementation()
   100  
   101  	if uplo == blas.Upper {
   102  		for i := n - 1; i >= n-nb; i-- {
   103  			iw := i - n + nb
   104  			if i < n-1 {
   105  				// Update A(0:i, i).
   106  				bi.Dgemv(blas.NoTrans, i+1, n-i-1, -1, a[i+1:], lda,
   107  					w[i*ldw+iw+1:], 1, 1, a[i:], lda)
   108  				bi.Dgemv(blas.NoTrans, i+1, n-i-1, -1, w[iw+1:], ldw,
   109  					a[i*lda+i+1:], 1, 1, a[i:], lda)
   110  			}
   111  			if i > 0 {
   112  				// Generate elementary reflector H_i to annihilate A(0:i-2,i).
   113  				e[i-1], tau[i-1] = impl.Dlarfg(i, a[(i-1)*lda+i], a[i:], lda)
   114  				a[(i-1)*lda+i] = 1
   115  
   116  				// Compute W(0:i-1, i).
   117  				bi.Dsymv(blas.Upper, i, 1, a, lda, a[i:], lda, 0, w[iw:], ldw)
   118  				if i < n-1 {
   119  					bi.Dgemv(blas.Trans, i, n-i-1, 1, w[iw+1:], ldw,
   120  						a[i:], lda, 0, w[(i+1)*ldw+iw:], ldw)
   121  					bi.Dgemv(blas.NoTrans, i, n-i-1, -1, a[i+1:], lda,
   122  						w[(i+1)*ldw+iw:], ldw, 1, w[iw:], ldw)
   123  					bi.Dgemv(blas.Trans, i, n-i-1, 1, a[i+1:], lda,
   124  						a[i:], lda, 0, w[(i+1)*ldw+iw:], ldw)
   125  					bi.Dgemv(blas.NoTrans, i, n-i-1, -1, w[iw+1:], ldw,
   126  						w[(i+1)*ldw+iw:], ldw, 1, w[iw:], ldw)
   127  				}
   128  				bi.Dscal(i, tau[i-1], w[iw:], ldw)
   129  				alpha := -0.5 * tau[i-1] * bi.Ddot(i, w[iw:], ldw, a[i:], lda)
   130  				bi.Daxpy(i, alpha, a[i:], lda, w[iw:], ldw)
   131  			}
   132  		}
   133  	} else {
   134  		// Reduce first nb columns of lower triangle.
   135  		for i := 0; i < nb; i++ {
   136  			// Update A(i:n, i)
   137  			bi.Dgemv(blas.NoTrans, n-i, i, -1, a[i*lda:], lda,
   138  				w[i*ldw:], 1, 1, a[i*lda+i:], lda)
   139  			bi.Dgemv(blas.NoTrans, n-i, i, -1, w[i*ldw:], ldw,
   140  				a[i*lda:], 1, 1, a[i*lda+i:], lda)
   141  			if i < n-1 {
   142  				// Generate elementary reflector H_i to annihilate A(i+2:n,i).
   143  				e[i], tau[i] = impl.Dlarfg(n-i-1, a[(i+1)*lda+i], a[min(i+2, n-1)*lda+i:], lda)
   144  				a[(i+1)*lda+i] = 1
   145  
   146  				// Compute W(i+1:n,i).
   147  				bi.Dsymv(blas.Lower, n-i-1, 1, a[(i+1)*lda+i+1:], lda,
   148  					a[(i+1)*lda+i:], lda, 0, w[(i+1)*ldw+i:], ldw)
   149  				bi.Dgemv(blas.Trans, n-i-1, i, 1, w[(i+1)*ldw:], ldw,
   150  					a[(i+1)*lda+i:], lda, 0, w[i:], ldw)
   151  				bi.Dgemv(blas.NoTrans, n-i-1, i, -1, a[(i+1)*lda:], lda,
   152  					w[i:], ldw, 1, w[(i+1)*ldw+i:], ldw)
   153  				bi.Dgemv(blas.Trans, n-i-1, i, 1, a[(i+1)*lda:], lda,
   154  					a[(i+1)*lda+i:], lda, 0, w[i:], ldw)
   155  				bi.Dgemv(blas.NoTrans, n-i-1, i, -1, w[(i+1)*ldw:], ldw,
   156  					w[i:], ldw, 1, w[(i+1)*ldw+i:], ldw)
   157  				bi.Dscal(n-i-1, tau[i], w[(i+1)*ldw+i:], ldw)
   158  				alpha := -0.5 * tau[i] * bi.Ddot(n-i-1, w[(i+1)*ldw+i:], ldw,
   159  					a[(i+1)*lda+i:], lda)
   160  				bi.Daxpy(n-i-1, alpha, a[(i+1)*lda+i:], lda,
   161  					w[(i+1)*ldw+i:], ldw)
   162  			}
   163  		}
   164  	}
   165  }