Matrix

Define dense numeric matrices with the math module.

Matrices are a first-class Rain value (typeOf(m) is "matrix"). There is no matrix literal syntax: you build them through benutzen "math" helpers.

Use a nested array with math::fromArray for small explicit matrices. Use zeros, ones, or randomMatrix when you only know the shape. math::shape(m) returns #[rows, cols].

A flat 1D array becomes a column vector: shape (n, 1). Nested rows must all have the same length.

Printing a matrix shows <matrix RxC>, not every cell. For matmul, transpose, save/load, and training helpers, see the **math** module under Modules.

From a nested array

Pass a 2D array of numbers to `math::fromArray`. Outer array = rows, inner arrays = columns.

From a nested array
benutzen "math";

var m = math::fromArray(#[
  #[1, 2],
  #[3, 4]
]);

print m;
print typeOf(m);
print math::shape(m);
output
<matrix 2x2>
matrix
#[2, 2]

Zeros, ones, and shape

Allocate by size when you do not have literal cell values yet.

Zeros, ones, and shape
benutzen "math";

var z = math::zeros(2, 3);
var o = math::ones(2, 2);

print z;
print math::shape(z);
print math::shape(o);
output
<matrix 2x3>
#[2, 3]
#[2, 2]

Column vector from a 1D array

`fromArray` on a flat list builds an `(n, 1)` matrix: handy for labels or bias vectors.

Column vector from a 1D array
benutzen "math";

var v = math::fromArray(#[10, 20, 30]);
print math::shape(v);
output
#[3, 1]

Multiply two matrices

`math::matmul(A, B)` needs `A.cols == B.rows`. Result shape is `(A.rows, B.cols)`.

Multiply two matrices
benutzen "math";

var A = math::fromArray(#[#[1, 2], #[3, 4]]);
var B = math::fromArray(#[#[5, 6], #[7, 8]]);
var C = math::matmul(A, B);

print math::shape(C);
output
#[2, 2]

Ways to define a matrix

All of these live on the `math` module after `benutzen "math"`.

SymbolDescriptionExample
fromArray(arr)2D array → matrix; 1D → column vector `(n, 1)`
benutzen "math"; var m = math::fromArray(#[#[1, 2], #[3, 4]]);
zeros(rows, cols)Matrix filled with 0
benutzen "math"; var z = math::zeros(2, 3);
ones(rows, cols)Matrix filled with 1
benutzen "math"; var o = math::ones(2, 2);
randomMatrix(rows, cols)Xavier-style random init
benutzen "math"; var W = math::randomMatrix(128, 784);
loadMatrix(path)Read a binary matrix file
benutzen "math"; var W = math::loadMatrix("weights/w1.bin");
shape(m)Returns `#[rows, cols]`
benutzen "math"; print math::shape(m);

Notes