github.com/gonum/lapack@v0.0.0-20181123203213-e4cdc5a0bff9/testlapack/dgebak.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/rand" 10 "testing" 11 12 "github.com/gonum/blas" 13 "github.com/gonum/blas/blas64" 14 "github.com/gonum/lapack" 15 ) 16 17 type Dgebaker interface { 18 Dgebak(job lapack.Job, side lapack.EVSide, n, ilo, ihi int, scale []float64, m int, v []float64, ldv int) 19 } 20 21 func DgebakTest(t *testing.T, impl Dgebaker) { 22 rnd := rand.New(rand.NewSource(1)) 23 24 for _, job := range []lapack.Job{lapack.None, lapack.Permute, lapack.Scale, lapack.PermuteScale} { 25 for _, side := range []lapack.EVSide{lapack.LeftEV, lapack.RightEV} { 26 for _, n := range []int{0, 1, 2, 3, 4, 5, 6, 10, 18, 31, 53} { 27 for _, extra := range []int{0, 11} { 28 for cas := 0; cas < 100; cas++ { 29 m := rnd.Intn(n + 1) 30 v := randomGeneral(n, m, m+extra, rnd) 31 var ilo, ihi int 32 if v.Rows > 0 { 33 ihi = rnd.Intn(n) 34 ilo = rnd.Intn(ihi + 1) 35 } else { 36 ihi = -1 37 } 38 testDgebak(t, impl, job, side, ilo, ihi, v, rnd) 39 } 40 } 41 } 42 } 43 } 44 } 45 46 func testDgebak(t *testing.T, impl Dgebaker, job lapack.Job, side lapack.EVSide, ilo, ihi int, v blas64.General, rnd *rand.Rand) { 47 const tol = 1e-15 48 n := v.Rows 49 m := v.Cols 50 extra := v.Stride - v.Cols 51 52 // Create D and D^{-1} by generating random scales between ilo and ihi. 53 d := eye(n, n) 54 dinv := eye(n, n) 55 scale := nanSlice(n) 56 if job == lapack.Scale || job == lapack.PermuteScale { 57 if ilo == ihi { 58 scale[ilo] = 1 59 } else { 60 for i := ilo; i <= ihi; i++ { 61 scale[i] = 2 * rnd.Float64() 62 d.Data[i*d.Stride+i] = scale[i] 63 dinv.Data[i*dinv.Stride+i] = 1 / scale[i] 64 } 65 } 66 } 67 68 // Create P by generating random column swaps. 69 p := eye(n, n) 70 if job == lapack.Permute || job == lapack.PermuteScale { 71 // Make up some random permutations. 72 for i := n - 1; i > ihi; i-- { 73 scale[i] = float64(rnd.Intn(i + 1)) 74 blas64.Swap(n, 75 blas64.Vector{p.Stride, p.Data[i:]}, 76 blas64.Vector{p.Stride, p.Data[int(scale[i]):]}) 77 } 78 for i := 0; i < ilo; i++ { 79 scale[i] = float64(i + rnd.Intn(ihi-i+1)) 80 blas64.Swap(n, 81 blas64.Vector{p.Stride, p.Data[i:]}, 82 blas64.Vector{p.Stride, p.Data[int(scale[i]):]}) 83 } 84 } 85 86 got := cloneGeneral(v) 87 impl.Dgebak(job, side, n, ilo, ihi, scale, m, got.Data, got.Stride) 88 89 prefix := fmt.Sprintf("Case job=%v, side=%v, n=%v, ilo=%v, ihi=%v, m=%v, extra=%v", 90 job, side, n, ilo, ihi, m, extra) 91 92 if !generalOutsideAllNaN(got) { 93 t.Errorf("%v: out-of-range write to V\n%v", prefix, got.Data) 94 } 95 96 // Compute D*V or D^{-1}*V and store into dv. 97 dv := zeros(n, m, m) 98 if side == lapack.RightEV { 99 blas64.Gemm(blas.NoTrans, blas.NoTrans, 1, d, v, 0, dv) 100 } else { 101 blas64.Gemm(blas.NoTrans, blas.NoTrans, 1, dinv, v, 0, dv) 102 } 103 // Compute P*D*V or P*D^{-1}*V and store into want. 104 want := zeros(n, m, m) 105 blas64.Gemm(blas.NoTrans, blas.NoTrans, 1, p, dv, 0, want) 106 107 if !equalApproxGeneral(want, got, tol) { 108 t.Errorf("%v: unexpected value of V", prefix) 109 } 110 }