The Polysign Distance Function

The native distance function was first exposed by Jonathan Doolin in a USENET discussion.

A four-signed value
 - a + b * c # d 
has magnitude
 sqrt( aa + bb + cc + dd - (2/3)(ab + ac + ad + bc + bd + cd)) .
Likewise a three-signed value
   - a + b * c  
has distance
 sqrt( aa + bb + cc - ab - ac - bc ) 
from the origin.
For two-signed values
   - a + b 
the distance is
   sqrt( aa + bb - 2ab ).

The following C++ algorithm provides the distance for any sign level (signature):

double nSigned::Magnitude() const
{    
    double d = 0;
    double fac = - 1.0 / ( n - 1 );
    for( int i = 0; i < n; i++ )
    {
        for( int j = 0; j < n; j++ )
        {
            if( i == j ) d += x[i] * x[j];
            else d += fac * x[i] * x[j];
        }
    }
    return sqrt(d);
}
where n is the signature and x[i] are the magnitudes.

This distance function exactly matches the standard Cartesian distance when the polysign value is transformed.

Back to Polysigned Numbers