CJC Demo 1: Matrix Multiplication

Deterministic tensor operations with the full lex → parse → eval pipeline
CJC Language All Assertions Passed

What This Proves

  • Tensor runtime — multi-dimensional array creation, indexing, arithmetic
  • Matrix multiplication — correct A(2×3) @ B(3×2) = C(2×2)
  • nogc-safe buffers — tensors live in Layer 1 (no garbage collector)
  • Reproducibility — deterministic results, verifiable element-by-element
  • Full pipeline — source → lexer → parser → AST → interpreter → output
CJC Source Code — demo1_matmul.cjc
// Build two matrices from literal data
let a = Tensor.from_vec(
    [1.0, 2.0, 3.0,
     4.0, 5.0, 6.0],
    [2, 3]
);

let b = Tensor.from_vec(
    [7.0,  8.0,
     9.0,  10.0,
     11.0, 12.0],
    [3, 2]
);

// Matrix multiply: C = A @ B
let c = matmul(a, b);

// Verify every element
assert_eq(c.get([0, 0]), 58.0);
assert_eq(c.get([0, 1]), 64.0);
assert_eq(c.get([1, 0]), 139.0);
assert_eq(c.get([1, 1]), 154.0);

print("All assertions passed!");
print("Sum of C:", c.sum());
print("Mean of C:", c.mean());
Live Output — $ cjc run demo1_matmul.cjc
Matrix A (2x3):
Tensor(shape=[2, 3],
  data=[1.0, 2.0, 3.0,
        4.0, 5.0, 6.0])

Matrix B (3x2):
Tensor(shape=[3, 2],
  data=[7.0, 8.0,
        9.0, 10.0,
        11.0, 12.0])

C = A @ B (2x2):
Tensor(shape=[2, 2],
  data=[58.0, 64.0,
        139.0, 154.0])

Expected:
Tensor(shape=[2, 2],
  data=[58.0, 64.0,
        139.0, 154.0])

All assertions passed!
Shape of C: [2, 2]
Sum of C: 415
Mean of C: 103.75

The Math

A @ B = | 1 2 3 | | 7 8 | | 1*7+2*9+3*11 1*8+2*10+3*12 | | 58 64 | | 4 5 6 | x | 9 10 | = | 4*7+5*9+6*11 4*8+5*10+6*12 | = | 139 154 | |11 12 |