matrix/mat64: provide sugar for easy matrix extraction
MODIFY
matrix/mat64/gsvd_example_test.go
matrix/mat64/gsvd_example_test.go
@@ -25,22 +25,19 @@ func ExampleGSVD() {
log.Fatal("GSVD factorization failed")
}
- var u, v mat64.Dense
- gsvd.UTo(&u)
- gsvd.VTo(&v)
+ u := gsvd.UTo(nil)
+ v := gsvd.VTo(nil)
s1 := gsvd.ValuesA(nil)
s2 := gsvd.ValuesB(nil)
fmt.Printf("Africa\n\ts1 = %.4f\n\n\tU = %.4f\n\n",
- s1, mat64.Formatted(&u, mat64.Prefix("\t "), mat64.Excerpt(2)))
+ s1, mat64.Formatted(u, mat64.Prefix("\t "), mat64.Excerpt(2)))
fmt.Printf("Latin America/Caribbean\n\ts2 = %.4f\n\n\tV = %.4f\n",
- s2, mat64.Formatted(&v, mat64.Prefix("\t "), mat64.Excerpt(2)))
+ s2, mat64.Formatted(v, mat64.Prefix("\t "), mat64.Excerpt(2)))
- var zeroR, q mat64.Dense
- gsvd.ZeroRTo(&zeroR)
- gsvd.QTo(&q)
- q.Mul(&zeroR, &q)
+ var q mat64.Dense
+ q.Mul(gsvd.ZeroRTo(nil), gsvd.QTo(nil))
fmt.Printf("\nCommon basis vectors\n\n\tQ^T = %.4f\n",
mat64.Formatted(q.T(), mat64.Prefix("\t ")))

MODIFY
matrix/mat64/gsvd_test.go
matrix/mat64/gsvd_test.go
@@ -106,14 +106,13 @@ func TestGSVD(t *testing.T) {
}
func extractGSVD(gsvd *GSVD) (c, s []float64, s1, s2, zR, u, v, q *Dense) {
- var s1m, s2m, zeroR, um, vm, qm Dense
- gsvd.SigmaATo(&s1m)
- gsvd.SigmaBTo(&s2m)
- gsvd.ZeroRTo(&zeroR)
- gsvd.UTo(&um)
- gsvd.VTo(&vm)
- gsvd.QTo(&qm)
+ s1 = gsvd.SigmaATo(nil)
+ s2 = gsvd.SigmaBTo(nil)
+ zR = gsvd.ZeroRTo(nil)
+ u = gsvd.UTo(nil)
+ v = gsvd.VTo(nil)
+ q = gsvd.QTo(nil)
c = gsvd.ValuesA(nil)
s = gsvd.ValuesB(nil)
- return c, s, &s1m, &s2m, &zeroR, &um, &vm, &qm
+ return c, s, s1, s2, zR, u, v, q
}

MODIFY
matrix/mat64/hogsvd_example_test.go
matrix/mat64/hogsvd_example_test.go
@@ -24,15 +24,13 @@ func ExampleHOGSVD() {
}
for i, n := range []string{"Africa", "Asia", "Latin America/Caribbean", "Oceania"} {
- var u mat64.Dense
- gsvd.UTo(&u, i)
+ u := gsvd.UTo(nil, i)
s := gsvd.Values(nil, i)
fmt.Printf("%s\n\ts_%d = %.4f\n\n\tU_%[2]d = %.4[4]f\n",
- n, i, s, mat64.Formatted(&u, mat64.Prefix("\t ")))
+ n, i, s, mat64.Formatted(u, mat64.Prefix("\t ")))
}
- var v mat64.Dense
- gsvd.VTo(&v)
+ v := gsvd.VTo(nil)
fmt.Printf("\nCommon basis vectors\n\n\tV^T = %.4f",
mat64.Formatted(v.T(), mat64.Prefix("\t ")))

MODIFY
matrix/mat64/hogsvd_test.go
matrix/mat64/hogsvd_test.go
@@ -79,11 +79,9 @@ func extractHOGSVD(gsvd *HOGSVD) (u []*Dense, s [][]float64, v *Dense) {
u = make([]*Dense, gsvd.Len())
s = make([][]float64, gsvd.Len())
for i := 0; i < gsvd.Len(); i++ {
- u[i] = &Dense{}
- gsvd.UTo(u[i], i)
+ u[i] = gsvd.UTo(nil, i)
s[i] = gsvd.Values(nil, i)
}
- v = &Dense{}
- gsvd.VTo(v)
+ v = gsvd.VTo(nil)
return u, s, v
}

MODIFY
matrix/mat64/lq_test.go
matrix/mat64/lq_test.go
@@ -29,17 +29,16 @@ func TestLQ(t *testing.T) {
var lq LQ
lq.Factorize(a)
- var l, q Dense
- lq.QTo(&q)
+ q := lq.QTo(nil)
- if !isOrthonormal(&q, 1e-10) {
+ if !isOrthonormal(q, 1e-10) {
t.Errorf("Q is not orthonormal: m = %v, n = %v", m, n)
}
- lq.LTo(&l)
+ l := lq.LTo(nil)
var got Dense
- got.Mul(&l, &q)
+ got.Mul(l, q)
if !EqualApprox(&got, &want, 1e-12) {
t.Errorf("LQ does not equal original matrix. \nWant: %v\nGot: %v", want, got)
}

MODIFY
matrix/mat64/lu_test.go
matrix/mat64/lu_test.go
@@ -23,15 +23,13 @@ func TestLUD(t *testing.T) {
var lu LU
lu.Factorize(a)
- var l, u TriDense
- lu.LTo(&l)
- lu.UTo(&u)
+ l := lu.LTo(nil)
+ u := lu.UTo(nil)
var p Dense
pivot := lu.Pivot(nil)
p.Permutation(n, pivot)
var got Dense
- got.Mul(&p, &l)
- got.Mul(&got, &u)
+ got.Product(&p, l, u)
if !EqualApprox(&got, &want, 1e-12) {
t.Errorf("PLU does not equal original matrix.\nWant: %v\n Got: %v", want, got)
}

MODIFY
matrix/mat64/qr_test.go
matrix/mat64/qr_test.go
@@ -32,17 +32,16 @@ func TestQR(t *testing.T) {
var qr QR
qr.Factorize(a)
- var q, r Dense
- qr.QTo(&q)
+ q := qr.QTo(nil)
- if !isOrthonormal(&q, 1e-10) {
+ if !isOrthonormal(q, 1e-10) {
t.Errorf("Q is not orthonormal: m = %v, n = %v", m, n)
}
- qr.RTo(&r)
+ r := qr.RTo(nil)
var got Dense
- got.Mul(&q, &r)
+ got.Mul(q, r)
if !EqualApprox(&got, &want, 1e-12) {
t.Errorf("QR does not equal original matrix. \nWant: %v\nGot: %v", want, got)
}

MODIFY
matrix/mat64/svd_test.go
matrix/mat64/svd_test.go
@@ -169,9 +169,5 @@ func TestSVD(t *testing.T) {
}
func extractSVD(svd *SVD) (s []float64, u, v *Dense) {
- var um, vm Dense
- svd.UTo(&um)
- svd.VTo(&vm)
- s = svd.Values(nil)
- return s, &um, &vm
+ return svd.Values(nil), svd.UTo(nil), svd.VTo(nil)
}

matrix/mat64: provide sugar for easy matrix extraction