This repository has been archived by the owner on Dec 10, 2018. It is now read-only.
/
cholesky.go
140 lines (125 loc) · 3.18 KB
/
cholesky.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
// 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 CholeskyDecomposition class from Jama 1.0.3.
package mat64
import (
"math"
"github.com/gonum/blas"
"github.com/gonum/blas/blas64"
"github.com/gonum/internal/asm"
)
const badTriangle = "mat64: invalid triangle"
// Cholesky calculates the Cholesky decomposition of the matrix A and returns
// whether the matrix is positive definite. The returned matrix is either a
// lower triangular matrix such that A = L * L^T or an upper triangular matrix
// such that A = U^T * U depending on the upper parameter.
func (t *TriDense) Cholesky(a *SymDense, upper bool) (ok bool) {
n := a.Symmetric()
if t.isZero() {
t.mat = blas64.Triangular{
N: n,
Stride: n,
Diag: blas.NonUnit,
Data: use(t.mat.Data, n*n),
}
} else if n != t.mat.N {
panic(ErrShape)
}
mat := t.mat.Data
stride := t.mat.Stride
if upper {
t.mat.Uplo = blas.Upper
for j := 0; j < n; j++ {
var d float64
for k := 0; k < j; k++ {
s := asm.DdotInc(
mat, mat,
uintptr(k),
uintptr(stride), uintptr(stride),
uintptr(k), uintptr(j),
)
s = (a.at(j, k) - s) / t.at(k, k)
t.set(k, j, s)
d += s * s
}
d = a.at(j, j) - d
if d <= 0 {
t.Reset()
return false
}
t.set(j, j, math.Sqrt(math.Max(d, 0)))
}
} else {
t.mat.Uplo = blas.Lower
for j := 0; j < n; j++ {
var d float64
for k := 0; k < j; k++ {
s := asm.DdotUnitary(mat[k*stride:k*stride+(n-k)], mat[j*stride:j*stride+(n-k)])
s = (a.at(j, k) - s) / t.at(k, k)
t.set(j, k, s)
d += s * s
}
d = a.at(j, j) - d
if d <= 0 {
t.Reset()
return false
}
t.set(j, j, math.Sqrt(math.Max(d, 0)))
}
}
return true
}
// SolveCholesky finds the matrix x that solves A * X = B where A = L * L^T or
// A = U^T * U, and U or L are represented by t. The matrix A must be symmetric
// and positive definite.
func (m *Dense) SolveCholesky(t Triangular, b Matrix) {
_, n := t.Dims()
bm, bn := b.Dims()
if n != bm {
panic(ErrShape)
}
m.reuseAs(bm, bn)
if b != m {
m.Copy(b)
}
// TODO(btracey): Implement an algorithm that doesn't require a copy into
// a blas64.Triangular.
ta := getBlasTriangular(t)
switch ta.Uplo {
case blas.Upper:
blas64.Trsm(blas.Left, blas.Trans, 1, ta, m.mat)
blas64.Trsm(blas.Left, blas.NoTrans, 1, ta, m.mat)
case blas.Lower:
blas64.Trsm(blas.Left, blas.NoTrans, 1, ta, m.mat)
blas64.Trsm(blas.Left, blas.Trans, 1, ta, m.mat)
default:
panic(badTriangle)
}
}
// SolveTri finds the matrix x that solves op(A) * X = B where A is a triangular
// matrix and op is specified by trans.
func (m *Dense) SolveTri(a Triangular, trans bool, b Matrix) {
n, _ := a.Triangle()
bm, bn := b.Dims()
if n != bm {
panic(ErrShape)
}
m.reuseAs(bm, bn)
if b != m {
m.Copy(b)
}
// TODO(btracey): Implement an algorithm that doesn't require a copy into
// a blas64.Triangular.
ta := getBlasTriangular(a)
t := blas.NoTrans
if trans {
t = blas.Trans
}
switch ta.Uplo {
case blas.Upper, blas.Lower:
blas64.Trsm(blas.Left, t, 1, ta, m.mat)
default:
panic(badTriangle)
}
}