Matrix

Reference

class Matrix
module:
static new4x4()

Create new 4x4 identity matrix

Returns:

new matrix

Return type:

Matrix

static new3x3()

Create new 3x3 identity matrix

Returns:

new matrix

Return type:

Matrix

new(rows, columns, value)

Create new matrix

if rows is a table then sets rows as matrix. if rows is a table of structure {1,2,3} then it sets it as a vector matrix. if rows and columns are given and are numbers, returns a matrix with size rowsxcolumns. if num is given then returns a matrix with given size and all values set to num. if rows is given as number and columns is “I”, will return an identity matrix of size rowsxrows.

Parameters:
  • rows (number) – number of rows

  • columns (number) – number of columns

  • value (number) – value to fill

Returns:

new matrix

Return type:

Matrix

static add(m1, m2)

Add two matrices. m2 may be of bigger size than m1

Parameters:
  • m1 (Matrix) – first matrix

  • m2 (Matrix) – second matrix

Returns:

the result

Return type:

Matrix

static sub(m1, m2)

Subtract two matrices. m2 may be of bigger size than m1

Parameters:
  • m1 (Matrix) – first matrix

  • m2 (Matrix) – second matrix

Returns:

the result

Return type:

Matrix

static mul(m1, m2)

Multiply two matrices. m1 columns must be equal to m2 rows

e.g. #m1[1] == #m2

Parameters:
  • m1 (Matrix) – first matrix

  • m2 (Matrix) – second matrix

Returns:

the result

Return type:

Matrix

static div(m1, m2)

Divide two matrices. m1 columns must be equal to m2 rows.

m2 must be square, to be inverted, if that fails returns the rank of m2 as second argument. e.g. #m1[1] == #m2; #m2 == #m2[1]

Parameters:
  • m1 (Matrix) – first matrix

  • m2 (Matrix) – second matrix

Returns:

the result

Return type:

Matrix

static mulnum(m1, num)

Multiply matrix with a number.

num may be of type ‘number’ or ‘complex number’. strings get converted to complex number, if that fails then to symbol

Parameters:
  • m1 (Matrix) – matrix to multiply

  • num (number) – number to multiply with

Returns:

the result

Return type:

Matrix

static divnum(m1, num)

Divide matrix by a number.

num may be of type ‘number’ or ‘complex number’. strings get converted to complex number, if that fails then to symbol.

Parameters:
  • m1 (Matrix) – matrix to divide

  • num (number) – number to divide by

Returns:

the result

Return type:

Matrix

mulVec3(vec)

Multiply matrix with Vector3

Parameters:

vec (Vector3) – the vector to multiply

mulVec4(vec)

Multiply matrix with Vector4

Parameters:

vec (Vector4) – the vector to multiply

static det(m1)

Calculate the determinant of a matrix.

m1 needs to be square. Can calc the det for symbolic matrices up to 3x3 too. The function to calculate matrices bigger 3x3 is quite fast and for matrices of medium size ~(100x100) and average values quite accurate. here we try to get the nearest element to | 1 |, (smallest pivot element) os that usually we have | mtx[i][j]/subdet | > 1 or mtx[i][j]. with complex matrices we use the complex.abs function to check if it is bigger or smaller

Parameters:

m1 (Matrix) – matrix to use

Returns:

determinant

Return type:

number

static invert(m1)

Get the inverted matrix or m1

matrix must be square and not singular on success: returns inverted matrix on failure: returns nil,’rank of matrix’

Parameters:

m1 (Matrix) – matrix to invert

static transpose(m1)

Transpose a matrix

Parameters:

m1 (Matrix) – matrix to transpose

Returns:

new matrix

Return type:

Matrix

Examples