Class Matrix<T>

The Matrix class represents a mathematical matrix with generic type T. It implements the MatrixInterface<T, any> interface.

Type Param

The type of of the Numerical implementation

Remarks

This class provides methods for matrix operations such as addition, subtraction, multiplication, solving systems, etc.

Example

// Create a new instance of Matrix with number type
const numberMatrix: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
// Create a new instance of Matrix with string type
const stringMatrix: Matrix<bigint> = new Matrix([[1n, 2n], [3n, 4n]]);
// Create a new instance of Matrix with a custom type
const customMatrix: Matrix<custom type> = new Matrix([c1,c2,c3,c4],{rows:2,columns:4, numerical: "Custom class for calculations"})

Type Parameters

  • T

    The type of elements contained in the matrix.

Hierarchy

  • Matrix

Implements

Constructors

  • Constructs a matrix object.

    Type Parameters

    • T

    Parameters

    • entries: T[] | T[][]

      The entries of the matrix.

    • Optional options: {
          columns?: number;
          numerical?: Numerical<T>;
          rows?: number;
      }

      The options for the matrix class

      • Optional columns?: number

        The number of columns in the matrix.

      • Optional numerical?: Numerical<T>

        The Numerical class that controls the operations for the matrix.

      • Optional rows?: number

        The number of rows in the matrix.

    Returns Matrix<T>

    Remark

    • A Numerical class argument is not needed if the datatype is number or bigint

    Example

    // Create a new instance of Matrix with number type and 2 rows and 2 columns
    const matrix: Matrix<number> = new Matrix([[1, 2], [3, 4]]);

    // Create a new instance of Matrix with bigint type and 2 rows and 2 columns
    const matrix2: Matrix<bigint> = new Matrix([BigInt(1), BigInt(2), BigInt(3), BigInt(4)], { rows: 2, columns: 2});

    // Create a new instance of Matrix with a custom type and 2 rows and 2 columns
    const matrix3: Matrix<ctype> = new Matrix([c1, c2], [c3, c4], { rows: 2, columns: 2, numerical: "The custom class that implements the Numerical interface goes here" });

    Throws

    If the entries are not an array, or if the entries are a 1D array but rows and columns are not defined or not numbers or are 0 or negative, or if the length of the 1D array does not equal rows * columns, or if the entries are a 2D array but not all rows have the same length, or if the matrix has a depth greater than one.

    Throws

    If the matrix datatype is neither a number nor a bigint and no appropriate Numeric implementation was provided.

Properties

columns: number = Infinity

The number of columns in the matrix.

dataType: string = ""

The data type of the matrix elements.

isSquare: boolean = false

Indicates whether the matrix is square.

isTall: boolean = false

Indicates whether the matrix is tall (more rows than columns).

isWide: boolean = false

Indicates whether the matrix is wide (more columns than rows).

mElements: T[] = []

The elements of the matrix.

numerical: Numerical<any>

The numercal class to use for calculations

rows: number = Infinity

The number of rows in the matrix.

shape: string = "0"

The shape of the matrix.

size: number = Infinity

The total number of elements in the matrix.

Methods

  • Performs LUP decomposition on the matrix with partial pivoting. This method does not modify the original matrix.

    Returns {
        L: Matrix<T>;
        P: Matrix<T>;
        U: Matrix<T>;
        permutationCount: number;
    }

    An object containing the L, U, and P matrices.

    Throws

    If the matrix is not square or is singular.

    Example

    const matrix: Matrix<number> = new Matrix([[2, -1, 3], [4, 3, -1], [-2, 2, 1]]);
    const result: { L: Matrix<number>, U: Matrix<number>, P: Matrix<number>, permutationCount: number } = matrix.LUPDecomposition();
    console.log(`L Matrix:\n ${result.L.toString()}`);
    console.log(`U Matrix:\n ${result.U.toString()}`);
    console.log(`P Matrix:\n ${result.P.toString()}`);
    // Output:
    L Matrix:
    " 1.00 0.00 0.00"
    "-0.50 1.00 0.00"
    " 0.50 -0.71 1.00"

    U Matrix:
    "4.00 3.00 -1.00"
    "0.00 3.50 0.50"
    "0.00 0.00 3.86"

    P Matrix:
    "0.00 1.00 0.00"
    "0.00 0.00 1.00"
    "1.00 0.00 0.00"
  • Performs QR decomposition on the matrix. This method does not modify the original matrix.

    Returns {
        Q: Matrix<T>;
        R: Matrix<T>;
    }

    An object containing the Q and R matrices.

    Example

    const matrix: Matrix<number> = new Matrix([[12, -51, 4], [6, 167, -68], [-4, 24, -41]]);
    const result: { Q: Matrix<number>, R: Matrix<number> } = matrix.QRDecomposition();
    console.log(`Q Matrix:\n ${result.Q.toString()}`);
    console.log(`R Matrix:\n ${result.R.toString()}`);
    // Output:
    Q Matrix:
    " 0.86 -0.39 0.33"
    " 0.43 0.90 -0.03"
    "-0.29 0.17 -0.94"

    R Matrix:
    "14.00 21.00 -14.00"
    "0.00 175.00 -70.00"
    "0.00 0.00 35.00"
  • Returns a new matrix where each element is the absolute value of the corresponding element in the original matrix.

    Returns Matrix<T>

    A new matrix with absolute values of the elements.

    Example

    const matrix: Matrix<number> = new Matrix<number>([[-2, 4], [-6, 8]]);
    const absoluteMatrix: Matrix<number> = matrix.abs();
    console.log(absoluteMatrix.toString());
    // Output:
    "2 4"
    "6 8"
  • Adds another matrix to this matrix.

    Parameters

    • B: Matrix<T>

      The matrix to add.

    Returns Matrix<T>

    The resulting matrix.

    Throws

    If the dimensions of the two matrices are not the same.

    Throws

    If the argument is not an instance of Matrix.

    Example

    const matrixA: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
    const matrixB: Matrix<number> = new Matrix([[5, 6], [7, 8]]);
    const resultMatrix: Matrix<number> = matrixA.add(matrixB);
    console.log(resultMatrix.toString());
    // Output:
    "6 8"
    "10 12"
  • Computes the adjugate of the matrix.

    Returns Matrix<T>

    The adjugate matrix

    Throws

    If the matrix is not square.

    Example

    const matrix:Matrix<number> = new Matrix<number>([[1, 2], [3, 4]]);
    const adjugateMatrix:Matrix<number> = matrix.adjugate();
    console.log(adjugateMatrix.toString());
    // Output:
    " 4 -2"
    "-3 1"
  • Augments the current matrix with another matrix.

    Parameters

    • B: Matrix<T>

      The matrix to be augmented with.

    Returns Matrix<T>

    A new matrix that is the result of augmenting the current matrix with the provided matrix.

    Throws

    If the argument is not an instance of Matrix, or if the current matrix and the provided matrix do not have the same number of rows.

    Example

    const matrixA: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
    const matrixB: Matrix<number> = new Matrix([[5, 6], [7, 8]]);
    const resultMatrix: Matrix<number> = matrixA.augment(matrixB);
    console.log(resultMatrix.toString());
    // Output:
    "1 2 5 6"
    "3 4 7 8"
  • Performs back-substitution on an upper triangular matrix to solve a system of linear equations.

    Parameters

    • b: T[]

      The right-hand side of the system of linear equations.

    Returns T[]

    Solution to the system of linear equations.

    Throws

    If the matrix is not upper triangular, if b is not an array, or if the matrix contains a zero on the diagonal (unsolvable system).

    Example

    const matrix: Matrix<number> = new Matrix([[1, 2], [0, 1]]);
    const b: number[] = [8, 5];
    const solution: number[] = matrix.backSubstitution(b);
    console.log(solution);
    // Output: [-2, 5]
  • Computes the cofactor of an element given a row and column

    Parameters

    • row: number
    • column: number

    Returns T

    The cofactor of the element

    Throws

    If the matrix is not square.

    Example

    const matrix: Matix<number> = new Matrix([[1, 4, 7], [3, 0, 5], [-1, 9, 11]]);
    const cofactor: number = matrix.cofactor(1,2);
    console.log(cofactor);
    // Output: -13
  • Computes the cofactor matrix.

    Returns Matrix<T>

    The cofactor matrix

    Throws

    If the matrix is not square.

    Example

    const matrix: Matrix<number> = new Matrix([[1, 2, 3], [0, 4, 5], [1, 0, 6]]);
    const cofactorMatrix: Matrix<number> = matrix.cofactorMatrix();
    console.log(cofactorMatrix.toString());
    // Output:
    " 24 5 -4"
    "-12 3 2"
    "-2 -5 -4"
  • Computes the condition number of this matrix.

    Returns number

    The condition number of the matrix.

    Throws

    If the matrix is not square.

    Example

    const matrix: Matrix<number>  = new Matrix([[1, 2], [3, 4]]);
    const conditionNumber: number = matrix.conditionNumber();
    console.log(conditionNumber);
    // Output: 14.933034373659268
  • Computes the determinant of this matrix.

    Returns number

    The determinant of the matrix.

    Throws

    If the matrix is not square.

    Throws

    If the matrix is singular and LU decomposition cannot be performed.

    Example

    const matrix: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
    const determinant: number = matrix.det();
    console.log(determinant);
    // Output: -2
  • Returns the diagonal elements of the matrix.

    Parameters

    • k: number = 0

      The offset from the main diagonal. Default is 0 which represents the main diagonal.

    Returns T[]

    An array of the diagonal elements.

    Throws

    If the absolute value of k is greater than or equal to the number of rows for a square matrix, or if k is greater than or equal to the number of columns or the absolute value of k is greater than or equal to the number of rows for a wide or tall matrix.

    Example

    const matrix: Matrix<number> = new Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
    const mainDiag: number[] = matrix.diag();
    const fistDiag: number[] = matrix.diag(1);
    console.log(mainDiag);
    // Output: [1, 5, 9]
    console.log(fistDiag);
    // Output: [2,6]
  • Checks if the current matrix is equal to another matrix.

    Parameters

    • B: Matrix<T>

      The matrix to compare with.

    • threshold: number = Constants.DELTA

      Threshold for the method to return true defaults to 1e-12

    Returns boolean

    'true' if the matrices are equal, 'false' otherwise.

    Example

    const matrixA: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
    const matrixB: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
    const isEqual: boolean = matrixA.equal(matrixB);
    console.log(isEqual);
    // Output: true
  • Performs forward-substitution on a lower triangular matrix to solve a system of linear equations.

    Parameters

    • b: T[]

      The right-hand side of the system of linear equations.

    Returns T[]

    Solution to the system of linear equations.

    Throws

    If the matrix is not lower triangular, if b is not an array, or if the matrix contains a zero on the diagonal (unsolvable system).

    Example

    const matrix: Matrix<number> = new Matrix([[1, 0], [2, 3]]);
    const b: number[] = [4, 5];
    const solution: number[] = matrix.forwardSubstitution(b);
    console.log(solution);
    // Output: [4, -1]
  • Calculates the Fourier transformation of a matrix.

    Parameters

    • option: {
          method: "dft" | "fft";
          sequence: "row" | "column";
      } = ...

      The options for the Fourier transform.

      • method: "dft" | "fft"

        The method to use for the Fourier transform ("dft" or "fft"). Default is "fft".

      • sequence: "row" | "column"

        The sequence in which the Fourier transform is applied ("row" or "column"). Default is "row".

    Returns Matrix<ComplexNumber>

    The Fourier transformed matrix.

    Throws

    Throws an error if the matrix data type is not "number" or if the input sequences are not a power of two (only for "dft" method).

    Example

    const matrix: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
    const result: Matrix<ComplexNumber> = matrix.fourier({ method: "fft", sequence: "row" });
    console.log(result.toString());
    // Output:
    "3 + 0i -1 + 0i"
    "7 + 0i -1 + 0i"
  • Converts the matrix to Reduced Row Echelon Form (RREF) using the Gauss-Jordan elimination method. This method does not modify the original matrix.

    Parameters

    • options: {
          solve?: boolean;
      } = ...

      The options for the Gauss-Jordan elimination.

      • Optional solve?: boolean

        Indicates whether to solve the system of equations. Default is false.

    Returns Matrix<T> | T[]

    • A new matrix in RREF or the solution to the system of equations as an array if options.solve is true.

    Example

    const matrix: Matrix<number> = new Matrix([[3, 2, 1], [4, 2, 2], [5, 4, 4]]);
    const resultMatrix: Matrix<number> = matrix.gaussJordan();
    console.log(resultMatrix.toString());
    // Output:
    "1 0 0"
    "0 1 0"
    "0 0 1"
  • Converts the matrix to Row Echelon Form (REF) using the Gaussian elimination method. This method does not modify the original matrix.

    Parameters

    • options: {
          solve?: boolean;
      } = ...

      The options for Gaussian elimination.

      • Optional solve?: boolean

        Indicates whether to solve the system of equations. Default is false.

    Returns Matrix<T> | T[]

    • A new matrix in REF or the solution to the system of equations as an array if options.solve is true.

    Example

    const matrix: Matrix<number> = new Matrix([[3, 2, 1], [4, 2, 2], [5, 4, 4]]);
    const resultMatrix: Matrix<number> = matrix.gaussianElimination();
    console.log(resultMatrix.toString());
    // Output:
    "1 4 1"
    "0 -6 0"
    "0 0 -1"
  • Gets a specific column of the matrix.

    Parameters

    • columnIndex: number

      The index of the column to retrieve.

    Returns T[]

    An array representing the specified column of the matrix.

    Throws

    If the columnIndex is out of bounds.

    Example

    const matrix: Matrix<number> = new Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
    const column: Number[] = matrix.getColumn(1);
    console.log(column);
    // Output: [2, 5, 8]
  • Gets the value of an element in the matrix.

    Parameters

    • row: number

      The row index of the element zero indexed.

    • column: number

      The column index of the element zero indexed.

    Returns T

    The value of the element.

    Example

    const matrix: Matrix<number> = new Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
    const value: number = matrix.getElement(1, 2);
    console.log(value);
    // Output: 6

    Throws

    • If index is out of bounds
  • Private

    Retrieves the name of the interface or class of a given value.

    Type Parameters

    • T

      The type of the value.

    Parameters

    • value: T

      The value to retrieve the interface or class name from.

    Returns string

    The name of the interface or class.

  • Gets a specific row of the matrix.

    Parameters

    • rowIndex: number

      The index of the row to retrieve (starting from 0).

    Returns T[]

    An array representing the specified row of the matrix.

    Throws

    If the rowIndex is out of bounds.

    Example

    const matrix: Matrix<number> = new Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
    const row: number[] = matrix.getRow(2);
    console.log(row);
    // Output: [7, 8, 9]
  • Retrieves a submatrix from the current matrix.

    Parameters

    • startRow: number

      The starting row index of the submatrix.

    • endRow: number

      The ending row index of the submatrix (inclusive).

    • startCol: number

      The starting column index of the submatrix.

    • endCol: number

      The ending column index of the submatrix (inclusive).

    Returns Matrix<T>

    A new Matrix object representing the submatrix.

    Example

    const matrix: Matrix<number> = new Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
    const subMatrix: Matrix<number> = matrix.getSubMatrix(1, 2, 1, 2);
    console.log(subMatrix.toString());
    // Output:
    "5 6"
    "8 9"
  • Private

    Retrieves the type of a given value.

    Type Parameters

    • T

      The type of the value.

    Parameters

    • value: T

      The value to retrieve the type from.

    Returns string

    The type of the value.

  • Performs the Gram-Schmidt process for the columns of the given matrix. The process is an algorithm to orthonormalize a set of vectors in an inner product space, generally Euclidean n-space.

    The method takes the columns (considered as vectors) of the current matrix instance and generates an orthogonal set of vectors that spans the same column space as the original set. The set of orthonormal vectors is computed sequentially by subtracting the projections of a matrix column vector onto the previously computed orthogonal vectors from the column vector itself.

    Returns Matrix<T>

    A new Matrix instance constructed using the orthonormal vectors as columns.

    Throws

    If any column obtained during the process is nearly zero (having euclidean norm lesser than a small constant - DELTA). In this case, this means that the provided set is not linearly independent.

    Example

    const matrix: Matrix<number> = new Matrix([[1, 1, 1], [0, 1, 0], [0, 0, 1]]);
    const resultMatrixx: Matrix<number> = matrix.gramSmith();
    console.log(resultMatrix.toString());
    // Output:
    "1 0 0"
    "0 1 0"
    "0 0 1"
  • Calculates the infinity norm of the matrix, which is the maximum value of the sum of absolute values in each row.

    Returns number

    The infinity norm of the matrix.

    Example

    const matrix: Matrix<number> = new Matrix([[1, 2], [-3, 4], [-5, -6]]);
    const infNorm: number = matrix.infNorm();
    console.log(infNorm);
    // Output: 11
  • Inverts a lower triangular matrix.

    Returns Matrix<T>

    The inverted lower triangular matrix.

    Throws

    If the original matrix is not square or a lower triangular matrix , an error is thrown.

    Example

    const matrix = new Matrix([[1, 0], [2, 1]]);
    const resultMatrix = matrix.invertLower();
    console.log(resultMatrix.toString());
    // Output:
    " 1 0"
    "-2 1"
  • Inverts a square matrix.

    Returns Matrix<T>

    The inverse of the square matrix.

    Throws

    If the matrix is not square.

    Example

    const matrix: Matrix<number> = new Matrix([[4, 7], [2, 6]]);
    const resultMatrix: Matrix<number> = matrix.invertSquare();
    console.log(resultMatrix.toString());
    // Output:
    " 0.6 -0.7"
    "-0.2 0.4"
  • Inverts an upper triangular matrix.

    Returns Matrix<T>

    The inverted upper triangular matrix.

    Throws

    If the original matrix is not square or an upper triangular matrix, an error is thrown.

    Example

    const matrix: Matrix<number> = new Matrix([[1, 2], [0, 1]]);
    const resultMatrix: Matrix<number> = matrix.invertUpper();
    console.log(resultMatrix.toString());
    // Output:
    "1 -2"
    "0 1"
  • Private

    Checks if the given entries are a one-dimensional array of a specific type.

    Parameters

    • entries: any

      The entries to check.

    Returns entries is T[]

    'true' if the entries are a one-dimensional array of type T, 'false' otherwise.

  • Calculates the Manhattan norm of the matrix, which is the maximum value of the sum of absolute values in each column.

    Returns number

    The Manhattan norm of the matrix.

    Example

    const matrix: Matrix<number> = new Matrix<number>([[1, 2], [-3, 4], [-5, -6]]);
    const manhattanNorm: number = matrix.manhattanNorm();
    console.log(manhattanNorm);
    // Output: 12
  • Applies a callback function to each element in the matrix and returns a new matrix with the result.

    Parameters

    • callbackFn: ((value, index, array) => T)

      A function to execute for each element in the matrix. It takes three arguments: the current element being processed, the index of the current element, and the matrix.mElements on which map() was called. Its return value is added as a single element in the new mElement.

        • (value, index, array): T
        • Parameters

          • value: T
          • index: number
          • array: T[]

          Returns T

    Returns Matrix<T>

    A new matrix with the callback function applied to each element.

    Example

    const matrix: Matrix<number> = new Matrix<number>([[1, 2], [3, 4]]);
    const mappedMatrix: Matrix<number> = matrix.map((element: number) => {
    return element * 2;
    });
    console.log(mappedMatrix.toString());
    // Output:
    " 1 0"
    "-2 1"
  • Returns the maximum value in the matrix.

    Returns T

    The maximum value.

    Example

    const matrix: Matrix<number> = new Matrix([[1, 2, 3], [4, 6, 5]]);
    const max: number = matrix.max();
    console.log(max);
    // Output: 6
  • Calculates the arithmetic mean value of all elements in the matrix.

    Returns T

    The mean value of the elements.

    Example

    const matrix: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
    const mean: number = matrix.mean();
    console.log(mean);
    // Output: 2.5
  • Returns the minimum value in the matrix.

    Returns T

    The minimum value.

    Example

    const matrix: Matrix<number> = new Matrix([[2, 1, 3], [4, 5, 6]]);
    const min: number = matrix.min();
    console.log(min);
    // Output: 1
  • Multiplies this matrix with another matrix using the naive algorithm.

    Parameters

    • B: Matrix<T>

      The matrix to multiply with.

    Returns Matrix<T>

    The resulting matrix.

    Throws

    If the dimensions of the two matrices are not compatible for multiplication.

    Example

    const matrixA: Matrix<number>  = new Matrix([[1, 2], [3, 4]]);
    const matrixB: Matrix<number> = new Matrix([[5, 6], [7, 8]]);
    const resultMatrix: Matrix<number> = matrixA.multiply(matrixB);
    console.log(resultMatrix.toString());
    // Output:
    "19 22"
    "43 50"
  • Computes the Frobenius norm of this matrix.

    Returns number

    The Frobenius norm of the matrix.

    Example

    const matrix: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
    const norm: number = matrix.norm();
    console.log(norm);
    // Output: 5.477225575051661
  • Calculates the p-norm of the matrix.

    Parameters

    • p: number

      The power to which each element is raised.

    Returns number

    The p-norm of the matrix.

    Throws

    If the provided p is not a positive integer.

    Example

    const matrix: Matrix<number> = new Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
    const pNorm: number = matrix.pNorm(2);
    console.log(pNorm);
    // Output: 16.881943016134134
  • Raises the matrix to the power of exp.

    Parameters

    • exp: number

      The exponent to raise the matrix to.

    Returns Matrix<T>

    The resulting matrix after raising it to the power of exp.

    Example

    const matrix: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
    const resultMatrix: Matrix<number> = matrix.pow(2);
    console.log(resultMatrix.toString());
    // Output:
    "7 10"
    "15 22"

    Throws

    if the matrix is not square.

  • Prints the matrix in a formatted way to the console.

    Returns void

    Example

    const matrix: Matrix<number> = new Matrix([[1, 2, 3], [4, 5, 6]]);
    matrix.print();
    // Output:
    "1 2 3"
    "4 5 6"
  • Calculates the rank of the matrix. The rank of a matrix is defined as the maximum number of linearly independent rows or columns.

    Returns number

    The rank of the matrix.

    Example

    *
    const matrix: Matrix<number> = new Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
    const rank: number = matrix.rank();
    console.log(rank);
    // Output: 2
  • Removes a column from the matrix and returns a new matrix with the column removed.

    Parameters

    • columnNum: number

      The index of the column to remove.

    Returns Matrix<T>

    A new matrix with the specified column removed.

    Throws

    If the columnNum argument is not a number.

    Throws

    If the columnNum argument is greater than the number of columns in the matrix.

    Example

    const matrix:Matrix<number> = new Matrix<number>([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
    const newMatrix:Matrix<number> = matrix.removeColumn(1);
    console.log(newMatrix.toString());
    // Output:
    "1 3"
    "4 6"
    "7 9"
  • Removes a row from the matrix and returns a new matrix with the row removed.

    Parameters

    • rowNum: number

      The index of the row to remove.

    Returns Matrix<T>

    A new matrix with the specified row removed.

    Throws

    If the rowNum argument is not a number.

    Throws

    If the rowNum argument is greater than the number of rows in the matrix.

    Example

    const matrix: Matrix<number> = new Matrix([[1, 2], [3, 4], [5, 6]]);
    const newMatrix: Matrix<number> = matrix.removeRow(1);
    console.log(newMatrix.toString());
    // Output:
    "1 2"
    "5 6"
  • Scales the matrix and returns a new matrix with the result of the scaling.

    Parameters

    • scalar: T

      The scalar to scale the matrix with.

    Returns Matrix<T>

    The scaled matrix.

    Example

    const matrix: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
    const scaledMatrix: Matrix<number> = matrix.scale(2);
    console.log(scaledMatrix.toString());
    // Output:
    "2 4"
    "6 8"
  • Sets a specific column of the matrix.

    Parameters

    • columnIndex: number

      The index of the column to set .

    • values: T[]

      An array comprising the values to set.

    Returns void

    Throws

    If the columnIndex is out of bounds or the size of the input array does not match the row size of the matrix.

    Example

    const matrix: Matrix<number> = new Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
    matrix.setColumn(2, [7, 8, 9]);
    console.log(matrix.toString());
    // Output:
    "1 2 7"
    "4 5 8"
    "7 8 9"
  • Sets the value of an element in the matrix.

    Parameters

    • row: number

      The row index of the element starts from zero.

    • column: number

      The column index of the element starts from zero.

    • value: T

      The value to set.

    Returns void

    Throws

    • If the value is an invalid element or index is out of bounds

    Example

    const matrix: Matrix<Number> = new Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
    matrix.setElement(0, 1, 10);
    console.log(matrix.getElement(0, 1));
    // Output: 10
  • Sets a specific row of the matrix.

    Parameters

    • rowIndex: number

      The index of the row to set .

    • values: T[]

      An array comprising the values to set.

    Returns void

    Throws

    If the rowIndex is out of bounds or the size of the input array does not match the column size of the matrix.

    Example

    const matrix: Matrix<number> = new Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
    matrix.setRow(1, [3, 2, 1]);
    console.log(matrix.toString());
    // Output:
    "1 2 7"
    "3 2 1"
    "7 8 9"
  • Sets the submatrix of a given matrix

    Parameters

    • startRow: number

      The starting row index of the submatrix.

    • endRow: number

      The ending row index of the submatrix (exclusive).

    • startCol: number

      The starting column index of the submatrix.

    • endCol: number

      The ending column index of the submatrix (exclusive).

    • subMatrix: Matrix<T>

      The elements of the submatrix to be set.

    Returns void

    Throws

    If the start or end indices are out of bounds, or if the dimensions of the submatrix do not match the specified dimensions.

    Example

    const matrix: Matrix<number> = new Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
    const subMatrix: Matrix<number> = new Matrix([[3, 3], [3, 3]]);
    matrix.setSubMatrix(1, 3, 1, 3, subMatrix);
    console.log(matrix.toString());
    // Output:
    "1 2 7"
    "4 3 3"
    "7 3 3"
  • Performs matrix multiplication using the Strassen's algorithm.

    Parameters

    • B: Matrix<T>

      The matrix to multiply with.

    Returns Matrix<T>

    The result of matrix multiplication.

    Throws

    If both matrixes are not square

    Example

    const matrixA: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
    const matrixB: Matrix<number> = new Matrix([[5, 6], [7, 8]]);
    const resultMatrix: Matrix<number> = matrixA.strassenMultiply(matrixB);
    console.log(resultMatrix.toString());
    // Output:
    "19 22"
    "43 50"

    Throws

    If the matrices are not square.

  • Subtracts another matrix from this matrix.

    Parameters

    • B: Matrix<T>

      The matrix to subtract.

    Returns Matrix<T>

    The resulting matrix.

    Throws

    If the dimensions of the two matrices are not compatible for subtraction.

    Example

    const matrixA = new Matrix([[1, 2], [3, 4]]);
    const matrixB = new Matrix([[5, 6], [7, 8]]);
    const resultMatrix = matrixA.subtract(matrixB);
    console.log(resultMatrix.toString());
    // Output:
    "-4 -4"
    "-4 -4"

    Throws

    If the dimensions of the two matrices are not compatible for subtraction.

  • Computes the sum of the elements in the matrix.

    Returns T

    The sum of the elements in the matrix.

    Example

    const matrix: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
    const sum: number = matrix.sum();
    console.log(sum);
    // Output: 10
  • Swaps two columns of the matrix.

    Parameters

    • col1: number

      The index of the first column to swap.

    • col2: number

      The index of the second column to swap.

    Returns void

    Throws

    If the matrix has less than two columns or either column index is out of bounds.

    Example

    const matrix: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
    matrix.swapColumns(0, 1);
    console.log(matrix.toString());
    // Output:
    "2 1"
    "4 3"
  • Swaps two rows of the matrix.

    Parameters

    • row1: number

      The index of the first row to swap.

    • row2: number

      The index of the second row to swap.

    Returns void

    Throws

    If either row index is out of bounds.

    Example

    const matrix: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
    matrix.swapRows(0, 1);
    console.log(matrix.toString());
    // Output:
    "3 4"
    "1 2"
  • Converts the matrix to a 2D array.

    Returns T[][]

    The matrix as a 2D array.

    Example

    const matrix: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
    const matrixArray: number[][] = matrix.toArray();
    console.log(matrixArray);
    // Output: [[1, 2], [3, 4]]
  • Converts the matrix to a printable string

    Returns string

    The printable matrix in a nice format

    Example

    const matrix: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
    const matrixString: string = matrix.toString();
    console.log(matrixString);
    // Output:
    "1 2"
    "3 4"
  • Calculates the trace of the matrix. Throws an error if the matrix is not square.

    Returns T

    The trace value.

    Throws

    If the matrix is not square.

    Example

    const matrix: Matrix<number> = new Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
    const trace: number = matrix.trace();
    console.log(trace);
    // Output: 15
  • Transposes a matrix.

    Returns Matrix<T>

    The transposed matrix.

    Example

    const matrix: Matrix<number> = new Matrix([[1, 2, 3], [4, 5, 6]]);
    const resultMatrix: Matrix<number> = matrix.transpose();
    console.log(resultMatrix.toString());
    // Output:
    "1 4"
    "2 5"
    "3 6"
  • Private

    Updates all aspects of Matrix.

    Returns void

  • Private

    Updates the shape of the Matrix.

    Returns void

  • Private

    Updates matrix-related specifcation

    Returns void

  • Performs vector-matrix multiplication by multiplying each element of the matrix by the corresponding element in the input vector.

    Parameters

    • vector: T[]

      The input vector.

    Returns Matrix<T>

    A new matrix resulting from the vector-matrix multiplication.

    Throws

    If the input vector is not an array or if its length doesn't match the number of columns in the matrix.

    Example

    const matrix: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
    const vector: number[] = [2, 3];
    const resultMatrix: Matrix<number> = matrix.vMultiply(vector);
    console.log(resultMatrix.toString());
    // Output:
    "2 6"
    "6 12"
  • Private

    Validates the entries of a 1d T array to ensure they are valid.

    Parameters

    • entries: T[]

      The entries of the matrix.

    Returns void

  • Deep clones the matrix instance and returns the clone

    Type Parameters

    • T

    Parameters

    Returns Matrix<T>

    The cloned matrix

    Static

    Example

    const matrix: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
    const clonedMatrix: Matrix<number> = Matrix.clone(matrix);
    console.log(clonedMatrix.toString());
    // Output:
    "1 2"
    "3 4"
  • Creates an identity matrix with the specified dimension.

    Type Parameters

    • T

    Parameters

    • dimension: number

      The dimension of the identity matrix.

    • Optional numerical: Numerical<T>

    Returns Matrix<T>

    The identity matrix.

    Static

    Throws

    • If the dimension is less than or equal to 0.

    Example

    const identityMatrix: Matrix<number> = Matrix.identity(3);
    console.log(identityMatrix.toString());
    // Output:
    "1 0 0"
    "0 1 0"
    "0 0 1"
  • Checks if the given matrix is diagonal.

    Type Parameters

    • T

      The type of elements in the matrix.

    Parameters

    • A: Matrix<T>

      The matrix to check.

    Returns boolean

    • True if the matrix is diagonal, false otherwise.

    Throws

    • If the argument is not an instance of Matrix.

    Example

    const matrix: Matrix<number> = new Matrix([[1, 0], [0, 2]]);
    const diagonal: Boolean = Matrix.isDiagonal(matrix);
    console.log(diagonal);
    // Output: true
  • Checks if the given matrix is empty.

    Type Parameters

    • T

      The type of elements in the matrix.

    Parameters

    • A: Matrix<T>

      The matrix to check.

    Returns boolean

    • True if the matrix is empty, false otherwise.

    Throws

    • If the argument is not an instance of Matrix.

    Example

    const matrix: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
    const empty: boolean = Matrix.isEmpty(matrix);
    console.log(empty);
    // Output: false
  • Checks if the given matrix is an identity matrix.

    Type Parameters

    • T

      The type of elements in the matrix.

    Parameters

    • A: Matrix<T>

      The matrix to check.

    Returns boolean

    • True if the matrix is an identity matrix, false otherwise.

    Throws

    • If the argument is not an instance of Matrix.

    Example

    const matrix: Matrix<number> = new Matrix([[1, 0], [0, 1]]);
    const identity: boolean = Matrix.isIdentity(matrix);
    console.log(identity);
    // Output: true
  • Checks if a given matrix contains only integer elements.

    Parameters

    • A: Matrix<number>

      The matrix to check.

    Returns boolean

    True if all elements in the matrix are integers, false otherwise.

    Throws

    If the argument is not an instance of Matrix.

    Example

    const matrix: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
    console.log(Matrix.isIntMatrix(matrix));
    // Output: true

    const nonIntMatrix: Matrix<number> = new Matrix([[1.5, 2.7], [3.1, 4.6]]);
    console.log(Matrix.isIntMatrix(nonIntMatrix));
    // Output: false
  • Checks if a matrix is invertible.

    Type Parameters

    • T

      The type of elements in the matrix.

    Parameters

    • A: Matrix<T>

      The matrix to check for invertibility.

    Returns boolean

    True if the matrix is invertible, false otherwise.

    Example

    const matrixA: Matrix<number> = new Matrix([[1, 2], [3, 4]]);
    const invertible: Boolean = Matrix.isInvertible(matrixA);
    console.log(invertible);
    // Output: true

    const matrixB: Matrix<number> = new Matrix([[1, 2], [2, 4]]);
    const invertible: boolean = Matrix.isInvertible(matrixB);
    console.log(invertible);
    // Output: false
  • This method checks if the matrix is lower triangular. A matrix is said to be lower triangular if all its entries above the main diagonal are zero.

    Type Parameters

    • T

    Parameters

    • A: Matrix<T>

      The matrix to check

    Returns Boolean

    • Returns true if the matrix is lower triangular, false otherwise.

    Static

    Example

    const lowerTriangularMatrix: Matrix<number> = new Matrix([[1, 0, 0], [2, 3, 0], [4, 5, 6]]);
    console.log(Matrix.isLowerTriangular(lowerTriangularMatrix));
    // Output: true

    const nonLowerTriangularMatrix: Matrix<number> = new Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
    console.log(Matrix.isLowerTriangular(nonLowerTriangularMatrix));
    // Output: false
  • Checks if the given matrix is symmetric.

    Type Parameters

    • T

      The type of elements in the matrix.

    Parameters

    • A: Matrix<T>

      The matrix to check.

    Returns boolean

    • True if the matrix is symmetric, false otherwise.

    Throws

    • If the argument is not an instance of Matrix.

    Example

    const matrix: Matrix<number> = new Matrix([[1, 2], [2, 3]]);
    const symmetric: boolean = Matrix.isSymmetric(matrix);
    console.log(symmetric);
    // Output: true
  • This method checks if the given matrix is upper triangular. A matrix is said to be upper triangular if all its entries below the main diagonal are zero.

    Type Parameters

    • T

    Parameters

    • A: Matrix<T>

      The matrix to check

    Returns boolean

    Returns true if the matrix is upper triangular, false otherwise.

    Static

    Example

    const upperTriangularMatrix: Matrix<number> = new Matrix([[1, 2, 3], [0, 4, 5], [0, 0, 6]]);
    console.log(Matrix.isUpperTriangular(upperTriangularMatrix));
    // Output: true

    const nonUpperTriangularMatrix: Matrix<number> = new Matrix([[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
    console.log(Matrix.isUpperTriangular(nonUpperTriangularMatrix));
    // Output: false
  • Sets all elements of the matrix to their respective zero values.

    Type Parameters

    • T

      The type of elements in the matrix.

    Parameters

    • A: Matrix<T>

      The matrix to nullify.

    Returns void

    Throws

    If the argument is not an instance of Matrix.

    Example

    const matrix: Matrix<number> = new Matrix<number>([[1, 2], [3, 4]]);
    Matrix.null(matrix);
    console.log(matrix.toString());
    // Output:
    "0 0"
    "0 0"
  • Creates a matrix filled with ones with the specified number of rows and columns.

    Type Parameters

    • T

    Parameters

    • rows: number

      The number of rows in the matrix.

    • columns: number

      The number of columns in the matrix.

    • Optional numerical: Numerical<T>

    Returns Matrix<T>

    • The matrix filled with ones.

    Static

    Throws

    • If the rows and or columns is less than or equal to 0.

    Example

    const onesMatrix: Matrix<number> = Matrix.ones(2, 2);
    console.log(onesMatrix.toString());
    // Output:
    "1 1"
    "1 1"
  • Method used to pad the matrix dimensions to the nearest power of two.

    Type Parameters

    • T

    Parameters

    • A: Matrix<T>

      The matrix to pad

    Returns Matrix<T>

    The padded matrix with dimensions as a power of two.

    Static

    Example

    const matrix: Matrix<number> = new Matrix([[1, 2, 3], [4, 5, 6]]);
    const paddedMatrix: Matrix<number> = Matrix.padMatrixToPowerOfTwo(matrix);
    console.log(paddedMatrix.toString());
    // Output:
    "1 2 3 0"
    "4 5 6 0"
    "0 0 0 0"
    "0 0 0 0"
  • Creates a random matrix with the specified number of rows and columns.

    Type Parameters

    • T

    Parameters

    • rows: number

      The number of rows in the matrix.

    • columns: number

      The number of columns in the matrix

    • Optional numerical: Numerical<T>

    Returns Matrix<T>

    The randomized matrix

    Static

    Throws

    • If the rows and or columns is less than or equal to 0.

    Example

    const randomMatrix: Matrix<number> = Matrix.random(2, 2);
    console.log(randomMatrix.toString());
    // Output: (will vary due to randomness)
    "45 78"
    "12 67"
  • Reshapes a matrix into a new matrix with the specified number of rows and columns.

    Type Parameters

    • T

    Parameters

    • array: Matrix<T>

      The matrix to reshape

    • newRows: number

      The number of rows in the reshaped matrix.

    • newColumns: number

      The number of columns in the reshaped matrix.

    Returns Matrix<T>

    The reshaped matrix.

    Static

    Throws

    • If the length of the array is not equal to newRows * newColumns.

    Example

    const matrix: Matrix<number> = new Matrix([[1,2,3],[4,5,6]])
    const reshapedMatrix: Matrix<number> = Matrix.reshape(matrix, 3, 2);
    console.log(reshapedMatrix.toString());
    // Output:
    "1 2"
    "3 4"
    "5 6"
  • Reshapes a 1D array into a matrix with the specified number of rows and columns.

    Type Parameters

    • T

    Parameters

    • array: T[]

      The 1D array to reshape.

    • newRows: number

      The number of rows in the reshaped matrix.

    • newColumns: number

      The number of columns in the reshaped matrix.

    • Optional numerical: Numerical<T>

    Returns Matrix<T>

    The reshaped matrix.

    Static

    Throws

    • If the length of the array is not equal to newRows * newColumns.

    Example

    const array: number[] = [1, 2, 3, 4];
    const reshapedMatrix: Matrix<number> = Matrix.reshape(array, 2, 2);
    console.log(reshapedMatrix.toString());
    // Output:
    "1 2"
    "3 4"
  • Rounds values close to zero in the given array and modifies the matrix in place

    Type Parameters

    • T

    Parameters

    • A: Matrix<T>

      Matrix to round

    • threshold: number = Constants.DELTA

    Returns void

    Static

    Example

    const matrix: Matrix<number> = new Matrix([[0.001, 1], [2, 0.0001]]);
    Matrix.roundMatrixToZero(matrix);
    console.log(matrix.toString());
    // Output:
    "0 1"
    "2 0"
  • Rounds all elements of a matrix in place to a specified number of decimal places using a specified base.

    Parameters

    • A: Matrix<number>

      The matrix to round.

    • digits: number

      The number of decimal places to round to.

    • base: number = 10

      The base to use for rounding. Defaults to 10 if not provided.

    Returns void

    Static

    Example

    const matrix: Matrix<number> = new Matrix([[1.2345, 2.3456], [3.4567, 4.5678]]);
    Matrix.toFixedMatrix(matrix, 2);
    console.log(matrix.toString());
    // Output:
    "1.23 2.35"
    "3.46 4.57"
  • Creates a matrix filled with zeros with the specified number of rows and columns.

    Type Parameters

    • T

    Parameters

    • rows: number

      The number of rows in the matrix.

    • columns: number

      The number of columns in the matrix.

    • Optional numerical: Numerical<T>

    Returns Matrix<T>

    • The matrix filled with zero elements.

    Static

    Throws

    • If the rows and or columns is less than or equal to 0.

    Example

    const zerosMatrix: Matrix<number> = Matrix.zeros(2, 2);
    console.log(zerosMatrix.toString());
    // Output:
    "0 0"
    "0 0"

Generated using TypeDoc