ashbyBg_21

ashbyBg_21

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)
end

def 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)
end

def parse1() do
v = parseA()
Enum.chunk_every(v, j())
end

def parse2() do
v = parseB()
Enum.chunk_every(v, j())
end

def 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)])]
end

def 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)
end

def 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)]
end

def 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)
end

defp parseB() do
{:ok, contents} = File.read(“MatrizB.txt”)
contents |> String.split(“\t”, trim: true) |> Enum.map(fn n → {v, _} = Float.parse(n); v end)
end

defp random_number() do
:rand.uniform() * 100
|> Float.round(8)
end

end

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.

Showing Posts 1 to 1

elcritch

elcritch

Ah, matrix maths! There’s been a few previous discussions on the topic. The summary is that pure Elixir code isn’t ideal for matrix maths. See this thread for more info: Matrix in Elixir

If possible, I’d recommend using the Matrex library (GitHub - versilov/matrex: A blazing fast matrix library for Elixir/Erlang with C implementation using CBLAS. · GitHub) as probably the most complete matrix math library in elixir. There’s some alternatives that might be good for you in the other forum posting I linked to above.

Specifically in your code replace all of the matrix multiplications, transposes with the equivalent Matrex functions. Also consider storing your matrix data in a binary format. Matrex supports the matlab binary format which should be exportable from Python, Matlab or others. The binary format should help speed up loading the data. One main limit of Matrex` is that it only uses single precision floating points.

If you really want to use pure Elixir, look into parallelizing the matrix operations into sub-matrices using multiple GenServer’s or Tasks. Or look at the Matrax library that uses OTP atomics to do matrix math: Matrax library - use :atomics as a matrix .

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews