1 // Copyright ©2016 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/lapack"
18 type Dgebaler interface {
19 Dgebal(job lapack.Job, n int, a []float64, lda int, scale []float64) (int, int)
22 func DgebalTest(t *testing.T, impl Dgebaler) {
23 rnd := rand.New(rand.NewSource(1))
25 for _, job := range []lapack.Job{lapack.None, lapack.Permute, lapack.Scale, lapack.PermuteScale} {
26 for _, n := range []int{0, 1, 2, 3, 4, 5, 6, 10, 18, 31, 53, 100} {
27 for _, extra := range []int{0, 11} {
28 for cas := 0; cas < 100; cas++ {
29 a := unbalancedSparseGeneral(n, n, n+extra, 2*n, rnd)
30 testDgebal(t, impl, job, a)
37 func testDgebal(t *testing.T, impl Dgebaler, job lapack.Job, a blas64.General) {
48 want := cloneGeneral(a)
50 ilo, ihi := impl.Dgebal(job, n, a.Data, a.Stride, scale)
52 prefix := fmt.Sprintf("Case job=%v, n=%v, extra=%v", job, n, extra)
54 if !generalOutsideAllNaN(a) {
55 t.Errorf("%v: out-of-range write to A\n%v", prefix, a.Data)
60 t.Errorf("%v: unexpected ilo when n=0. Want 0, got %v", prefix, ilo)
63 t.Errorf("%v: unexpected ihi when n=0. Want -1, got %v", prefix, ihi)
68 if job == lapack.None {
70 t.Errorf("%v: unexpected ilo when job=None. Want 0, got %v", prefix, ilo)
73 t.Errorf("%v: unexpected ihi when job=None. Want %v, got %v", prefix, n-1, ihi)
76 for i := range scale {
83 t.Errorf("%v: unexpected scale[%v] when job=None. Want 1, got %v", prefix, k, scale[k])
85 if !equalApproxGeneral(a, want, 0) {
86 t.Errorf("%v: unexpected modification of A when job=None", prefix)
91 if ilo < 0 || ihi < ilo || n <= ihi {
92 t.Errorf("%v: invalid ordering of ilo=%v and ihi=%v", prefix, ilo, ihi)
95 if ilo >= 2 && !isUpperTriangular(blas64.General{ilo - 1, ilo - 1, a.Stride, a.Data}) {
96 t.Errorf("%v: T1 is not upper triangular", prefix)
98 m := n - ihi - 1 // Order of T2.
100 if m >= 2 && !isUpperTriangular(blas64.General{m, m, a.Stride, a.Data[k*a.Stride+k:]}) {
101 t.Errorf("%v: T2 is not upper triangular", prefix)
104 if job == lapack.Permute || job == lapack.PermuteScale {
105 // Check that all rows in [ilo:ihi+1] have at least one nonzero
106 // off-diagonal element.
108 for i := ilo; i <= ihi; i++ {
110 for j := ilo; j <= ihi; j++ {
111 if i != j && a.Data[i*a.Stride+j] != 0 {
121 if zeroRow != -1 && ilo != ihi {
122 t.Errorf("%v: row %v has only zero off-diagonal elements, ilo=%v, ihi=%v", prefix, zeroRow, ilo, ihi)
124 // Check that all columns in [ilo:ihi+1] have at least one nonzero
125 // off-diagonal element.
127 for j := ilo; j <= ihi; j++ {
129 for i := ilo; i <= ihi; i++ {
130 if i != j && a.Data[i*a.Stride+j] != 0 {
140 if zeroCol != -1 && ilo != ihi {
141 t.Errorf("%v: column %v has only zero off-diagonal elements, ilo=%v, ihi=%v", prefix, zeroCol, ilo, ihi)
144 // Create the permutation matrix P.
146 for j := n - 1; j > ihi; j-- {
148 blas64.Vector{p.Stride, p.Data[j:]},
149 blas64.Vector{p.Stride, p.Data[int(scale[j]):]})
151 for j := 0; j < ilo; j++ {
153 blas64.Vector{p.Stride, p.Data[j:]},
154 blas64.Vector{p.Stride, p.Data[int(scale[j]):]})
156 // Compute P^T*A*P and store into want.
158 blas64.Gemm(blas.NoTrans, blas.NoTrans, 1, want, p, 0, ap)
159 blas64.Gemm(blas.Trans, blas.NoTrans, 1, p, ap, 0, want)
161 if job == lapack.Scale || job == lapack.PermuteScale {
162 // Modify want by D and D^{-1}.
165 for i := ilo; i <= ihi; i++ {
166 d.Data[i*d.Stride+i] = scale[i]
167 dinv.Data[i*dinv.Stride+i] = 1 / scale[i]
170 blas64.Gemm(blas.NoTrans, blas.NoTrans, 1, want, d, 0, ad)
171 blas64.Gemm(blas.NoTrans, blas.NoTrans, 1, dinv, ad, 0, want)
173 if !equalApproxGeneral(want, a, tol) {
174 t.Errorf("%v: unexpected value of A, ilo=%v, ihi=%v", prefix, ilo, ihi)