1 // Copyright ©2015 The Gonum Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
11 "golang.org/x/exp/rand"
13 "gonum.org/v1/gonum/blas"
14 "gonum.org/v1/gonum/blas/blas64"
15 "gonum.org/v1/gonum/floats"
16 "gonum.org/v1/gonum/lapack"
19 type Dpoconer interface {
22 Dlansy(norm lapack.MatrixNorm, uplo blas.Uplo, n int, a []float64, lda int, work []float64) float64
23 Dpocon(uplo blas.Uplo, n int, a []float64, lda int, anorm float64, work []float64, iwork []int) float64
26 func DpoconTest(t *testing.T, impl Dpoconer) {
27 for _, test := range []struct {
41 cond: 0.050052137643379,
51 cond: 0.050052137643379,
53 // Dgecon does not match Dpocon for this case. https://github.com/xianyi/OpenBLAS/issues/664.
56 2.9995576045549965, -2.0898894566158663, 3.965560740124006,
57 0, 1.9634729526261008, -2.8681002706874104,
58 0, 0, 5.502416670471008,
62 cond: 0.024054837369015203,
66 a := make([]float64, len(test.a))
70 work := make([]float64, 3*n)
71 anorm := impl.Dlansy(lapack.MaxColumnSum, uplo, n, a, lda, work)
72 // Compute cholesky decomposition
73 ok := impl.Dpotrf(uplo, n, a, lda)
75 t.Errorf("Bad test, matrix not positive definite")
78 iwork := make([]int, n)
79 cond := impl.Dpocon(uplo, n, a, lda, anorm, work, iwork)
80 // Error if not the same order, otherwise log the difference.
81 if !floats.EqualWithinAbsOrRel(cond, test.cond, 1e0, 1e0) {
82 t.Errorf("Cond mismatch. Want %v, got %v.", test.cond, cond)
83 } else if !floats.EqualWithinAbsOrRel(cond, test.cond, 1e-14, 1e-14) {
84 log.Printf("Dpocon cond mismatch. Want %v, got %v.", test.cond, cond)
87 rnd := rand.New(rand.NewSource(1))
88 bi := blas64.Implementation()
89 // Randomized tests compared against Dgecon.
90 for _, uplo := range []blas.Uplo{blas.Lower, blas.Upper} {
91 for _, test := range []struct {
97 for trial := 0; trial < 100; trial++ {
103 a := make([]float64, n*lda)
105 a[i] = rnd.NormFloat64()
108 // Multiply a by itself to make it symmetric positive definite.
109 aCopy := make([]float64, len(a))
111 bi.Dgemm(blas.Trans, blas.NoTrans, n, n, n, 1, aCopy, lda, aCopy, lda, 0, a, lda)
113 aDat := make([]float64, len(aCopy))
116 aDense := make([]float64, len(a))
117 if uplo == blas.Upper {
118 for i := 0; i < n; i++ {
119 for j := i; j < n; j++ {
126 for i := 0; i < n; i++ {
127 for j := 0; j <= i; j++ {
134 work := make([]float64, 4*n)
135 iwork := make([]int, n)
137 anorm := impl.Dlansy(lapack.MaxColumnSum, uplo, n, a, lda, work)
138 ok := impl.Dpotrf(uplo, n, a, lda)
140 t.Errorf("Bad test, matrix not positive definite")
143 got := impl.Dpocon(uplo, n, a, lda, anorm, work, iwork)
145 denseNorm := impl.Dlange(lapack.MaxColumnSum, n, n, aDense, lda, work)
146 ipiv := make([]int, n)
147 impl.Dgetrf(n, n, aDense, lda, ipiv)
148 want := impl.Dgecon(lapack.MaxColumnSum, n, aDense, lda, denseNorm, work, iwork)
149 // Error if not the same order, otherwise log the difference.
150 if !floats.EqualWithinAbsOrRel(want, got, 1e0, 1e0) {
151 t.Errorf("Dpocon and Dgecon mismatch. Dpocon %v, Dgecon %v.", got, want)
152 } else if !floats.EqualWithinAbsOrRel(want, got, 1e-14, 1e-14) {
153 log.Printf("Dpocon and Dgecon mismatch. Dpocon %v, Dgecon %v.", got, want)