Metric Spaces API

class metricspaces.MetricSpace(points=(), dist=None, cache=None, turnoffcache=False, pointclass=<function MetricSpace.<lambda>>)

A class to store a metric space. The standard usage is to specify a collection of points and a distance function. Either the points or the distance function can be omitted. If only the points are provided, the distance function will default to the dist method on the point objects. If only the distance function is provided, then it is not possible to iterate over the points. This would be the case for infinite metric spaces.

add(point)

Add point to the metric space.

comparedist(x, a, b, delta=0, alpha=1)

Return True iff x is closer to a than to b.

Technically, it returns d(x,a) < alpha * d(x,b) - delta, where delta is optional and defaults to zero.

dist(a, b)

Return the distance from`a` to b.

The metric used will depend on the distfn.

distlt(a, b, delta=0)

Return True iff d(a,b) < delta.

distsq(a, b)

Return the squared distance from a to b.

fromstrings(strings, parser)

Take a collection of strings as input and parse each as a point.

The string is read only up to the first semicolon (if present).

pointdist(b)

Return the distance from a to b as measured using the metric supplied by the input points.

This will be set as the default distance if no other distance function is provided.

subspace(points=())

Return a subspace of the metric space

It takes a subset of points of the metric space as input and returns a new MetricSpace object created with those points and the superspace’s parameters.