/** @import { Metric } from "../metrics/index.js" */ /** @import { ParametersLSH } from "./index.js" */ /** * Locality Sensitive Hashing (LSH) for approximate nearest neighbor search. * * LSH uses hash functions that map similar items to the same buckets with high probability. * This implementation uses Random Projection hashing (SimHash-style) which works well for * cosine similarity and Euclidean distance. * * Key concepts: * - Multiple hash tables increase recall probability * - Each hash function projects data onto random hyperplanes * - Points on the same side of hyperplanes are hashed together * - Combines results from all tables for better accuracy * * Best suited for: * - High-dimensional data where exact methods fail * - Approximate nearest neighbor needs * - Large datasets where linear scan is too slow * - When some false positives/negatives are acceptable * * @class * @category KNN * @template {number[] | Float64Array} T * @extends KNN * @see {@link https://en.wikipedia.org/wiki/Locality-sensitive_hashing} */ export class LSH extends KNN { /** * Creates a new LSH index. * * @param {T[]} elements - Elements to index * @param {ParametersLSH} [parameters={}] - Configuration parameters */ constructor(elements: T[], parameters?: ParametersLSH); _metric: Metric; _numHashTables: number; _numHashFunctions: number; _seed: number; _randomizer: Randomizer; /** @type {Map[]} */ _hashTables: Map[]; /** @type {Float64Array[][]} */ _projections: Float64Array[][]; /** @type {number[][]} */ _offsets: number[][]; /** @type {number} */ _dim: number; /** * Initialize random projection vectors for all hash tables. * @private */ private _initializeHashFunctions; /** * Compute hash signature for an element using random projections. * @private * @param {T} element * @param {number} tableIndex * @returns {string} Hash signature */ private _computeHash; /** * Add elements to the LSH index. * @param {T[]} elements * @returns {this} */ add(elements: T[]): this; /** * Search for k approximate nearest neighbors. * @param {T} query * @param {number} [k=5] * @returns {{ element: T; index: number; distance: number }[]} */ search( query: T, k?: number, ): { element: T; index: number; distance: number; }[]; /** * @param {number} i * @param {number} [k=5] * @returns {{ element: T; index: number; distance: number }[]} */ search_by_index( i: number, k?: number, ): { element: T; index: number; distance: number; }[]; } import type { ParametersLSH } from "./index.js"; import { KNN } from "./KNN.js"; import type { Metric } from "../metrics/index.js"; import { Randomizer } from "../util/index.js"; //# sourceMappingURL=LSH.d.ts.map