Skip to content

Instantly share code, notes, and snippets.

@jochasinga jochasinga/matrix.ml
Created Oct 28, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.