mathjslab
Version:
MathJSLab - An interpreter with language syntax like MATLAB®/Octave, ISBN 978-65-00-82338-7.
275 lines (274 loc) • 10.2 kB
TypeScript
import { type ComplexType } from './Complex';
import { MultiArray } from './MultiArray';
import { type NodeReturnList } from './AST';
/**
* `LinearAlgebra` configuration options type.
*/
type LinearAlgebraConfig = {
/**
* LU Waste.
*/
wasteLU: number;
qrPhaseEpsilon: number;
};
export declare const LinearAlgebraConfigKeyTable: (keyof LinearAlgebraConfig)[];
/**
* # LinearAlgebra
*
* LinearAlgebra abstract class. Implements static methods related to linear algebra operations and algorithms.
*
* ## References
*
* * [Linear Algebra at Wolfram MathWorld](https://mathworld.wolfram.com/LinearAlgebra.html)
* * [Fundamental Theorem of Linear Algebra at Wolfram MathWorld](https://mathworld.wolfram.com/FundamentalTheoremofLinearAlgebra.html)
* * [Linear algebra at Wikipedia](https://en.wikipedia.org/wiki/Linear_algebra)
*/
declare abstract class LinearAlgebra {
/**
* `LinearAlgebra` default settings.
*/
static readonly defaultSettings: LinearAlgebraConfig;
/**
* `LinearAlgebra` current settings.
*/
static readonly settings: LinearAlgebraConfig;
/**
* Set configuration options for `LinearAlgebra`.
* @param config Configuration options.
*/
static readonly set: (config: Partial<LinearAlgebraConfig>) => void;
/**
* Identity matrix
* @param args
* * `eye(N)` - create identity N x N
* * `eye(N,M)` - create identity N x M
* * `eye([N,M])` - create identity N x M
* @returns Identity matrix
*/
static readonly eye: (...args: MultiArray[] | ComplexType[]) => MultiArray | ComplexType;
/**
* Return a diagonal matrix with vector V on diagonal K.
* @param args
* @returns
*/
static readonly diag: (...args: MultiArray[] | ComplexType[]) => MultiArray;
/**
* Sum of diagonal elements.
* @param M Matrix.
* @returns Trace of matrix.
*/
static readonly trace: (M: MultiArray) => ComplexType;
/**
* Transpose and apply function.
* @param M Matrix.
* @returns Transpose matrix with `func` applied to each element.
*/
private static readonly applyTranspose;
/**
* Transpose.
* @param M Matrix.
* @returns Transpose matrix.
*/
static readonly transpose: (M: MultiArray) => MultiArray;
/**
* Complex conjugate transpose.
* @param M Matrix.
* @returns Complex conjugate transpose matrix.
*/
static readonly ctranspose: (M: MultiArray) => MultiArray;
/**
* Matrix product.
* @param left Matrix.
* @param right Matrix.
* @returns left * right.
*/
static mul(left: MultiArray, right: MultiArray): MultiArray;
/**
* Matrix power (multiple multiplication).
* @param left
* @param right
* @returns
*/
static readonly power: (left: MultiArray, right: ComplexType) => MultiArray;
/**
* Matrix determinant using LU decomposition with pivot sign correction.
* Uses `LinearAlgebra.luDecomposition`.
* @param M Matrix.
* @returns Matrix determinant.
*/
static readonly det: (M: MultiArray) => ComplexType;
/**
* Computes the LU decomposition with partial pivoting.
* @param M Input square matrix.
* @returns An object { L, U, P, swaps } where:
* - L: lower-triangular with unit diagonal (MultiArray)
* - U: upper-triangular (MultiArray)
* - P: permutation matrix (MultiArray)
* - swaps: number of row swaps performed (integer)
*
* ## References
* * https://www.codeproject.com/Articles/1203224/A-Note-on-PA-equals-LU-in-Javascript
* * https://rosettacode.org/wiki/LU_decomposition#JavaScript
*/
static readonly luDecomposition: (A: MultiArray) => {
L: MultiArray;
U: MultiArray;
P: MultiArray;
swaps: number;
};
/**
* PLU matrix factorization.
* @param M Matrix.
* @returns L, U and P matrices as multiple output.
*/
static readonly lu: (M: MultiArray) => NodeReturnList;
/**
* Returns the inverse of matrix `M`.
* inv(A) wrapper using LAPACK.getrf_blocked + LAPACK.getrs.
* Behavior: MATLAB-like: if factorization reports info !== 0, emit warning and return matrix filled with Inf.
* @param M Matrix.
* @returns Inverted matrix.
*/
static readonly inv: (A: MultiArray) => MultiArray;
/**
* Gaussian elimination algorithm for solving systems of linear equations.
* Adapted from: https://github.com/itsravenous/gaussian-elimination
* ## References
* * https://mathworld.wolfram.com/GaussianElimination.html
* @param M Matrix.
* @param m Vector.
* @returns Solution of linear system.
*/
static readonly gauss: (M: MultiArray, m: MultiArray) => MultiArray;
/**
* High-performance dot product. Fully ND-aware, column-major, no index
* conversions (≈2-3× faster). Computes sum(conj(A).*B, dim) with minimal
* per-element overhead.
* C = dot(A,B) or C = dot(A,B,dim)
* Sums conj(A).*B along the specified dimension (zero-based operateDim). If dim is omitted,
* use the first non-singleton dimension (zero-based).
* @param A First array (MultiArray).
* @param B Second array (MultiArray).
* @param dim (optional) Dimension along which to operate (ComplexType representing integer, 1-based externally).
* @returns Scalar (ComplexType) if result is single value, else a MultiArray.
*/
static readonly dot: (A: MultiArray, B: MultiArray, dim?: ComplexType) => MultiArray | ComplexType;
/**
* Cross product along dimension `dim` (MATLAB semantics).
* A and B must have the same size except along `dim` where size must be 3.
* dim is optional and is 1-based like MATLAB; internally converted to 0-based.
* @param A
* @param B
* @param dim
* @returns
*/
static readonly cross: (A: MultiArray, B: MultiArray, dim?: any) => MultiArray;
/**
*
* @param A
* @param B
* @returns
*/
static readonly kron: (A: MultiArray, B: MultiArray) => MultiArray;
/**
* Normalize phases so that R diagonal becomes real non-negative:
* For k = 0..minmn-1:
* phi = R[k][k] / |R[k][k]|
* R[k, j] := R[k, j] / phi (j = k..n-1)
* Q[i, k] := Q[i, k] * phi (i = 0..m-1)
* @param Q
* @param R
* @param phis
*/
static readonly qrPhaseNormalize: (phis: ComplexType[], R: MultiArray, Q?: MultiArray) => void;
static readonly lqPhaseNormalize: (phis: ComplexType[], L: MultiArray, Q?: MultiArray) => void;
/**
*
* @param A
* @param result
* @returns
*/
static readonly qrDecomposition: (A: MultiArray, result: 1 | 2 | 3) => {
Q?: MultiArray;
R: MultiArray;
P?: MultiArray;
};
/**
*
* @param M
* @returns
*/
static readonly qr: (M: MultiArray) => NodeReturnList;
/**
* Eigenvalue decomposition wrapper - similar à qrDecomposition.
* Retorno varia com o parâmetro `result`:
* 1 → λ
* 2 → V, λ
* 3 → V, λ, T (tridiagonal)
*/
/**
* eigDecomposition - wrapper that performs eigen decomposition using blocked tridiagonalization.
*
* Returns object depending on `result`:
* 1 -> { values: MultiArray } (column vector n x 1)
* 2 -> { values: MultiArray, vectors: MultiArray } (vectors columns = eigenvectors)
* 3 -> { values: MultiArray, vectors: MultiArray, T: MultiArray } (T = tridiagonal matrix)
*
* Uses:
* - LAPACK.sytrd_blocked_w(Acopy, nb) -> { diag: ComplexType[], offdiag: ComplexType[], taus: ComplexType[] }
* - LAPACK.steqr_values(diag, offdiag) -> ComplexType[]
* - LAPACK.steqr_vectors(diag, offdiag) -> { D: ComplexType[], V: MultiArray }
* - LAPACK.orgtr_blocked_w(Acopy, taus, nb) -> MultiArray Q0
* - BLAS.gemm_block(Q0, Z, Vout, Complex.one(), Complex.zero(), nb)
*/
/**
* eigDecomposition - wrapper that performs eigen decomposition using blocked tridiagonalization.
*
* Returns object depending on `result`:
* 1 -> { values: MultiArray } (column vector n x 1)
* 2 -> { values: MultiArray, vectors: MultiArray } (vectors columns = eigenvectors)
* 3 -> { values: MultiArray, vectors: MultiArray, T: MultiArray } (T = tridiagonal matrix)
*
* Uses:
* - LAPACK.sytrd_blocked_w(Acopy, nb) -> { diag: ComplexType[], offdiag: ComplexType[], taus: ComplexType[] }
* - LAPACK.steqr_values(diag, offdiag) -> ComplexType[]
* - LAPACK.steqr_vectors(diag, offdiag) -> { D: ComplexType[], V: MultiArray }
* - LAPACK.orgtr_blocked_w(Acopy, taus, nb) -> MultiArray Q0
* - BLAS.gemm_block(Q0, Z, Vout, Complex.one(), Complex.zero(), nb)
*/
/**
* eigDecomposition - updated to use steqr_values/steqr_vectors returning MultiArray
*
* Returns:
* result === 1 -> { values: MultiArray }
* result === 2 -> { values: MultiArray, vectors: MultiArray }
* result === 3 -> { values: MultiArray, vectors: MultiArray, T: MultiArray }
*/
static readonly eigDecomposition_original: (A: MultiArray, result: 1 | 2 | 3, nb?: number, order?: "asc" | "desc" | "none") => {
values: MultiArray;
vectors?: MultiArray;
T?: MultiArray;
};
static readonly eigDecomposition: (A: MultiArray, result: 1 | 2 | 3, order?: "asc" | "desc" | "none", blockSize?: number) => {
values: MultiArray;
vectors?: MultiArray;
T?: MultiArray;
};
/**
* Wrapper MATLAB/Octave-style para eig.
* Permite múltiplos retornos via AST.nodeReturnList + handler.
*/
static eig: (M: MultiArray) => NodeReturnList;
static test(A: MultiArray): NodeReturnList;
/**
* LinearAlgebra functions.
*/
static readonly functions: {
[F in keyof LinearAlgebra]: Function;
};
}
export { LinearAlgebra };
declare const _default: {
LinearAlgebra: typeof LinearAlgebra;
};
export default _default;