# Matrices

It is important to note that the calls that are referenced in this section are made using the standard matrix tools, but can alternatively be made using the matrixf tools to utilize the floating point library. The float versions of the operations are more computationally efficient, but less accurate.

## Constructors

• Create a matrix from a two-dimensional Lua table
• d = matrix({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
• Create a matrix from a one-dimensional Lua table
• d = matrix(3, {1, 2, 3, 4, 5, 6, 7, 8, 9});
• Create a matrix with all elements initialized to the given value
• d = matrix(3, 3, 0.5);
• Create a matrix with all elements initialized to zero
• d = matrix(3, 3);
• Create an identity matrix of the order specified
• d = matrix.eye(3);
• Create a matrix using a delegate to initialise elements
• d = matrix(3, 3, function (i,j) return i+(2*j); end);
• Create a sparse matrix from a dense Lua table
• d = matrix.sparse({{0, 0, 0}, {0, 1, 0}, {5, 0, 0}});
• Create a sparse matrix from a Lua table using {[row]={[col]=#, ... }, ... }, e.g. to create {{1,0,0}, {0,0,0}, {0,5,7}} do
• d = matrix.sparse(3, 3, {{1}, ={=5, =7}});
• Create a diagonal matrix given a Lua table, e.g. to create {{1,0,0}, {0,2,0}, {0,0,1}} do
• d = matrix.diagonal({1, 2, 1})
• Create a diagonal matrix given a Lua table, e.g. to create {{1,0,0,0}, {0,2,0,0}, {0,0,1,0}} do
• d = matrix.diagonal(3, 4, {1, 2, 1})
• Create a diagonal matrix with the diagonal initialized to the given value, e.g. to create {{5,0,0,0}, {0,5,0,0}, {0,0,5,0}} do
• d = matrix.diagonal(3, 4, 5);
• Create a dense matrix from a one-dimensional Lua table
• d = matrix.dense(3, {1, 2, 3, 4, 5, 6, 7, 8, 9});
• Create a dense matrix from a two-dimensional Lua table
• d = matrix.dense({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
• Create a dense matrix using a delegate to initialize the elements
• d = matrix.dense(3, 3, function (i,j) return 3*i+j; end);
• Create a dense matrix with all elements initialized to the give value
• d = matrix.dense(3, 3, 0.5);
• Create a dense matrix with all elements initialized to zero
• d = matrix.dense(3, 3);
• Create a double-precision sparse matrix using a delegate to initialize the elements
• d = matrix.dense(3, 3, function (i,j) return 3*i+j; end);
• Create a random matrix
• d = matrix.random(3, 3);
• Create a random matrix with the specified size, minimum and maximum values, and random seed
• d = matrix.random(3, 3, 0, 1, 1234)
• Create a random matrix with the specified size, uniformly distributed between the minimum and maximum values
• d = matrix.random(3, 3, 0, 10)

## Submatrices

• Return the submatrix starting at the given row and column and including the rest of the rows and columns
• new = d:submatrix(row, column)
• Return the submatrix starting at the given row and column and including the given number of rows and columns
• new = d:submatrix(startRow, numRows, startColumn, numColumns)
• Modify current matrix by setting submatrix starting at the given row and column, and including the rest of the rows and columns, to new value
• d:submatrix(row, column, value)
• Modify current matrix by setting submatrix starting at the given row and column, and including the given number of rows and columns, to new value
• d:submatrix(startRow, numRows, startColumn, numColumns, value)

## Miscellaneous

• Access number of rows/columns
• numR = d.rowCount
• numC = d.columnCount
• Set the value in the specified row and column
• d:at(1, 1, 8.53)
• Return number of elements in specified row
• n = d.count
• Clone a matrix
• m = d:clone()
• Clone the specified row as a vector
• v = d:clone()
• Return a matrix whose row and column elements have been cycled by the given integers, e.g. for d = {{1,2}, {3,4}, {5,6}}, set m = {{4,3}, {6,5}, {2,1}} using
• m = d:cycle(1,1)
• Convert a matrix to a matrixf
• m = d:toFloat()
• Convert a matrixf to a matrix
• m = d:toDouble()

## Math

• Return a matrix of the ceiling of each element
• m = d:ceiling()
• Return a matrix of the floor of each element
• m = d:floor()
• Return the condition number of a matrix
• n = d:cond()
• Return the determinant of a matrix
• n = d:det()
• Return the power of a matrix
• m = d:pow(2)
• Return the trace of a matrix
• n = d:trace()
• Return the transpose of a matrix
• m = d:transpose()
• Return the infinity-norm of a matrix
• n = d:normInf()
• Return the L1-norm of a matrix
• n = d:normL1()
• Return the L2-norm of a matrix
• n = d:normL2()
• Return the point-wise division of each element (d by c)
• m = d:pdiv(c)
• Return the point-wise exponent of each element
• m = d:pexp()
• Return the point-wise logarithm of each element
• m = d:plog()
• Return the point-wise modulus of each element (d by c)
• m = d:pmod(c)
• Return the point-wise multiplication of each element (d and c)
• m = d:pmul(c)
• Return each matrix element to the specified exponent
• m = d:ppow(2)
• Return the point-wise remainder of each element (d by c)
• m = d:prem(c)
• Return the point-wise square root of each element
• m = d:psqrt()
• Return the point-wise trigonometric function result of each matrix element
• m = d:psin()
• m = d:pcos()
• m = d:ptan()
• m = d:pasin()
• m = d:pacos()
• m = d:patan()
• m = d:psinh()
• m = d:pcosh()
• m = d:ptanh()

## Comparison

• Return a matrix of 1's and 0's for the less-than comparison (d < e, where e is a matrix or a number)
• m = d:less(e)
• Return a matrix of 1's and 0's for the less-than or equal to comparison (d <= e, where e is a matrix or a number)
• m = d:lessOrEqual(e)
• Return a matrix of 1's and 0's for the greater-than comparison (d > e, where e is a matrix or a number)
• m = d:greater(e)
• Return a matrix of 1's and 0's for the greater-than or equal to comparison (d >= e, where e is a matrix or a number)
• m = d:greaterOrEqual(e)
• Return a matrix of 1's, -1's and 0's denoting the sign of each matrix element
• m = d:sign()

## Indexing

• local var = d;
• ans = d.at(1,2);

## Operators

• Equality comparison (==)
• Inequality comparison (!=)
• Addition (+)
• Including the addition of matrices and scalars
• Subtraction (-)
• Including the subtraction of matrices and scalars, and negation
• Multiplication (*)
• Including multiplication of matrices and vectors, and scalars
• Division of a matrix and a scalar (/)
• Modulus (%)
• Including the modulus of matrices and scalars