Skip to content

Instantly share code, notes, and snippets.

@jochasinga
Created October 28, 2018 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jochasinga/2edda3ad1efd2e602966799b96b31012 to your computer and use it in GitHub Desktop.
Save jochasinga/2edda3ad1efd2e602966799b96b31012 to your computer and use it in GitHub Desktop.
Experiment with matrix multiplication and dot product
let transpose A =
let new_mat = Array.make_matrix (Array.length A.(0)) (Array.length A) 0
in
for y = 0 to (Array.length A) - 1 do
for x = 0 to (Array.length A) - 1 do
new_mat.(y).(x) <- A.(x).(y)
done
done;
new_mat
in
let dot_of_vecs a b =
let sum = ref 0 in
for n = 0 to (Array.length a) - 1 do
sum := !sum + (a.(n) * b.(x))
done;
!sum
in
let product_of A B =
let init_mat = Array.make_matrix (Array.length A) (Array.length (transpose B)) 0 in
for y = 0 to (Array.length (transpose B)) - 1 do
for x = 0 to (Array.length A) - 1 do
init_matrix.(x).(y) <- dot_of_vecs A.(x) (transpose B).(y)
done
done;
init_mat
in
let some_product_of A B =
if Array.length A <> Array.length B.(0) then None
else Some(product_of A B)
in
match some_product_of X X with
| Some sqrt_x -> true
| _ -> false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment