final class PackedMatrix<Element> Conforms to
SendablePacked kernels are the GEMM-like throughput path. They are useful when the right-hand side is reused across many query batches.
import NumKong
let a = try Tensor<Float32>.fromArray([1, 2, 3, 4, 5, 6], rows: 2, columns: 3)
let b = try Tensor<Float32>.fromArray([7, 8, 9, 1, 0, 1], rows: 2, columns: 3)
let packed = try b.packForDots() // PackedMatrix<Float32>, owned
let dots = try a.dotsPacked(packed) // Tensor<Float64>, 2x2
let angs = try a.angularsPacked(packed) // Tensor<Float64>, 2x2
let eucs = try a.euclideansPacked(packed)// Tensor<Float64>, 2x2
assert(dots.rows == 2 && dots.columns == 2)The free-function API (dotsPacked, angularsPacked, etc.) accepts MatrixView, PackedMatrix, and MatrixSpan directly for cases where you need manual buffer management — see the verbosity comparison in the Tensors section above.
Symmetric kernels compute self-similarity or self-distance matrices.
They are the right shape for SYRK-like workloads and row-window partitioning.
import NumKong
let vectors = try Tensor<Float32>.fromArray([
1, 0, 0,
0, 1, 0,
0, 0, 1,
], rows: 3, columns: 3)
let gram = try vectors.dotsSymmetric() // Tensor<Float64>, 3x3
let dists = try vectors.euclideansSymmetric()// Tensor<Float64>, 3x3
let angs = try vectors.angularsSymmetric() // Tensor<Float64>, 3x3
assert(gram.rows == 3 && gram.columns == 3)The free-function form (dotsSymmetric, angularsSymmetric, etc.) exposes rowsBegin and rowsEnd parameters for external partitioning.
PackedMatrixOwns a kernel-optimized packed copy of a matrix for batch distance computations.6rowslet rows: Int columnslet columns: Int byteCountlet byteCount: Int rawBufferProvides read-only access to the underlying packed bytes.var rawBuffer: UnsafeRawBufferPointer { get } initPacks the given matrix view into a kernel-optimized layout for batch dot products.convenience init(packing matrix: MatrixView<Element>) throws where Element : NumKongDotsMatrixElement convenience init(packing tensor: Tensor<Element>) throws where Element : NumKongDotsMatrixElement Packs a tensor's storage into a kernel-optimized layout for batch dot products.
shapeReads the packed matrix shape, [rows,columns], from the buffer's self-describing header.var shape: (rows: Int, columns: Int) { get throws } where Element : NumKongDotsMatrixElement NumKongDotsMatrixElementElement type that supports batch dot-product matrix operations.1DotsOutputThe element type of the result matrices that dotsPacked and dotsSymmetric write.associatedtype DotsOutput NumKongSpatialsMatrixElementElement type that supports batch angular and Euclidean matrix operations.1protocol NumKongSpatialsMatrixElement : NumKongDotsMatrixElement SpatialOutputThe element type of the result matrices of the angular and Euclidean batch functions.associatedtype SpatialOutput NumKongSetsMatrixElementElement type that supports batch Hamming and Jaccard matrix operations.2protocol NumKongSetsMatrixElement : NumKongDotsMatrixElement HammingOutputThe element type of the result matrices that hammingsPacked and hammingsSymmetric write.associatedtype HammingOutput JaccardOutputThe element type of the result matrices that jaccardsPacked and jaccardsSymmetric write.associatedtype JaccardOutput dotsPackedComputes dot products between each row of a and every row packed in bPacked.func dotsPacked<Element>(
_ a: MatrixView<Element>,
_ bPacked: PackedMatrix<Element>,
_ result: inout MatrixSpan<Element.DotsOutput>
) throws where Element : NumKongDotsMatrixElement dotsSymmetricComputes the symmetric dot-product matrix for all row pairs in vectors.func dotsSymmetric<Element>(
_ vectors: MatrixView<Element>,
_ result: inout MatrixSpan<Element.DotsOutput>,
rowsBegin: Int = 0,
rowsEnd: Int? = nil
) throws where Element : NumKongDotsMatrixElement angularsPackedComputes angular distances between each row of a and every row packed in bPacked.func angularsPacked<Element>(
_ a: MatrixView<Element>,
_ bPacked: PackedMatrix<Element>,
_ result: inout MatrixSpan<Element.SpatialOutput>
) throws where Element : NumKongSpatialsMatrixElement euclideansPackedComputes Euclidean distances between each row of a and every row packed in bPacked.func euclideansPacked<Element>(
_ a: MatrixView<Element>,
_ bPacked: PackedMatrix<Element>,
_ result: inout MatrixSpan<Element.SpatialOutput>
) throws where Element : NumKongSpatialsMatrixElement angularsSymmetricComputes the symmetric angular-distance matrix for all row pairs in vectors.func angularsSymmetric<Element>(
_ vectors: MatrixView<Element>,
_ result: inout MatrixSpan<Element.SpatialOutput>,
rowsBegin: Int = 0,
rowsEnd: Int? = nil
) throws where Element : NumKongSpatialsMatrixElement euclideansSymmetricComputes the symmetric Euclidean-distance matrix for all row pairs in vectors.func euclideansSymmetric<Element>(
_ vectors: MatrixView<Element>,
_ result: inout MatrixSpan<Element.SpatialOutput>,
rowsBegin: Int = 0,
rowsEnd: Int? = nil
) throws where Element : NumKongSpatialsMatrixElement hammingsPackedComputes Hamming distances between each row of a and every row packed in bPacked.func hammingsPacked<Element>(
_ a: MatrixView<Element>,
_ bPacked: PackedMatrix<Element>,
_ result: inout MatrixSpan<Element.HammingOutput>
) throws where Element : NumKongSetsMatrixElement hammingsSymmetricComputes the symmetric Hamming-distance matrix for all row pairs in vectors.func hammingsSymmetric<Element>(
_ vectors: MatrixView<Element>,
_ result: inout MatrixSpan<Element.HammingOutput>,
rowsBegin: Int = 0,
rowsEnd: Int? = nil
) throws where Element : NumKongSetsMatrixElement jaccardsPackedComputes Jaccard distances between each row of a and every row packed in bPacked.func jaccardsPacked<Element>(
_ a: MatrixView<Element>,
_ bPacked: PackedMatrix<Element>,
_ result: inout MatrixSpan<Element.JaccardOutput>
) throws where Element : NumKongSetsMatrixElement jaccardsSymmetricComputes the symmetric Jaccard-distance matrix for all row pairs in vectors.func jaccardsSymmetric<Element>(
_ vectors: MatrixView<Element>,
_ result: inout MatrixSpan<Element.JaccardOutput>,
rowsBegin: Int = 0,
rowsEnd: Int? = nil
) throws where Element : NumKongSetsMatrixElement