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.
10 "gonum.org/v1/gonum/blas"
11 "gonum.org/v1/gonum/blas/blas64"
12 "gonum.org/v1/gonum/lapack"
15 // Dlaexc swaps two adjacent diagonal blocks of order 1 or 2 in an n×n upper
16 // quasi-triangular matrix T by an orthogonal similarity transformation.
18 // T must be in Schur canonical form, that is, block upper triangular with 1×1
19 // and 2×2 diagonal blocks; each 2×2 diagonal block has its diagonal elements
20 // equal and its off-diagonal elements of opposite sign. On return, T will
21 // contain the updated matrix again in Schur canonical form.
23 // If wantq is true, the transformation is accumulated in the n×n matrix Q,
24 // otherwise Q is not referenced.
26 // j1 is the index of the first row of the first block. n1 and n2 are the order
27 // of the first and second block, respectively.
29 // work must have length at least n, otherwise Dlaexc will panic.
31 // If ok is false, the transformed matrix T would be too far from Schur form.
32 // The blocks are not swapped, and T and Q are not modified.
34 // If n1 and n2 are both equal to 1, Dlaexc will always return true.
36 // Dlaexc is an internal routine. It is exported for testing purposes.
37 func (impl Implementation) Dlaexc(wantq bool, n int, t []float64, ldt int, q []float64, ldq int, j1, n1, n2 int, work []float64) (ok bool) {
38 checkMatrix(n, n, t, ldt)
40 checkMatrix(n, n, q, ldq)
42 if j1 < 0 || n <= j1 {
43 panic("lapack: index j1 out of range")
49 panic("lapack: invalid value of n1")
52 panic("lapack: invalid value of n2")
55 if n == 0 || n1 == 0 || n2 == 0 {
59 // TODO(vladimir-ch): Reference LAPACK does this check whether
60 // the start of the second block is in the matrix T. It returns
61 // true if it is not and moreover it does not check whether the
62 // whole second block fits into T. This does not feel
63 // satisfactory. The only caller of Dlaexc is Dtrexc, so if the
64 // caller makes sure that this does not happen, we could be
72 bi := blas64.Implementation()
74 if n1 == 1 && n2 == 1 {
75 // Swap two 1×1 blocks.
79 // Determine the transformation to perform the interchange.
80 cs, sn, _ := impl.Dlartg(t[j1*ldt+j2], t22-t11)
82 // Apply transformation to the matrix T.
84 bi.Drot(n-j3, t[j1*ldt+j3:], 1, t[j2*ldt+j3:], 1, cs, sn)
87 bi.Drot(j1, t[j1:], ldt, t[j2:], ldt, cs, sn)
94 // Accumulate transformation in the matrix Q.
95 bi.Drot(n, q[j1:], ldq, q[j2:], ldq, cs, sn)
101 // Swapping involves at least one 2×2 block.
103 // Copy the diagonal block of order n1+n2 to the local array d and
108 impl.Dlacpy(blas.All, nd, nd, t[j1*ldt+j1:], ldt, d[:], ldd)
109 dnorm := impl.Dlange(lapack.MaxAbs, nd, nd, d[:], ldd, work)
111 // Compute machine-dependent threshold for test for accepting swap.
113 thresh := math.Max(10*eps*dnorm, dlamchS/eps)
115 // Solve T11*X - X*T22 = scale*T12 for X.
118 scale, _, _ := impl.Dlasy2(false, false, -1, n1, n2, d[:], ldd, d[n1*ldd+n1:], ldd, d[n1:], ldd, x[:], ldx)
120 // Swap the adjacent diagonal blocks.
122 case n1 == 1 && n2 == 2:
123 // Generate elementary reflector H so that
124 // ( scale, X11, X12 ) H = ( 0, 0, * )
125 u := [3]float64{scale, x[0], 1}
126 _, tau := impl.Dlarfg(3, x[1], u[:2], 1)
129 // Perform swap provisionally on diagonal block in d.
130 impl.Dlarfx(blas.Left, 3, 3, u[:], tau, d[:], ldd, work)
131 impl.Dlarfx(blas.Right, 3, 3, u[:], tau, d[:], ldd, work)
133 // Test whether to reject swap.
134 if math.Max(math.Abs(d[2*ldd]), math.Max(math.Abs(d[2*ldd+1]), math.Abs(d[2*ldd+2]-t11))) > thresh {
138 // Accept swap: apply transformation to the entire matrix T.
139 impl.Dlarfx(blas.Left, 3, n-j1, u[:], tau, t[j1*ldt+j1:], ldt, work)
140 impl.Dlarfx(blas.Right, j2+1, 3, u[:], tau, t[j1:], ldt, work)
147 // Accumulate transformation in the matrix Q.
148 impl.Dlarfx(blas.Right, n, 3, u[:], tau, q[j1:], ldq, work)
151 case n1 == 2 && n2 == 1:
152 // Generate elementary reflector H so that:
153 // H ( -X11 ) = ( * )
156 u := [3]float64{1, -x[ldx], scale}
157 _, tau := impl.Dlarfg(3, -x[0], u[1:], 1)
160 // Perform swap provisionally on diagonal block in D.
161 impl.Dlarfx(blas.Left, 3, 3, u[:], tau, d[:], ldd, work)
162 impl.Dlarfx(blas.Right, 3, 3, u[:], tau, d[:], ldd, work)
164 // Test whether to reject swap.
165 if math.Max(math.Abs(d[ldd]), math.Max(math.Abs(d[2*ldd]), math.Abs(d[0]-t33))) > thresh {
169 // Accept swap: apply transformation to the entire matrix T.
170 impl.Dlarfx(blas.Right, j3+1, 3, u[:], tau, t[j1:], ldt, work)
171 impl.Dlarfx(blas.Left, 3, n-j1-1, u[:], tau, t[j1*ldt+j2:], ldt, work)
178 // Accumulate transformation in the matrix Q.
179 impl.Dlarfx(blas.Right, n, 3, u[:], tau, q[j1:], ldq, work)
182 default: // n1 == 2 && n2 == 2
183 // Generate elementary reflectors H_1 and H_2 so that:
184 // H_2 H_1 ( -X11 -X12 ) = ( * * )
185 // ( -X21 -X22 ) ( 0 * )
186 // ( scale 0 ) ( 0 0 )
187 // ( 0 scale ) ( 0 0 )
188 u1 := [3]float64{1, -x[ldx], scale}
189 _, tau1 := impl.Dlarfg(3, -x[0], u1[1:], 1)
191 temp := -tau1 * (x[1] + u1[1]*x[ldx+1])
192 u2 := [3]float64{1, -temp * u1[2], scale}
193 _, tau2 := impl.Dlarfg(3, -temp*u1[1]-x[ldx+1], u2[1:], 1)
195 // Perform swap provisionally on diagonal block in D.
196 impl.Dlarfx(blas.Left, 3, 4, u1[:], tau1, d[:], ldd, work)
197 impl.Dlarfx(blas.Right, 4, 3, u1[:], tau1, d[:], ldd, work)
198 impl.Dlarfx(blas.Left, 3, 4, u2[:], tau2, d[ldd:], ldd, work)
199 impl.Dlarfx(blas.Right, 4, 3, u2[:], tau2, d[1:], ldd, work)
201 // Test whether to reject swap.
202 m1 := math.Max(math.Abs(d[2*ldd]), math.Abs(d[2*ldd+1]))
203 m2 := math.Max(math.Abs(d[3*ldd]), math.Abs(d[3*ldd+1]))
204 if math.Max(m1, m2) > thresh {
208 // Accept swap: apply transformation to the entire matrix T.
210 impl.Dlarfx(blas.Left, 3, n-j1, u1[:], tau1, t[j1*ldt+j1:], ldt, work)
211 impl.Dlarfx(blas.Right, j4+1, 3, u1[:], tau1, t[j1:], ldt, work)
212 impl.Dlarfx(blas.Left, 3, n-j1, u2[:], tau2, t[j2*ldt+j1:], ldt, work)
213 impl.Dlarfx(blas.Right, j4+1, 3, u2[:], tau2, t[j2:], ldt, work)
221 // Accumulate transformation in the matrix Q.
222 impl.Dlarfx(blas.Right, n, 3, u1[:], tau1, q[j1:], ldq, work)
223 impl.Dlarfx(blas.Right, n, 3, u2[:], tau2, q[j2:], ldq, work)
228 // Standardize new 2×2 block T11.
229 a, b := t[j1*ldt+j1], t[j1*ldt+j2]
230 c, d := t[j2*ldt+j1], t[j2*ldt+j2]
232 t[j1*ldt+j1], t[j1*ldt+j2], t[j2*ldt+j1], t[j2*ldt+j2], _, _, _, _, cs, sn = impl.Dlanv2(a, b, c, d)
234 bi.Drot(n-j1-2, t[j1*ldt+j1+2:], 1, t[j2*ldt+j1+2:], 1, cs, sn)
237 bi.Drot(j1, t[j1:], ldt, t[j2:], ldt, cs, sn)
240 bi.Drot(n, q[j1:], ldq, q[j2:], ldq, cs, sn)
244 // Standardize new 2×2 block T22.
247 a, b := t[j3*ldt+j3], t[j3*ldt+j4]
248 c, d := t[j4*ldt+j3], t[j4*ldt+j4]
250 t[j3*ldt+j3], t[j3*ldt+j4], t[j4*ldt+j3], t[j4*ldt+j4], _, _, _, _, cs, sn = impl.Dlanv2(a, b, c, d)
252 bi.Drot(n-j3-2, t[j3*ldt+j3+2:], 1, t[j4*ldt+j3+2:], 1, cs, sn)
254 bi.Drot(j3, t[j3:], ldt, t[j4:], ldt, cs, sn)
256 bi.Drot(n, q[j3:], ldq, q[j4:], ldq, cs, sn)