Skip to main content

ControlForge Linear Algebra (MAT_*) Guide

James M. Belcher Founder, JMB Technical Services LLC October 2026 | ControlForge v1.0.1819


1. What it is​

Fifteen MAT_* functions do dense matrix math inside a scan: solve a small linear system, fit a calibration curve by least squares, invert a matrix, take eigenvalues of a symmetric one. Uses: a state observer, a sensor calibration, a small optimization.

Three promises make them safe on a controller:

  • The same bits everywhere. Every result comes from one kernel (pkg/linalg). The interpreter and the compute engine get bit-identical answers, and so do x86-64 and arm64 boxes.
  • Failures never fault a task. A call that can fail returns a status code and leaves its output exactly as it was ("hold last"). Your program decides what to do.
  • You know the cost before Download. Validation prices every call from the declared array shapes and tells you the flops per scan.

2. The rules for a matrix​

  • A matrix is an ARRAY of REAL or LREAL with one or two dimensions, each 1 to 64 long. One dimension is a vector.
  • ARRAY[1..3, 1..3] OF LREAL is a 3×3 matrix: rows first, then columns.
  • Use LREAL for numerical work. REAL is accepted, but it carries about 7 significant digits against LREAL's 15–16.
  • A function that returns a matrix (MAT_ADD, MAT_SUB, MAT_MUL, MAT_SCALE, MAT_T) goes on the right of a whole-array assignment: C := MAT_MUL(A, B);. Calls nest: T := MAT_MUL(MAT_T(S), S);.
  • A function that can fail writes its answer into an out argument and returns a DINT status.
  • Shape mistakes (multiplying 3×2 by 3×2, solving with a non-square matrix) are caught at validation, with the same message the scan would give.

3. The functions​

Arithmetic (return a matrix)​

FunctionSignatureWhat it does
MAT_ADDMAT_ADD(A, B) : ARRAYA + B, element by element (equal shapes)
MAT_SUBMAT_SUB(A, B) : ARRAYA − B, element by element (equal shapes)
MAT_MULMAT_MUL(A, B) : ARRAYMatrix product: (m×k)(k×n) → m×n
MAT_SCALEMAT_SCALE(A, s : LREAL) : ARRAYs·A
MAT_TMAT_T(A) : ARRAYTranspose: m×n → n×m

Scalars from a matrix​

FunctionSignatureWhat it does
MAT_DETMAT_DET(A) : LREALDeterminant. No threshold: an exactly singular matrix gives 0.0
MAT_DOTMAT_DOT(x, y) : LREALInner product of two equal-length vectors
MAT_TRACEMAT_TRACE(A) : LREALSum of a square matrix's diagonal
MAT_NORMMAT_NORM(A, kind) : LREAL1 max column sum, 2 or 'FRO' Frobenius (a vector's 2-norm), 'INF' max row sum

Setting a matrix​

FunctionSignatureWhat it does
MAT_EYEMAT_EYE(A) : DINTSets the square array A to the identity; returns 0

Solvers and decompositions (return a status)​

FunctionSignatureWhat it does
MAT_SOLVEMAT_SOLVE(A, b, x) : DINTSolves A·x = b (LU, partial pivoting); A n×n, b and x n-vectors
MAT_INVMAT_INV(A, X) : DINTX := A⁻¹
MAT_LSQMAT_LSQ(A, b, x) : DINTLeast squares: minimizes ‖A·x − b‖ for A m×n with m ≥ n (Householder QR)
MAT_CHOLMAT_CHOL(A, L) : DINTCholesky A = L·Lᵀ, read from A's lower triangle; zeros above L's diagonal
MAT_EIG_SYMMAT_EIG_SYM(A, w, V [, sweeps : INT]) : DINTEigenvalues w (ascending) and eigenvectors V (columns, largest component positive) of a symmetric A, by Jacobi in at most sweeps (default 10, max 30)

Status codes​

Every solver returns one of these. On anything but 0 the out arguments are left exactly as they were.

CodeNameMeaning
0OKThe answer is written
1SINGULARMAT_SOLVE / MAT_INV: the matrix has no inverse
2NOT_POS_DEFMAT_CHOL: the matrix is not positive definite
3NONFINITEAn input held NaN or ±Inf
4NOT_CONVERGEDMAT_EIG_SYM: not converged within sweeps
5RANK_DEFICIENTMAT_LSQ: the columns of A are not independent

4. Examples​

Each of these was run on a live v1.0.1819 runtime; the values shown are what it returned.

Solve a 3×3 system​

PROGRAM P_Solve
VAR
A : ARRAY[1..3,1..3] OF LREAL;
b : ARRAY[1..3] OF LREAL;
x : ARRAY[1..3] OF LREAL;
status : DINT;
det : LREAL;
END_VAR
A[1,1] := 2.0; A[1,2] := 1.0; A[1,3] := -1.0;
A[2,1] := -3.0; A[2,2] := -1.0; A[2,3] := 2.0;
A[3,1] := -2.0; A[3,2] := 1.0; A[3,3] := 2.0;
b[1] := 8.0; b[2] := -11.0; b[3] := -3.0;
det := MAT_DET(A);
status := MAT_SOLVE(A, b, x);
END_PROGRAM

Result: status = 0, x = [2, 3.0000000000000004, -0.9999999999999999] (exactly 2, 3, −1 to within the last bit of an LREAL), det = -0.9999999999999993. Compare with a tolerance, never with =, after any floating-point solve.

Calibrate a sensor by least squares​

Five raw readings against a reference; fit eng = gain * raw + offset. Each row of A is [raw, 1].

PROGRAM P_Calibrate
VAR
A : ARRAY[1..5,1..2] OF LREAL;
y : ARRAY[1..5] OF LREAL;
c : ARRAY[1..2] OF LREAL;
status : DINT;
gain : LREAL;
offset : LREAL;
END_VAR
A[1,1] := 410.0; A[1,2] := 1.0; y[1] := 0.0;
A[2,1] := 1630.0; A[2,2] := 1.0; y[2] := 25.1;
A[3,1] := 2850.0; A[3,2] := 1.0; y[3] := 49.9;
A[4,1] := 4080.0; A[4,2] := 1.0; y[4] := 75.2;
A[5,1] := 5290.0; A[5,2] := 1.0; y[5] := 99.8;
status := MAT_LSQ(A, y, c);
IF status = 0 THEN
gain := c[1];
offset := c[2];
END_IF;
END_PROGRAM

Result: gain = 0.02045050870377128, offset = -8.324850823155696. This matches the closed-form regression to about 15 significant digits.

What a failure looks like: hold last​

PROGRAM P_HoldLast
VAR
A : ARRAY[1..2,1..2] OF LREAL;
b : ARRAY[1..2] OF LREAL;
x : ARRAY[1..2] OF LREAL;
status : DINT;
END_VAR
x[1] := 111.0; x[2] := 222.0; (* the last good answer *)
A[1,1] := 1.0; A[1,2] := 2.0;
A[2,1] := 2.0; A[2,2] := 4.0; (* row 2 = 2 x row 1: singular *)
b[1] := 3.0; b[2] := 6.0;
status := MAT_SOLVE(A, b, x); (* 1 = SINGULAR; x keeps 111, 222 *)
END_PROGRAM

Result: status = 1, x = [111, 222]. The task keeps running. Check the status every scan and decide: hold the last good value, alarm, or switch to a fallback.

5. Cost and the scan​

Validation prints the cost of every call it can type from the declared shapes, and a total per POU:

MAT_SOLVE 3×3: 39 flops (in p_solve)
p_solve: matrix cost 55 flops per scan
  • On a periodic task, if the matrix cost (at the speed measured once per process) is more than a quarter of the task's period, the task's status and compute report carry matrix_budget_note. It is a warning, never a refusal.
  • A call inside a loop is counted once. If you loop over a solve, multiply it yourself.
  • Any single call's order is capped at 64.
  • The functions allocate no memory on a scan.

6. On the compute engine​

A task with compute: true runs MAT_* on the bytecode VM with the same kernels, so the equivalence gate certifies it and its results match the interpreter bit for bit. Two limits:

  • A MAT_NORM whose kind is a variable, a matrix result used anywhere but a whole assignment, or an integer array cannot be compiled. The task then stays on the interpreter, with the reason in its compute report.
  • On the compute engine, a program-local array that a MAT_* call writes is not published to the variable store. Read it through scalars, or keep the array in a GVL if the HMI or API needs it.

7. Not in this version​

SVD, pseudo-inverse, general (non-symmetric) eigenvalues, matrix exponential, condition-number estimates and complex matrices.

See also​

Back to All Guides