mirror of
https://github.com/gonum/gonum.git
synced 2025-10-07 16:11:03 +08:00
mat: change cholesky.To to cholesky.ToSym (#250)
* mat: change cholesky.To to cholesky.ToSym Fixes #133
This commit is contained in:
@@ -268,10 +268,10 @@ func (c *Cholesky) LTo(dst *TriDense) *TriDense {
|
|||||||
return dst
|
return dst
|
||||||
}
|
}
|
||||||
|
|
||||||
// To reconstructs the original positive definite matrix given its
|
// ToSym reconstructs the original positive definite matrix given its
|
||||||
// Cholesky decomposition into dst and returns the result. If dst is nil
|
// Cholesky decomposition into dst and returns the result. If dst is nil
|
||||||
// a new SymDense is allocated.
|
// a new SymDense is allocated.
|
||||||
func (c *Cholesky) To(dst *SymDense) *SymDense {
|
func (c *Cholesky) ToSym(dst *SymDense) *SymDense {
|
||||||
if !c.valid() {
|
if !c.valid() {
|
||||||
panic(badCholesky)
|
panic(badCholesky)
|
||||||
}
|
}
|
||||||
|
@@ -91,7 +91,7 @@ func ExampleCholesky_SymRankOne() {
|
|||||||
// Rank-1 update the matrix a.
|
// Rank-1 update the matrix a.
|
||||||
a.SymRankOne(a, 1, x)
|
a.SymRankOne(a, 1, x)
|
||||||
|
|
||||||
au := chol.To(nil)
|
au := chol.ToSym(nil)
|
||||||
|
|
||||||
// Print the matrix that was updated directly.
|
// Print the matrix that was updated directly.
|
||||||
fmt.Printf("\nA' = %0.4v\n", mat.Formatted(a, mat.Prefix(" ")))
|
fmt.Printf("\nA' = %0.4v\n", mat.Formatted(a, mat.Prefix(" ")))
|
||||||
|
@@ -219,7 +219,7 @@ func TestCholeskySolveVec(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCholeskyTo(t *testing.T) {
|
func TestCholeskyToSym(t *testing.T) {
|
||||||
for _, test := range []*SymDense{
|
for _, test := range []*SymDense{
|
||||||
NewSymDense(3, []float64{
|
NewSymDense(3, []float64{
|
||||||
53, 59, 37,
|
53, 59, 37,
|
||||||
@@ -232,7 +232,7 @@ func TestCholeskyTo(t *testing.T) {
|
|||||||
if !ok {
|
if !ok {
|
||||||
t.Fatal("unexpected Cholesky factorization failure: not positive definite")
|
t.Fatal("unexpected Cholesky factorization failure: not positive definite")
|
||||||
}
|
}
|
||||||
s := chol.To(nil)
|
s := chol.ToSym(nil)
|
||||||
|
|
||||||
if !EqualApprox(s, test, 1e-12) {
|
if !EqualApprox(s, test, 1e-12) {
|
||||||
t.Errorf("Cholesky reconstruction not equal to original matrix.\nWant:\n% v\nGot:\n% v\n", Formatted(test), Formatted(s))
|
t.Errorf("Cholesky reconstruction not equal to original matrix.\nWant:\n% v\nGot:\n% v\n", Formatted(test), Formatted(s))
|
||||||
@@ -338,7 +338,7 @@ func TestCholeskySymRankOne(t *testing.T) {
|
|||||||
a.SymRankOne(&a, alpha, x)
|
a.SymRankOne(&a, alpha, x)
|
||||||
|
|
||||||
var achol SymDense
|
var achol SymDense
|
||||||
chol.To(&achol)
|
chol.ToSym(&achol)
|
||||||
if !EqualApprox(&achol, &a, 1e-13) {
|
if !EqualApprox(&achol, &a, 1e-13) {
|
||||||
t.Errorf("n=%v, alpha=%v: mismatch between updated matrix and from Cholesky:\nupdated:\n%v\nfrom Cholesky:\n%v",
|
t.Errorf("n=%v, alpha=%v: mismatch between updated matrix and from Cholesky:\nupdated:\n%v\nfrom Cholesky:\n%v",
|
||||||
n, alpha, Formatted(&a), Formatted(&achol))
|
n, alpha, Formatted(&a), Formatted(&achol))
|
||||||
@@ -413,7 +413,7 @@ func TestCholeskySymRankOne(t *testing.T) {
|
|||||||
a.SymRankOne(a, test.alpha, x)
|
a.SymRankOne(a, test.alpha, x)
|
||||||
|
|
||||||
var achol SymDense
|
var achol SymDense
|
||||||
chol.To(&achol)
|
chol.ToSym(&achol)
|
||||||
if !EqualApprox(&achol, a, 1e-13) {
|
if !EqualApprox(&achol, a, 1e-13) {
|
||||||
t.Errorf("Case %v: mismatch between updated matrix and from Cholesky:\nupdated:\n%v\nfrom Cholesky:\n%v",
|
t.Errorf("Case %v: mismatch between updated matrix and from Cholesky:\nupdated:\n%v\nfrom Cholesky:\n%v",
|
||||||
i, Formatted(a), Formatted(&achol))
|
i, Formatted(a), Formatted(&achol))
|
||||||
|
@@ -151,7 +151,7 @@ func (w *Wishart) RandSym(x *mat.SymDense) *mat.SymDense {
|
|||||||
}
|
}
|
||||||
var c mat.Cholesky
|
var c mat.Cholesky
|
||||||
w.RandChol(&c)
|
w.RandChol(&c)
|
||||||
c.To(x)
|
c.ToSym(x)
|
||||||
return x
|
return x
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -204,6 +204,6 @@ func (w *Wishart) RandChol(c *mat.Cholesky) *mat.Cholesky {
|
|||||||
func (w *Wishart) setV() {
|
func (w *Wishart) setV() {
|
||||||
w.once.Do(func() {
|
w.once.Do(func() {
|
||||||
w.v = mat.NewSymDense(w.dim, nil)
|
w.v = mat.NewSymDense(w.dim, nil)
|
||||||
w.cholv.To(w.v)
|
w.cholv.ToSym(w.v)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@@ -242,7 +242,7 @@ func TestConditionNormal(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var sigma mat.SymDense
|
var sigma mat.SymDense
|
||||||
newNormal.chol.To(&sigma)
|
newNormal.chol.ToSym(&sigma)
|
||||||
if !mat.EqualApprox(test.newSigma, &sigma, 1e-12) {
|
if !mat.EqualApprox(test.newSigma, &sigma, 1e-12) {
|
||||||
t.Errorf("Updated sigma mismatch\n.Want:\n% v\nGot:\n% v\n", test.newSigma, sigma)
|
t.Errorf("Updated sigma mismatch\n.Want:\n% v\nGot:\n% v\n", test.newSigma, sigma)
|
||||||
}
|
}
|
||||||
@@ -280,7 +280,7 @@ func TestConditionNormal(t *testing.T) {
|
|||||||
t.Fatalf("Bad test, update failed")
|
t.Fatalf("Bad test, update failed")
|
||||||
}
|
}
|
||||||
var newSigma mat.SymDense
|
var newSigma mat.SymDense
|
||||||
newNormal.chol.To(&newSigma)
|
newNormal.chol.ToSym(&newSigma)
|
||||||
trueMean := test.mu[0] + rho*(std[0]/std[1])*(test.value-test.mu[1])
|
trueMean := test.mu[0] + rho*(std[0]/std[1])*(test.value-test.mu[1])
|
||||||
if math.Abs(trueMean-newNormal.mu[0]) > 1e-14 {
|
if math.Abs(trueMean-newNormal.mu[0]) > 1e-14 {
|
||||||
t.Errorf("Mean mismatch. Want %v, got %v", trueMean, newNormal.mu[0])
|
t.Errorf("Mean mismatch. Want %v, got %v", trueMean, newNormal.mu[0])
|
||||||
@@ -377,7 +377,7 @@ func TestConditionNormal(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
var sigma mat.SymDense
|
var sigma mat.SymDense
|
||||||
newNormal.chol.To(&sigma)
|
newNormal.chol.ToSym(&sigma)
|
||||||
if !mat.EqualApprox(&sigma, subEstCov, 1e-1) {
|
if !mat.EqualApprox(&sigma, subEstCov, 1e-1) {
|
||||||
t.Errorf("Covariance mismatch. Want:\n%0.8v\nGot:\n%0.8v\n", subEstCov, sigma)
|
t.Errorf("Covariance mismatch. Want:\n%0.8v\nGot:\n%0.8v\n", subEstCov, sigma)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user