forked from openshift/origin
/
lq.go
193 lines (168 loc) · 4.27 KB
/
lq.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
// 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.
package mat64
import (
"math"
"github.com/gonum/blas"
"github.com/gonum/blas/blas64"
)
type LQFactor struct {
LQ *Dense
lDiag []float64
}
// LQ computes an LQ Decomposition for an m-by-n matrix a with m <= n by Householder
// reflections. The LQ decomposition is an m-by-n orthogonal matrix q and an m-by-m
// lower triangular matrix l so that a = l.q. LQ will panic with ErrShape if m > n.
//
// The LQ decomposition always exists, even if the matrix does not have full rank,
// so LQ will never fail unless m > n. The primary use of the LQ decomposition is
// in the least squares solution of non-square systems of simultaneous linear equations.
// This will fail if LQIsFullRank() returns false. The matrix a is overwritten by the
// decomposition.
func LQ(a *Dense) LQFactor {
// Initialize.
m, n := a.Dims()
if m > n {
panic(ErrShape)
}
lq := *a
lDiag := make([]float64, m)
projs := NewVector(m, nil)
// Main loop.
for k := 0; k < m; k++ {
hh := lq.RawRowView(k)[k:]
norm := blas64.Nrm2(len(hh), blas64.Vector{Inc: 1, Data: hh})
lDiag[k] = norm
if norm != 0 {
hhNorm := (norm * math.Sqrt(1-hh[0]/norm))
if hhNorm == 0 {
hh[0] = 0
} else {
// Form k-th Householder vector.
s := 1 / hhNorm
hh[0] -= norm
blas64.Scal(len(hh), s, blas64.Vector{Inc: 1, Data: hh})
// Apply transformation to remaining columns.
if k < m-1 {
a = lq.View(k+1, k, m-k-1, n-k).(*Dense)
projs = projs.ViewVec(0, m-k-1)
projs.MulVec(a, false, NewVector(len(hh), hh))
for j := 0; j < m-k-1; j++ {
dst := a.RawRowView(j)
blas64.Axpy(len(dst), -projs.at(j),
blas64.Vector{Inc: 1, Data: hh},
blas64.Vector{Inc: 1, Data: dst},
)
}
}
}
}
}
*a = lq
return LQFactor{a, lDiag}
}
// IsFullRank returns whether the L matrix and hence a has full rank.
func (f LQFactor) IsFullRank() bool {
for _, v := range f.lDiag {
if v == 0 {
return false
}
}
return true
}
// L returns the lower triangular factor for the LQ decomposition.
func (f LQFactor) L() *Dense {
lq, lDiag := f.LQ, f.lDiag
m, _ := lq.Dims()
l := NewDense(m, m, nil)
for i, v := range lDiag {
for j := 0; j < m; j++ {
if i < j {
l.set(j, i, lq.at(j, i))
} else if i == j {
l.set(j, i, v)
}
}
}
return l
}
// replaces x with Q.x
func (f LQFactor) applyQTo(x *Dense, trans bool) {
nh, nc := f.LQ.Dims()
m, n := x.Dims()
if m != nc {
panic(ErrShape)
}
proj := make([]float64, n)
if trans {
for k := nh - 1; k >= 0; k-- {
hh := f.LQ.RawRowView(k)[k:]
sub := x.View(k, 0, m-k, n).(*Dense)
blas64.Gemv(blas.Trans,
1, sub.mat, blas64.Vector{Inc: 1, Data: hh},
0, blas64.Vector{Inc: 1, Data: proj},
)
for i := k; i < m; i++ {
row := x.RawRowView(i)
blas64.Axpy(n, -hh[i-k],
blas64.Vector{Inc: 1, Data: proj},
blas64.Vector{Inc: 1, Data: row},
)
}
}
} else {
for k := 0; k < nh; k++ {
hh := f.LQ.RawRowView(k)[k:]
sub := x.View(k, 0, m-k, n).(*Dense)
blas64.Gemv(blas.Trans,
1, sub.mat, blas64.Vector{Inc: 1, Data: hh},
0, blas64.Vector{Inc: 1, Data: proj},
)
for i := k; i < m; i++ {
row := x.RawRowView(i)
blas64.Axpy(n, -hh[i-k],
blas64.Vector{Inc: 1, Data: proj},
blas64.Vector{Inc: 1, Data: row},
)
}
}
}
}
// Solve computes minimum norm least squares solution of a.x = b where b has as many rows as a.
// A matrix x is returned that minimizes the two norm of Q*R*X-B. Solve will panic
// if a is not full rank.
func (f LQFactor) Solve(b *Dense) (x *Dense) {
lq := f.LQ
lDiag := f.lDiag
m, n := lq.Dims()
bm, bn := b.Dims()
if bm != m {
panic(ErrShape)
}
if !f.IsFullRank() {
panic(ErrSingular)
}
x = NewDense(n, bn, nil)
x.Copy(b)
tau := make([]float64, m)
for i := range tau {
tau[i] = lq.at(i, i)
lq.set(i, i, lDiag[i])
}
lqT := blas64.Triangular{
// N omitted since it is not used by Trsm.
Stride: lq.mat.Stride,
Data: lq.mat.Data,
Uplo: blas.Lower,
Diag: blas.NonUnit,
}
x.mat.Rows = bm
blas64.Trsm(blas.Left, blas.NoTrans, 1, lqT, x.mat)
x.mat.Rows = n
for i := range tau {
lq.set(i, i, tau[i])
}
f.applyQTo(x, true)
return x
}