Matrix operations
Generated by tools/gen_example_pages.py from mnd_scripts/examples/example_matrix_ops.mnd — the output below is a real run, not a transcription. Regenerating the page reruns it.
the integer linear-algebra core — matmul, transpose, scalar_mul, the elementwise family, and the four whole-tensor reductions.
The program#
# ============================================================================
# example_matrix_ops.mnd
# ----------------------------------------------------------------------------
# FUNCTIONALITY: the integer linear-algebra core -- matmul, transpose,
# scalar_mul, the elementwise family, and the four whole-tensor reductions.
#
# Maned's runtime value is an INTEGER tensor: a shape plus row-major int64
# data. There is no float in the execution engine. Accumulation happens in
# int64 (wide enough for INT32 MAC chains), and the dtype/scale that turns
# these back into reals is quantization metadata carried alongside, never
# mixed into the arithmetic. That is what makes a local run and an FPGA run
# bit-identical rather than merely close.
#
# matmul 2 ops inner dimensions must agree
# transpose 1 op swaps the last two axes (rank >= 2)
# scalar_mul 2 ops tensor * scalar
# add sub mul div broadcast: scalar-scalar, same-shape, or
# elemwise_add/sub/mul/div scalar-against-tensor
# tensor_sum / tensor_mean / tensor_max / tensor_min reduce over ALL
# elements
#
# INTEGER SEMANTICS TO KNOW: division truncates toward zero, tensor_mean is an
# integer mean, and division by zero yields 0 rather than trapping.
#
# RUN: ../maned-run examples/example_matrix_ops.mnd
# ============================================================================
mnd::quantmax=1000;
mnd::quantmin=-1000;
mnd::quantres=0;
in::a = [[1,2,3],[4,5,6]]; # 2x3
in::b = [[1,0],[0,1],[1,1]]; # 3x2
in::k = 3;
in::zero = 0;
calc::lambda_flow linalg(a, b, k, zero) {
# --- matrix product: (2x3) * (3x2) -> (2x2) ---------------------------
a b matmul product =
# --- transpose: (2x3) -> (3x2) ----------------------------------------
a transpose a_t =
# (A * B)^T == B^T * A^T -- verified in-language, elementwise.
product transpose lhs =
b transpose b_t =
b_t a_t matmul rhs =
lhs rhs == transpose_identity_holds =
# --- Gram matrix A^T A: always square, symmetric, positive semidefinite
a_t a matmul gram =
# --- scaling ------------------------------------------------------------
a k scalar_mul scaled =
# --- elementwise family, same shape ------------------------------------
a a elemwise_add doubled =
a a elemwise_sub cancelled =
a a elemwise_mul squared_entries =
a a elemwise_div all_ones =
# --- broadcasting a scalar against a tensor ---------------------------
a k add shifted =
a k div integer_divided =
# --- integer honesty: divide by zero yields zero, it does not trap -----
a zero elemwise_div div_by_zero =
# --- whole-tensor reductions ------------------------------------------
a tensor_sum total =
a tensor_mean mean_int = # 21 / 6 -> 3, truncated
a tensor_max largest =
a tensor_min smallest =
} return product, a_t, transpose_identity_holds, gram, scaled,
doubled, cancelled, squared_entries, all_ones,
shifted, integer_divided, div_by_zero,
total, mean_int, largest, smallest;
The run#
$ maned-run mnd_scripts/examples/example_matrix_ops.mnd
flow 'linalg' outputs:
product : [2x2] = [4, 5, 10, 11]
a_t : [3x2] = [1, 4, 2, 5, 3, 6]
transpose_identity_holds : [2x2] = [1, 1, 1, 1]
gram : [3x3] = [17, 22, 27, 22, 29, 36, 27, 36, 45]
scaled : [2x3] = [3, 6, 9, 12, 15, 18]
doubled : [2x3] = [2, 4, 6, 8, 10, 12]
cancelled : [2x3] = [0, 0, 0, 0, 0, 0]
squared_entries : [2x3] = [1, 4, 9, 16, 25, 36]
all_ones : [2x3] = [1, 1, 1, 1, 1, 1]
shifted : [2x3] = [4, 5, 6, 7, 8, 9]
integer_divided : [2x3] = [0, 0, 1, 1, 1, 2]
div_by_zero : [2x3] = [0, 0, 0, 0, 0, 0]
total : [1] = [21]
mean_int : [1] = [3]
largest : [1] = [6]
smallest : [1] = [1]
Next#
- Language guide — every operation the examples use, in the full table.
- RPN syntax specification — the normative rules.