Skip to content

Commit

Permalink
floats: match Norm behaviour in Distance
Browse files Browse the repository at this point in the history
  • Loading branch information
kortschak committed Oct 25, 2019
1 parent e2ba7f0 commit 7e1144f
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 7 deletions.
8 changes: 2 additions & 6 deletions floats/floats.go
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion floats/floats_test.go
Expand Up @@ -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)
}
}
Expand Down
28 changes: 28 additions & 0 deletions internal/asm/f32/l2norm.go
Expand Up @@ -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)
}
29 changes: 29 additions & 0 deletions internal/asm/f32/l2norm_test.go
Expand Up @@ -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:])
}
}
}
28 changes: 28 additions & 0 deletions internal/asm/f64/l2norm.go
Expand Up @@ -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)
}
29 changes: 29 additions & 0 deletions internal/asm/f64/l2norm_test.go
Expand Up @@ -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:])
}
}
}

0 comments on commit 7e1144f

Please sign in to comment.