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.
8 "gonum.org/v1/gonum/blas"
9 "gonum.org/v1/gonum/blas/blas64"
10 "gonum.org/v1/gonum/lapack"
13 // Dgehrd reduces a block of a real n×n general matrix A to upper Hessenberg
14 // form H by an orthogonal similarity transformation Q^T * A * Q = H.
16 // The matrix Q is represented as a product of (ihi-ilo) elementary
18 // Q = H_{ilo} H_{ilo+1} ... H_{ihi-1}.
19 // Each H_i has the form
20 // H_i = I - tau[i] * v * v^T
21 // where v is a real vector with v[0:i+1] = 0, v[i+1] = 1 and v[ihi+1:n] = 0.
22 // v[i+2:ihi+1] is stored on exit in A[i+2:ihi+1,i].
24 // On entry, a contains the n×n general matrix to be reduced. On return, the
25 // upper triangle and the first subdiagonal of A will be overwritten with the
26 // upper Hessenberg matrix H, and the elements below the first subdiagonal, with
27 // the slice tau, represent the orthogonal matrix Q as a product of elementary
30 // The contents of a are illustrated by the following example, with n = 7, ilo =
48 // where a denotes an element of the original matrix A, h denotes a
49 // modified element of the upper Hessenberg matrix H, and vi denotes an
50 // element of the vector defining H_i.
52 // ilo and ihi determine the block of A that will be reduced to upper Hessenberg
53 // form. It must hold that 0 <= ilo <= ihi < n if n > 0, and ilo == 0 and ihi ==
54 // -1 if n == 0, otherwise Dgehrd will panic.
56 // On return, tau will contain the scalar factors of the elementary reflectors.
57 // Elements tau[:ilo] and tau[ihi:] will be set to zero. tau must have length
58 // equal to n-1 if n > 0, otherwise Dgehrd will panic.
60 // work must have length at least lwork and lwork must be at least max(1,n),
61 // otherwise Dgehrd will panic. On return, work[0] contains the optimal value of
64 // If lwork == -1, instead of performing Dgehrd, only the optimal value of lwork
65 // will be stored in work[0].
67 // Dgehrd is an internal routine. It is exported for testing purposes.
68 func (impl Implementation) Dgehrd(n, ilo, ihi int, a []float64, lda int, tau, work []float64, lwork int) {
70 case ilo < 0 || max(0, n-1) < ilo:
72 case ihi < min(ilo, n-1) || n <= ihi:
74 case lwork < max(1, n) && lwork != -1:
76 case len(work) < lwork:
80 checkMatrix(n, n, a, lda)
81 if len(tau) != n-1 && n > 0 {
91 // Compute the workspace requirements.
92 nb := min(nbmax, impl.Ilaenv(1, "DGEHRD", " ", n, ilo, ihi, -1))
93 lwkopt := n*nb + tsize
95 work[0] = float64(lwkopt)
99 // Set tau[:ilo] and tau[ihi:] to zero.
100 for i := 0; i < ilo; i++ {
103 for i := ihi; i < n-1; i++ {
107 // Quick return if possible.
114 // Determine the block size.
117 if 1 < nb && nb < nh {
118 // Determine when to cross over from blocked to unblocked code
119 // (last block is always handled by unblocked code).
120 nx = max(nb, impl.Ilaenv(3, "DGEHRD", " ", n, ilo, ihi, -1))
122 // Determine if workspace is large enough for blocked code.
123 if lwork < n*nb+tsize {
124 // Not enough workspace to use optimal nb:
125 // determine the minimum value of nb, and reduce
126 // nb or force use of unblocked code.
127 nbmin = max(2, impl.Ilaenv(2, "DGEHRD", " ", n, ilo, ihi, -1))
128 if lwork >= n*nbmin+tsize {
129 nb = (lwork - tsize) / n
136 ldwork := nb // work is used as an n×nb matrix.
139 if nb < nbmin || nh <= nb {
140 // Use unblocked code below.
144 bi := blas64.Implementation()
145 iwt := n * nb // Size of the matrix Y and index where the matrix T starts in work.
146 for i = ilo; i < ihi-nx; i += nb {
149 // Reduce columns [i:i+ib] to Hessenberg form, returning the
150 // matrices V and T of the block reflector H = I - V*T*V^T
151 // which performs the reduction, and also the matrix Y = A*V*T.
152 impl.Dlahr2(ihi+1, i+1, ib, a[i:], lda, tau[i:], work[iwt:], ldt, work, ldwork)
154 // Apply the block reflector H to A[:ihi+1,i+ib:ihi+1] from the
155 // right, computing A := A - Y * V^T. V[i+ib,i+ib-1] must be set
157 ei := a[(i+ib)*lda+i+ib-1]
158 a[(i+ib)*lda+i+ib-1] = 1
159 bi.Dgemm(blas.NoTrans, blas.Trans, ihi+1, ihi-i-ib+1, ib,
161 a[(i+ib)*lda+i:], lda,
163 a[(i+ib)*lda+i+ib-1] = ei
165 // Apply the block reflector H to A[0:i+1,i+1:i+ib-1] from the
167 bi.Dtrmm(blas.Right, blas.Lower, blas.Trans, blas.Unit, i+1, ib-1,
168 1, a[(i+1)*lda+i:], lda, work, ldwork)
169 for j := 0; j <= ib-2; j++ {
170 bi.Daxpy(i+1, -1, work[j:], ldwork, a[i+j+1:], lda)
173 // Apply the block reflector H to A[i+1:ihi+1,i+ib:n] from the
175 impl.Dlarfb(blas.Left, blas.Trans, lapack.Forward, lapack.ColumnWise,
177 a[(i+1)*lda+i:], lda, work[iwt:], ldt, a[(i+1)*lda+i+ib:], lda, work, ldwork)
180 // Use unblocked code to reduce the rest of the matrix.
181 impl.Dgehd2(n, i, ihi, a, lda, tau, work)
182 work[0] = float64(lwkopt)