LuaSTG-x Core API
math::matrix Class Reference

Public Member Functions

static function __add (local a, local b)
 
static function __concat (local a, local b)
 
static function __div (local a, local b)
 
static function __eq (local a, local b)
 
static function __mul (local a, local b)
 
static function __pow (local a, local b)
 
static function __sub (local a, local b)
 
static function __tostring (local a)
 
static function abs (local a)
 applies abs function to symbol More...
 
static function add (local m1, local m2)
 functions are designed to be light on checks More...
 
static function columns (local mtx)
 
static function concath (local m1, local m2)
 
static function concatv (local m1, local m2)
 
static function copy (local m1)
 local functions to copy matrix values More...
 
static function cross (local m1, local m2)
 
function det ()
 
static function div (local m1, local m2)
 
static function divnum (local m1, local num)
 
function dogauss ()
 locals More...
 
static function elementstostrings (local mtx)
 
static function getelement (local mtx, local i, local j)
 
function gsub (local from, local to)
 Performs string.gsub on symbol. More...
 
function invert ()
 
static function ipairs (local mtx)
 
static function latex (local mtx, local align)
 
static function len (local m1)
 
static function makereplacer (local ...)
 creates function that replaces one letter by something else More...
 
static function mul (local m1, local m2)
 
static function mulnum (local m1, local num)
 
static function new (local o)
 note '...' is always faster than 'arg1,arg2,...' if it can be used More...
 
function new (local rows, local columns, local value)
 access to the metatable we set at the end of the file More...
 
static function normf (local mtx)
 
static function normmax (local mtx)
 
static function pow (local m1, local num)
 
static function print (local ...)
 
static function random (local mtx, local start, local stop, local idp)
 
static function replace (local m1, local func, local ...)
 
static function root (local m1, local root, local iters)
 
static function rotl (local m1)
 
static function rotr (local m1)
 
static function round (local mtx, local idp)
 Functions changing the matrix itself. More...
 
static function rows (local mtx)
 
static function scalar (local m1, local m2)
 a vector is defined as a 3x1 matrix More...
 
static function setelement (local mtx, local i, local j, local value)
 
static function size (local mtx)
 
static function solve (local m1)
 
static function sqrt (local a)
 applies sqrt function to symbol More...
 
static function sqrt (local m1, local iters)
 you have to determin for yourself if the result is sufficent enough for you More...
 
static function sub (local m1, local m2)
 
static function subm (local m1, local i1, local j1, local i2, local j2)
 
static function tostring (local mtx, local formatstr)
 symbol( arg ) More...
 
static function transpose (local m1)
 
function type ()
 Returns "symbol" if object is a symbol type, else nothing. More...
 
static function type (local mtx)
 

Public Attributes

string _NAME
 
string _TYPE
 
string _VERSION
 
table symbol
 

Member Function Documentation

◆ __add()

static function __add ( local  a,
local  b 
)

◆ __concat()

static function __concat ( local  a,
local  b 
)

◆ __div()

static function __div ( local  a,
local  b 
)

◆ __eq()

static function __eq ( local  a,
local  b 
)

◆ __mul()

static function __mul ( local  a,
local  b 
)

◆ __pow()

static function __pow ( local  a,
local  b 
)

◆ __sub()

static function __sub ( local  a,
local  b 
)

◆ __tostring()

static function __tostring ( local  a)

◆ abs()

static function abs ( local  a)

applies abs function to symbol

◆ add()

static function add ( local  m1,
local  m2 
)

functions are designed to be light on checks

matrix ( rows [, comlumns [, value]] )

set __call behaviour of matrix

for matrix( ... ) as matrix.new( ... )so we get Lua errors instead on wrong input

matrix.<functions> should handle any table of structure t[i][j] = value

we always return a matrix with scripts metatable

cause its faster than setmetatable( mtx, getmetatable( input matrix ) )

for real, complex and symbolic matrices //–

note: real and complex matrices may be added, subtracted, etc.

matrix.add ( m1, m2 )

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

◆ columns()

static function columns ( local  mtx)

matrix.columns ( mtx )

return number of columns

◆ concath()

static function concath ( local  m1,
local  m2 
)

matrix.concath( m1, m2 )

Concatenate two matrices, horizontal

will return m1 m2; rows have to be the same

e.g.: #m1 == #m2

◆ concatv()

static function concatv ( local  m1,
local  m2 
)

matrix.concatv ( m1, m2 )

Concatenate two matrices, vertical

will return m1

              m2

columns have to be the same; e.g.: #m1[1] == #m2[1]

◆ copy()

static function copy ( local  m1)

local functions to copy matrix values

matrix.copy ( m1 )

Copy a matrix

simple copy, one can write other functions oneself

◆ cross()

static function cross ( local  m1,
local  m2 
)

matrix.cross ( m1, m2 )

returns the Cross Product of two 3x1 matrices (vectors)

◆ det()

function det ( )

matrix.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

◆ div()

static function div ( local  m1,
local  m2 
)

matrix.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]

◆ divnum()

static function divnum ( local  m1,
local  num 
)

matrix.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

◆ dogauss()

function dogauss ( )

locals

matrix.dogauss ( mtx )

Gauss elimination, Gauss-Jordan Method

this function changes the matrix itself

returns on success: true,

returns on failure: false,'rank of matrix'checking here for the element nearest but not equal to zero (smallest pivot element).

This way the factor in dogauss will be >= 1, which

can give better results. note: in –// ... //– we have a way that does no divison,

however with big number and matrices we get problems since we do no reducing

◆ elementstostrings()

static function elementstostrings ( local  mtx)

matrix.remcomplex ( mtx )

set the matrix elements to strings

IMPROVE: tostring v.s. tostringelements confusing

◆ getelement()

static function getelement ( local  mtx,
local  i,
local  j 
)

matrix.getelement ( mtx, i, j )

return specific element ( row,column )

returns element on success and nil on failure

◆ gsub()

function gsub ( local  from,
local  to 
)

Performs string.gsub on symbol.

for use in matrix.replace

◆ invert()

function invert ( )

matrix.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'

◆ ipairs()

static function ipairs ( local  mtx)

matrix.ipairs ( mtx )

iteration, same for complex

◆ latex()

static function latex ( local  mtx,
local  align 
)

matrix.latex ( mtx [, align] )

LaTeX output

◆ len()

static function len ( local  m1)

matrix.len ( m1 )

returns the Length of a 3x1 matrix (vector)

◆ makereplacer()

static function makereplacer ( local ...  )

creates function that replaces one letter by something else

makereplacer( "a",4,"b",7, ... )(x)

will replace a with 4 and b with 7 in symbol x.

for use in matrix.replace

◆ mul()

static function mul ( local  m1,
local  m2 
)

matrix.mul ( m1, m2 )

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

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

◆ mulnum()

static function mulnum ( local  m1,
local  num 
)

matrix.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

◆ new() [1/2]

static function new ( local  o)

note '...' is always faster than 'arg1,arg2,...' if it can be used

MetaTable

as we declaired on top of the page

local/shared metatable

matrix_meta Set add "+" behaviour Set subtract "-" behaviour Set multiply "*" behaviour Set division "/" behaviour Set unary minus "-" behavior Set power "^" behaviour

if opt is any integer number will do mtx^opt

(returning nil if answer doesn't exist)

if opt is 'T' then it will return the transpose matrix

only for complex:

if opt is '*' then it returns the complex conjugate matrix Set equal "==" behaviour Set tostring "tostring( mtx )" behaviour set __call "mtx( [formatstr] )" behaviour, mtx [, formatstr]

__index handling

access to the symbolic metatable

◆ new() [2/2]

function new ( local  rows,
local  columns,
local  value 
)

access to the metatable we set at the end of the file

matrix:new ( rows [, columns [, value]] )

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

Returns
math.matrix

◆ normf()

static function normf ( local  mtx)

matrix.normf ( mtx )

calculates the Frobenius norm of the matrix.

||mtx||_F = sqrt(SUM_{i,j} |a_{i,j}|^2)

http://en.wikipedia.org/wiki/Frobenius_norm#Frobenius_norm

◆ normmax()

static function normmax ( local  mtx)

matrix.normmax ( mtx )

calculates the max norm of the matrix.

||mtx||_{max} = max{|a_{i,j}|}

Does not work with symbolic matrices

http://en.wikipedia.org/wiki/Frobenius_norm#Max_norm

◆ pow()

static function pow ( local  m1,
local  num 
)

for real and complex matrices only //–

matrix.pow ( m1, num )

Power of matrix; mtx^(num)

num is an integer and may be negative

m1 has to be square

if num is negative and inverting m1 fails

returns the rank of matrix m1 as second argument

◆ print()

static function print ( local ...  )

matrix.print ( mtx [, formatstr] )

print out the matrix, just calls tostring

◆ random()

static function random ( local  mtx,
local  start,
local  stop,
local  idp 
)

matrix.random( mtx [,start] [, stop] [, idip] )

fillmatrix with random values

◆ replace()

static function replace ( local  m1,
local  func,
local ...   
)

matrix.replace (mtx, func, ...)

for each element e in the matrix mtx, replace it with func(mtx, ...).

◆ root()

static function root ( local  m1,
local  root,
local  iters 
)

matrix.root ( m1, root [,iters] )

calculate any root of a matrix

source: http://www.dm.unipi.it/~cortona04/slides/bruno.pdf

m1 and root have to be given;(m1 = matrix, root = number)

conditions same as matrix.sqrt

returns same values as matrix.sqrt

◆ rotl()

static function rotl ( local  m1)

matrix.rotl ( m1 )

Rotate Left, 90 degrees

◆ rotr()

static function rotr ( local  m1)

matrix.rotr ( m1 )

Rotate Right, 90 degrees

◆ round()

static function round ( local  mtx,
local  idp 
)

Functions changing the matrix itself.

matrix.round ( mtx [, idp] )

perform round on elements

◆ rows()

static function rows ( local  mtx)

matrix.rows ( mtx )

return number of rows

◆ scalar()

static function scalar ( local  m1,
local  m2 
)

a vector is defined as a 3x1 matrix

get a vector; vec = matrix{{ 1,2,3 }}^'T'

matrix.scalar ( m1, m2 )

returns the Scalar Product of two 3x1 matrices (vectors)

◆ setelement()

static function setelement ( local  mtx,
local  i,
local  j,
local  value 
)

matrix.setelement( mtx, i, j, value )

set an element ( i, j, value )

returns 1 on success and nil on failure

◆ size()

static function size ( local  mtx)

matrix.size ( mtx )

get matrix size as string rows,columns

◆ solve()

static function solve ( local  m1)

matrix.solve ( m1 )

solve; tries to solve a symbolic matrix to a number

◆ sqrt() [1/2]

static function sqrt ( local  a)

applies sqrt function to symbol

◆ sqrt() [2/2]

static function sqrt ( local  m1,
local  iters 
)

you have to determin for yourself if the result is sufficent enough for you

matrix.sqrt ( m1 [,iters] )

calculate the square root of a matrix using "Denman Beavers square root iteration"

condition: matrix rows == matrix columns; must have a invers matrix and a square root

if called without additional arguments, the function finds the first nearest square root to

input matrix, there are others but the error between them is very small

if called with agument iters, the function will return the matrix by number of iterations

the script returns:local average error square root function

◆ sub()

static function sub ( local  m1,
local  m2 
)

matrix.sub ( m1 ,m2 )

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

◆ subm()

static function subm ( local  m1,
local  i1,
local  j1,
local  i2,
local  j2 
)

matrix.subm ( m1, i1, j1, i2, j2 )

Submatrix out of a matrix

input: i1,j1,i2,j2

i1,j1 are the start element

i2,j2 are the end element

condition: i1,j1,i2,j2 are elements of the matrix

◆ tostring()

static function tostring ( local  e,
local  fstr 
)

symbol( arg )

matrix.tostring ( mtx, formatstr )

tostring function

same as symbol.to( arg )

set __call behaviour of symbol Converts object to string, optionally with formatting.

◆ transpose()

static function transpose ( local  m1)

matrix.transpose ( m1 )

Transpose a matrix

switch rows and columns

◆ type() [1/2]

function type ( )

Returns "symbol" if object is a symbol type, else nothing.

◆ type() [2/2]

static function type ( local  mtx)

matrix.type ( mtx )

get type of matrix, normal/complex/symbol or tensor

Member Data Documentation

◆ _NAME

string _NAME

◆ _TYPE

string _TYPE

◆ _VERSION

string _VERSION

◆ symbol

table symbol

The documentation for this class was generated from the following file: