github.com/gopherd/gonum@v0.0.4/lapack/testlapack/dlagtm.go (about) 1 // Copyright ©2020 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. 4 5 package testlapack 6 7 import ( 8 "fmt" 9 "testing" 10 11 "math/rand" 12 13 "github.com/gopherd/gonum/blas" 14 "github.com/gopherd/gonum/blas/blas64" 15 "github.com/gopherd/gonum/floats" 16 "github.com/gopherd/gonum/lapack" 17 ) 18 19 type Dlagtmer interface { 20 Dlagtm(trans blas.Transpose, m, n int, alpha float64, dl, d, du []float64, b []float64, ldb int, beta float64, c []float64, ldc int) 21 } 22 23 func DlagtmTest(t *testing.T, impl Dlagtmer) { 24 rnd := rand.New(rand.NewSource(1)) 25 for _, trans := range []blas.Transpose{blas.NoTrans, blas.Trans, blas.ConjTrans} { 26 t.Run(transToString(trans), func(t *testing.T) { 27 for _, m := range []int{0, 1, 2, 3, 4, 5, 10} { 28 for _, n := range []int{0, 1, 2, 3, 4, 5, 10} { 29 for _, ldb := range []int{max(1, n), n + 3} { 30 for _, ldc := range []int{max(1, n), n + 4} { 31 for _, alpha := range []float64{0, 1, rnd.NormFloat64()} { 32 for _, beta := range []float64{0, 1, rnd.NormFloat64()} { 33 dlagtmTest(t, impl, rnd, trans, m, n, ldb, ldc, alpha, beta) 34 } 35 } 36 } 37 } 38 } 39 } 40 }) 41 } 42 } 43 44 func dlagtmTest(t *testing.T, impl Dlagtmer, rnd *rand.Rand, trans blas.Transpose, m, n int, ldb, ldc int, alpha, beta float64) { 45 const ( 46 tol = 1e-14 47 extra = 10 48 ) 49 50 name := fmt.Sprintf("Case m=%v,n=%v,ldb=%v,ldc=%v,alpha=%v,beta=%v", m, n, ldb, ldc, alpha, beta) 51 52 // Generate three random diagonals. 53 dl := randomSlice(n+extra, rnd) 54 dlCopy := make([]float64, len(dl)) 55 copy(dlCopy, dl) 56 57 d := randomSlice(n+1+extra, rnd) 58 dCopy := make([]float64, len(d)) 59 copy(dCopy, d) 60 61 du := randomSlice(n+extra, rnd) 62 duCopy := make([]float64, len(du)) 63 copy(duCopy, du) 64 65 b := randomGeneral(m, n, ldb, rnd) 66 bCopy := cloneGeneral(b) 67 68 got := randomGeneral(m, n, ldc, rnd) 69 want := cloneGeneral(got) 70 71 // Deal with zero-sized matrices early. 72 if m == 0 || n == 0 { 73 impl.Dlagtm(trans, m, n, alpha, dl, d, du, b.Data, b.Stride, beta, got.Data, got.Stride) 74 if !floats.Same(dl, dlCopy) { 75 t.Errorf("%v: unexpected modification in dl", name) 76 } 77 if !floats.Same(d, dCopy) { 78 t.Errorf("%v: unexpected modification in d", name) 79 } 80 if !floats.Same(du, duCopy) { 81 t.Errorf("%v: unexpected modification in du", name) 82 } 83 if !floats.Same(b.Data, bCopy.Data) { 84 t.Errorf("%v: unexpected modification in B", name) 85 } 86 if !floats.Same(got.Data, want.Data) { 87 t.Errorf("%v: unexpected modification in C", name) 88 } 89 return 90 } 91 92 impl.Dlagtm(trans, m, n, alpha, dl, d, du, b.Data, b.Stride, beta, got.Data, got.Stride) 93 94 if !floats.Same(dl, dlCopy) { 95 t.Errorf("%v: unexpected modification in dl", name) 96 } 97 if !floats.Same(d, dCopy) { 98 t.Errorf("%v: unexpected modification in d", name) 99 } 100 if !floats.Same(du, duCopy) { 101 t.Errorf("%v: unexpected modification in du", name) 102 } 103 if !floats.Same(b.Data, bCopy.Data) { 104 t.Errorf("%v: unexpected modification in B", name) 105 } 106 107 // Generate a dense representation of the matrix and compute the wanted result. 108 a := zeros(m, m, m) 109 for i := 0; i < m-1; i++ { 110 a.Data[i*a.Stride+i] = d[i] 111 a.Data[i*a.Stride+i+1] = du[i] 112 a.Data[(i+1)*a.Stride+i] = dl[i] 113 } 114 a.Data[(m-1)*a.Stride+m-1] = d[m-1] 115 116 blas64.Gemm(trans, blas.NoTrans, alpha, a, b, beta, want) 117 118 for i := 0; i < m; i++ { 119 for j := 0; j < n; j++ { 120 got.Data[i*got.Stride+j] -= want.Data[i*want.Stride+j] 121 } 122 } 123 diff := dlange(lapack.MaxColumnSum, got.Rows, got.Cols, got.Data, got.Stride) 124 if diff > tol { 125 t.Errorf("%v: unexpected result; diff=%v, want<=%v", name, diff, tol) 126 } 127 }