CODESYS Math Libraries
Introduction
CODESYS Math Libraries includes the Matrix library and the internal FloatingPointUtils library. The Matrix library supports matrices of arbitrary dimensions and basic mathematical operations on them. The following operations are supported: addition, multiplication, solving linear equations, inversion, and calculation of the determinant.
Product Description
The Matrix library provides a data type to define matrices of arbitrary dimension and functions to perform basic operations on matrices.
Data Types and Functions
Matrices are defined through the mtx.Matrix data type. The mtx.Matrix data type saves a matrix as ARRAY of LREAL. The array is in row-major form.
Basic mathematical operations are provided as functions which have 3 matrices as a VAR_IN_OUT argument: result, source, target. For example, the function for matrix addition has the following interface:
(* Adds two matrices : C := A + B. * A, B, and C must have identical dimensions. * Note: A, B, and C may all be the same matrix. *) FUNCTION AddM : ResultCode VAR_IN_OUT C : Matrix ; (* The result *) A : Matrix ; (* The first summand *) B : Matrix ; (* The second summand *) END_VAR
The library offers following mathematical operations:
Addition of matrices (element-wise):
AddMSubtraction of matrices (element-wise):
SubMMultiplication of matrices (element-wise):
TimesMDivision of matrices (element-wise):
RDivideMScalar multiplication of a matrix:
MultMSMultiplication of matrices:
MultMTransposition of a matrix:
TransposeM
There are also some auxiliary functions for initializing matrices, copying, and accessing elements:
Initialize a matrix with an
ARRAYof values:InitMatrixCopy
ARRAYelements to matrix:CopyElemsCopy matrices with same dimension:
CopyMatrixInitialize as identity matrix:
IdentityMatrixInitialize as zero matrix:
ZeroMatrixRead and write elements:
Elem,SetElem
More complex operations are also provided:
Solve a linear equation
A * X = B:SolveLUInvert a quadratic matrix:
InvertLUCalculate the determinant of a quadratic matrix:
DeterminantLUDetermine an
LUdecomposition (this decomposition serves as the basis for the above three functions):DecomposeLU
Memory Management
The user is responsible for memory management. Matrices will be initialized with a pointer to the memory (via the InitMatrix function). In some cases, it is possible for the user to provide a suitable memory. Furthermore, the auxiliary function blocks MatrixS, ColVectorS, and RowVectorS can be used to initialize matrices with arrays of constant size. They implement the IMatrixAllocator interface which is also available to the user.