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/blas64"
13 // Dlasy2 solves the Sylvester matrix equation where the matrices are of order 1
14 // or 2. It computes the unknown n1×n2 matrix X so that
15 // TL*X + sgn*X*TR = scale*B, if tranl == false and tranr == false,
16 // TL^T*X + sgn*X*TR = scale*B, if tranl == true and tranr == false,
17 // TL*X + sgn*X*TR^T = scale*B, if tranl == false and tranr == true,
18 // TL^T*X + sgn*X*TR^T = scale*B, if tranl == true and tranr == true,
19 // where TL is n1×n1, TR is n2×n2, B is n1×n2, and 1 <= n1,n2 <= 2.
21 // isgn must be 1 or -1, and n1 and n2 must be 0, 1, or 2, but these conditions
24 // Dlasy2 returns three values, a scale factor that is chosen less than or equal
25 // to 1 to prevent the solution overflowing, the infinity norm of the solution,
26 // and an indicator of success. If ok is false, TL and TR have eigenvalues that
27 // are too close, so TL or TR is perturbed to get a non-singular equation.
29 // Dlasy2 is an internal routine. It is exported for testing purposes.
30 func (impl Implementation) Dlasy2(tranl, tranr bool, isgn, n1, n2 int, tl []float64, ldtl int, tr []float64, ldtr int, b []float64, ldb int, x []float64, ldx int) (scale, xnorm float64, ok bool) {
31 // TODO(vladimir-ch): Add input validation checks conditionally skipped
32 // using the build tag mechanism.
35 // Quick return if possible.
36 if n1 == 0 || n2 == 0 {
37 return scale, xnorm, ok
40 // Set constants to control overflow.
42 smlnum := dlamchS / eps
45 if n1 == 1 && n2 == 1 {
46 // 1×1 case: TL11*X + sgn*X*TR11 = B11.
47 tau1 := tl[0] + sgn*tr[0]
59 x[0] = b[0] * scale / tau1
60 xnorm = math.Abs(x[0])
61 return scale, xnorm, ok
68 tmp [4]float64 // tmp is used as a 2×2 row-major matrix.
71 if n1 == 1 && n2 == 2 {
72 // 1×2 case: TL11*[X11 X12] + sgn*[X11 X12]*op[TR11 TR12] = [B11 B12].
74 smin = math.Abs(tl[0])
75 smin = math.Max(smin, math.Max(math.Abs(tr[0]), math.Abs(tr[1])))
76 smin = math.Max(smin, math.Max(math.Abs(tr[ldtr]), math.Abs(tr[ldtr+1])))
77 smin = math.Max(eps*smin, smlnum)
78 tmp[0] = tl[0] + sgn*tr[0]
79 tmp[3] = tl[0] + sgn*tr[ldtr+1]
82 tmp[2] = sgn * tr[ldtr]
84 tmp[1] = sgn * tr[ldtr]
90 // 2×1 case: op[TL11 TL12]*[X11] + sgn*[X11]*TR11 = [B11].
91 // [TL21 TL22]*[X21] [X21] [B21]
92 smin = math.Abs(tr[0])
93 smin = math.Max(smin, math.Max(math.Abs(tl[0]), math.Abs(tl[1])))
94 smin = math.Max(smin, math.Max(math.Abs(tl[ldtl]), math.Abs(tl[ldtl+1])))
95 smin = math.Max(eps*smin, smlnum)
96 tmp[0] = tl[0] + sgn*tr[0]
97 tmp[3] = tl[ldtl+1] + sgn*tr[0]
109 // Solve 2×2 system using complete pivoting.
110 // Set pivots less than smin to smin.
112 bi := blas64.Implementation()
113 ipiv := bi.Idamax(len(tmp), tmp[:], 1)
114 // Compute the upper triangular matrix [u11 u12].
117 if math.Abs(u11) <= smin {
121 locu12 := [4]int{1, 0, 3, 2} // Index in tmp of the element on the same row as the pivot.
122 u12 := tmp[locu12[ipiv]]
123 locl21 := [4]int{2, 3, 0, 1} // Index in tmp of the element on the same column as the pivot.
124 l21 := tmp[locl21[ipiv]] / u11
125 locu22 := [4]int{3, 2, 1, 0} // Index in tmp of the remaining element.
126 u22 := tmp[locu22[ipiv]] - l21*u12
127 if math.Abs(u22) <= smin {
131 if ipiv&0x2 != 0 { // true for ipiv equal to 2 and 3.
132 // The pivot was in the second row, swap the elements of
133 // the right-hand side.
134 btmp[0], btmp[1] = btmp[1], btmp[0]-l21*btmp[1]
136 btmp[1] -= l21 * btmp[0]
139 if 2*smlnum*math.Abs(btmp[1]) > math.Abs(u22) || 2*smlnum*math.Abs(btmp[0]) > math.Abs(u11) {
140 scale = 0.5 / math.Max(math.Abs(btmp[0]), math.Abs(btmp[1]))
144 // Solve the system [u11 u12] [x21] = [ btmp[0] ].
145 // [ 0 u22] [x22] [ btmp[1] ]
147 x21 := btmp[0]/u11 - (u12/u11)*x22
148 if ipiv&0x1 != 0 { // true for ipiv equal to 1 and 3.
149 // The pivot was in the second column, swap the elements
156 xnorm = math.Abs(x[0]) + math.Abs(x[1])
159 xnorm = math.Max(math.Abs(x[0]), math.Abs(x[ldx]))
161 return scale, xnorm, ok
164 // 2×2 case: op[TL11 TL12]*[X11 X12] + SGN*[X11 X12]*op[TR11 TR12] = [B11 B12].
165 // [TL21 TL22] [X21 X22] [X21 X22] [TR21 TR22] [B21 B22]
167 // Solve equivalent 4×4 system using complete pivoting.
168 // Set pivots less than smin to smin.
170 smin := math.Max(math.Abs(tr[0]), math.Abs(tr[1]))
171 smin = math.Max(smin, math.Max(math.Abs(tr[ldtr]), math.Abs(tr[ldtr+1])))
172 smin = math.Max(smin, math.Max(math.Abs(tl[0]), math.Abs(tl[1])))
173 smin = math.Max(smin, math.Max(math.Abs(tl[ldtl]), math.Abs(tl[ldtl+1])))
174 smin = math.Max(eps*smin, smlnum)
177 t[0][0] = tl[0] + sgn*tr[0]
178 t[1][1] = tl[0] + sgn*tr[ldtr+1]
179 t[2][2] = tl[ldtl+1] + sgn*tr[0]
180 t[3][3] = tl[ldtl+1] + sgn*tr[ldtr+1]
193 t[0][1] = sgn * tr[1]
194 t[1][0] = sgn * tr[ldtr]
195 t[2][3] = sgn * tr[1]
196 t[3][2] = sgn * tr[ldtr]
198 t[0][1] = sgn * tr[ldtr]
199 t[1][0] = sgn * tr[1]
200 t[2][3] = sgn * tr[ldtr]
201 t[3][2] = sgn * tr[1]
210 // Perform elimination.
211 var jpiv [4]int // jpiv records any column swaps for pivoting.
212 for i := 0; i < 3; i++ {
217 for ip := i; ip < 4; ip++ {
218 for jp := i; jp < 4; jp++ {
219 if math.Abs(t[ip][jp]) >= xmax {
220 xmax = math.Abs(t[ip][jp])
227 // The pivot is not in the top row of the unprocessed
228 // block, swap rows ipsv and i of t and btmp.
229 t[ipsv], t[i] = t[i], t[ipsv]
230 btmp[ipsv], btmp[i] = btmp[i], btmp[ipsv]
233 // The pivot is not in the left column of the
234 // unprocessed block, swap columns jpsv and i of t.
235 for k := 0; k < 4; k++ {
236 t[k][jpsv], t[k][i] = t[k][i], t[k][jpsv]
240 if math.Abs(t[i][i]) < smin {
244 for k := i + 1; k < 4; k++ {
246 btmp[k] -= t[k][i] * btmp[i]
247 for j := i + 1; j < 4; j++ {
248 t[k][j] -= t[k][i] * t[i][j]
252 if math.Abs(t[3][3]) < smin {
257 if 8*smlnum*math.Abs(btmp[0]) > math.Abs(t[0][0]) ||
258 8*smlnum*math.Abs(btmp[1]) > math.Abs(t[1][1]) ||
259 8*smlnum*math.Abs(btmp[2]) > math.Abs(t[2][2]) ||
260 8*smlnum*math.Abs(btmp[3]) > math.Abs(t[3][3]) {
262 maxbtmp := math.Max(math.Abs(btmp[0]), math.Abs(btmp[1]))
263 maxbtmp = math.Max(maxbtmp, math.Max(math.Abs(btmp[2]), math.Abs(btmp[3])))
264 scale = 1 / 8 / maxbtmp
270 // Compute the solution of the upper triangular system t * tmp = btmp.
272 for i := 3; i >= 0; i-- {
274 tmp[i] = btmp[i] * temp
275 for j := i + 1; j < 4; j++ {
276 tmp[i] -= temp * t[i][j] * tmp[j]
279 for i := 2; i >= 0; i-- {
281 tmp[i], tmp[jpiv[i]] = tmp[jpiv[i]], tmp[i]
288 xnorm = math.Max(math.Abs(tmp[0])+math.Abs(tmp[1]), math.Abs(tmp[2])+math.Abs(tmp[3]))
289 return scale, xnorm, ok