1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
|
// Copyright ©2016 The Gonum Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gonum
import "gonum.org/v1/gonum/lapack"
// Dtrexc reorders the real Schur factorization of a n×n real matrix
//
// A = Q*T*Qᵀ
//
// so that the diagonal block of T with row index ifst is moved to row ilst.
//
// On entry, T must be in Schur canonical form, that is, block upper triangular
// with 1×1 and 2×2 diagonal blocks; each 2×2 diagonal block has its diagonal
// elements equal and its off-diagonal elements of opposite sign.
//
// On return, T will be reordered by an orthogonal similarity transformation Z
// as Zᵀ*T*Z, and will be again in Schur canonical form.
//
// If compq is lapack.UpdateSchur, on return the matrix Q of Schur vectors will be
// updated by post-multiplying it with Z.
// If compq is lapack.UpdateSchurNone, the matrix Q is not referenced and will not be
// updated.
// For other values of compq Dtrexc will panic.
//
// ifst and ilst specify the reordering of the diagonal blocks of T. The block
// with row index ifst is moved to row ilst, by a sequence of transpositions
// between adjacent blocks.
//
// If ifst points to the second row of a 2×2 block, ifstOut will point to the
// first row, otherwise it will be equal to ifst.
//
// ilstOut will point to the first row of the block in its final position. If ok
// is true, ilstOut may differ from ilst by +1 or -1.
//
// It must hold that
//
// 0 <= ifst < n, and 0 <= ilst < n,
//
// otherwise Dtrexc will panic.
//
// If ok is false, two adjacent blocks were too close to swap because the
// problem is very ill-conditioned. T may have been partially reordered, and
// ilstOut will point to the first row of the block at the position to which it
// has been moved.
//
// work must have length at least n, otherwise Dtrexc will panic.
//
// Dtrexc is an internal routine. It is exported for testing purposes.
func (impl Implementation) Dtrexc(compq lapack.UpdateSchurComp, n int, t []float64, ldt int, q []float64, ldq int, ifst, ilst int, work []float64) (ifstOut, ilstOut int, ok bool) {
switch {
case compq != lapack.UpdateSchur && compq != lapack.UpdateSchurNone:
panic(badUpdateSchurComp)
case n < 0:
panic(nLT0)
case ldt < max(1, n):
panic(badLdT)
case ldq < 1, compq == lapack.UpdateSchur && ldq < n:
panic(badLdQ)
case (ifst < 0 || n <= ifst) && n > 0:
panic(badIfst)
case (ilst < 0 || n <= ilst) && n > 0:
panic(badIlst)
}
// Quick return if possible.
if n == 0 {
return ifst, ilst, true
}
switch {
case len(t) < (n-1)*ldt+n:
panic(shortT)
case compq == lapack.UpdateSchur && len(q) < (n-1)*ldq+n:
panic(shortQ)
case len(work) < n:
panic(shortWork)
}
// Quick return if possible.
if n == 1 {
return ifst, ilst, true
}
// Determine the first row of specified block
// and find out it is 1×1 or 2×2.
if ifst > 0 && t[ifst*ldt+ifst-1] != 0 {
ifst--
}
nbf := 1 // Size of the first block.
if ifst+1 < n && t[(ifst+1)*ldt+ifst] != 0 {
nbf = 2
}
// Determine the first row of the final block
// and find out it is 1×1 or 2×2.
if ilst > 0 && t[ilst*ldt+ilst-1] != 0 {
ilst--
}
nbl := 1 // Size of the last block.
if ilst+1 < n && t[(ilst+1)*ldt+ilst] != 0 {
nbl = 2
}
ok = true
wantq := compq == lapack.UpdateSchur
switch {
case ifst == ilst:
return ifst, ilst, true
case ifst < ilst:
// Update ilst.
switch {
case nbf == 2 && nbl == 1:
ilst--
case nbf == 1 && nbl == 2:
ilst++
}
here := ifst
for here < ilst {
// Swap block with next one below.
if nbf == 1 || nbf == 2 {
// Current block either 1×1 or 2×2.
nbnext := 1 // Size of the next block.
if here+nbf+1 < n && t[(here+nbf+1)*ldt+here+nbf] != 0 {
nbnext = 2
}
ok = impl.Dlaexc(wantq, n, t, ldt, q, ldq, here, nbf, nbnext, work)
if !ok {
return ifst, here, false
}
here += nbnext
// Test if 2×2 block breaks into two 1×1 blocks.
if nbf == 2 && t[(here+1)*ldt+here] == 0 {
nbf = 3
}
continue
}
// Current block consists of two 1×1 blocks each of
// which must be swapped individually.
nbnext := 1 // Size of the next block.
if here+3 < n && t[(here+3)*ldt+here+2] != 0 {
nbnext = 2
}
ok = impl.Dlaexc(wantq, n, t, ldt, q, ldq, here+1, 1, nbnext, work)
if !ok {
return ifst, here, false
}
if nbnext == 1 {
// Swap two 1×1 blocks, no problems possible.
impl.Dlaexc(wantq, n, t, ldt, q, ldq, here, 1, nbnext, work)
here++
continue
}
// Recompute nbnext in case 2×2 split.
if t[(here+2)*ldt+here+1] == 0 {
nbnext = 1
}
if nbnext == 2 {
// 2×2 block did not split.
ok = impl.Dlaexc(wantq, n, t, ldt, q, ldq, here, 1, nbnext, work)
if !ok {
return ifst, here, false
}
} else {
// 2×2 block did split.
impl.Dlaexc(wantq, n, t, ldt, q, ldq, here, 1, 1, work)
impl.Dlaexc(wantq, n, t, ldt, q, ldq, here+1, 1, 1, work)
}
here += 2
}
return ifst, here, true
default: // ifst > ilst
here := ifst
for here > ilst {
// Swap block with next one above.
nbnext := 1
if here >= 2 && t[(here-1)*ldt+here-2] != 0 {
nbnext = 2
}
if nbf == 1 || nbf == 2 {
// Current block either 1×1 or 2×2.
ok = impl.Dlaexc(wantq, n, t, ldt, q, ldq, here-nbnext, nbnext, nbf, work)
if !ok {
return ifst, here, false
}
here -= nbnext
// Test if 2×2 block breaks into two 1×1 blocks.
if nbf == 2 && t[(here+1)*ldt+here] == 0 {
nbf = 3
}
continue
}
// Current block consists of two 1×1 blocks each of
// which must be swapped individually.
ok = impl.Dlaexc(wantq, n, t, ldt, q, ldq, here-nbnext, nbnext, 1, work)
if !ok {
return ifst, here, false
}
if nbnext == 1 {
// Swap two 1×1 blocks, no problems possible.
impl.Dlaexc(wantq, n, t, ldt, q, ldq, here, nbnext, 1, work)
here--
continue
}
// Recompute nbnext in case 2×2 split.
if t[here*ldt+here-1] == 0 {
nbnext = 1
}
if nbnext == 2 {
// 2×2 block did not split.
ok = impl.Dlaexc(wantq, n, t, ldt, q, ldq, here-1, 2, 1, work)
if !ok {
return ifst, here, false
}
} else {
// 2×2 block did split.
impl.Dlaexc(wantq, n, t, ldt, q, ldq, here, 1, 1, work)
impl.Dlaexc(wantq, n, t, ldt, q, ldq, here-1, 1, 1, work)
}
here -= 2
}
return ifst, here, true
}
}
|