The Vector, Matrix, VectorInt, and MatrixInt value types overload operators for natural math syntax. The set differs between the floating-point types (Vector/Matrix) and the integer types (VectorInt/MatrixInt).
Supported operators:
| Category | Vector / Matrix (float) | VectorInt / MatrixInt (integer) |
|---|---|---|
Arithmetic + - * / | element-wise; / is real division | element-wise; / is integer division |
Unary minus - | yes | yes |
Comparison = <> < <= > >= | yes (returns Boolean) | yes (returns Boolean) |
Bitwise / logical and or xor not | no | yes |
Shift shl shr | no | yes |
| Scalar operand | double and complex TCplx | integer |
| Pool-managed operand | TVec / TMtx / TMtxVec | TVecInt / TMtxInt / TMtxVecInt |
Key points:
*is element-wise on every value type — it is not matrix multiplication. For a matrix product use theMulmethod onTMtx.- Every binary operator also has scalar forms (
a * 2.0,iv shl 1) and cross-type forms (Matrix * Vector,VectorInt + TVecInt). - Bitwise, logical, and shift operators exist only on the integer types.
In C# these map to == != < <= > >= for comparison and & | ^ ~ << >> for the integer bitwise/logical/shift operators.
How expressions work internally:
The expression c := a + b * d is evaluated as:
b * d→ creates temporary, stores resulta + temp→ creates temporary, stores result- Assignment
c :=→ shallow copies the result to c
Each intermediate result allocates a temporary vector from the object cache. The temporaries are returned to the cache when no longer referenced.
The copy in step 3 is shallow: each value type holds a single reference to its internal object (Vector wraps TVec, Matrix wraps TMtx, VectorInt wraps TVecInt, MatrixInt wraps TMtxInt), and assignment copies that reference — the element data itself is never duplicated.
When to use operators vs methods:
| Use case | Prefer | Reason |
|---|---|---|
| Simple expressions (2-3 ops) | Operators | Readable, temporaries are cheap |
| Complex expressions (4+ ops) | Methods | Control allocation, use compound ops |
| Performance-critical loops | Methods | Zero temporaries, use in-place forms |
| Block processing | Methods | Required for BlockInit/BlockNext pattern |
Example — operators vs methods:
// Operator syntax (readable, allocates temporaries)
Vector c = (a + b) * d;
// Method syntax (no temporaries, faster)
c.Add(a, b); // c = a + b
c.Mul(d); // c = c * d
// Even better — compound operation (single pass)
c.AddAndMul(a, b, d); // c = (a + b) * d
Mixed scalar/vector expressions:
Vector c = a * 2.0 + b; // scalar multiply then add
Vector c = a + Math387.Cplx(1, 2); // complex scalar add
Important: Operators are only available on value types (Vector, Matrix, VectorInt, MatrixInt). Pool-managed types (TVec, TMtx) do not support operators — use methods.