Errors

@teakit/matrix matches ml-matrix: it throws native Error / TypeError / RangeError. There are no stable error codes — match on the error type (and message if needed).

Common cases:

  • Construction: new Matrix(...) throws TypeError for a non-2D-array / 1D array ("Data must be a 2D array…"), a negative size ("nColumns must be a positive integer"), or non-numeric data; RangeError for inconsistent row lengths ("Inconsistent array dimensions").
  • Index access: get/set/row/column helpers throw RangeError when an index is out of range; row/column index helpers validate the index.
  • Dimension mismatch: element-wise matrix ops (add/sub/… with a matrix operand) throw RangeError ("Matrices dimensions must be equal"); mmul requires a.columns === b.rows.
  • Square-only operations: determinant, mpow, etc. throw when the matrix is not square; mpow also requires a non-negative integer exponent.
  • Decompositions: CholeskyDecomposition throws if the input is not symmetric, and its solve throws if not positive-definite; LuDecomposition. solve throws on a singular matrix; QrDecomposition.solve throws when rank-deficient.
  • Special matrices: SymmetricMatrix throws TypeError for non-symmetric data; DistanceMatrix throws if the data is not a valid distance matrix; applyMask throws RangeError on a mask size mismatch.
  • Wrappers: wrap throws if the argument is not an array; WrapperMatrix1D throws if the data length is not divisible by rows.
import { Matrix } from "@teakit/matrix";

try {
  Matrix.add(new Matrix(2, 2), new Matrix(3, 3));
} catch (error) {
  error instanceof RangeError; // true
}