/** * Matrix Compatibility Bridge * * Wraps the native MathTS DenseMatrix (Float64Array-backed) with the interface * that the activated mathjs-derived factory functions expect: * ._data, ._size, .storage(), .datatype(), .valueOf(), .size(), * .get(), .set(), .map(), .forEach(), .clone(), .subset(), .create() * * This enables ~89 dormant factory functions to operate on matrices without * rewriting each one. */ import { DenseMatrix } from '@danielsimonjr/mathts-matrix'; /** The subset of the mathjs `Index` API the bridge matrices consume. */ interface MatrixIndexLike { isScalar(): boolean; size(): number[]; dimension(dim: number): unknown; } /** * Dense matrix of numbers with the mathjs `DenseMatrix` interface that the activated * factory functions expect. * * The matrix keeps its data as a nested array in `_data`. Use `toNative` and `fromNative` * to convert to and from the MathTS `DenseMatrix`. */ export declare class MathJSDenseMatrix { _data: number[][]; _size: number[]; _datatype: string | undefined; constructor(data?: number[][] | { data: number[][]; size: number[]; }); /** * Row count — required by @danielsimonjr/mathts-core isMatrix duck-type check. */ get rows(): number; /** * Column count — required by @danielsimonjr/mathts-core isMatrix duck-type check. */ get cols(): number; storage(): string; datatype(): string | undefined; valueOf(): number[][]; size(): number[]; toArray(): number[][] | number[]; toJSON(): { mathjs: string; data: number[][]; size: number[]; }; /** * Create a new matrix of the same type from nested array data. * Used internally by mathjs factories (e.g. transpose calls x.create(...)). */ create(data: number[][], datatype?: string): MathJSDenseMatrix; /** * Same as create() but named createDenseMatrix for factories that call it * on the matrix instance (e.g. transpose). */ createDenseMatrix(opts: { data: number[][]; size: number[]; datatype?: string; }): MathJSDenseMatrix; /** * Map over all elements, producing a new matrix. * * Callbacks are invoked ARITY-APPROPRIATELY (mathjs contract): a typed or * fixed-arity callback declared as `(value)` or `(value, index)` must not be * force-fed 3 arguments — a typed 2-arg expression lambda (`f(x, i) = …`) * previously threw "Too many arguments" out of the expression language. */ map(callback: (value: number, index: number[], matrix: MathJSDenseMatrix) => number): MathJSDenseMatrix; /** * Iterate over all elements (callback invoked arity-appropriately; see map). */ forEach(callback: (value: number, index: number[], matrix: MathJSDenseMatrix) => void): void; /** * Get element at [row, col]. * * Accepts BOTH index conventions in the monorepo: the mathjs factory form * `get([row, col])` (single array arg) and the `@danielsimonjr/mathts-core` * form `get(row, col)` (two scalar args) used by core's `Matrix~>Array` * typed-function conversion. Supporting only the array form silently broke * that conversion — e.g. `multiply(R, Q)` inside the `schur` factory, where * the two DenseMatrix operands have no direct `Matrix,Matrix` signature and * fall back to Array conversion (core calls `matrix.get(i, j)`). */ get(indexOrRow: number[] | number, col?: number): number; /** * Set element at [row, col]. Mutates in place and returns this. */ set(index: number[], value: number, _defaultValue?: number): MathJSDenseMatrix; /** * Subset access. Accepts either a plain coordinate `number[]` (internal * callers) or a mathjs `Index` object (what the factory `subset(value, index)` * passes — `value.subset(index)`). Index support was missing, so scalar * extraction like `subset(M, index(k, k))` — used throughout `sylvester` / * `lyap` — passed the whole Index object to `get`, reading `_data[Index]` = * undefined. */ subset(index: number[] | MatrixIndexLike, replacement?: unknown, defaultValue?: number): number | MathJSDenseMatrix; /** Read a scalar (Index.isScalar) or a sub-matrix from selected indices. */ private _getSubset; /** Write a scalar or a sub-matrix at the selected indices; returns this. */ private _setSubset; /** * Deep clone. */ clone(): MathJSDenseMatrix; /** * Resize the matrix to the given size. Mutates in place and returns this. * (mathjs factory code does `m.resize(size)` and continues using `m`) */ resize(newSize: number[], defaultValue?: number): MathJSDenseMatrix; /** * Reshape the matrix. */ reshape(newSize: number[]): MathJSDenseMatrix; /** * Extract the k-th diagonal as a new column vector matrix. * k=0 is main diagonal, k>0 is above, k<0 is below. */ diagonal(k?: number): MathJSDenseMatrix; /** * Get the data type of elements (used by getMatrixDataType). */ getDataType(): string; /** * Matrix product `this · other` via the native backend. */ multiply(other: MathJSDenseMatrix): MathJSDenseMatrix; /** * Matrix transpose via the native backend. */ transpose(): MathJSDenseMatrix; /** Convert to native Float64Array-backed DenseMatrix. */ toNative(): DenseMatrix; /** Create from native DenseMatrix. */ static fromNative(dm: DenseMatrix): MathJSDenseMatrix; /** * Swap rows i and j (by reference) in a raw 2-D data array. Required by the * dense `lup` factory's partial-pivoting step (`DenseMatrix._swapRows(j, pi, * data)`); its absence made any LU decomposition that needed a pivot swap * throw `_swapRows is not a function`. Mirrors mathjs's static of the same * name and the sibling `MathJSSparseMatrix._swapRows`. */ static _swapRows(i: number, j: number, data: unknown[][]): void; /** * Create a diagonal matrix. Used by identity(), diag(), and other factories. * * @param size - [rows, cols] * @param value - scalar value to place on the diagonal * @param k - diagonal offset (0 = main, >0 = above, <0 = below) * @param defaultValue - fill value for non-diagonal elements */ static diagonal(size: number[], value: number | number[], k?: number, defaultValue?: number): MathJSDenseMatrix; } /** * mathjs-compatible sparse matrix using CSC format. * * Internal storage: * _values: non-zero values (or null for pattern-only matrices) * _index: row indices for each non-zero * _ptr: column pointers (length = cols + 1) * _size: [rows, cols] * * This enables the algebra/sparse factories (csChol, csLu, slu, csPermute, * etc.) to operate directly on CSC data without conversion. */ export declare class MathJSSparseMatrix { _values: unknown[] | null; _index: number[]; _ptr: number[]; _size: number[]; _datatype: string | undefined; constructor(data?: unknown[][] | { values?: unknown[] | null; index?: number[]; ptr?: number[]; _values?: unknown[] | null; _index?: number[]; _ptr?: number[]; size?: number[]; _size?: number[]; data?: unknown[][]; datatype?: string; _datatype?: string; }); /** * Convert a dense 2D array to CSC format. */ private _fromDenseArray; get rows(): number; get cols(): number; storage(): string; datatype(): string | undefined; size(): number[]; /** * Get element at [row, col]. Returns 0 for structural zeros. */ get(index: number[]): unknown; /** * Set element at [row, col]. Inserts, updates, or removes entries as needed. * Returns this for chaining (mathjs convention). */ set(index: number[], value: unknown, _defaultValue?: unknown): MathJSSparseMatrix; /** * Convert to dense nested array. */ valueOf(): unknown[][]; toArray(): unknown[][]; /** * Deep clone. */ clone(): MathJSSparseMatrix; /** * Create a new matrix of the same type from nested array data. * Used internally by mathjs factories (e.g., transpose calls x.create(...)). */ create(data: unknown[][], datatype?: string): MathJSSparseMatrix; /** * Instance-level createSparseMatrix — used by csPermute and other sparse * algebra factories that call a.createSparseMatrix({...}). */ createSparseMatrix(opts: { values?: unknown[] | null; index?: number[]; ptr?: number[]; size: number[]; datatype?: string; }): MathJSSparseMatrix; /** * Map over non-zero elements, producing a new sparse matrix. * Callback signature: (value, index, matrix) => newValue */ map(callback: (value: unknown, index: number[], matrix: MathJSSparseMatrix) => unknown): MathJSSparseMatrix; /** * Iterate over non-zero elements. * Callback signature: (value, index, matrix) => void */ forEach(callback: (value: unknown, index: number[], matrix: MathJSSparseMatrix) => void): void; /** * Resize the matrix. Truncates or extends as needed. */ resize(newSize: number[], _defaultValue?: unknown): MathJSSparseMatrix; toJSON(): { mathjs: string; values: unknown[] | null; index: number[]; ptr: number[]; size: number[]; }; toString(): string; /** * Static diagonal constructor — creates a sparse diagonal matrix. */ static diagonal(size: number[], value: unknown | unknown[], k?: number, defaultValue?: number, _datatype?: string): MathJSSparseMatrix; /** * Swap rows j and pi in CSC arrays. Used by LUP decomposition. */ static _swapRows(j: number, pi: number, n: number, values: unknown[], index: number[], ptr: number[]): void; /** * Iterate over entries in row j across all columns. Used by LUP decomposition. */ static _forEachRow(j: number, values: unknown[], index: number[], ptr: number[], callback: (col: number, value: unknown) => void): void; } /** * Creates the `matrix` factory function that mathjs factories call as: * matrix() — empty dense matrix * matrix(data) — dense matrix from nested array * matrix('sparse') — empty sparse matrix (format-only arg) * matrix('default') — empty dense matrix (format-only arg) * matrix(data, 'sparse') — sparse matrix from data */ export declare function createMatrixBridge(): (data?: unknown[][] | MathJSDenseMatrix | MathJSSparseMatrix | string, storageType?: string) => MathJSDenseMatrix | MathJSSparseMatrix; export {}; //# sourceMappingURL=matrix-bridge.d.ts.map