DataPoint is a Swift protocol for points used by clustering algorithms. A conforming type is comparable and printable, exposes a fixed dimension count, stores coordinate values as Doubles, and can initialize from those values. section 6.1
The protocol requires:
- Equatable conformance for comparing points and detecting unchanged centroids. section 6.1 section 6.2
- CustomStringConvertible conformance for readable printed points. section 6.1
- A static numDimensions value shared by every point of the type. section 6.1
- A mutable dimensions array and init(values:) for coordinate access and construction. section 6.1
A protocol extension supplies Euclidean distance by zipping two coordinate arrays, squaring coordinate differences, summing them, and taking the square root. The same implementation works for any number of dimensions. section 6.1
A three-dimensional point
Point3D stores x, y, and z while maintaining dimensions = [x, y, z]. It implements initialization from an array, coordinate equality, and printable output. For j = (2, 1, 1) and k = (2, 2, 5), j.distance(to: k) computes their Euclidean distance. KMeans<Point3D> can then normalize these points, assign them to centroids, and recalculate cluster centers. section 6.1 section 6.2
public protocol DataPoint: CustomStringConvertible, Equatable {
static var numDimensions: UInt { get }
var dimensions: [Double] { get set }
init(values: [Double])
}
Inspect the minimal DataPoint protocol declaration, then compare it with the distance extension and Point3D implementation. section 6.1
