Files
rose-ash/lib/ocaml/baseline/mat_mul.ml
giles bafa2410e4
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
ocaml: phase 5.1 mat_mul.ml baseline (3x3 row-major matrix multiply, sum = 621)
Triple-nested for loop with row-major indexing:

  for i = 0 to n - 1 do
    for j = 0 to n - 1 do
      for k = 0 to n - 1 do
        c.(i * n + j) <- c.(i * n + j) + a.(i * n + k) * b.(k * n + j)
      done
    done
  done

For 3x3 matrices A=[[1..9]] and B=[[9..1]], the resulting C has sum
621. Tests deeply nested for loops on Array, Array.make + arr.(i) +
arr.(i) <- v + Array.fold_left.

50 baseline programs total — milestone.
2026-05-09 11:00:00 +00:00

19 lines
396 B
OCaml

let mat_mul a b n =
let c = Array.make (n * n) 0 in
for i = 0 to n - 1 do
for j = 0 to n - 1 do
for k = 0 to n - 1 do
c.(i * n + j) <- c.(i * n + j) + a.(i * n + k) * b.(k * n + j)
done
done
done;
c
;;
let n = 3 in
let a = Array.of_list [1;2;3; 4;5;6; 7;8;9] in
let b = Array.of_list [9;8;7; 6;5;4; 3;2;1] in
let c = mat_mul a b n in
Array.fold_left (+) 0 c