mat: move SolveCholesky* onto Cholesky
MODIFY
mat/cholesky.go
mat/cholesky.go
@@ -158,13 +158,13 @@ func (c *Cholesky) LogDet() float64 {
return det
}
-// SolveCholesky finds the matrix m that solves A * m = b where A is represented
-// by the Cholesky decomposition, placing the result in the receiver.
-func (m *Dense) SolveCholesky(chol *Cholesky, b Matrix) error {
- if !chol.valid() {
+// Solve finds the matrix m that solves A * m = b where A is represented
+// by the Cholesky decomposition, placing the result in m.
+func (c *Cholesky) Solve(m *Dense, b Matrix) error {
+ if !c.valid() {
panic(badCholesky)
}
- n := chol.chol.mat.N
+ n := c.chol.mat.N
bm, bn := b.Dims()
if n != bm {
panic(ErrShape)
@@ -174,10 +174,10 @@ func (m *Dense) SolveCholesky(chol *Cholesky, b Matrix) error {
if b != m {
m.Copy(b)
}
- blas64.Trsm(blas.Left, blas.Trans, 1, chol.chol.mat, m.mat)
- blas64.Trsm(blas.Left, blas.NoTrans, 1, chol.chol.mat, m.mat)
- if chol.cond > ConditionTolerance {
- return Condition(chol.cond)
+ blas64.Trsm(blas.Left, blas.Trans, 1, c.chol.mat, m.mat)
+ blas64.Trsm(blas.Left, blas.NoTrans, 1, c.chol.mat, m.mat)
+ if c.cond > ConditionTolerance {
+ return Condition(c.cond)
}
return nil
}
@@ -205,13 +205,13 @@ func (m *Dense) solveTwoChol(a, b *Cholesky) error {
return nil
}
-// SolveCholeskyVec finds the vector v that solves A * v = b where A is represented
-// by the Cholesky decomposition, placing the result in the receiver.
-func (v *Vector) SolveCholeskyVec(chol *Cholesky, b *Vector) error {
- if !chol.valid() {
+// SolveVec finds the vector v that solves A * v = b where A is represented
+// by the Cholesky decomposition, placing the result in v.
+func (c *Cholesky) SolveVec(v, b *Vector) error {
+ if !c.valid() {
panic(badCholesky)
}
- n := chol.chol.mat.N
+ n := c.chol.mat.N
vn := b.Len()
if vn != n {
panic(ErrShape)
@@ -223,10 +223,10 @@ func (v *Vector) SolveCholeskyVec(chol *Cholesky, b *Vector) error {
if v != b {
v.CopyVec(b)
}
- blas64.Trsv(blas.Trans, chol.chol.mat, v.mat)
- blas64.Trsv(blas.NoTrans, chol.chol.mat, v.mat)
- if chol.cond > ConditionTolerance {
- return Condition(chol.cond)
+ blas64.Trsv(blas.Trans, c.chol.mat, v.mat)
+ blas64.Trsv(blas.NoTrans, c.chol.mat, v.mat)
+ if c.cond > ConditionTolerance {
+ return Condition(c.cond)
}
return nil

MODIFY
mat/cholesky_example_test.go
mat/cholesky_example_test.go
@@ -35,7 +35,7 @@ func ExampleCholesky() {
// Use the factorization to solve the system of equations a * x = b.
b := mat.NewVector(4, []float64{1, 2, 3, 4})
var x mat.Vector
- if err := x.SolveCholeskyVec(&chol, b); err != nil {
+ if err := chol.SolveVec(&x, b); err != nil {
fmt.Println("Matrix is near singular: ", err)
}
fmt.Println("Solve a * x = b")

MODIFY
mat/cholesky_test.go
mat/cholesky_test.go
@@ -103,7 +103,7 @@ func TestCholeskySolve(t *testing.T) {
}
var x Dense
- x.SolveCholesky(&chol, test.b)
+ chol.Solve(&x, test.b)
if !EqualApprox(&x, test.ans, 1e-12) {
t.Error("incorrect Cholesky solve solution")
}
@@ -208,7 +208,7 @@ func TestCholeskySolveVec(t *testing.T) {
}
var x Vector
- x.SolveCholeskyVec(&chol, test.b)
+ chol.SolveVec(&x, test.b)
if !EqualApprox(&x, test.ans, 1e-12) {
t.Error("incorrect Cholesky solve solution")
}

MODIFY
optimize/newton.go
optimize/newton.go
@@ -122,7 +122,7 @@ func (n *Newton) NextDirection(loc *Location, dir []float64) (stepSize float64)
pd := n.chol.Factorize(n.hess)
if pd {
// Store the solution in d's backing array, dir.
- d.SolveCholeskyVec(&n.chol, grad)
+ n.chol.SolveVec(d, grad)
d.ScaleVec(-1, d)
return 1
}

MODIFY
stat/distmat/wishart.go
stat/distmat/wishart.go
@@ -131,7 +131,7 @@ func (w *Wishart) logProbSymChol(cholX *mat.Cholesky) float64 {
cholX.UTo(&u)
var vinvx mat.Dense
- err := vinvx.SolveCholesky(&w.cholv, u.T())
+ err := w.cholv.Solve(&vinvx, u.T())
if err != nil {
return math.Inf(-1)
}

MODIFY
stat/distmv/statdist.go
stat/distmv/statdist.go
@@ -163,7 +163,7 @@ func (KullbackLeibler) DistNormal(l, r *Normal) float64 {
var u mat.TriDense
l.chol.UTo(&u)
var m mat.Dense
- err := m.SolveCholesky(&r.chol, u.T())
+ err := r.chol.Solve(&m, u.T())
if err != nil {
return math.NaN()
}

MODIFY
stat/distmv/studentst.go
stat/distmv/studentst.go
@@ -154,7 +154,7 @@ func studentsTConditional(observed []int, values []float64, nu float64, mu []flo
// Compute mu_1 + sigma_{2,1}^T * sigma_{2,2}^-1 (v - mu_2).
v := mat.NewVector(ob, mu2)
var tmp, tmp2 mat.Vector
- err := tmp.SolveCholeskyVec(&chol, v)
+ err := chol.SolveVec(&tmp, v)
if err != nil {
return math.NaN(), nil, nil
}
@@ -167,7 +167,7 @@ func studentsTConditional(observed []int, values []float64, nu float64, mu []flo
// Compute tmp4 = sigma_{2,1}^T * sigma_{2,2}^-1 * sigma_{2,1}.
// TODO(btracey): Should this be a method of SymDense?
var tmp3, tmp4 mat.Dense
- err = tmp3.SolveCholesky(&chol, sigma21)
+ err = chol.Solve(&tmp3, sigma21)
if err != nil {
return math.NaN(), nil, nil
}
@@ -259,7 +259,7 @@ func (s *StudentsT) LogProb(y []float64) float64 {
x := mat.NewVector(s.dim, shift)
var tmp mat.Vector
- tmp.SolveCholeskyVec(&s.chol, x)
+ s.chol.SolveVec(&tmp, x)
dot := mat.Dot(&tmp, x)

MODIFY
stat/statmat.go
stat/statmat.go
@@ -138,7 +138,7 @@ func Mahalanobis(x, y *mat.Vector, chol *mat.Cholesky) float64 {
var diff mat.Vector
diff.SubVec(x, y)
var tmp mat.Vector
- err := tmp.SolveCholeskyVec(chol, &diff)
+ err := chol.SolveVec(&tmp, &diff)
if err != nil {
return math.NaN()
}

mat: move SolveCholesky* onto Cholesky