Class Matrix

A matrix data structure.

Hierarchy

  • Matrix

Implements

Constructors

  • Creates a new matrix with the specified number of rows and columns.

    Parameters

    • rows: number

      The number of rows in the matrix.

    • cols: number

      The number of columns in the matrix.

    • Optional value: number | ((row, col) => number) | "identity"

      The initial value of the matrix. Can be a number, a function that returns a number, or the string identity.

      If a number is provided, all elements of the matrix will be set to that number.

      If a function is provided, it will be called for each element of the matrix to determine its initial value.

      If identity is provided, the matrix will be initialized as an identity matrix.

    Returns Matrix

    Throws

    An error if the number of rows or columns is less than or equal to 1.

Properties

_data: number[][] = []
cols: number

The number of columns in the matrix.

rows: number

The number of rows in the matrix.

Accessors

  • get diagonal(): number[]
  • Returns an array containing the diagonal elements of the matrix. If the matrix is not square, the diagonal is truncated to the smaller dimension.

    Returns number[]

    An array containing the diagonal elements of the matrix.

  • get hasRoom(): boolean
  • Returns a boolean indicating whether the matrix has room for more elements.

    Returns boolean

    True if the matrix has room for more elements, false otherwise.

  • get isEmpty(): boolean
  • Returns a boolean indicating whether the matrix is empty or not.

    Returns boolean

    True if the matrix is empty, false otherwise.

  • get isFull(): boolean
  • Returns a boolean indicating whether the matrix is full or not.

    Returns boolean

    True if the matrix is full, false otherwise.

  • get items(): number[][]
  • Returns a copy of the matrix data as a two-dimensional array.

    Returns number[][]

    A copy of the matrix data.

  • get meanCols(): number[]
  • Returns an array containing the mean value of each column in the matrix.

    Returns number[]

    An array containing the mean value of each column in the matrix.

  • get meanRows(): number[]
  • Returns an array containing the mean value of each row in the matrix.

    Returns number[]

    An array containing the mean value of each row in the matrix.

  • get sum(): number
  • Returns the sum of all elements in the matrix.

    Returns number

    The sum of all elements in the matrix.

Methods

  • Returns an iterator that yields each row of the matrix.

    Returns Generator<number[], void, unknown>

    An iterator that yields each row of the matrix.

    Yields

    The current row after each iteration.

  • Returns a new matrix that is a clone of the current matrix instance.

    Returns Matrix

    A new matrix that is a clone of the current matrix instance.

  • Concatenates two matrices either horizontally or vertically.

    Parameters

    • mat: Matrix

      The matrix to concatenate with.

    • type: "horizontal" | "vertical" = 'horizontal'

      The type of concatenation to perform. Can be 'horizontal' or 'vertical'. Defaults to 'horizontal'.

    Returns Matrix

    A new matrix that is the result of the concatenation.

    Throws

    An error if the matrices do not have the same number of rows (for horizontal concatenation) or columns (for vertical concatenation).

  • Calculates the determinant of a square matrix.

    Returns number

    The determinant of the matrix.

    Throws

    An error if the matrix is not quadratic.

  • Returns the dot product of the current matrix and the passed matrix.

    Parameters

    • matrix: Matrix | number[][]

      The matrix to multiply with the current matrix.

    Returns Matrix

    A new matrix that is the result of the dot product.

    Throws

    An error if the number of columns of the current matrix is different from the number of rows of the passed matrix.

  • Returns the value at the specified row and column in the matrix.

    Parameters

    • row: number

      The row index of the element to retrieve.

    • col: number

      The column index of the element to retrieve.

    Returns number

    The value at the specified row and column in the matrix.

  • Returns an array containing the elements of the specified column in the matrix.

    Parameters

    • col: number

      The index of the column to retrieve.

    Returns number[]

    An array containing the elements of the specified column.

    Throws

    An error if the passed index exceeds the total number of columns in the matrix.

  • Returns the row at the specified index.

    Parameters

    • row: number

      The index of the row to retrieve.

    Returns number[]

    The row at the specified index.

    Throws

    An error if the passed index exceeds the total number of rows in the matrix.

  • Calculates the inverse of a square matrix.

    Returns Matrix

    The inverse of the matrix.

    Throws

    An error if the matrix is not quadratic.

    Throws

    An error if the matrix not invertible due to the determinant equal to zero.

  • Returns a generator that iterates over the columns of the matrix.

    Returns Generator<number[], void, unknown>

    A generator that yields each column of the matrix.

    Yields

    The current column after each iteration.

  • Returns a generator that iterates over the rows of the matrix.

    Returns Generator<number[], void, unknown>

    A generator that yields each row of the matrix.

    Yields

    The current row after each iteration.

  • Applies a binary operation to each element of the current matrix and another matrix.

    Parameters

    • matrix: Matrix | number[][]

      The matrix to operate with.

    • value: ((left, right) => number)

      The binary operation to apply to each element.

        • (left, right): number
        • Parameters

          • left: number
          • right: number

          Returns number

    Returns number[][]

    A new matrix with the result of the operation.

    Throws

    If the number of columns of the current matrix is different from the number of rows of the passed matrix.

  • Sets the value of a specific cell in the matrix.

    Parameters

    • row: number

      The row index of the cell to set.

    • col: number

      The column index of the cell to set.

    • value: number

      The value to set in the cell.

    Returns number

    The value that was set in the cell.

  • Sets the values of a given column in the matrix.

    Parameters

    • col: number

      The index of the column to set.

    • values: number[]

      An array of values to set in the column.

    Returns Matrix

    The updated matrix.

    Throws

    An error if the passed index exceeds the total number of columns in the matrix.

    Throws

    An error if the passed values exceed the total number of columns in the matrix.

  • Sets the values of a row in the matrix.

    Parameters

    • row: number

      The index of the row to set.

    • values: number[]

      The values to set for the row.

    Returns Matrix

    The updated matrix.

    Throws

    If the passed index exceeds the total number of rows in the matrix or if the passed values exceed the total number of rows in the matrix.

  • Returns a new matrix that is a submatrix of the current matrix with the specified row and column removed.

    Parameters

    • row: number

      The row to remove.

    • col: number

      The column to remove.

    Returns Matrix

    A new matrix that is a submatrix of the current matrix with the specified row and column removed.

  • Swaps two columns in the matrix.

    Parameters

    • col1: number

      The index of the first column to swap.

    • col2: number

      The index of the second column to swap.

    Returns Matrix

    The updated matrix with the swapped columns.

  • Swaps two rows in the matrix.

    Parameters

    • row1: number

      The index of the first row to swap.

    • row2: number

      The index of the second row to swap.

    Returns Matrix

    The updated matrix with the swapped rows.

  • Updates the value at the specified row and column index using the provided update function.

    Parameters

    • row: number

      The row index of the value to update.

    • col: number

      The column index of the value to update.

    • value: ((old) => number)

      The update function that takes the old value as input and returns the new value.

        • (old): number
        • Parameters

          • old: number

          Returns number

    Returns number

    The new value after the update.

  • Creates a new matrix instance from the given parameters.

    Parameters

    • array: number[][]

      The array to use for the new matrix instance.

    Returns Matrix

    A new matrix instance.

    Throws

    An error if the array is empty or not all the columns of the 2D array have the same length.

  • Parameters

    • array: number[]
    • fill: "row" | "col" | "diag"

    Returns Matrix