github.com/gopherd/gonum@v0.0.4/internal/asm/c128/l2norm_test.go (about) 1 // Copyright ©2019 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 c128_test 6 7 import ( 8 "fmt" 9 "math" 10 "math/cmplx" 11 "testing" 12 13 . "github.com/gopherd/gonum/internal/asm/c128" 14 ) 15 16 // nanwith copied from floats package 17 func nanwith(payload uint64) complex128 { 18 const ( 19 nanBits = 0x7ff8000000000000 20 nanMask = 0xfff8000000000000 21 ) 22 nan := math.Float64frombits(nanBits | (payload &^ nanMask)) 23 return complex(nan, nan) 24 } 25 26 func TestL2NormUnitary(t *testing.T) { 27 const tol = 1e-15 28 29 var src_gd complex128 = 1 30 for j, v := range []struct { 31 want float64 32 x []complex128 33 }{ 34 {want: 0, x: []complex128{}}, 35 {want: 2, x: []complex128{2}}, 36 {want: 2, x: []complex128{2i}}, 37 {want: math.Sqrt(8), x: []complex128{2 + 2i}}, 38 {want: 3.7416573867739413, x: []complex128{1, 2, 3}}, 39 {want: 3.7416573867739413, x: []complex128{-1, -2, -3}}, 40 {want: 3.7416573867739413, x: []complex128{1i, 2i, 3i}}, 41 {want: 3.7416573867739413, x: []complex128{-1i, -2i, -3i}}, 42 {want: math.Sqrt(28), x: []complex128{1 + 1i, 2 + 2i, 3 + 3i}}, 43 {want: math.Sqrt(28), x: []complex128{-1 - 1i, -2 - 2i, -3 - 3i}}, 44 {want: nan, x: []complex128{cnan}}, 45 {want: nan, x: []complex128{1, cinf, 3, nanwith(25), 5}}, 46 {want: 17.88854381999832, x: []complex128{8, -8, 8, -8, 8}}, 47 {want: 2.23606797749979, x: []complex128{0, 1, 0, -1, 0, 1, 0, -1, 0, 1}}, 48 {want: 17.88854381999832, x: []complex128{8i, -8i, 8i, -8i, 8i}}, 49 {want: 2.23606797749979, x: []complex128{0, 1i, 0, -1i, 0, 1i, 0, -1i, 0, 1i}}, 50 {want: math.Sqrt(640), x: []complex128{8 + 8i, -8 - 8i, 8 + 8i, -8 - 8i, 8 + 8i}}, 51 {want: math.Sqrt(10), x: []complex128{0, 1 + 1i, 0, -1 - 1i, 0, 1 + 1i, 0, -1 - 1i, 0, 1 + 1i}}, 52 } { 53 g_ln := 4 + j%2 54 v.x = guardVector(v.x, src_gd, g_ln) 55 src := v.x[g_ln : len(v.x)-g_ln] 56 ret := L2NormUnitary(src) 57 if !sameApprox(ret, v.want, tol) { 58 t.Errorf("Test %d L2Norm error Got: %f Expected: %f", j, ret, v.want) 59 } 60 if !isValidGuard(v.x, src_gd, g_ln) { 61 t.Errorf("Test %d Guard violated in src vector %v %v", j, v.x[:g_ln], v.x[len(v.x)-g_ln:]) 62 } 63 } 64 } 65 66 func TestL2DistanceUnitary(t *testing.T) { 67 const tol = 1e-15 68 69 var src_gd complex128 = 1 70 for j, v := range []struct { 71 want float64 72 x, y []complex128 73 }{ 74 {want: 0, x: []complex128{}, y: []complex128{}}, 75 {want: 2, x: []complex128{3}, y: []complex128{1}}, 76 {want: 2, x: []complex128{3i}, y: []complex128{1i}}, 77 {want: 3.7416573867739413, x: []complex128{2, 4, 6}, y: []complex128{1, 2, 3}}, 78 {want: 3.7416573867739413, x: []complex128{1, 2, 3}, y: []complex128{2, 4, 6}}, 79 {want: 3.7416573867739413, x: []complex128{2i, 4i, 6i}, y: []complex128{1i, 2i, 3i}}, 80 {want: 3.7416573867739413, x: []complex128{1i, 2i, 3i}, y: []complex128{2i, 4i, 6i}}, 81 {want: math.Sqrt(28), x: []complex128{2 + 2i, 4 + 4i, 6 + 6i}, y: []complex128{1 + 1i, 2 + 2i, 3 + 3i}}, 82 {want: math.Sqrt(28), x: []complex128{1 + 1i, 2 + 2i, 3 + 3i}, y: []complex128{2 + 2i, 4 + 4i, 6 + 6i}}, 83 {want: nan, x: []complex128{cnan}, y: []complex128{0}}, 84 {want: 17.88854381999832, x: []complex128{9, -9, 9, -9, 9}, y: []complex128{1, -1, 1, -1, 1}}, 85 {want: 2.23606797749979, x: []complex128{0, 1, 0, -1, 0, 1, 0, -1, 0, 1}, y: []complex128{0, 2, 0, -2, 0, 2, 0, -2, 0, 2}}, 86 {want: 17.88854381999832, x: []complex128{9i, -9i, 9i, -9i, 9i}, y: []complex128{1i, -1i, 1i, -1i, 1i}}, 87 {want: 2.23606797749979, x: []complex128{0, 1i, 0, -1i, 0, 1i, 0, -1i, 0, 1i}, y: []complex128{0, 2i, 0, -2i, 0, 2i, 0, -2i, 0, 2i}}, 88 {want: math.Sqrt(640), x: []complex128{9 + 9i, -9 - 9i, 9 + 9i, -9 - 9i, 9 + 9i}, y: []complex128{1 + 1i, -1 - 1i, 1 + 1i, -1 - 1i, 1 + 1i}}, 89 {want: math.Sqrt(10), x: []complex128{0, 1 + 1i, 0, -1 - 1i, 0, 1 + 1i, 0, -1 - 1i, 0, 1 + 1i}, y: []complex128{0, 2 + 2i, 0, -2 - 2i, 0, 2 + 2i, 0, -2 - 2i, 0, 2 + 2i}}, 90 } { 91 g_ln := 4 + j%2 92 v.x = guardVector(v.x, src_gd, g_ln) 93 v.y = guardVector(v.y, src_gd, g_ln) 94 srcX := v.x[g_ln : len(v.x)-g_ln] 95 srcY := v.y[g_ln : len(v.y)-g_ln] 96 ret := L2DistanceUnitary(srcX, srcY) 97 if !sameApprox(ret, v.want, tol) { 98 t.Errorf("Test %d L2Distance error Got: %f Expected: %f", j, ret, v.want) 99 } 100 if !isValidGuard(v.x, src_gd, g_ln) { 101 t.Errorf("Test %d Guard violated in src vector %v %v", j, v.x[:g_ln], v.x[len(v.x)-g_ln:]) 102 } 103 } 104 } 105 106 func BenchmarkL2NormNetlib(b *testing.B) { 107 netlib := func(x []complex128) (sum float64) { 108 var scale float64 109 sumSquares := 1.0 110 for _, v := range x { 111 if v == 0 { 112 continue 113 } 114 absxi := cmplx.Abs(v) 115 if math.IsNaN(absxi) { 116 return math.NaN() 117 } 118 if scale < absxi { 119 s := scale / absxi 120 sumSquares = 1 + sumSquares*s*s 121 scale = absxi 122 } else { 123 s := absxi / scale 124 sumSquares += s * s 125 } 126 } 127 if math.IsInf(scale, 1) { 128 return math.Inf(1) 129 } 130 return scale * math.Sqrt(sumSquares) 131 } 132 133 tests := []struct { 134 name string 135 f func(x []complex128) float64 136 }{ 137 {"L2NormUnitaryNetlib", netlib}, 138 {"L2NormUnitary", L2NormUnitary}, 139 } 140 x[0] = 4 // replace the leading zero (edge case) 141 for _, test := range tests { 142 for _, ln := range []uintptr{1, 3, 10, 30, 1e2, 3e2, 1e3, 3e3, 1e4, 3e4, 1e5} { 143 b.Run(fmt.Sprintf("%s-%d", test.name, ln), func(b *testing.B) { 144 b.SetBytes(int64(64 * ln)) 145 x := x[:ln] 146 b.ResetTimer() 147 for i := 0; i < b.N; i++ { 148 test.f(x) 149 } 150 }) 151 } 152 } 153 }