Classdesc

A TypeScript matrix math library for matrix operations and calculations.

Version

1.0.0

Author

Liron Abutbul (ladunjexa)

License

MIT

See

(Wikipedia)

Hierarchy

  • Matrix

Constructors

  • Creates an instance of matrix with the specified number of rows and columns, initialized with the provided data or with zeroes.

    Parameters

    • Optional rows: number | number[][]

      The number of rows in the matrix.

    • Optional columns: number | number[][]

      The number of columns in the matrix.

    • Optional data: number[][]

      The initial data to populate the matrix (optional).

    Returns Matrix

    Example

    // Initialize a 1x1 matrix with zero
    const matrix = new Matrix();

    // Initialize a 3x3 matrix with zeroes
    const matrix = new Matrix(3, 3);

    // Initialize a 3x3 matrix with provided data
    const matrix = new Matrix(3, 3, data);

    // Initialize a 3x3 matrix with provided data
    const matrix = new Matrix(data);

    // Initialize a nxn matrix with provided data
    const matrix = new Matrix(3, data);

    // Initialize a nxn matrix with zeroes
    const matrix = new Matrix(3);

Properties

_columns: number
_data: number[][]
_rows: number

Accessors

  • get columns(): number
  • Gets the columns of the matrix.

    Returns number

    The number of columns in the matrix.

  • get data(): number[][]
  • Gets the data of the matrix.

    Returns number[][]

    The data of the matrix.

  • get rows(): number
  • Gets the rows of the matrix.

    Returns number

    The number of rows in the matrix.

Methods

  • Private

    Initializes the matrix.

    Parameters

    • rows: number

      The number of rows.

    • columns: number

      The number of columns.

    • data: number[][]

      The data of the matrix.

    Returns void

    Description

    Checks if the specified row and column indices are within the matrix boundaries. If the data is provided, it is used to initialize the matrix. Otherwise, the matrix is initialized with zeroes.

  • Private

    Retrieves the null space of the matrix.

    Parameters

    • row: number

      The row index.

    • column: number

      The column index.

    Returns void

    Description

    Checks if the specified row and column indices are within the matrix boundaries.

    Throws

    Throws an error if the indices are out of bounds.

  • Adds the specified matrix to the current matrix.

    Parameters

    • matrix: Matrix

      The matrix to add.

    Returns Matrix

    Returns the resulting matrix.

    Throws

    Throws an error if the matrix dimensions are not same.

    Example

    const matrix1 = new Matrix(2, 3, [[1, 2, 3], [4, 5, 6]]);
    const matrix2 = new Matrix(2, 3, [[1, 2, 3], [4, 5, 6]]);
    matrix1.add(matrix2); // [[2, 4, 6], [8, 10, 12]]

    See

    https://en.wikipedia.org/wiki/Matrix_addition

  • Retrieves the value at the specified row and column indices.

    Parameters

    • row: number

      The row index.

    • column: number

      The column index.

    Returns number

    Returns the value at the specified indices.

    Example

    const matrix = new Matrix(2, 3, [[1, 2, 3], [4, 5, 6]]);
    matrix.at(0, 0); // 1
    matrix.at(0, 1); // 2
  • Creates a clone of the matrix.

    Returns Matrix

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

    Example

    const matrix = new Matrix(2, 3, [[1, 2, 3], [4, 5, 6]]);
    const clonedMatrix = matrix.clone();
  • Retrieves the column space of the matrix.

    Returns Matrix

    The column space of the matrix.

    Description

    The column space of a matrix is the collection of all linear combinations of its column vectors.

    Example

    const matrix = new Matrix(3, 3, [
    [1, 2, 3],
    [0, 1, 4],
    [5, 6, 0]
    ]);
    matrix.colSpace();
    // => [
    // [1, 0, 5],
    // [2, 1, 6],
    // [3, 4, 0]
    // ]

    See

    https://en.wikipedia.org/wiki/Row_and_column_spaces

  • Calculates the determinant of the matrix.

    Returns number

    Returns the determinant of the matrix.

    Throws

    Throws an error if the matrix is not square.

    Example

    const matrix = new Matrix(3, 3, [[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
    const result = matrix.determinant();
    console.log(result);
    // => 0

    See

    https://en.wikipedia.org/wiki/Determinant

  • Checks if the matrix is equal to another matrix.

    Parameters

    • matrix: Matrix

      The matrix to compare.

    Returns boolean

    Returns true if the matrix is equal to another matrix, false otherwise.

    Example

    const matrix = new Matrix(3, 3, [
    [1, 2, 3],
    [0, 1, 4],
    [5, 6, 0]
    ]);
    const otherMatrix = new Matrix(3, 3, [
    [1, 2, 3],
    [0, 1, 4],
    [5, 6, 0]
    ]);
    matrix.equals(otherMatrix); // true
  • Calculates the cofactor of the matrix for the specified row and column.

    Parameters

    • row: number

      The row index.

    • column: number

      The column index.

    Returns number

    Returns the cofactor of the matrix for the specified row and column.

    Example

    const matrix = new Matrix(3, 3, [[1, 2, 3], [4, 5, 6], [7, 8, 9]]);
    const result = matrix.getCofactor(0, 0);
    console.log(result);
    // => 0

    See

    https://en.wikipedia.org/wiki/Cofactor_(linear_algebra)

  • Calculates the minor of the matrix for the specified row and column.

    Parameters

    • row: number

      The row index.

    • column: number

      The column index.

    Returns Matrix

    Returns the minor matrix.

    Example

    const matrix = new Matrix(3, 3, [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
    ]);
    const result = matrix.getMinor(0, 0);
    console.log(result);
    // => Matrix {
    // rows: 2,
    // columns: 2,
    // data: [
    // [ 5, 6 ],
    // [ 8, 9 ]
    // ]
    // }

    See

    https://en.wikipedia.org/wiki/Minor_(linear_algebra)

  • Inverse the matrix.

    Returns Matrix

    Returns the inverse of the matrix.

    Throws

    Throws an error if the matrix is not square.

    Throws

    Throws an error if the matrix is not invertible.

    Example

    const matrix = new Matrix(3, 3, [
    [1, 2, 3],
    [0, 1, 4],
    [5, 6, 0]
    ]);
    matrix.inverse();
    // => Matrix {
    // rows: 3,
    // columns: 3,
    // data: [
    // [ -24, 18, 5 ],
    // [ 20, -15, -4 ],
    // [ -5, 4, 1 ]
    // ]
    // }

    See

    https://en.wikipedia.org/wiki/Invertible_matrix

  • Checks if the matrix is identity.

    Returns boolean

    Returns true if the matrix is identity, false otherwise.

    Description

    An identity matrix is a square matrix with ones on the main diagonal.

    Example

    const matrix = new Matrix(3, 3, [
    [1, 0, 0],
    [0, 1, 0],
    [0, 0, 1]
    ]);

    matrix.isIdentity(); // true

    See

    https://en.wikipedia.org/wiki/Identity_matrix

  • Checks if the matrix has the same order as the specified matrix.

    Parameters

    • matrix: Matrix

      The matrix to compare against.

    Returns boolean

    Returns true if the matrices have the same order, false otherwise.

    Example

    const matrix1 = new Matrix(2, 3, [[1, 2, 3], [4, 5, 6]]);
    const matrix2 = new Matrix(2, 3, [[7, 8, 9], [10, 11, 12]]);
    const result = matrix1.isSameOrder(matrix2);
    console.log(result);
    // => true

    See

    https://en.wikipedia.org/wiki/Matrix_(mathematics)#Equality_and_inequality

  • Checks if the matrix is square.

    Returns boolean

    Returns true if the matrix is square, false otherwise.

    Example

    const matrix = new Matrix(3, 3, [
    [1, 2, 3],
    [2, 4, 5],
    [3, 5, 6]
    ]);
    matrix.isSquare(); // true

    See

    https://en.wikipedia.org/wiki/Square_matrix

  • Checks if the matrix is symmetric.

    Returns boolean

    Returns true if the matrix is symmetric, false otherwise.

    Example

    const matrix = new Matrix(3, 3, [
    [1, 2, 3],
    [2, 4, 5],
    [3, 5, 6]
    ]);
    matrix.isSymmetric(); // true

    See

    https://en.wikipedia.org/wiki/Symmetric_matrix

  • Applies the modulo operation to each element in the matrix.

    Parameters

    • modulo: number

      The modulo to apply.

    Returns Matrix

    Returns the matrix with the modulo applied.

    Description

    This method performs the modulo operation on each element of the matrix, using the specified modulo value. The modulo operation calculates the remainder when dividing each element by the modulo value.

    Example

    const matrix = new Matrix(2, 2, [
    [1, 2],
    [3, 4]
    ]);
    matrix.modulo(3);
    // => Matrix {
    // rows: 2,
    // columns: 2,
    // data: [
    // [ 1, 2 ],
    // [ 0, 1 ]
    // ]
    // }

    See

    https://en.wikipedia.org/wiki/Modulo_operation

  • Multiplies the current matrix with the specified matrix or scalar. If the specified value is a scalar, it is multiplied with each element of the matrix. If the specified value is a matrix, it is multiplied with the current matrix.

    Parameters

    • matrixOrScalar: number | Matrix

      The matrix or scalar to multiply with.

    Returns Matrix

    Returns the resulting matrix.

    Throws

    Throws an error if the matrix dimensions are not compatible.

    Example

    const matrix1 = new Matrix(2, 3, [[1, 2, 3], [4, 5, 6]]);
    const matrix2 = new Matrix(3, 2, [[1, 2], [3, 4], [5, 6]]);
    matrix1.multiply(matrix2); // [[22, 28], [49, 64]]
    matrix1.multiply(2); // [[2, 4, 6], [8, 10, 12]]

    See

  • Reset the matrix to all zeroes.

    Returns void

    Returns the matrix with all zeroes.

    Example

    const matrix = new Matrix(2, 3, [[1, 2, 3], [4, 5, 6]]);
    matrix.reset();
  • Rotate the matrix by a given angle.

    Parameters

    • angle: number

      The angle to rotate the matrix by, in radians.

    Returns Matrix

    Returns the rotated matrix.

    Throws

    Throws an error if the angle is not a multiple of 90 degrees.

    Example

    const matrix = new Matrix(2, 2, [
    [1, 2],
    [3, 4]
    ]);
    matrix.rotate(Math.PI / 2);

    // => Matrix {
    // rows: 2,
    // columns: 2,
    // data: [
    // [ 3, 1 ],
    // [ 4, 2 ]
    // ]
    // }

    See

    https://en.wikipedia.org/wiki/Rotation_matrix

  • Retrieves the row space of the matrix.

    Returns Matrix

    The row space of the matrix.

    Description

    The row space of a matrix is the collection of all linear combinations of its row vectors.

    Example

    const matrix = new Matrix(3, 3, [
    [1, 2, 3],
    [0, 1, 4],
    [5, 6, 0]
    ]);
    matrix.rowSpace();
    // => [
    // [1, 2, 3],
    // [0, 1, 4],
    // [5, 6, 0]
    // ]

    See

    https://en.wikipedia.org/wiki/Row_and_column_spaces

  • Sets the value at the specified row and column indices.

    Parameters

    • row: number

      The row index.

    • column: number

      The column index.

    • value: number

      The value to set.

    Returns void

    Example

    const matrix = new Matrix(2, 3);
    matrix.set(0, 0, 1);
    matrix.set(0, 1, 2);
  • Sets the matrix as identity matrix.

    Returns void

    Description

    An identity matrix is a square matrix with ones on the main diagonal.

    Throws

    Matrix must be square.

    Example

    const matrix = new Matrix(3, 3);
    matrix.setAsIdentity();
    matrix.data; // [[1, 0, 0], [0, 1, 0], [0, 0, 1]]

    See

    https://en.wikipedia.org/wiki/Identity_matrix

  • Sets the data of the matrix

    Parameters

    • data: number[][]

      The data to set.

    • Optional override: boolean

      Whether to override the dimensions of the matrix.

    Returns void

    Description

    Sets the data of the matrix. If the data provided does not match the dimensions of the matrix, an error will be thrown. If the override flag is set to true, the dimensions of the matrix will be changed to match the data provided.

  • Sets the shape of the matrix and initializes it with zeroes.

    Parameters

    • shape: number[]

      The shape of the matrix.

    Returns void

    Throws

    Invalid shape provided.

  • Gets the shape of the matrix.

    Returns number[]

    The shape of the matrix.

    Example

    const matrix = new Matrix(2, 3);
    matrix.shape(); // [2, 3]
  • Gets the size of the matrix.

    Returns number

    The size of the matrix.

    Example

    const matrix = new Matrix(2, 3);
    matrix.size(); // 6
  • Subtracts the specified matrix from the current matrix.

    Parameters

    • matrix: Matrix

      The matrix to subtract.

    Returns Matrix

    Returns the resulting matrix.

    Throws

    Throws an error if the matrix dimensions are not same.

    Example

    const matrix1 = new Matrix(2, 3, [[1, 2, 3], [4, 5, 6]]);
    const matrix2 = new Matrix(2, 3, [[1, 2, 3], [4, 5, 6]]);
    matrix1.subtract(matrix2); // [[0, 0, 0], [0, 0, 0]]

    See

    https://en.wikipedia.org/wiki/Matrix_addition

  • Calculates the trace of the matrix.

    Returns number

    Returns the trace of the matrix.

    Throws

    Throws an error if the matrix is not square.

    Example

    const matrix = new Matrix(3, 3, [
    [1, 2, 3],
    [4, 5, 6],
    [7, 8, 9]
    ]);

    matrix.trace(); // 15

    See

    https://en.wikipedia.org/wiki/Trace_(linear_algebra)

  • Transposes the matrix.

    Returns Matrix

    Returns the transposed matrix.

    Example

    const matrix = new Matrix(2, 3, [
    [1, 2, 3],
    [4, 5, 6]
    ]);
    const result = matrix.transpose();
    console.log(result);

    // => Matrix {
    // rows: 3,
    // columns: 2,
    // data: [
    // [ 1, 4 ],
    // [ 2, 5 ],
    // [ 3, 6 ]
    // ]
    // }

    See

    https://en.wikipedia.org/wiki/Transpose

Generated using TypeDoc