Fork me on GitHub

src/arraymancer/spatial/distances

  Source Edit

Procs

proc distance(metric: typedesc[Euclidean]; v, w: Tensor[float];
              squared: static bool = false): float

Computes the Euclidean distance between points v and w. Both need to be rank 1 tensors with k elements, where k is the dimensionality of the points.

The Euclidean metric is defined as:

d = ( Σ_i | v_i - w_i |^2 )^(1/2)

If squared is true returns the square of the distance

  Source Edit
proc distance(metric: typedesc[Jaccard]; v, w: Tensor[float]): float

Computes the Jaccard distance between points v and w. Both need to be rank 1 tensors with k elements, where k is the dimensionality of the points.

The Jaccard distance is defined as:

d = 1 - J(A, B) = ( | A ∪ B | - | A ∩ B | ) / ( | A ∪ B | )

  Source Edit
proc distance(metric: typedesc[Manhattan]; v, w: Tensor[float]): float

Computes the Manhattan distance between points v and w. Both need to be rank 1 tensors with k elements, where k is the dimensionality of the points.

The Manhattan metric is defined as:

d = Σ_i | v_i - w_i |

  Source Edit
proc distance(metric: typedesc[Minkowski]; v, w: Tensor[float]; p = 2.0;
              squared: static bool = false): float

Computes the Minkowski distance between points v and w. Both need to be rank 1 tensors with k elements, where k is the dimensionality of the points.

The Minkowski metric is defined as:

d = ( Σ_i | v_i - w_i |^p )^(1/p)

Thus, it reduces to the Manhattan distance for p = 1 and the Euclidean metric for p = 2.

If squared is true returns the p-th power of the metric. For the Euclidean case this is the square of the distance, hence the name.

  Source Edit
proc distanceMatrix(metric: typedesc[AnyMetric]; x, y: Tensor[float]; p = 2.0;
                    squared: static bool = false): Tensor[float]
Computes the distance matrix between all points in x and y. x and y need to be tensors of rank 2 with:
  • [n_observations, n_dimensions]

The first argument is the metric to compute the distance under. If the Minkowski metric is selected the power p is used.

If squared is true and we are computing under a Minkowski or Euclidean metric, we return the p-th power of the distances.

Result is a tensor of rank 2, a symmetric matrix where element (i, j) is the distance between x_i and y_j.

  Source Edit
proc pairwiseDistances(metric: typedesc[AnyMetric]; x, y: Tensor[float];
                       p = 2.0; squared: static bool = false): Tensor[float]
Computes all distances between all pairs in x and y. That is if x and y are rank 2 tensors of each:
  • [n_observations, n_dimensions]

we compute the distance between each observation x_i and y_i.

One of the arguments may have only 1 observation and thus be of shape [1, n_dimensions]. In this case all distances between this point and all in the other input will be computed so that the result is always of shape [n_observations]. If one input has only shape [n_dimensions] it is unsqueezed to [1, n_dimensions].

The first argument is the metric to compute the distance under. If the Minkowski metric is selected the power p is used.

If squared is true and we are computing under a Minkowski or Euclidean metric, we return the p-th power of the distances.

Result is a tensor of rank 1, with one element for each distance.

  Source Edit
Arraymancer Technical reference Tutorial Spellbook (How-To's) Under the hood