Module matrix

A basic matrix type and common matrix operations.

This may be useful when working with linear algebra, transformations, and mathematical computations.

An introduction to matrices can be found on Wikipedia.

Constructors

new (rows, columns, func) Constructs a new matrix of rows by columns, filling it using the provided function or scalar.
from2DArray (arr) Constructs a matrix from a 2D array (table of tables).
fromVector (v, row) Constructs a matrix from a Vector, as either a row or column matrix.
fromQuaternion (q) Constructs a rotation matrix from a quaternion.
identity (rows, columns) Constructs an identity matrix of given dimensions.

Class Matrix

matrix.rows The number of rows in the matrix.
matrix.columns The number of columns in the matrix.
matrix:add (self, other) Adds two matrices together, or adds a scalar to all elements.
matrix:sub (self, other) Subtracts two matrices, or subtracts a scalar from all elements.
matrix:mul (self, other) Multiplies a matrix by a scalar or performs matrix multiplication.
matrix:div (self, other) Divides a matrix by a scalar or another matrix.
matrix:unm (self) Negates all elements in a matrix.
matrix:pow (self, n) Raises a square matrix to a non-negative integer power.
matrix:length (self) Computes the total number of elements in the matrix.
matrix:tostring (self) Creates a string representation of the matrix.
matrix:equals (self, other) Determines if two matrices are equal.
matrix:minor (self, row, column) Computes the minor matrix by removing a specified row and column.
matrix:determinant (self) Computes the determinant of a square matrix.
matrix:transpose (self) Computes the transpose of the matrix (rows become columns).
matrix:cofactor (self) Computes the cofactor matrix.
matrix:adjugate (self) Computes the adjugate (adjoint) matrix.
matrix:inverse (self) Computes the inverse of a square matrix.
matrix:trace (self) Computes the trace (sum of diagonal elements) of a square matrix.
matrix:rank (self) Computes the rank of the matrix using row reduction.
matrix:frobenius_norm (self) Computes the Frobenius norm (square root of sum of squared elements).
matrix:max_norm (self) Computes the max norm (maximum absolute value of any element).
matrix:hadamard_product (self, other) Computes the Hadamard product (element-wise multiplication).
matrix:elementwise_div (self, other) Computes element-wise division.
matrix:is_symmetric (self) Checks if the matrix is symmetric.
matrix:is_diagonal (self) Checks if the matrix is diagonal.
matrix:is_identity (self) Checks if the matrix is an identity matrix.
matrix:clone (self) Returns a copy of this matrix, with the same data.


Constructors

new (rows, columns, func)
Constructs a new matrix of rows by columns, filling it using the provided function or scalar.

Usage:

  • m = matrix.new(3, 3, function(r, c) return r + c end)
  • m = matrix.new(2, 4, 5) -- fills all elements with 5
  • m = matrix.new(2, 2) -- fills all elements with 1
from2DArray (arr)
Constructs a matrix from a 2D array (table of tables).

Usage:

    m = matrix.from2DArray({{1, 2}, {3, 4}})
fromVector (v, row)
Constructs a matrix from a Vector, as either a row or column matrix.

Usage:

  • m = matrix.fromVector(vector.new(1, 2, 3), true) -- row matrix
  • m = matrix.fromVector(vector.new(1, 2, 3), false) -- column matrix
fromQuaternion (q)
Constructs a rotation matrix from a quaternion.

See also:

Usage:

    m = matrix.fromQuaternion(quaternion.new(1, vector.new(0, 0, 0)))
identity (rows, columns)
Constructs an identity matrix of given dimensions.

Usage:

    m = matrix.identity(3, 3)

Class Matrix

A matrix, with dimensions rows x columns.

This is suitable for representing linear transformations, systems of equations, and general numerical computations.

matrix.rows
The number of rows in the matrix.
matrix.columns
The number of columns in the matrix.
matrix:add (self, other)
Adds two matrices together, or adds a scalar to all elements. Supports broadcasting with row vectors and column vectors.

Usage:

  • m1:add(m2)
  • m1 + m2
  • m + 5
matrix:sub (self, other)
Subtracts two matrices, or subtracts a scalar from all elements.

Usage:

  • m1:sub(m2)
  • m1 - m2
matrix:mul (self, other)
Multiplies a matrix by a scalar or performs matrix multiplication.

Usage:

  • m:mul(3)
  • m * 3
  • m1:mul(m2)
  • m1 * m2
matrix:div (self, other)
Divides a matrix by a scalar or another matrix.

Usage:

  • m:div(2)
  • m / 2
  • m1:div(m2)
  • m1 / m2
matrix:unm (self)
Negates all elements in a matrix.

Usage:

  • m:unm()
  • -m
matrix:pow (self, n)
Raises a square matrix to a non-negative integer power.

Usage:

  • m:pow(3)
  • m ^ 3
matrix:length (self)
Computes the total number of elements in the matrix.

Usage:

  • m:length()
  • #m
matrix:tostring (self)
Creates a string representation of the matrix.

Usage:

  • m:tostring()
  • m .. ""
matrix:equals (self, other)
Determines if two matrices are equal.

Usage:

  • m1:equals(m2)
  • m1 == m2
matrix:minor (self, row, column)
Computes the minor matrix by removing a specified row and column.

Usage:

    m:minor(1, 2)
matrix:determinant (self)
Computes the determinant of a square matrix.

Usage:

    m:determinant()
matrix:transpose (self)
Computes the transpose of the matrix (rows become columns).

Usage:

    m:transpose()
matrix:cofactor (self)
Computes the cofactor matrix.

Usage:

    m:cofactor()
matrix:adjugate (self)
Computes the adjugate (adjoint) matrix.

Usage:

    m:adjugate()
matrix:inverse (self)
Computes the inverse of a square matrix.

Usage:

    m:inverse()
matrix:trace (self)
Computes the trace (sum of diagonal elements) of a square matrix.

Usage:

    m:trace()
matrix:rank (self)
Computes the rank of the matrix using row reduction.

Usage:

    m:rank()
matrix:frobenius_norm (self)
Computes the Frobenius norm (square root of sum of squared elements).

Usage:

    m:frobenius_norm()
matrix:max_norm (self)
Computes the max norm (maximum absolute value of any element).

Usage:

    m:max_norm()
matrix:hadamard_product (self, other)
Computes the Hadamard product (element-wise multiplication).

Usage:

    m1:hadamard_product(m2)
matrix:elementwise_div (self, other)
Computes element-wise division.

Usage:

    m1:elementwise_div(m2)
matrix:is_symmetric (self)
Checks if the matrix is symmetric.

Usage:

    m:is_symmetric()
matrix:is_diagonal (self)
Checks if the matrix is diagonal.

Usage:

    m:is_diagonal()
matrix:is_identity (self)
Checks if the matrix is an identity matrix.

Usage:

    m:is_identity()
matrix:clone (self)
Returns a copy of this matrix, with the same data.

Usage:

    m2 = m1:clone()
generated by LDoc 1.5.0 Last updated 2025-11-21 05:09:11