gonum.org/v1/gonum@v0.14.0/internal/asm/f32/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 f32_test 6 7 import ( 8 "testing" 9 10 . "gonum.org/v1/gonum/internal/asm/f32" 11 ) 12 13 func TestL2NormUnitary(t *testing.T) { 14 const tol = 1e-7 15 16 var src_gd float32 = 1 17 for j, v := range []struct { 18 want float32 19 x []float32 20 }{ 21 {want: 0, x: []float32{}}, 22 {want: 2, x: []float32{2}}, 23 {want: 3.7416573867739413, x: []float32{1, 2, 3}}, 24 {want: 3.7416573867739413, x: []float32{-1, -2, -3}}, 25 {want: nan, x: []float32{nan}}, 26 {want: 17.88854381999832, x: []float32{8, -8, 8, -8, 8}}, 27 {want: 2.23606797749979, x: []float32{0, 1, 0, -1, 0, 1, 0, -1, 0, 1}}, 28 } { 29 g_ln := 4 + j%2 30 v.x = guardVector(v.x, src_gd, g_ln) 31 src := v.x[g_ln : len(v.x)-g_ln] 32 ret := L2NormUnitary(src) 33 if !sameApprox(ret, v.want, tol) { 34 t.Errorf("Test %d L2Norm error Got: %f Expected: %f", j, ret, v.want) 35 } 36 if !isValidGuard(v.x, src_gd, g_ln) { 37 t.Errorf("Test %d Guard violated in src vector %v %v", j, v.x[:g_ln], v.x[len(v.x)-g_ln:]) 38 } 39 } 40 } 41 42 func TestL2NormInc(t *testing.T) { 43 const tol = 1e-7 44 45 var src_gd float32 = 1 46 for j, v := range []struct { 47 inc int 48 want float32 49 x []float32 50 }{ 51 {inc: 2, want: 0, x: []float32{}}, 52 {inc: 3, want: 2, x: []float32{2}}, 53 {inc: 10, want: 3.7416573867739413, x: []float32{1, 2, 3}}, 54 {inc: 5, want: 3.7416573867739413, x: []float32{-1, -2, -3}}, 55 {inc: 3, want: nan, x: []float32{nan}}, 56 {inc: 15, want: 17.88854381999832, x: []float32{8, -8, 8, -8, 8}}, 57 {inc: 1, want: 2.23606797749979, x: []float32{0, 1, 0, -1, 0, 1, 0, -1, 0, 1}}, 58 } { 59 g_ln, ln := 4+j%2, len(v.x) 60 v.x = guardIncVector(v.x, src_gd, v.inc, g_ln) 61 src := v.x[g_ln : len(v.x)-g_ln] 62 ret := L2NormInc(src, uintptr(ln), uintptr(v.inc)) 63 if !sameApprox(ret, v.want, tol) { 64 t.Errorf("Test %d L2NormInc error Got: %f Expected: %f", j, ret, v.want) 65 } 66 checkValidIncGuard(t, v.x, src_gd, v.inc, g_ln) 67 } 68 } 69 70 func TestL2DistanceUnitary(t *testing.T) { 71 const tol = 1e-7 72 73 var src_gd float32 = 1 74 for j, v := range []struct { 75 want float32 76 x, y []float32 77 }{ 78 {want: 0, x: []float32{}, y: []float32{}}, 79 {want: 2, x: []float32{3}, y: []float32{1}}, 80 {want: 3.7416573867739413, x: []float32{2, 4, 6}, y: []float32{1, 2, 3}}, 81 {want: 3.7416573867739413, x: []float32{1, 2, 3}, y: []float32{2, 4, 6}}, 82 {want: nan, x: []float32{nan}, y: []float32{0}}, 83 {want: 17.88854381999832, x: []float32{9, -9, 9, -9, 9}, y: []float32{1, -1, 1, -1, 1}}, 84 {want: 2.23606797749979, x: []float32{0, 1, 0, -1, 0, 1, 0, -1, 0, 1}, y: []float32{0, 2, 0, -2, 0, 2, 0, -2, 0, 2}}, 85 } { 86 g_ln := 4 + j%2 87 v.x = guardVector(v.x, src_gd, g_ln) 88 v.y = guardVector(v.y, src_gd, g_ln) 89 srcX := v.x[g_ln : len(v.x)-g_ln] 90 srcY := v.y[g_ln : len(v.y)-g_ln] 91 ret := L2DistanceUnitary(srcX, srcY) 92 if !sameApprox(ret, v.want, tol) { 93 t.Errorf("Test %d L2Distance error Got: %f Expected: %f", j, ret, v.want) 94 } 95 if !isValidGuard(v.x, src_gd, g_ln) { 96 t.Errorf("Test %d Guard violated in src vector %v %v", j, v.x[:g_ln], v.x[len(v.x)-g_ln:]) 97 } 98 } 99 }