The main idea of this library is to use :atomics
as a matrix with all its benefits preserved.
iex> matrax = Matrax.new(7, 4, seed_fun: fn _, {row, col} -> row + col end)
iex> matrax |> Matrax.put({0, 0}, 1989) # atomics are mutable
iex> matrax |> Matrax.to_list_of_lists()
[
[1989, 1, 2, 3],
[1, 2, 3, 4],
[2, 3, 4, 5],
[3, 4, 5, 6],
[4, 5, 6, 7],
[5, 6, 7, 8],
[6, 7, 8, 9]
]
All :atomics
functions are wrapped and a few matrix specific functions are provided:
API summary
See https://hexdocs.pm/matrax for full documentation.
-
Matrax.new/2
- Create new matrix. -
Matrax.get/2
- Returns the integer at the given position. -
Matrax.put/3
- Puts the given integer into the given position. -
Matrax.add/3
- Adds the given increment to the value at given position atomically. -
Matrax.add_get/3
- Adds the given increment to the value at given position atomically and returns result. -
Matrax.sub/3
- Subtracts the given decrement to the value at given position atomically. -
Matrax.sub_get/3
- Subtracts the given decrement to the value at given position atomically and returns result. -
Matrax.exchange/3
- Exchanges the given integer at the given position atomically. -
Matrax.compare_exchange/4
- Compares & exchanges the given integer at the given position atomically. -
Matrax.index_to_position/2
- Converts the given atomics index to position tuple. -
Matrax.position_to_index/2
- Converts the given position tuple to atomics index. -
Matrax.min/1
- Returns smallest integer in matrix. -
Matrax.max/1
- Returns largest integer in matrix. -
Matrax.sum/1
- Returns sum of integers in matrix. -
Matrax.member?/2
- Checks if value exists within matrix. -
Matrax.apply/2
- Applies the given function to all elements of matrix. -
Matrax.to_list/1
- Converts matrix to a flat list. -
Matrax.to_list_of_lists/1
- Converts matrix to list of lists. -
Matrax.row_to_list/2
- Converts row at given index of matrix to list. -
Matrax.column_to_list/2
- Converts column at given index of matrix to list. -
Matrax.transpose/1
- Transposes the given matrix. (access path modification only) -
Matrax.copy/1
- Returns a copy of the matrix with a new atomics reference. Can be used to finish transpose. -
Matrax.submatrix/3
- Returns a new submatrix. Creates new atomics and copies values over.
GitHub: https://github.com/preciz/matrax
What is the use case?
An example use case is when you need concurrent access to a matrix of integers / bitmasks.
(my use case was a constraint solving algorithm)
:atomics
"utilizes only atomic hardware instructions without any software level locking, which makes it very efficient for concurrent access. " - Erlang :atomics
documentation.
Feedback is welcome.
(I would like to also mention that I’m looking for employment, if you are an employer or your company is hiring I would be happy to talk with you. PM me. Thank you!)