UNPKG

sparse-matrix

Version:
88 lines (80 loc) 2.9 kB
interface Term { row: number; col: number; value: number; } /** * Sparse Matrix. (Should be sorted) */ interface SparseMatrix extends Array<Term> { 0: Term; length: number; } declare class DimensionError extends Error { matrices: SparseMatrix[]; constructor(...matrices: SparseMatrix[]); } declare class OutOfRangeError extends RangeError { row: number; col: number; matrix: SparseMatrix; constructor(row: number, col: number, matrix: SparseMatrix); } /** * Fast Transpose `O(A.cols + A.terms)` * @param A Sparse Matrix * @returns Transposed Sparse Matrix `A^T` */ declare function transpose(A: SparseMatrix): SparseMatrix; /** * Adds two sparse matrices. `O(A.terms + B.terms)`. * @param A The first matrix. * @param B The second matrix. * @returns The sum of the two matrices. */ declare function add(A: SparseMatrix, B: SparseMatrix): SparseMatrix; /** * Multiply two matrices. `O(A.terms * B.cols + B.terms * A.rows)`. * @param A The first matrix. * @param B The second matrix. * @returns The product of the two matrices. */ declare function multiply(A: SparseMatrix, B: SparseMatrix): SparseMatrix; /** * A Matrix class that uses sparse matrix representation under the hood. */ declare class Matrix { data: SparseMatrix; /** Create a matrix by sparese matrix array */ constructor(data: SparseMatrix); /** Get the number of row */ get row(): number; /** Get the number of column */ get col(): number; /** Get the number of non-empty elements */ get size(): number; /** Create a new matrix, which is the transposed matrix of the original one */ transpose(): Matrix; /** Create a new matrix, which is the result of the addition of this and an other matrix */ add(other: Matrix): Matrix; /** Create a new matrix, which is the result of the multiplication of this and an other matrix */ multiply(other: Matrix): Matrix; /** Apply a map function to each non-zero element of this matrix */ map(func: (value: number, row: number, col: number) => number): this; /** Set the element value of this matrix */ set(row: number, col: number, value: number): this; /** Get the element value of this matrix */ get(row: number, col: number): number; /** Get this matrix in 2D array form */ to2d(): number[][]; /** Validate the matrix */ validate(): this; /** Create an empty matrix with given shape */ static empty(row: number, col: number): Matrix; /** Create a matrix from 2D array form */ static from2d(data: number[][]): Matrix; private find; } declare function in_range(row: number, col: number, matrix: SparseMatrix): boolean; declare function validate_matrix(matrix: SparseMatrix): void; export { DimensionError, Matrix, OutOfRangeError, SparseMatrix, Term, add, in_range, multiply, transpose, validate_matrix };