Hello everyone, here I share a code that I have been working on:
defmodule GenerateMatrixRandom do
def mat() do
{:ok, file} = File.open(“MatrizA.txt”, [:write])
Enum.map(1…j(), fn _ →
for _ ← 1 …j(), do: IO.write(file, “#{random_number()}\t”)
end)
File.close(file){:ok, file} = File.open(“MatrizB.txt”, [:write])
Enum.map(1…j(), fn _ →
for _ ← 1 …j(), do: IO.write(file, “#{random_number()}\t”)
end)
File.close(file)
enddef mat_c(c)when is_list(c) do
{:ok, file} = File.open(“MatrizC.txt”, [:write])
v = Enum.concat(c)
Enum.reduce(v, fn x, acc → IO.write(file, “#{x}\t”) end)
File.close(file)
enddef parse1() do
v = parseA()
Enum.chunk_every(v, j())
enddef parse2() do
v = parseB()
Enum.chunk_every(v, j())
enddef transpose(), do:
def transpose([ | xss]), do: transpose(xss)
def transpose([[x | xs] | xss]) do
[[x | (for [h | ] ← xss, do: h)] | transpose([xs | (for [ | t] ← xss, do: t)])]
enddef multiply(_, ), 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(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)
enddef multiply(result, , ), do: result
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)]
enddef now, do: ({msecs, secs, musecs} = :erlang.timestamp; ((msecs*1000000 + secs)*1000000 + musecs)/1000000)
defp j(), do: 1000000
defp parseA() do
{:ok, contents} = File.read(“MatrizA.txt”)
contents |> String.split(“\t”, trim: true) |> Enum.map(fn n → {v, _} = Float.parse(n); v end)
enddefp parseB() do
{:ok, contents} = File.read(“MatrizB.txt”)
contents |> String.split(“\t”, trim: true) |> Enum.map(fn n → {v, _} = Float.parse(n); v end)
enddefp random_number() do
:rand.uniform() * 100
|> Float.round(8)
endend
As you will see, I create two matrices of Millonxmillon I keep them in txt and then extract, multiply and save the answer in MatrixC.txt
Could you give me your comments and advise how I can make the code more optimal and have a faster execution.