Function Repository Resource:

FindDistanceInstance

Source Notebook

Find an instance of n-dimensional vectors that produce a specified distance matrix

Contributed by: Rob Knapp, Paritosh Mokhasi

ResourceFunction["FindDistanceInstance"][mat,n]

finds an instance of n-dimensional vectors that will generate the specified distance matrix mat.

Details and Options

mat must be a symmetric square numeric matrix.
n must be a positive integer.
ResourceFunction["FindDistanceInstance"] takes the option Tolerancet and returns a result if the vectors have the specified distances within the specified tolerance t. The default Tolerance value is 10-8.

Examples

Basic Examples (2) 

Find a set of three 2D points that are equidistant:

In[1]:=
pts = ResourceFunction["FindDistanceInstance"][({
    {0, 1, 1},
    {1, 0, 1},
    {1, 1, 0}
   }), 2]
Out[1]=

Verify that the points produce the specified distance matrix:

In[2]:=
DistanceMatrix[pts]
Out[2]=

Scope (3) 

Find a set of four 3D points that are equidistant:

In[3]:=
pts = ResourceFunction["FindDistanceInstance"][
  SparseArray[{{i_, j_} /; i != j -> 1.}, {4, 4}, 0.], 3]
Out[3]=

Verify that the points are equidistant:

In[4]:=
DistanceMatrix[pts]
Out[4]=

The points form a regular tetrahedron in 3D:

In[5]:=
ConvexHullMesh[pts, ImageSize -> Small]
Out[5]=

Options (2) 

Tolerance (2) 

A 4D Hilbert matrix with its diagonal zeroed cannot be a distance matrix because it fails to satisfy some triangle inequalities:

In[6]:=
mat = N[HilbertMatrix[4]];
ResourceFunction["FindDistanceInstance"][
 mat - DiagonalMatrix[Diagonal[mat]], 3]
Out[7]=

Use a larger tolerance to get a result anyway:

In[8]:=
ResourceFunction["FindDistanceInstance"][
 mat - DiagonalMatrix[Diagonal[mat]], 3, Tolerance -> 10^-1]
Out[8]=

Applications (2) 

Find a set of three 2D points whose distances form a HilbertMatrix:

In[9]:=
mat = N@HilbertMatrix[3];
pts = ResourceFunction["FindDistanceInstance"][
  mat - DiagonalMatrix[Diagonal[mat]], 2]
Out[10]=
In[11]:=
Graphics[{FaceForm[Pink], EdgeForm[Black], Polygon[pts]}, ImageSize -> Small]
Out[11]=

Find a set of three 2D points whose distances form a Toeplitz matrix:

In[12]:=
pts = ResourceFunction["FindDistanceInstance"][
  ToeplitzMatrix[{0, 1, 2, 3}], 2]
Out[12]=
In[13]:=
Graphics[{{FaceForm[Pink], EdgeForm[Black], Polygon[pts]}, {Red, PointSize[0.03], Point[pts]}}, ImageSize -> Small]
Out[13]=

Publisher

Paritosh Mokhasi

Version History

  • 1.0.0 – 10 April 2020

Related Resources

License Information