![]() |
GeMA
The GeMA main application
|
Matrix objects are responsible for providing algebraic operations over matrices and vectors. Common operations like addition, subtraction, multiplication by a scalar and multiplication by other matrices are defined with the usual semantics. Matrix objects also define some basic common operations like the transpose and the determinant. Vector objects are simply a column matrix.
Matrix and vector objects are returned by accessors when querying values for non scalar attributes and also by several other object methods, like shape function evaluation or returning cell node coordinates. New vectors and matrices can also be created by calling the Matrix()
and Vector()
functions.
Example:
Index:
Matrix(nlin, ncol) Matrix(values) | ||
---|---|---|
Description: | Creates a new matrix, with size either explicitly given by its number of lines and columns or implicitly given by the initialization data. | |
Parameters: | nlin, ncol | The number of lines and columns of the matrix. When this set of parameters is given, the matrix will be initialized with zeros. |
values | An initialization table. Can be a table of tables or a single table. In the second case, a matrix with a single column will be created. When this parameter is given, the matrix size will be inferred by the initialization table layout. | |
Returns: | Returns the new matrix object. |
Example:
Vector(nlin) Vector(values) | ||
---|---|---|
Description: | Creates a new vector (column matrix), with size either explicitly given by its number of lines or implicitly given by the initialization data. | |
Parameters: | nlin | The number of vector lines. When this parameter is given, the vector will be initialized with zeros. |
values | An initialization table. When this parameter is given, the vector size will be inferred by the initialization table layout. | |
Returns: | Returns the new vector (column matrix) object. |
Example:
mat:nlin() | |
---|---|
Description: | Returns the number of lines in the matrix or vector. |
Parameters: | None. |
Returns: | Returns the number of lines in the matrix. |
Example:
mat:ncol() | |
---|---|
Description: | Returns the number of columns in the matrix or vector. |
Parameters: | None. |
Returns: | Returns the number of columns in the matrix. |
Example:
mat:size() | |
---|---|
Description: | Returns the number of values (lines * columns) in the matrix or vector. |
Parameters: | None. |
Returns: | Returns the number of values (lines * columns) in the matrix. |
Example:
mat:at(lin, col) vec:at(lin) mat(lin, col) vec(lin) | |
---|---|
Description: | Returns the value of one matrix / vector entry. |
Parameters: | lin, col - The line and column defining the matrix position to be returned. Vectors (column matrices) can be indexed with a line only. As a convenience, the syntax mat(line, column) is a shorthand for mat:at(line, column) , and vec(line) a shorthand for vec:at(line) . IMPORTANT: like any Lua table, line and column indices are indexed from 1 (and not zero). |
Returns: | Returns the requested matrix value. |
Example:
mat:set(lin, col, val) vec:set(lin, val) mat:set(scalarVal) mat:set(valuesTable) | ||
---|---|---|
Description: | Updates the value of one matrix / vector entry for versions taking an index (line and column for matrices, just line for vectors) and a value. Updates the whole matrix contents for versions taking a single value parameter. | |
Parameters: | lin, col | The line and column defining the matrix position to be updated. Vectors (column matrices) can be indexed with a line only. IMPORTANT: like any Lua table, line and column indices are indexed from 1 (and not zero). |
val | The value to be set for versions taking a line, column index. | |
scalarVal | A single value that will be set over the whole matrix / vector, if this is the single set() parameter. | |
valuesTable | A table with new matrix / vector values for all its entries, either given as a table of tables (each sub-table storing one line's values) or as a flat table linearized in column major order (FORTRAN style). The given table must have exactly the same number of values and layout as the matrix object. No matrix resizing is allowed. | |
Returns: | Nothing. |
Example:
mat:col(col) | |
---|---|
Description: | Returns the matrix data in the given column as a column vector. |
Parameters: | col - The column number. |
Returns: | Returns the requested matrix column as a new vector. |
Example:
mat:line(lin) | |
---|---|
Description: | Returns the matrix data in the given line as a row vector (a matrix with one line). |
Parameters: | row - The line number. |
Returns: | Returns the requested matrix line as a new matrix. |
Example:
mat:setCol(col, val) | ||
---|---|---|
Description: | Updates the contents of a matrix column with the values in the given column vector or Lua table. | |
Parameters: | col | The column to be updated. IMPORTANT: like any Lua table, column indices are indexed from 1 (and not zero). |
val | Either a vector object or a Lua table with the new column contents, with size equal to the number of matrix lines. When val is a Lua table, it is usually a flat table, but can also be table of tables representing a column matrix. | |
Returns: | Nothing. |
Example:
mat:setLine(lin, val) | ||
---|---|---|
Description: | Updates the contents of a matrix line with the values in the given row matrix or Lua table. | |
Parameters: | lin | The line to be updated. IMPORTANT: like any Lua table, line indices are indexed from 1 (and not zero). |
val | Either a row matrix object or a Lua table with the new line contents, with size equal to the number of matrix columns. When val is a Lua table, it is usually a flat table, but can also be table of tables representing a row matrix. | |
Returns: | Nothing. |
Example:
Unary - operator | |
---|---|
Description: | The unary minus (- ) operator is applied to each matrix / vector element and returns a new matrix with the signal exchanged for each of its elements. |
Parameters: | A matrix object. |
Returns: | Returns a new matrix object. |
Example:
Binary +, -, / and % operators | |
---|---|
Description: | The standard binary + , - and / operations for matrix / vector objects. They are applied between each corresponding element of the two matrices or between a matrix element and a scalar. The % operator does an element wise multiplication for two matrices (not the standrad matrix multiplication). |
Parameters: | Two parameters, that can be numbers or matrices, with at least one of them being a matrix. If two matrices are given, they must have the same size. |
Returns: | Returns a new matrix object. |
Example:
Binary * operator | |
---|---|
Description: | The standard * operator for matrix and vector objects. When applied between a scalar and a matrix, every matrix element is multipled by the scalar value. When applied between two matrices, the usuall matrix multiplication rules apply. |
Parameters: | Two parameters, that can be numbers or matrices, with at least one of them being a matrix. If two matrices are given, they must have sizes compatible with matrix multiplication rules. |
Returns: | Returns a new matrix object. |
Example:
== and ~= operators | |
---|---|
Description: | The standard == and ~= operators for matrix and vector objects. Two matrices will be considered equal if they have the same size and each individual element in the first matrix is equal to the corresponding element on the second one. IMPORTANT: This operations are meaningfull mostly for matrices with integer values since floating point values are compared without any tolerance. See the mat: equal() method for an alternative approach. |
Parameters: | Two matrix objects. |
Returns: | Returns true or false depending on the matrices being equal or not. |
Example:
mat:equal(otherMat, relTol, absTol) | ||
---|---|---|
Description: | Compares the object matrix with the other given matrix using the equal() function for comparing each matrix number. Two matrices will be considered equal if they have the same size and each individual element in the first matrix compares equal to the corresponding element on the second one. | |
Parameters: | otherMat | The other matrix with which the object matrix will be compared. |
relTol | The relative tolerance used for comparisson. Two numbers a and b are considered equal if their absolute difference is smaller or equal to relTol * max(a, b). Default = 1e-8 (0.000001%). | |
absTol | Absolute tolerance used for comparisson between values close to zero. Default = 1e-12. | |
Returns: | Returns true if the matrices are equal, false if not. |
Example:
mat:t() | |
---|---|
Description: | Returns a new matrix with the transpose of the current matrix. |
Parameters: | None. |
Returns: | Returns the transposed matrix. |
Example:
mat:det() | |
---|---|
Description: | Returns the determinant of a square matrix |
Parameters: | None. |
Returns: | Returns the matrix determinant. |
Example:
vec:dot(otherVec) | |
---|---|
Description: | Returns the dot product between the vector and the given other vector. Vectors must be of the same size. |
Parameters: | The second vector for the dot product. |
Returns: | Returns the dot product value (a scalar number). |
Example:
vec:cross(otherVec) | |
---|---|
Description: | Returns the cross product between the vector and the given other vector. Vectors must have 3 components each. |
Parameters: | The second vector for the cross product. |
Returns: | Returns the cross product value (another column vector with 3 lines). |
Example:
mat:norm() | |
---|---|
Description: | Returns the Euclidian norm of the vector. Not valid for matrix objects. |
Parameters: | None. |
Returns: | Returns the vector norm. |
Example:
mat:toTable() | |
---|---|
Description: | Returns a Lua table serializing the matrix contents using a column major format (FORTRAN style). |
Parameters: | None. |
Returns: | Returns the matrix equivalent Lua table. |
Example:
mat:solve(b) | |
---|---|
Description: | Solves a linear system of equations Ax = b , where A is the current matrix, b is the given vector (or matrix) and x is the returned result. Solved using Armadillo's solve() method. Accepts non square matrices where the number of rows in A is equal to the number of rows in b . |
Parameters: | The b matrix. |
Returns: | Returns the result matrix (vector) or nil if the system could not be solved. |
Example:
mat:inv() | |
---|---|
Description: | Returns the inverse of the current square matrix or nil if the matrix is not invertible. Solved using Armadillo's inv() method. |
Parameters: | None |
Returns: | Returns the inverse matrix or nil if the matrix is not invertible. |
Example:
mat:eigSym() | |
---|---|
Description: | Calculates the eigen values and eigen vectors of a symmetric matrix. Solved using Armadillo's eig_sym() method. |
Parameters: | None. |
Returns: | Returns the eigen values as a column vector and the eigen vectors as a matrix with vectors in columns. |
Example:
mat:print(format) | |
---|---|
Description: | Prints the object matrix. |
Parameters: | Optional string using printf like format (ex: '10.4f'), used to format the printed numbers. |
Returns: | Nothing. |
Example:
mat:toString(format) | |
---|---|
Description: | Serializes the object matrix to a formatted string. |
Parameters: | Optional string using printf like format (ex: '10.4f'), used to format the printed numbers. |
Returns: | The serialized matrix as a string. |
Example: