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