forked from openshift/origin
/
lu.go
212 lines (189 loc) · 4.44 KB
/
lu.go
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
// Copyright ©2013 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.
// Based on the LUDecomposition class from Jama 1.0.3.
package mat64
import (
"math"
)
type LUFactors struct {
LU *Dense
Pivot []int
Sign int
}
// LU performs an LU Decomposition for an m-by-n matrix a.
//
// If m >= n, the LU decomposition is an m-by-n unit lower triangular matrix L,
// an n-by-n upper triangular matrix U, and a permutation vector piv of length m
// so that A(piv,:) = L*U.
//
// If m < n, then L is m-by-m and U is m-by-n.
//
// The LU decompostion with pivoting always exists, even if the matrix is
// singular, so LU will never fail. The primary use of the LU decomposition
// is in the solution of square systems of simultaneous linear equations. This
// will fail if IsSingular() returns true.
func LU(a *Dense) LUFactors {
// Use a "left-looking", dot-product, Crout/Doolittle algorithm.
m, n := a.Dims()
lu := a
piv := make([]int, m)
for i := range piv {
piv[i] = i
}
sign := 1
// Outer loop.
luColj := make([]float64, m)
for j := 0; j < n; j++ {
// Make a copy of the j-th column to localize references.
for i := 0; i < m; i++ {
luColj[i] = lu.at(i, j)
}
// Apply previous transformations.
for i := 0; i < m; i++ {
luRowi := lu.RawRowView(i)
// Most of the time is spent in the following dot product.
kmax := min(i, j)
var s float64
for k, v := range luRowi[:kmax] {
s += v * luColj[k]
}
luColj[i] -= s
luRowi[j] = luColj[i]
}
// Find pivot and exchange if necessary.
p := j
for i := j + 1; i < m; i++ {
if math.Abs(luColj[i]) > math.Abs(luColj[p]) {
p = i
}
}
if p != j {
for k := 0; k < n; k++ {
t := lu.at(p, k)
lu.set(p, k, lu.at(j, k))
lu.set(j, k, t)
}
piv[p], piv[j] = piv[j], piv[p]
sign = -sign
}
// Compute multipliers.
if j < m && lu.at(j, j) != 0 {
for i := j + 1; i < m; i++ {
lu.set(i, j, lu.at(i, j)/lu.at(j, j))
}
}
}
return LUFactors{lu, piv, sign}
}
// IsSingular returns whether the the upper triangular factor and hence a is
// singular.
func (f LUFactors) IsSingular() bool {
lu := f.LU
_, n := lu.Dims()
for j := 0; j < n; j++ {
if lu.at(j, j) == 0 {
return true
}
}
return false
}
// L returns the lower triangular factor of the LU decomposition.
func (f LUFactors) L() *Dense {
lu := f.LU
m, n := lu.Dims()
if m < n {
n = m
}
l := NewDense(m, n, nil)
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
if i > j {
l.set(i, j, lu.at(i, j))
} else if i == j {
l.set(i, j, 1)
}
}
}
return l
}
// U returns the upper triangular factor of the LU decomposition.
func (f LUFactors) U() *Dense {
lu := f.LU
m, n := lu.Dims()
u := NewDense(m, n, nil)
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
if i <= j {
u.set(i, j, lu.at(i, j))
}
}
}
return u
}
// Det returns the determinant of matrix a decomposed into lu. The matrix
// a must have been square.
func (f LUFactors) Det() float64 {
lu, sign := f.LU, f.Sign
m, n := lu.Dims()
if m != n {
panic(ErrSquare)
}
d := float64(sign)
for j := 0; j < n; j++ {
d *= lu.at(j, j)
}
return d
}
// Solve computes a solution of a.x = b where b has as many rows as a. A matrix x
// is returned that minimizes the two norm of L*U*X - B(piv,:). Solve will panic
// if a is singular. The matrix b is overwritten during the call.
func (f LUFactors) Solve(b *Dense) (x *Dense) {
lu, piv := f.LU, f.Pivot
m, n := lu.Dims()
bm, bn := b.Dims()
if bm != m {
panic(ErrShape)
}
if f.IsSingular() {
panic(ErrSingular)
}
// Copy right hand side with pivoting
nx := bn
x = pivotRows(b, piv)
// Solve L*Y = B(piv,:)
for k := 0; k < n; k++ {
for i := k + 1; i < n; i++ {
for j := 0; j < nx; j++ {
x.set(i, j, x.at(i, j)-x.at(k, j)*lu.at(i, k))
}
}
}
// Solve U*X = Y;
for k := n - 1; k >= 0; k-- {
for j := 0; j < nx; j++ {
x.set(k, j, x.at(k, j)/lu.at(k, k))
}
for i := 0; i < k; i++ {
for j := 0; j < nx; j++ {
x.set(i, j, x.at(i, j)-x.at(k, j)*lu.at(i, k))
}
}
}
return x
}
func pivotRows(a *Dense, piv []int) *Dense {
visit := make([]bool, len(piv))
_, n := a.Dims()
tmpRow := make([]float64, n)
for to, from := range piv {
for to != from && !visit[from] {
visit[from], visit[to] = true, true
a.Row(tmpRow, from)
a.SetRow(from, a.rowView(to))
a.SetRow(to, tmpRow)
to, from = from, piv[from]
}
}
return a
}