JavaScript · Dot Products

Dot Products

import * as numkong from 'numkong'; 8.0.0-rc.1 · b1f866ab

Dot products are separate from distances because dtype tagging and low-precision storage matter more here.

import { dot, inner } from "numkong";

const a = new Float32Array([1, 2, 3]);
const b = new Float32Array([4, 5, 6]);

console.log(dot(a, b));
console.log(inner(a, b)); // alias for ecosystem familiarity

For non-native numeric layouts, pass an explicit DType or wrap the storage in a typed NumKong view.

API

Functions

innerComputes the inner product of two vectors, the same as the dot product.
numkong.ts:278 ↗
function inner(a: NumericArray, b: NumericArray): number
function inner(
    a: DistanceArray,
    b: DistanceArray,
    dtype: DType,
): number
function inner(a: TensorBase, b: TensorBase): number

Parameters

aThe first vector, as TypedArray or TensorBase.
bThe second vector, matching the type and length of a.
dtypeDTypeOptional dtype string for custom types, e.g. 'f16', 'bf16', 'e4m3'.

Returns

The inner product of a and b.
dotComputes the dot product of two vectors, the same as the inner product.
numkong.ts:293 ↗
function dot(a: NumericArray, b: NumericArray): number
function dot(
    a: DistanceArray,
    b: DistanceArray,
    dtype: DType,
): number
function dot(a: TensorBase, b: TensorBase): number

Parameters

aThe first vector, as TypedArray or TensorBase.
bThe second vector, matching the type and length of a.
dtypeDTypeOptional dtype string for custom types, e.g. 'f16', 'bf16', 'e4m3'.

Returns

The dot product of a and b.