RedcrabX – Matrix & Vector Guide
Matrices, vectors, rotation, quaternions | Version 1.0
Table of Contents
- 1. Matrix Input
- 2. Arithmetic Operations
- 3. Matrix Functions
- 4. Solving Linear Systems
- 5. Vector Functions
- 6. Rotation Matrices
- 7. Quaternions
- 8. Complete Examples
1. Matrix Input
Matrices are entered inside square brackets. Columns are separated by commas, rows are separated by semicolons.
A := [1, 2; 3, 4] // 2×2 matrix B := [1, 2, 3; 4, 5, 6] // 2×3 matrix v := [1; 2; 3] // 3×1 column vector
[1, 2, 3]) is treated as a 1-D array, not a matrix.
Visual Matrix Input
In a MathBox you can also enter matrices visually using the Physics Panel matrix button, which opens a dimension dialog and generates a grid of input fields.
2. Arithmetic Operations
Standard arithmetic operators work element-wise on matrices of the same size.
Matrix multiplication uses the * operator and follows the standard
linear-algebra rule (inner dimensions must match).
| Expression | Description | Example |
|---|---|---|
A + B | Element-wise addition | [1,2;3,4] + [1,0;0,1] → [2,2;3,5] |
A - B | Element-wise subtraction | [5,6;7,8] - [1,2;3,4] → [4,4;4,4] |
A * B | Matrix multiplication | [1,2;3,4] * [1;1] → [3;7] |
A * k | Scalar multiplication | [1,2;3,4] * 2 → [2,4;6,8] |
A / k | Scalar division | [2,4;6,8] / 2 → [1,2;3,4] |
A ^ n | Matrix power (square matrices only) | [1,1;1,0] ^ 5 → Fibonacci matrix |
A := [1, 2; 3, 4] B := [5, 6; 7, 8] A + B = [6, 8; 10, 12] A * B = [19, 22; 43, 50] A * 3 = [3, 6; 9, 12]
3. Matrix Functions
All matrix functions are available in the Physics Panel.
| Function | Description | Requires |
|---|---|---|
det(A) | Determinant | Square matrix |
inv(A) | Inverse matrix | Square, non-singular |
transpose(A) | Transpose (rows ↔ columns) | Any matrix |
trace(A) | Sum of diagonal elements | Square matrix |
rank(A) | Rank of the matrix | Any matrix |
norm(A) | Frobenius norm | Any matrix |
norm2(A) | Spectral norm (largest singular value) | Any matrix |
eigenvalues(A) | Array of real parts of eigenvalues | Square matrix |
chol(A) | Cholesky factor L (A = L·L′) | Square, symmetric, positive definite |
flat(A) | Flatten matrix to 1-D array (row-major) | Any matrix |
join(A,B) | Concatenate two arrays/matrices | Compatible dimensions |
A := [4, 3; 6, 3] det(A) = -6 inv(A) = [-0.5, 0.5; 1, -0.66667] transpose(A) = [4, 6; 3, 3] trace(A) = 7 rank(A) = 2 norm(A) = 8.66025 B := [2, 0; 0, 3] eigenvalues(B) = [2, 3] C := [4, 2; 2, 3] // symmetric positive definite chol(C) = [2, 0; 1, 1.41421]
4. Solving Linear Systems
solve(A, b) solves the system A x = b
for x, where A is a square matrix and b is a vector.
// 2x + 3y = 8 // 5x + y = 7 A := [2, 3; 5, 1] b := [8; 7] x := solve(A, b) = [1, 2] // x=1, y=2 // Verify A * x = [8; 7] // equals b
solve(transpose(A)*A, transpose(A)*b).
5. Vector Functions
1-D arrays are treated as vectors. The following functions operate on them directly.
Unary Vector Functions
| Function | Description |
|---|---|
vnorm(v) / vnorm2(v) | Euclidean (L2) norm – length of the vector |
vnorm1(v) | Manhattan (L1) norm |
vnorminf(v) | Maximum absolute element (infinity norm) |
normalize(v) / unit(v) | Unit vector (v divided by its length) |
vmag(v) / magnitude(v) | Alias for vnorm(v) |
vsum(v) | Sum of all elements (same as sum(v)) |
Binary Vector Functions
| Function | Description | Example |
|---|---|---|
dot(a, b) | Dot product (scalar) | dot([1,2,3],[4,5,6]) → 32 |
cross(a, b) | Cross product (3-D vectors only) | cross([1,0,0],[0,1,0]) → [0,0,1] |
distance(a, b) | Euclidean distance between two points | distance([0,0],[3,4]) → 5 |
angle(a, b) | Angle between two vectors (respects DEG/RAD) | angle([1,0],[0,1]) → 90 (DEG) |
proj(a, b) / project(a, b) | Projection of a onto b | proj([3,4],[1,0]) → [3,0] |
a := [3, 0, 0] b := [0, 4, 0] vnorm(a) = 3 dot(a, b) = 0 cross(a, b) = [0, 0, 12] distance(a, b) = 5 angle(a, b) = 90 // DEG mode normalize([3, 4]) = [0.6, 0.8]
6. Rotation Matrices
Rotation matrices are 3×3 matrices that rotate vectors around a coordinate axis. All functions respect the current angle mode (DEG / RAD). Available in the Physics Panel.
| Function | Description |
|---|---|
rotx(a) | Rotation around the X-axis by angle a |
roty(a) | Rotation around the Y-axis by angle a |
rotz(a) | Rotation around the Z-axis by angle a |
rpy(roll, pitch, yaw) | Combined rotation: Rz(yaw) · Ry(pitch) · Rx(roll) |
// Rotate the vector [1, 0, 0] by 90° around Z (DEG mode) R := rotz(90) v := [1; 0; 0] R * v = [0; 1; 0] // points in Y direction // Combined roll-pitch-yaw rotation M := rpy(0, 0, 45) // yaw 45° det(M) = 1 // rotation matrices have det = 1 // Chain rotations: first X then Z Rx := rotx(30) Rz := rotz(45) Combined := Rz * Rx
rpy uses the ZYX / extrinsic XYZ convention:
R = Rz(yaw) · Ry(pitch) · Rx(roll).
7. Quaternions
Quaternions are represented as 4-element arrays [w, x, y, z].
They are compact representations of 3-D rotations and avoid gimbal lock.
Unary Quaternion Functions
| Function | Description |
|---|---|
qconj(q) / qconjugate(q) | Conjugate: [w, −x, −y, −z] |
qinv(q) / qinverse(q) | Inverse quaternion |
qnorm(q) / qmag(q) | Magnitude (length) of the quaternion |
qnormalize(q) / qunit(q) | Unit quaternion |
qaxis(q) | Rotation axis as 3-D unit vector |
qangle(q) | Rotation angle (respects DEG / RAD) |
qtomatrix(q) | Convert to equivalent 3×3 rotation matrix |
qtoeuler(q) | Convert to Euler angles [roll, pitch, yaw] |
Binary Quaternion Functions
| Function | Description |
|---|---|
qmul(p, q) / qmultiply(p, q) | Quaternion multiplication (composition of rotations) |
qadd(p, q) | Element-wise addition |
qsub(p, q) | Element-wise subtraction |
qdot(p, q) | Dot product of the two quaternions |
// Identity quaternion (no rotation) q0 := [1, 0, 0, 0] qnorm(q0) = 1 // 90° rotation around Z-axis: w=cos(45°), z=sin(45°) q := [0.7071, 0, 0, 0.7071] qangle(q) = 90 // DEG mode qaxis(q) = [0, 0, 1] // Convert to rotation matrix M := qtomatrix(q) det(M) = 1 // Chain two rotations via quaternion multiplication q1 := [0.9239, 0.3827, 0, 0] // 45° around X q2 := [0.7071, 0, 0, 0.7071] // 90° around Z qc := qmul(q2, q1) // first X, then Z qnorm(qc) = 1
qnorm(q) = 1).
Use qnormalize(q) to correct numerical drift after many multiplications.
8. Complete Examples
Example 1 – 2D Transformation
// Rotate the point (3, 1) by 30° around the origin (DEG mode) angle := 30 R := rotz(angle) // Use homogeneous-like approach via 2×2 submatrix c := cos(angle) s := sin(angle) R2 := [c, -s; s, c] p := [3; 1] R2 * p = [2.09808, 2.09808] // rotated point
Example 2 – Solving a 3×3 System
// x + y + z = 6 // 2x + y + 3z = 14 // x + 2y + z = 8 A := [1,1,1; 2,1,3; 1,2,1] b := [6; 14; 8] x := solve(A, b) = [1, 2, 3] // x=1, y=2, z=3 det(A) = -3 // non-zero: unique solution
Example 3 – Eigenvalue Analysis
A := [4, 1; 2, 3] eigenvalues(A) = [5, 2] trace(A) = 7 // = sum of eigenvalues det(A) = 10 // = product of eigenvalues
Example 4 – 3-D Rotation Chain
// Rotate vector v: first 45° around X, then 90° around Z (DEG mode) v := [1; 0; 0] Rx := rotx(45) Rz := rotz(90) result := Rz * Rx * v = [0; 0.7071; 0.7071] // Verify with quaternion approach q1 := qnormalize([cos(22.5), sin(22.5), 0, 0]) q2 := qnormalize([cos(45), 0, 0, sin(45)]) qc := qmul(q2, q1) qtomatrix(qc) * v // same result