Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions typescript/src/internals/helpers/math.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright 2025 © BeeAI a Series of LF Projects, LLC
* SPDX-License-Identifier: Apache-2.0
*/

import { describe, expect, it } from "vitest";

import { cosineSimilarityMatrix } from "@/internals/helpers/math.js";

describe("cosineSimilarityMatrix", () => {
it("rejects matrices with differing column counts", () => {
expect(() => cosineSimilarityMatrix([[1, 2]], [[1, 2, 3]])).toThrowError(
"Matrices must have the same number of columns.",
);
expect(() => cosineSimilarityMatrix([[1, 2]], [])).toThrowError(
"Matrices must have the same number of columns.",
);
});

it("computes similarity for matching column counts", () => {
const result = cosineSimilarityMatrix([[1, 0]], [[1, 0]]);
expect(result[0][0]).toBeCloseTo(1);
});
});
2 changes: 1 addition & 1 deletion typescript/src/internals/helpers/math.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function cosineSimilarity(vecA: number[], vecB: number[]): number {
}

export function cosineSimilarityMatrix(matrixA: number[][], matrixB: number[][]): number[][] {
if ((matrixA[0]?.length ?? 0) !== (matrixA[0]?.length ?? 0)) {
if ((matrixA[0]?.length ?? 0) !== (matrixB[0]?.length ?? 0)) {
throw new ValueError("Matrices must have the same number of columns.");
}

Expand Down
Loading