github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/blas/testblas/dznrm2.go (about) 1 // Copyright ©2017 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 testblas 6 7 import ( 8 "fmt" 9 "math" 10 "testing" 11 12 "golang.org/x/exp/rand" 13 14 "github.com/jingcheng-WU/gonum/floats/scalar" 15 ) 16 17 type Dznrm2er interface { 18 Dznrm2(n int, x []complex128, incX int) float64 19 Dnrm2er 20 } 21 22 func Dznrm2Test(t *testing.T, impl Dznrm2er) { 23 tol := 1e-12 24 for tc, test := range []struct { 25 x []complex128 26 want float64 27 }{ 28 { 29 x: nil, 30 want: 0, 31 }, 32 { 33 x: []complex128{1 + 2i}, 34 want: 2.2360679774998, 35 }, 36 { 37 x: []complex128{1 + 2i, 3 + 4i}, 38 want: 5.4772255750517, 39 }, 40 { 41 x: []complex128{1 + 2i, 3 + 4i, 5 + 6i}, 42 want: 9.5393920141695, 43 }, 44 { 45 x: []complex128{1 + 2i, 3 + 4i, 5 + 6i, 7 + 8i}, 46 want: 1.4282856857086e+01, 47 }, 48 { 49 x: []complex128{1 + 2i, 3 + 4i, 5 + 6i, 7 + 8i, 9 + 10i}, 50 want: 1.9621416870349e+01, 51 }, 52 { 53 x: []complex128{1 + 2i, 3 + 4i, 5 + 6i, 7 + 8i, 9 + 10i, 11 + 12i}, 54 want: 2.5495097567964e+01, 55 }, 56 { 57 x: []complex128{1 + 2i, 3 + 4i, 5 + 6i, 7 + 8i, 9 + 10i, 11 + 12i, 13 + 14i}, 58 want: 3.1859064644148e+01, 59 }, 60 { 61 x: []complex128{1 + 2i, 3 + 4i, 5 + 6i, 7 + 8i, 9 + 10i, 11 + 12i, 13 + 14i, 15 + 16i}, 62 want: 3.8678159211627e+01, 63 }, 64 { 65 x: []complex128{1 + 2i, 3 + 4i, 5 + 6i, 7 + 8i, 9 + 10i, 11 + 12i, 13 + 14i, 15 + 16i, 17 + 18i}, 66 want: 4.5923850012820e+01, 67 }, 68 { 69 x: []complex128{1 + 2i, 3 + 4i, 5 + 6i, 7 + 8i, 9 + 10i, 11 + 12i, 13 + 14i, 15 + 16i, 17 + 18i, 19 + 20i}, 70 want: 5.3572380943915e+01, 71 }, 72 { 73 x: []complex128{1 + 2i, 3 + 4i, 5 + 6i, 7 + 8i, 9 + 10i, 11 + 12i, 13 + 14i, 15 + 16i, 17 + 18i, 19 + 20i, 21 + 22i}, 74 want: 6.1603571325046e+01, 75 }, 76 { 77 x: []complex128{1 + 2i, 3 + 4i, 5 + 6i, 7 + 8i, 9 + 10i, 11 + 12i, 13 + 14i, 15 + 16i, 17 + 18i, 19 + 20i, 21 + 22i, 23 + 24i}, 78 want: 70, 79 }, 80 } { 81 n := len(test.x) 82 for _, incX := range []int{-10, -1, 1, 2, 9, 17} { 83 x := makeZVector(test.x, incX) 84 xCopy := make([]complex128, len(x)) 85 copy(xCopy, x) 86 87 got := impl.Dznrm2(n, x, incX) 88 89 prefix := fmt.Sprintf("Case %v (n=%v,incX=%v):", tc, n, incX) 90 91 if !zsame(x, xCopy) { 92 t.Errorf("%v: unexpected modification of x", prefix) 93 } 94 95 if incX < 0 { 96 if got != 0 { 97 t.Errorf("%v: non-zero result when incX < 0. got %v", prefix, got) 98 } 99 continue 100 } 101 102 if !scalar.EqualWithinAbsOrRel(test.want, got, tol, tol) { 103 t.Errorf("%v: unexpected result. want %v, got %v", prefix, test.want, got) 104 } 105 } 106 } 107 108 tol = 1e-14 109 rnd := rand.New(rand.NewSource(1)) 110 for _, n := range []int{10, 50, 100} { 111 for _, incX := range []int{1, 2, 10} { 112 re := make([]float64, n) 113 for i := range re { 114 re[i] = rnd.NormFloat64() 115 } 116 im := make([]float64, n) 117 for i := range im { 118 im[i] = rnd.NormFloat64() 119 } 120 want := math.Hypot(impl.Dnrm2(n, re, 1), impl.Dnrm2(n, im, 1)) 121 122 x := make([]complex128, (n-1)*incX+1) 123 for i := range x { 124 x[i] = znan 125 } 126 for i := range re { 127 x[i*incX] = complex(re[i], im[i]) 128 } 129 130 got := impl.Dznrm2(n, x, incX) 131 132 if !scalar.EqualWithinAbsOrRel(want, got, tol, tol) { 133 t.Errorf("Case n=%v,incX=%v: unexpected result using Dnrm2. want %v, got %v", n, incX, want, got) 134 } 135 } 136 } 137 }