github.com/gonum/lapack@v0.0.0-20181123203213-e4cdc5a0bff9/testlapack/dormhr.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 Dormhrer interface { 18 Dormhr(side blas.Side, trans blas.Transpose, m, n, ilo, ihi int, a []float64, lda int, tau, c []float64, ldc int, work []float64, lwork int) 19 20 Dgehrder 21 } 22 23 func DormhrTest(t *testing.T, impl Dormhrer) { 24 rnd := rand.New(rand.NewSource(1)) 25 26 for _, side := range []blas.Side{blas.Left, blas.Right} { 27 for _, trans := range []blas.Transpose{blas.NoTrans, blas.Trans} { 28 for _, m := range []int{1, 2, 3, 4, 5, 8, 9, 10, 23} { 29 for _, n := range []int{1, 2, 3, 4, 5, 8, 9, 10, 23} { 30 for _, extra := range []int{0, 1, 13} { 31 for cas := 0; cas < 10; cas++ { 32 nq := m 33 if side == blas.Right { 34 nq = n 35 } 36 ilo := rnd.Intn(nq) 37 ihi := rnd.Intn(nq) 38 if ilo > ihi { 39 ilo, ihi = ihi, ilo 40 } 41 testDormhr(t, impl, side, trans, m, n, ilo, ihi, extra, true, rnd) 42 testDormhr(t, impl, side, trans, m, n, ilo, ihi, extra, false, rnd) 43 } 44 } 45 } 46 } 47 } 48 } 49 for _, side := range []blas.Side{blas.Left, blas.Right} { 50 for _, trans := range []blas.Transpose{blas.NoTrans, blas.Trans} { 51 testDormhr(t, impl, side, trans, 0, 0, 0, -1, 0, true, rnd) 52 testDormhr(t, impl, side, trans, 0, 0, 0, -1, 0, false, rnd) 53 } 54 } 55 } 56 57 func testDormhr(t *testing.T, impl Dormhrer, side blas.Side, trans blas.Transpose, m, n, ilo, ihi, extra int, optwork bool, rnd *rand.Rand) { 58 const tol = 1e-14 59 60 var nq, nw int 61 switch side { 62 case blas.Left: 63 nq = m 64 nw = n 65 case blas.Right: 66 nq = n 67 nw = m 68 } 69 70 // Compute the elementary reflectors and tau. 71 a := randomGeneral(nq, nq, nq+extra, rnd) 72 var tau []float64 73 if nq > 1 { 74 tau = nanSlice(nq - 1) 75 } 76 work := nanSlice(max(1, nq)) // Minimum work for Dgehrd. 77 impl.Dgehrd(nq, ilo, ihi, a.Data, a.Stride, tau, work, len(work)) 78 79 // Construct Q from the elementary reflectors in a and from tau. 80 q := eye(nq, nq) 81 qCopy := eye(nq, nq) 82 for j := ilo; j < ihi; j++ { 83 h := eye(nq, nq) 84 v := blas64.Vector{ 85 Inc: 1, 86 Data: make([]float64, nq), 87 } 88 v.Data[j+1] = 1 89 for i := j + 2; i < ihi+1; i++ { 90 v.Data[i] = a.Data[i*a.Stride+j] 91 } 92 blas64.Ger(-tau[j], v, v, h) 93 copy(qCopy.Data, q.Data) 94 blas64.Gemm(blas.NoTrans, blas.NoTrans, 1, qCopy, h, 0, q) 95 } 96 97 c := randomGeneral(m, n, n+extra, rnd) 98 99 // Compute the product of Q and C explicitly. 100 qc := randomGeneral(m, n, n+extra, rnd) 101 if side == blas.Left { 102 blas64.Gemm(trans, blas.NoTrans, 1, q, c, 0, qc) 103 } else { 104 blas64.Gemm(blas.NoTrans, trans, 1, c, q, 0, qc) 105 } 106 107 // Compute the product of Q and C using Dormhr. 108 if optwork { 109 work = nanSlice(1) 110 impl.Dormhr(side, trans, m, n, ilo, ihi, nil, a.Stride, nil, nil, c.Stride, work, -1) 111 work = nanSlice(int(work[0])) 112 } else { 113 work = nanSlice(max(1, nw)) 114 } 115 impl.Dormhr(side, trans, m, n, ilo, ihi, a.Data, a.Stride, tau, c.Data, c.Stride, work, len(work)) 116 117 // Compare the two answers. 118 prefix := fmt.Sprintf("Case side=%v, trans=%v, m=%v, n=%v, ilo=%v, ihi=%v, extra=%v, optwork=%v", 119 side, trans, m, n, ilo, ihi, extra, optwork) 120 if !generalOutsideAllNaN(c) { 121 t.Errorf("%v: out-of-range write to C\n%v", prefix, c.Data) 122 } 123 for i := 0; i < m; i++ { 124 for j := 0; j < n; j++ { 125 cij := c.Data[i*c.Stride+j] 126 qcij := qc.Data[i*qc.Stride+j] 127 if math.Abs(cij-qcij) > tol { 128 t.Errorf("%v: unexpected value of the QC product at [%v,%v]: want %v, got %v", prefix, i, j, qcij, cij) 129 } 130 } 131 } 132 }