ocaml: phase 5.1 mat_mul.ml baseline (3x3 row-major matrix multiply, sum = 621)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 26s
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.
This commit is contained in:
@@ -22,6 +22,7 @@
|
||||
"hanoi.ml": 1023,
|
||||
"fizzbuzz.ml": 57,
|
||||
"list_ops.ml": 30,
|
||||
"mat_mul.ml": 621,
|
||||
"json_pretty.ml": 24,
|
||||
"kadane.ml": 6,
|
||||
"lambda_calc.ml": 7,
|
||||
|
||||
18
lib/ocaml/baseline/mat_mul.ml
Normal file
18
lib/ocaml/baseline/mat_mul.ml
Normal file
@@ -0,0 +1,18 @@
|
||||
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
|
||||
Reference in New Issue
Block a user