floats: match Norm behaviour in Distance
MODIFY
floats/floats.go
floats/floats.go
@@ -152,14 +152,10 @@ func Distance(s, t []float64, L float64) float64 {
if len(s) == 0 {
return 0
}
- var norm float64
if L == 2 {
- for i, v := range s {
- diff := t[i] - v
- norm = math.Hypot(norm, diff)
- }
- return norm
+ return f64.L2DistanceUnitary(s, t)
}
+ var norm float64
if L == 1 {
for i, v := range s {
norm += math.Abs(t[i] - v)

MODIFY
floats/floats_test.go
floats/floats_test.go
@@ -261,7 +261,7 @@ func TestDistance(t *testing.T) {
copy(tmp, test.s)
Sub(tmp, test.t)
norm := Norm(tmp, L)
- if !EqualWithinAbsOrRel(dist, norm, 1e-15, 1e-15) {
+ if dist != norm { // Use equality because they should be identical.
t.Errorf("Distance does not match norm for case %v, %v. Expected %v, Found %v.", i, j, norm, dist)
}
}

MODIFY
internal/asm/f32/l2norm.go
internal/asm/f32/l2norm.go
@@ -60,3 +60,31 @@ func L2NormInc(x []float32, n, incX uintptr) (sum float32) {
}
return scale * math32.Sqrt(sumSquares)
}
+
+// L2DistanceUnitary is the L2 norm of x-y.
+func L2DistanceUnitary(x, y []float32) (sum float32) {
+ var scale float32
+ var sumSquares float32 = 1
+ for i, v := range x {
+ v -= y[i]
+ if v == 0 {
+ continue
+ }
+ absxi := math32.Abs(v)
+ if math32.IsNaN(absxi) {
+ return math32.NaN()
+ }
+ if scale < absxi {
+ s := scale / absxi
+ sumSquares = 1 + sumSquares*s*s
+ scale = absxi
+ } else {
+ s := absxi / scale
+ sumSquares += s * s
+ }
+ }
+ if math32.IsInf(scale, 1) {
+ return math32.Inf(1)
+ }
+ return scale * math32.Sqrt(sumSquares)
+}

MODIFY
internal/asm/f32/l2norm_test.go
internal/asm/f32/l2norm_test.go
@@ -58,3 +58,32 @@ func TestL2NormInc(t *testing.T) {
checkValidIncGuard(t, v.x, src_gd, v.inc, g_ln)
}
}
+
+func TestL2DistanceUnitary(t *testing.T) {
+ var src_gd float32 = 1
+ for j, v := range []struct {
+ want float32
+ x, y []float32
+ }{
+ {want: 0, x: []float32{}, y: []float32{}},
+ {want: 2, x: []float32{3}, y: []float32{1}},
+ {want: 3.7416573867739413, x: []float32{2, 4, 6}, y: []float32{1, 2, 3}},
+ {want: 3.7416573867739413, x: []float32{1, 2, 3}, y: []float32{2, 4, 6}},
+ {want: nan, x: []float32{nan}, y: []float32{0}},
+ {want: 17.88854381999832, x: []float32{9, -9, 9, -9, 9}, y: []float32{1, -1, 1, -1, 1}},
+ {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}},
+ } {
+ g_ln := 4 + j%2
+ v.x = guardVector(v.x, src_gd, g_ln)
+ v.y = guardVector(v.y, src_gd, g_ln)
+ srcX := v.x[g_ln : len(v.x)-g_ln]
+ srcY := v.y[g_ln : len(v.y)-g_ln]
+ ret := L2DistanceUnitary(srcX, srcY)
+ if !within(ret, v.want) {
+ t.Errorf("Test %d L2Distance error Got: %f Expected: %f", j, ret, v.want)
+ }
+ if !isValidGuard(v.x, src_gd, g_ln) {
+ t.Errorf("Test %d Guard violated in src vector %v %v", j, v.x[:g_ln], v.x[len(v.x)-g_ln:])
+ }
+ }
+}

MODIFY
internal/asm/f64/l2norm.go
internal/asm/f64/l2norm.go
@@ -60,3 +60,31 @@ func L2NormInc(x []float64, n, incX uintptr) (sum float64) {
}
return scale * math.Sqrt(sumSquares)
}
+
+// L2DistanceUnitary is the L2 norm of x-y.
+func L2DistanceUnitary(x, y []float64) (sum float64) {
+ var scale float64
+ var sumSquares float64 = 1
+ for i, v := range x {
+ v -= y[i]
+ if v == 0 {
+ continue
+ }
+ absxi := math.Abs(v)
+ if math.IsNaN(absxi) {
+ return math.NaN()
+ }
+ if scale < absxi {
+ s := scale / absxi
+ sumSquares = 1 + sumSquares*s*s
+ scale = absxi
+ } else {
+ s := absxi / scale
+ sumSquares += s * s
+ }
+ }
+ if math.IsInf(scale, 1) {
+ return math.Inf(1)
+ }
+ return scale * math.Sqrt(sumSquares)
+}

MODIFY
internal/asm/f64/l2norm_test.go
internal/asm/f64/l2norm_test.go
@@ -58,3 +58,32 @@ func TestL2NormInc(t *testing.T) {
checkValidIncGuard(t, v.x, src_gd, v.inc, g_ln)
}
}
+
+func TestL2DistanceUnitary(t *testing.T) {
+ var src_gd float64 = 1
+ for j, v := range []struct {
+ want float64
+ x, y []float64
+ }{
+ {want: 0, x: []float64{}, y: []float64{}},
+ {want: 2, x: []float64{3}, y: []float64{1}},
+ {want: 3.7416573867739413, x: []float64{2, 4, 6}, y: []float64{1, 2, 3}},
+ {want: 3.7416573867739413, x: []float64{1, 2, 3}, y: []float64{2, 4, 6}},
+ {want: nan, x: []float64{nan}, y: []float64{0}},
+ {want: 17.88854381999832, x: []float64{9, -9, 9, -9, 9}, y: []float64{1, -1, 1, -1, 1}},
+ {want: 2.23606797749979, x: []float64{0, 1, 0, -1, 0, 1, 0, -1, 0, 1}, y: []float64{0, 2, 0, -2, 0, 2, 0, -2, 0, 2}},
+ } {
+ g_ln := 4 + j%2
+ v.x = guardVector(v.x, src_gd, g_ln)
+ v.y = guardVector(v.y, src_gd, g_ln)
+ srcX := v.x[g_ln : len(v.x)-g_ln]
+ srcY := v.y[g_ln : len(v.y)-g_ln]
+ ret := L2DistanceUnitary(srcX, srcY)
+ if !within(ret, v.want) {
+ t.Errorf("Test %d L2Distance error Got: %f Expected: %f", j, ret, v.want)
+ }
+ if !isValidGuard(v.x, src_gd, g_ln) {
+ t.Errorf("Test %d Guard violated in src vector %v %v", j, v.x[:g_ln], v.x[len(v.x)-g_ln:])
+ }
+ }
+}

floats: match Norm behaviour in Distance