function dot
dot(
x: array,
y: array,
): number

Computes the dot product of two arrays.

Takes two arrays of equal length and computes their dot product (sum of element-wise products).

Examples

Dot product of two 1D arrays

import { assertEquals, assertThrows } from "jsr:@std/assert";

assertEquals(dot([5, 6, 3], [0.5, -3, 2.3]), -8.600000000000001);

Dot product of two arrays with negative numbers

import { assertEquals, assertThrows } from "jsr:@std/assert";

assertEquals(dot([-1, -2, -3], [-4, -5, -6]), 32);

Dot product of two identical arrays

import { assertEquals, assertThrows } from "jsr:@std/assert";

assertEquals(dot([1, 2, 3], [1, 2, 3]), 14);

Dot product of two arrays with zero values

import { assertEquals, assertThrows } from "jsr:@std/assert";

assertEquals(dot([0, 0, 0], [0, 0, 0]), 0);

Error when input arrays are of different sizes

import { assertEquals, assertThrows } from "jsr:@std/assert";

assertThrows(() => dot([1, 2], [1, 2, 3]), Error, "Arrays must have the same length");

Parameters

First array for dot product

Second array for dot product

Return Type

number

The dot product of the two arrays

Throws

If inputs are not arrays, arrays are not of the same size, or if no arguments are provided