@railpath/finance-toolkit
Version:
Production-ready finance library for portfolio construction, risk analytics, quantitative metrics, and ML-based regime detection
143 lines (142 loc) • 3.68 kB
TypeScript
/**
* Matrix Operations Utilities
*
* Collection of utility functions for matrix operations commonly used in
* mathematical optimization and portfolio analysis.
*/
/**
* Multiply a matrix by a vector
*
* @param A - Matrix (m×n)
* @param x - Vector (n×1)
* @returns Result vector (m×1)
*
* @example
* ```typescript
* const A = [[1, 2], [3, 4]];
* const x = [5, 6];
* const result = matrixVectorMultiply(A, x); // [17, 39]
* ```
*/
export declare function matrixVectorMultiply(A: number[][], x: number[]): number[];
/**
* Calculate the transpose of a matrix
*
* @param A - Input matrix (m×n)
* @returns Transpose matrix (n×m)
*
* @example
* ```typescript
* const A = [[1, 2, 3], [4, 5, 6]];
* const At = matrixTranspose(A); // [[1, 4], [2, 5], [3, 6]]
* ```
*/
export declare function matrixTranspose(A: number[][]): number[][];
/**
* Multiply two matrices
*
* @param A - First matrix (m×k)
* @param B - Second matrix (k×n)
* @returns Result matrix (m×n)
*
* @example
* ```typescript
* const A = [[1, 2], [3, 4]];
* const B = [[5, 6], [7, 8]];
* const result = matrixMatrixMultiply(A, B); // [[19, 22], [43, 50]]
* ```
*/
export declare function matrixMatrixMultiply(A: number[][], B: number[][]): number[][];
/**
* Calculate the trace of a square matrix
*
* @param A - Square matrix (n×n)
* @returns Trace (sum of diagonal elements)
*
* @example
* ```typescript
* const A = [[1, 2], [3, 4]];
* const trace = matrixTrace(A); // 5
* ```
*/
export declare function matrixTrace(A: number[][]): number;
/**
* Check if a matrix is symmetric
*
* @param A - Matrix to check
* @param tolerance - Tolerance for comparison (default: 1e-12)
* @returns True if matrix is symmetric
*
* @example
* ```typescript
* const A = [[1, 2], [2, 3]];
* const symmetric = isMatrixSymmetric(A); // true
* ```
*/
export declare function isMatrixSymmetric(A: number[][], tolerance?: number): boolean;
/**
* Check if a matrix is positive definite (simplified check)
*
* @param A - Square matrix to check
* @returns True if matrix appears to be positive definite
*
* @example
* ```typescript
* const A = [[2, -1], [-1, 2]]; // Positive definite
* const pd = isMatrixPositiveDefinite(A); // true
* ```
*/
export declare function isMatrixPositiveDefinite(A: number[][]): boolean;
/**
* Create an identity matrix of specified size
*
* @param size - Size of the identity matrix
* @returns Identity matrix (n×n)
*
* @example
* ```typescript
* const I = createIdentityMatrix(3);
* // [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
* ```
*/
export declare function createIdentityMatrix(size: number): number[][];
/**
* Create a zero matrix of specified dimensions
*
* @param rows - Number of rows
* @param cols - Number of columns
* @returns Zero matrix (m×n)
*
* @example
* ```typescript
* const Z = createZeroMatrix(2, 3);
* // [[0, 0, 0], [0, 0, 0]]
* ```
*/
export declare function createZeroMatrix(rows: number, cols: number): number[][];
/**
* Extract diagonal elements from a square matrix
*
* @param A - Square matrix
* @returns Array of diagonal elements
*
* @example
* ```typescript
* const A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]];
* const diag = matrixDiagonal(A); // [1, 5, 9]
* ```
*/
export declare function matrixDiagonal(A: number[][]): number[];
/**
* Calculate the Frobenius norm of a matrix
*
* @param A - Matrix
* @returns Frobenius norm (square root of sum of squares of all elements)
*
* @example
* ```typescript
* const A = [[1, 2], [3, 4]];
* const norm = matrixFrobeniusNorm(A); // √30 ≈ 5.477
* ```
*/
export declare function matrixFrobeniusNorm(A: number[][]): number;