Hi, I found this code, could you explain what it does and how it does it for each line.
I know that it creates a 100x100 matrix and multiplies it, but I would like a more detailed explanation of what each thing means.
defmodule Matrix do
def transpose([[x | xs] | xss]) do
[[x | (for [h | _] <- xss, do: h)] | transpose([xs | (for [_ | t] <- xss, do: t)])]
end
def transpose([[] | xss]), do: transpose(xss)
def transpose([]), do: []
def multiply(a = [x | _],b = [y | _]) when is_list(a) and is_list(b) and is_list(x) and is_list(y) do
multiply([], a, transpose(b))
end
def multiply(_, []), do: []
def multiply(a, b = [h | t]) when is_number(a) and is_list(b) and is_list(h) do
[Enum.map(h, &(&1*a)) | multiply(a, t)]
end
def multiply(b = [h | _], a) when is_number(a) and is_list(b) and is_list(h) do
multiply(a, b)
end
def multiply(result, [], []), do: result
def multiply(result, [a | rest_a], [b | rest_b]) when not is_list(a) and not is_list(b), do: multiply(a*b+result, rest_a, rest_b)
def multiply(result, [first_row_a | rest_a], b) when is_list(first_row_a) do
[Enum.reverse(Enum.reduce(b, [], fn(col_b,acc) -> [multiply(0, first_row_a, col_b) | acc] end)) | multiply(result, rest_a, b)]
end
def multiply(result, [], _), do: result
def rand(rows, cols) do
Enum.map(0..rows-1, fn _ ->
for _ <- 0..cols-1, do: :random.uniform
end)
end
defmodule Time do
def now, do: ({msecs, secs, musecs} = :erlang.timestamp; ((msecs*1000000 + secs)*1000000 + musecs)/1000000)
end
end
a = Matrix.rand(100,100)
b = Matrix.rand(100,100)
t = Time.now
iters = 100
Enum.each(1..iters, fn(_) -> Matrix.multiply(a,b) end)