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
|
// Copyright ©2017 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 "math"
// Dlags2 computes 2-by-2 orthogonal matrices U, V and Q with the
// triangles of A and B specified by upper.
//
// If upper is true
//
// Uᵀ*A*Q = Uᵀ*[ a1 a2 ]*Q = [ x 0 ]
// [ 0 a3 ] [ x x ]
//
// and
//
// Vᵀ*B*Q = Vᵀ*[ b1 b2 ]*Q = [ x 0 ]
// [ 0 b3 ] [ x x ]
//
// otherwise
//
// Uᵀ*A*Q = Uᵀ*[ a1 0 ]*Q = [ x x ]
// [ a2 a3 ] [ 0 x ]
//
// and
//
// Vᵀ*B*Q = Vᵀ*[ b1 0 ]*Q = [ x x ]
// [ b2 b3 ] [ 0 x ].
//
// The rows of the transformed A and B are parallel, where
//
// U = [ csu snu ], V = [ csv snv ], Q = [ csq snq ]
// [ -snu csu ] [ -snv csv ] [ -snq csq ]
//
// Dlags2 is an internal routine. It is exported for testing purposes.
func (impl Implementation) Dlags2(upper bool, a1, a2, a3, b1, b2, b3 float64) (csu, snu, csv, snv, csq, snq float64) {
if upper {
// Input matrices A and B are upper triangular matrices.
//
// Form matrix C = A*adj(B) = [ a b ]
// [ 0 d ]
a := a1 * b3
d := a3 * b1
b := a2*b1 - a1*b2
// The SVD of real 2-by-2 triangular C.
//
// [ csl -snl ]*[ a b ]*[ csr snr ] = [ r 0 ]
// [ snl csl ] [ 0 d ] [ -snr csr ] [ 0 t ]
_, _, snr, csr, snl, csl := impl.Dlasv2(a, b, d)
if math.Abs(csl) >= math.Abs(snl) || math.Abs(csr) >= math.Abs(snr) {
// Compute the [0, 0] and [0, 1] elements of Uᵀ*A and Vᵀ*B,
// and [0, 1] element of |U|ᵀ*|A| and |V|ᵀ*|B|.
ua11r := csl * a1
ua12 := csl*a2 + snl*a3
vb11r := csr * b1
vb12 := csr*b2 + snr*b3
aua12 := math.Abs(csl)*math.Abs(a2) + math.Abs(snl)*math.Abs(a3)
avb12 := math.Abs(csr)*math.Abs(b2) + math.Abs(snr)*math.Abs(b3)
// Zero [0, 1] elements of Uᵀ*A and Vᵀ*B.
if math.Abs(ua11r)+math.Abs(ua12) != 0 {
if aua12/(math.Abs(ua11r)+math.Abs(ua12)) <= avb12/(math.Abs(vb11r)+math.Abs(vb12)) {
csq, snq, _ = impl.Dlartg(-ua11r, ua12)
} else {
csq, snq, _ = impl.Dlartg(-vb11r, vb12)
}
} else {
csq, snq, _ = impl.Dlartg(-vb11r, vb12)
}
csu = csl
snu = -snl
csv = csr
snv = -snr
} else {
// Compute the [1, 0] and [1, 1] elements of Uᵀ*A and Vᵀ*B,
// and [1, 1] element of |U|ᵀ*|A| and |V|ᵀ*|B|.
ua21 := -snl * a1
ua22 := -snl*a2 + csl*a3
vb21 := -snr * b1
vb22 := -snr*b2 + csr*b3
aua22 := math.Abs(snl)*math.Abs(a2) + math.Abs(csl)*math.Abs(a3)
avb22 := math.Abs(snr)*math.Abs(b2) + math.Abs(csr)*math.Abs(b3)
// Zero [1, 1] elements of Uᵀ*A and Vᵀ*B, and then swap.
if math.Abs(ua21)+math.Abs(ua22) != 0 {
if aua22/(math.Abs(ua21)+math.Abs(ua22)) <= avb22/(math.Abs(vb21)+math.Abs(vb22)) {
csq, snq, _ = impl.Dlartg(-ua21, ua22)
} else {
csq, snq, _ = impl.Dlartg(-vb21, vb22)
}
} else {
csq, snq, _ = impl.Dlartg(-vb21, vb22)
}
csu = snl
snu = csl
csv = snr
snv = csr
}
} else {
// Input matrices A and B are lower triangular matrices
//
// Form matrix C = A*adj(B) = [ a 0 ]
// [ c d ]
a := a1 * b3
d := a3 * b1
c := a2*b3 - a3*b2
// The SVD of real 2-by-2 triangular C
//
// [ csl -snl ]*[ a 0 ]*[ csr snr ] = [ r 0 ]
// [ snl csl ] [ c d ] [ -snr csr ] [ 0 t ]
_, _, snr, csr, snl, csl := impl.Dlasv2(a, c, d)
if math.Abs(csr) >= math.Abs(snr) || math.Abs(csl) >= math.Abs(snl) {
// Compute the [1, 0] and [1, 1] elements of Uᵀ*A and Vᵀ*B,
// and [1, 0] element of |U|ᵀ*|A| and |V|ᵀ*|B|.
ua21 := -snr*a1 + csr*a2
ua22r := csr * a3
vb21 := -snl*b1 + csl*b2
vb22r := csl * b3
aua21 := math.Abs(snr)*math.Abs(a1) + math.Abs(csr)*math.Abs(a2)
avb21 := math.Abs(snl)*math.Abs(b1) + math.Abs(csl)*math.Abs(b2)
// Zero [1, 0] elements of Uᵀ*A and Vᵀ*B.
if (math.Abs(ua21) + math.Abs(ua22r)) != 0 {
if aua21/(math.Abs(ua21)+math.Abs(ua22r)) <= avb21/(math.Abs(vb21)+math.Abs(vb22r)) {
csq, snq, _ = impl.Dlartg(ua22r, ua21)
} else {
csq, snq, _ = impl.Dlartg(vb22r, vb21)
}
} else {
csq, snq, _ = impl.Dlartg(vb22r, vb21)
}
csu = csr
snu = -snr
csv = csl
snv = -snl
} else {
// Compute the [0, 0] and [0, 1] elements of Uᵀ *A and Vᵀ *B,
// and [0, 0] element of |U|ᵀ*|A| and |V|ᵀ*|B|.
ua11 := csr*a1 + snr*a2
ua12 := snr * a3
vb11 := csl*b1 + snl*b2
vb12 := snl * b3
aua11 := math.Abs(csr)*math.Abs(a1) + math.Abs(snr)*math.Abs(a2)
avb11 := math.Abs(csl)*math.Abs(b1) + math.Abs(snl)*math.Abs(b2)
// Zero [0, 0] elements of Uᵀ*A and Vᵀ*B, and then swap.
if (math.Abs(ua11) + math.Abs(ua12)) != 0 {
if aua11/(math.Abs(ua11)+math.Abs(ua12)) <= avb11/(math.Abs(vb11)+math.Abs(vb12)) {
csq, snq, _ = impl.Dlartg(ua12, ua11)
} else {
csq, snq, _ = impl.Dlartg(vb12, vb11)
}
} else {
csq, snq, _ = impl.Dlartg(vb12, vb11)
}
csu = snr
snu = csr
csv = snl
snv = csl
}
}
return csu, snu, csv, snv, csq, snq
}
|