Basics
| Command | Description |
|---|---|
x = 1 | Assign a variable. |
local x = 1 | Declare a local variable. Prefer this in scripts. |
print(x) | Print a value. |
printf("x = %g\n", x) | Formatted printing. |
-- comment | Single-line comment. |
--[[ comment block ]] | Block comment. |
require "a_module" | Load a module. |
Operators
| Operation | MAD-NG |
|---|---|
| Addition, subtraction | a + b a - b |
| Multiplication, division | a * b a / b |
| Power | a^2 |
| Modulo | a % b |
| String concatenation | "MAD".."-".."NG" |
| Length | #A |
| Equality / inequality | a == b a ~= b |
| Logical operators | not a a and b a or b |
The meaning of operators depends on the type. For example, A * B is algebraic multiplication for matrices, while A^2 is not an element-wise square unless the type defines it that way.
Data Types
| Type | Example |
|---|---|
| Number | x = 1.25 |
| Boolean | b = true c = false |
| String | s = "MAD-NG" |
| Table | t = {1, 2, 3} |
| Keyed table | t = {x = 1, y = 2} |
| Vector | v = vector {1, 2, 3} |
| Matrix | A = matrix {{1, 2}, {3, 4}} |
| Range | r = range(1, 10, 2) |
| Lambda Function | f = \x -> x^2 |
Ranges
| Command | Description |
|---|---|
range(1, 10) | Integer range from 1 to 10. |
range(1, 10, 2) | Integer range with step 2. |
nrange(0, 1, 11) | Numerical range object with 11 values. |
linspace(0, 1, 11) | Linearly spaced vector with 11 values. |
nrange creates a range object; linspace creates a vector.
Creating Vectors
| Command | Description |
|---|---|
A = vector {1, 2, 3} | Column vector. |
A = vector(3):fill {1, 2, 3} | Column vector filled from values. |
A = matrix {{1, 2, 3}} | Row vector as a 1 × n matrix. |
A = vector {1, 2, 3}:t 'in' | Row vector by in-place transpose, avoiding a temporary. |
A = {1, 2, 3} | Plain Lua table. Use vector(A) for algebra. |
A = linspace(1, 5, k) | Linearly spaced vector. |
Vectors are internally represented as matrices with shape n × 1. Row vectors are therefore represented as 1 × n matrices.
Creating Matrices
| Command | Description |
|---|---|
A = matrix {{1, 2}, {3, 4}} | Create a matrix from nested tables. |
A = matrix(2, 3) | Create a 2 × 3 matrix. |
A = matrix(2) | Create a 2 × 2 square matrix. |
A = cmatrix(2) | Create a 2 × 2 complex matrix. |
A = imatrix(2) | Create a 2 × 2 integer matrix. |
A = matrix(2, 3):ones() | 2 x 3 matrix of ones. |
A = matrix(3):eye() | 3 x 3 identity matrix. |
A = matrix(3):seq() | 3 x 3 matrix filled with a sequence 1.. |
A = matrix(2):random() | Matrix 2 x 2 filled with random values in [0,1). |
B = A:same() | Create a matrix with the same type and sizes. |
B = A:same(3, 4) | Create same-type matrix with different sizes. |
Newly created matrices are always initialized with zeros.
Indexing and Shape
| Command | Description |
|---|---|
nr, nc = A:sizes() | Number of rows and columns. |
A:get(2, 1) | Get element at row 2, column 1. |
A:set(2, 1, x) | Set element at row 2, column 1. |
A:getrow(2) | Extract row 2. |
A:getcol(1) | Extract column 1. |
A:getrow(range(1, 3)) | Extract rows 1 to 3. |
A:getcol(range(1, 3)) | Extract columns 1 to 3. |
A:remrow(3) | Remove row 3. |
A:reshape(3, 2) | Reshape a matrix. |
A:vec() | Vectorize a matrix. |
A:t() | Transpose. |
A:t(false) | Non-Hermitian transpose (without complex conjugation). |
Generic Math
| Command | Description |
|---|---|
sqrt(x) | Square root. |
exp(x) | Exponential. |
log(x) | Natural logarithm. |
sin(x), cos(x), tan(x) | Trigonometric functions. |
asin(x), acos(x), atan(x) | Inverse trigonometric functions. |
sinh(x), cosh(x), tanh(x) | Hyperbolic functions. |
abs(x), sign(x) | Absolute value and sign. |
real(z), imag(z), conj(z) | Complex-number helpers. |
rect(z), polar(z) | Complex-number rectangular and polar conversion. |
cabs(z), carg(z) | Complex-number magnitude and argument. |
floor(x), ceil(x), round(x) | Rounding functions. |
Generic math functions are important in MAD-NG because the same expression can apply to numbers, matrices, TPSA objects, DA maps, or other algebraic objects when supported.
Programming
| Command | Description |
|---|---|
if c then ... elseif d then ... else ... end | Conditional execution. |
for i=1,n do ... end | Numeric loop. |
for i=1,n,2 do ... end | Numeric loop with step 2. |
while c do ... end | While loop. |
for k,v in pairs(t) do ... end | Iterate over table key/value pairs. |
for i,v in ipairs(t) do ... end | Iterate over array-like table entries. |
function f(x) return x^2 end | Define a named function. |
f = function(x) return x^2 end | Function expression. |
f = \x -> x^2 | Lambda expression. |
f = \x,y -> x + y | Lambda with several arguments. |
f = \x,y -> (y, x) | Lambda returning several values. |
Tables and Objects
| Command | Description |
|---|---|
t = {1, 2, 3} | Array-like table. |
t = {x = 1, y = 2} | Dictionary-like table. |
t[1] | First array entry. |
t.x | Named field access. |
t["x"] | Equivalent named field access. |
t[#t+1] = x | Append to array part. |
obj:method(x) | Method call. Equivalent to obj.method(obj, x). |
Strings
| Command | Description |
|---|---|
s = "MAD-NG" | Create a string. |
s = 'MAD-NG' | Alternative string literal. |
s = [[multi line]] | Long string literal. |
#s | String length. |
s1 .. s2 | String concatenation. |
string.format("%g", x) | Formatted string. |
Input / Output
| Command | Description |
|---|---|
io.write(x) | Print string or number. |
print(x) | Print value. |
printf("x = %g\n", x) | Formatted output. |
A:print() | Print object using its own method. |
A:print("label") | Print object with a label, where supported. |
Common Pitfalls
| Pitfall | Correction |
|---|---|
| Using Python power syntax. | Use x^2, not x**2. |
| Assuming zero-based indexing. | MAD-NG follows Lua-style one-based indexing conventions. |
| Assuming element-wise vectorization. | Be explicit. Matrix/vector operations are algebraic unless a method defines element-wise behavior. |
| Using tables for algebra. | Convert plain tables with vector(A) or matrix(A). |
| Creating unnecessary temporaries. | Use in-place forms such as A:t 'in' when appropriate. |