Swift · Dense Distances

Dense Distances

import NumKong 8.0.0-rc.1 · b1f866ab

The collection extensions are the lightest entry point. They are a good fit for per-vector retrieval and ranking work.

import NumKong

let a: [Float16] = [1, 2, 3, 4]
let b: [Float16] = [4, 3, 2, 1]

let sqeuclidean = a.sqeuclidean(b) // widens to Float32
let euclidean = a.euclidean(b)
let angular = a.angular(b)

print(sqeuclidean as Any, euclidean as Any, angular as Any)

The widening is deliberate. That is the main difference from a naive same-storage implementation.

API

Types

NumKongSpatialConvenience alias for types supporting all four spatial distance metrics.
NumKong.swift:73 ↗
typealias NumKongSpatial = NumKongDot & NumKongAngular & NumKongEuclidean & NumKongSqEuclidean
ProtocolNumKongAngularA type that can compute SIMD-accelerated angular distances, also known as cosine distances.2
Scalar.swift:28 ↗
protocol NumKongAngular

Conforming types

AngularOutputThe type the angular distance is returned in.
Scalar.swift:30 ↗
associatedtype AngularOutput
angularComputes the angular distance between two vectors.
Scalar.swift:38 ↗
static func angular<A, B>(
    _ a: A,
    _ b: B
) -> AngularOutput? where Self == A.Element, A : Sequence, B : Sequence, A.Element == B.Element

Parameters

aThe first vector.
bThe second vector, as long as a.

Returns

The angular distance, or nil if the sequences are empty, differ in length, or the kernel reports an error.
ProtocolNumKongSqEuclideanA type that can compute SIMD-accelerated squared Euclidean distances.2
Scalar.swift:43 ↗
protocol NumKongSqEuclidean

Conforming types

SqEuclideanOutputThe type the squared Euclidean distance is returned in.
Scalar.swift:45 ↗
associatedtype SqEuclideanOutput
sqeuclideanComputes the squared Euclidean distance between two vectors.
Scalar.swift:53 ↗
static func sqeuclidean<A, B>(
    _ a: A,
    _ b: B
) -> SqEuclideanOutput? where Self == A.Element, A : Sequence, B : Sequence, A.Element == B.Element

Parameters

aThe first vector.
bThe second vector, as long as a.

Returns

The squared Euclidean distance, or nil if the sequences are empty, differ in length, or the kernel reports an error.
ProtocolNumKongEuclideanA type that can compute SIMD-accelerated Euclidean distances.2
Scalar.swift:58 ↗
protocol NumKongEuclidean

Conforming types

EuclideanOutputThe type the Euclidean distance is returned in.
Scalar.swift:60 ↗
associatedtype EuclideanOutput
euclideanComputes the Euclidean distance between two vectors.
Scalar.swift:68 ↗
static func euclidean<A, B>(
    _ a: A,
    _ b: B
) -> EuclideanOutput? where Self == A.Element, A : Sequence, B : Sequence, A.Element == B.Element

Parameters

aThe first vector.
bThe second vector, as long as a.

Returns

The Euclidean distance, or nil if the sequences are empty, differ in length, or the kernel reports an error.