github.com/gonum/lapack@v0.0.0-20181123203213-e4cdc5a0bff9/testlapack/dlasy2.go (about) 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. 4 5 package testlapack 6 7 import ( 8 "fmt" 9 "math" 10 "math/rand" 11 "testing" 12 13 "github.com/gonum/blas" 14 "github.com/gonum/blas/blas64" 15 ) 16 17 type Dlasy2er interface { 18 Dlasy2(tranl, tranr bool, isgn, n1, n2 int, tl []float64, ldtl int, tr []float64, ldtr int, b []float64, ldb int, x []float64, ldx int) (scale, xnorm float64, ok bool) 19 } 20 21 func Dlasy2Test(t *testing.T, impl Dlasy2er) { 22 rnd := rand.New(rand.NewSource(1)) 23 for _, tranl := range []bool{true, false} { 24 for _, tranr := range []bool{true, false} { 25 for _, isgn := range []int{1, -1} { 26 for _, n1 := range []int{0, 1, 2} { 27 for _, n2 := range []int{0, 1, 2} { 28 for _, extra := range []int{0, 1, 2, 13} { 29 for cas := 0; cas < 1000; cas++ { 30 testDlasy2(t, impl, tranl, tranr, isgn, n1, n2, extra, rnd) 31 } 32 } 33 } 34 } 35 } 36 } 37 } 38 } 39 40 func testDlasy2(t *testing.T, impl Dlasy2er, tranl, tranr bool, isgn, n1, n2, extra int, rnd *rand.Rand) { 41 const tol = 1e-11 42 43 tl := randomGeneral(n1, n1, n1+extra, rnd) 44 tr := randomGeneral(n2, n2, n2+extra, rnd) 45 b := randomGeneral(n1, n2, n2+extra, rnd) 46 x := randomGeneral(n1, n2, n2+extra, rnd) 47 48 scale, xnorm, ok := impl.Dlasy2(tranl, tranr, isgn, n1, n2, tl.Data, tl.Stride, tr.Data, tr.Stride, b.Data, b.Stride, x.Data, x.Stride) 49 if scale > 1 { 50 t.Errorf("invalid value of scale, want <= 1, got %v", scale) 51 } 52 if n1 == 0 || n2 == 0 { 53 return 54 } 55 56 prefix := fmt.Sprintf("Case n1=%v, n2=%v, isgn=%v", n1, n2, isgn) 57 58 // Check any invalid modifications of x. 59 if !generalOutsideAllNaN(x) { 60 t.Errorf("%v: out-of-range write to x\n%v", prefix, x.Data) 61 } 62 63 var xnormWant float64 64 for i := 0; i < n1; i++ { 65 var rowsum float64 66 for j := 0; j < n2; j++ { 67 rowsum += math.Abs(x.Data[i*x.Stride+j]) 68 } 69 if rowsum > xnormWant { 70 xnormWant = rowsum 71 } 72 } 73 if xnormWant != xnorm { 74 t.Errorf("%v: unexpected xnorm: want %v, got %v", prefix, xnormWant, xnorm) 75 } 76 77 // Multiply b by scale to get the wanted right-hand side. 78 for i := 0; i < n1; i++ { 79 for j := 0; j < n2; j++ { 80 b.Data[i*b.Stride+j] *= scale 81 } 82 } 83 // Compute the wanted left-hand side. 84 lhsWant := randomGeneral(n1, n2, n2, rnd) 85 if tranl { 86 blas64.Gemm(blas.Trans, blas.NoTrans, 1, tl, x, 0, lhsWant) 87 } else { 88 blas64.Gemm(blas.NoTrans, blas.NoTrans, 1, tl, x, 0, lhsWant) 89 } 90 if tranr { 91 blas64.Gemm(blas.NoTrans, blas.Trans, float64(isgn), x, tr, 1, lhsWant) 92 } else { 93 blas64.Gemm(blas.NoTrans, blas.NoTrans, float64(isgn), x, tr, 1, lhsWant) 94 } 95 // Compare them. 96 for i := 0; i < n1; i++ { 97 for j := 0; j < n2; j++ { 98 diff := lhsWant.Data[i*lhsWant.Stride+j] - b.Data[i*b.Stride+j] 99 if math.Abs(diff) > tol && ok { 100 t.Errorf("%v: unexpected result, diff[%v,%v]=%v", prefix, i, j, diff) 101 } 102 } 103 } 104 }