/** * @file Shared type definitions for the matrix module * @description Central type definitions used across all matrix-related files. * * Type Philosophy: * - Some `any` types are INTENTIONAL for typed-function runtime dispatch * - Callback parameters often use `any` because typed-function resolves * the actual types at runtime based on matrix._datatype * - We prefer explicit `any` annotations with JSDoc over implicit any */ import type { RangeForEachCallback, RangeFormatOptions, RangeJSON, RangeMapCallback } from '@danielsimonjr/mathts-core'; export type { RangeForEachCallback, RangeFormatOptions, RangeJSON, RangeMapCallback }; import type { NestedArray as CoreNestedArray } from '@danielsimonjr/mathts-core/internal'; /** * BigNumber interface - matches decimal.js structure * Used to avoid circular dependency with actual BigNumber type */ export interface BigNumberLike { toNumber(): number; plus(y: BigNumberLike | number | string): BigNumberLike; minus(y: BigNumberLike | number | string): BigNumberLike; times(y: BigNumberLike | number | string): BigNumberLike; div(y: BigNumberLike | number | string): BigNumberLike; mul(y: BigNumberLike | number | string): BigNumberLike; } /** * Complex number interface - matches complex.js structure */ export interface ComplexLike { re: number; im: number; add(y: ComplexLike | number): ComplexLike; sub(y: ComplexLike | number): ComplexLike; mul(y: ComplexLike | number): ComplexLike; div(y: ComplexLike | number): ComplexLike; } /** * Fraction interface - matches fraction.js structure */ export interface FractionLike { n: number; d: number; s: number; add(y: FractionLike | number): FractionLike; sub(y: FractionLike | number): FractionLike; mul(y: FractionLike | number): FractionLike; div(y: FractionLike | number): FractionLike; } /** * General matrix element value type. * * INTENTIONAL ANY: Matrix elements can be any type due to typed-function's * runtime type dispatch. The actual type is determined by matrix._datatype * at runtime, not at compile time. */ export type MatrixValue = unknown; /** * Data type string for matrix elements (e.g., 'number', 'BigNumber', 'Complex') */ export type DataType = string | undefined; /** * Recursive nested array type for matrix data. Consolidated onto * `@danielsimonjr/mathts-core`'s byte-identical generic definition (see * docs/Architecture/duplicate-symbols.json); the default type param is kept * local since core's version has none. */ export type NestedArray = CoreNestedArray; /** * Dense matrix internal data structure */ export type DenseMatrixData = NestedArray; /** * 2D array type (for sparse matrix valueOf/toArray results) */ export type MatrixArray = T[][]; /** * Binary callback function for matrix operations (e.g., add, multiply). * * INTENTIONAL ANY: typed-function dispatches to the correct implementation * based on runtime types. The actual signature like (number, number) => number * is resolved at runtime via typed.find(). */ export type MatrixCallback = (a: unknown, b: unknown) => unknown; /** * Scalar equality comparison function. * * INTENTIONAL ANY: Used with typed-function for type-specific comparisons. */ export type EqualScalarFunction = (a: unknown, b: unknown) => boolean; /** * Map callback for matrix.map() operations */ export type MapCallback = (value: T, index: number[], matrix: MatrixInterface) => R; /** * ForEach callback for matrix.forEach() operations */ export type ForEachCallback = (value: T, index: number[], matrix: MatrixInterface) => void; /** * Interface for typed-function dependency. * * INTENTIONAL ANY in some methods: typed-function's API uses dynamic types * that are resolved at runtime. */ export interface TypedFunction { /** * Find a specific signature of a typed function. * Returns the function matching the given type signature. */ find(fn: (...args: unknown[]) => unknown, signature: string[]): ((...args: unknown[]) => unknown) | null; /** * Convert a value to a specific datatype. * @param value - Value to convert (any type) * @param datatype - Target datatype string (e.g., 'number', 'BigNumber') */ convert(value: unknown, datatype: string): unknown; /** * Create a self-referential typed function. * Used when a function needs to recursively call itself with proper typing. * * INTENTIONAL ANY: The self parameter represents the typed function itself * which has dynamic signatures. */ referToSelf(fn: (self: T) => (...args: never[]) => unknown): (...args: unknown[]) => unknown; /** * Signatures of the typed function (optional) */ signatures?: Record unknown>; } /** * Index interface for matrix subsetting operations. * Represents a multi-dimensional index that can contain ranges, arrays, or scalars. */ export interface IndexInterface { readonly isIndex: boolean; readonly type: string; /** Get the size of each dimension */ size(): number[]; /** Get the minimum value for each dimension */ min(): (number | string | undefined)[]; /** Get the maximum value for each dimension */ max(): (number | string | undefined)[]; /** * Get a specific dimension of the index. * * INTENTIONAL ANY return: A dimension can be a number, Range, or ImmutableDenseMatrix. * The actual type depends on how the index was constructed. */ dimension(dim: number): unknown; /** Check if the index represents a scalar value */ isScalar(): boolean; /** Iterate over dimensions */ forEach(callback: (dimension: unknown, index: number, indexObject: IndexInterface) => void): void; /** Clone the index */ clone(): IndexInterface; /** Convert to array representation */ toArray(): unknown[]; /** Get primitive value */ valueOf(): unknown[]; /** Check if this is an object property index */ isObjectProperty?(): boolean; /** Get object property name if applicable */ getObjectProperty?(): string | null; } /** * Base Matrix interface defining common operations. * @template T - The element type stored in the matrix */ export interface MatrixInterface { readonly type: string; readonly isMatrix: boolean; /** Get the storage format ('dense' or 'sparse') */ storage(): string; /** Get the datatype string of matrix elements */ datatype(): DataType; /** Create a new matrix of the same type */ create(data: NestedArray | object, datatype?: string): MatrixInterface; /** Get matrix dimensions */ size(): number[]; /** Clone the matrix */ clone(): MatrixInterface; /** Convert to nested array */ toArray(): NestedArray; /** Get primitive value (nested array) */ valueOf(): NestedArray; /** Get a single element */ get(index: number[]): T; /** Set a single element */ set(index: number[], value: T, defaultValue?: T): MatrixInterface; /** Get or set a subset of the matrix */ subset(index: IndexInterface, replacement?: NestedArray | MatrixInterface | T, defaultValue?: T): MatrixInterface | T; /** Resize the matrix */ resize(size: number[], defaultValue?: T, copy?: boolean): MatrixInterface; /** Reshape the matrix */ reshape(size: number[], copy?: boolean): MatrixInterface; /** Map over elements */ map(callback: MapCallback, skipZeros?: boolean): MatrixInterface; /** Iterate over elements */ forEach(callback: ForEachCallback, skipZeros?: boolean): void; /** Format as string */ format(options?: MatrixFormatOptions | number | ((value: T) => string)): string; /** Convert to string */ toString(): string; } /** * DenseMatrix-specific interface */ export interface DenseMatrixInterface extends MatrixInterface { readonly isDenseMatrix: boolean; _data: DenseMatrixData; _size: number[]; _datatype?: DataType; createDenseMatrix(data: DenseMatrixData, datatype?: string): DenseMatrixInterface; getDataType(): string; rows?(): DenseMatrixInterface[]; columns?(): DenseMatrixInterface[]; diagonal?(k?: number | BigNumberLike): DenseMatrixInterface; swapRows?(i: number, j: number): DenseMatrixInterface; } /** * SparseMatrix-specific interface */ export interface SparseMatrixInterface extends MatrixInterface { readonly isSparseMatrix: boolean; _values?: T[]; _index: number[]; _ptr: number[]; _size: [number, number]; _datatype?: DataType; createSparseMatrix(data?: unknown, datatype?: string): SparseMatrixInterface; getDataType(): string; density(): number; diagonal?(k?: number | BigNumberLike): SparseMatrixInterface; swapRows?(i: number, j: number): SparseMatrixInterface; } /** * Options for formatting matrix output */ export interface MatrixFormatOptions { /** Number of significant digits */ precision?: number; /** Notation style */ notation?: 'fixed' | 'exponential' | 'engineering' | 'auto'; /** Lower exponent bound for exponential notation */ lowerExp?: number; /** Upper exponent bound for exponential notation */ upperExp?: number; /** Allow additional custom properties */ [key: string]: unknown; } /** * JSON representation of an ImmutableDenseMatrix */ export interface ImmutableDenseMatrixJSON { mathjs: 'ImmutableDenseMatrix'; data: DenseMatrixData; size: number[]; datatype?: DataType; min?: T; max?: T; } /** * JSON representation of an Index */ export interface IndexJSON { mathjs: 'Index'; dimensions: unknown[]; } /** * Data structure for DenseMatrix constructor */ export interface DenseMatrixConstructorData { data: DenseMatrixData; size: number[]; datatype?: DataType; } /** * Data structure for SparseMatrix constructor */ export interface SparseMatrixConstructorData { values?: T[]; index: number[]; ptr: number[]; size: [number, number]; datatype?: DataType; } /** * Data structure for ImmutableDenseMatrix constructor */ export interface ImmutableDenseMatrixConstructorData { data: DenseMatrixData; size: number[]; datatype?: DataType; min?: T; max?: T; } /** * Elementwise operation function type */ export type ElementwiseOperation = ((a: unknown, b: unknown) => unknown) & { signatures?: Record unknown>; }; /** * Algorithm function type (for sparse/dense matrix algorithms) */ export type AlgorithmFunction = (...args: unknown[]) => MatrixInterface; /** * Options for matrixAlgorithmSuite */ export interface MatrixAlgorithmSuiteOptions { /** Elementwise operation to use */ elop?: ElementwiseOperation; /** Algorithm for SparseMatrix + SparseMatrix */ SS?: AlgorithmFunction; /** Algorithm for DenseMatrix + SparseMatrix */ DS?: AlgorithmFunction; /** Algorithm for SparseMatrix + DenseMatrix (defaults to DS flipped) */ SD?: AlgorithmFunction; /** Algorithm for SparseMatrix + scalar */ Ss?: AlgorithmFunction; /** Algorithm for scalar + SparseMatrix (false means not implemented) */ sS?: AlgorithmFunction | false; /** Algorithm for DenseMatrix + scalar (true enables the default dense path) */ Ds?: AlgorithmFunction | boolean; /** typed-function scalar type (defaults to 'any') */ scalar?: string; } /** * Matrix signatures map for typed-function */ export type MatrixSignatures = Record unknown>; /** * Node in a Fibonacci heap */ export interface FibonacciHeapNode { key: number; value: T; degree: number; left?: FibonacciHeapNode; right?: FibonacciHeapNode; parent?: FibonacciHeapNode; child?: FibonacciHeapNode; mark?: boolean; } /** * Fibonacci heap interface */ export interface FibonacciHeapInterface { insert(key: number, value: T): FibonacciHeapNode; extractMinimum(): FibonacciHeapNode | null; remove(node: FibonacciHeapNode): void; size(): number; clear(): void; isEmpty(): boolean; } /** * Range interface */ export interface RangeInterface { readonly type: string; readonly isRange: boolean; start: number; end: number; step: number; clone(): RangeInterface; size(): [number]; min(): number | undefined; max(): number | undefined; forEach(callback: RangeForEachCallback): void; map(callback: RangeMapCallback): T[]; toArray(): number[]; valueOf(): number[]; format(options?: RangeFormatOptions | number | ((value: number) => string)): string; toString(): string; toJSON(): RangeJSON; } //# sourceMappingURL=types.d.ts.map