typealias NumKongSpatial = NumKongDot & NumKongAngular & NumKongEuclidean & NumKongSqEuclidean 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.
ProtocolNumKongAngularA type that can compute SIMD-accelerated angular distances, also known as cosine distances.2
AngularOutputThe type the angular distance is returned in.
associatedtype AngularOutput angularComputes the angular distance between two vectors.
static func angular<A, B>(
_ a: A,
_ b: B
) -> AngularOutput? where Self == A.Element, A : Sequence, B : Sequence, A.Element == B.Element Parameters
a | The first vector. | |
|---|---|---|
b | The 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
SqEuclideanOutputThe type the squared Euclidean distance is returned in.
associatedtype SqEuclideanOutput sqeuclideanComputes the squared Euclidean distance between two vectors.
static func sqeuclidean<A, B>(
_ a: A,
_ b: B
) -> SqEuclideanOutput? where Self == A.Element, A : Sequence, B : Sequence, A.Element == B.Element Parameters
a | The first vector. | |
|---|---|---|
b | The 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
EuclideanOutputThe type the Euclidean distance is returned in.
associatedtype EuclideanOutput euclideanComputes the Euclidean distance between two vectors.
static func euclidean<A, B>(
_ a: A,
_ b: B
) -> EuclideanOutput? where Self == A.Element, A : Sequence, B : Sequence, A.Element == B.Element Parameters
a | The first vector. | |
|---|---|---|
b | The 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.