matrax v0.1.0 Matrax View Source
Use :atomics
as an M x N matrix.
Examples
iex> matrax = Matrax.new(100, 100) # 100 x 100 matrix
iex> matrax |> Matrax.put({0, 0}, 10) # add 10 to position {0, 0}
iex> matrax |> Matrax.get({0, 0})
10
iex> matrax |> Matrax.add({0, 0}, 80)
iex> matrax |> Matrax.get({0, 0})
90
Enumerable protocol
Matrax
implements the Enumerable protocol, so all Enum functions can be used:
iex> matrax = Matrax.new(10, 10)
iex> matrax |> Matrax.put({0, 0}, 8)
iex> matrax |> Enum.max()
8
iex> matrax |> Enum.member?(7)
false
Link to this section Summary
Functions
Adds incr
to atomic at position
.
Atomic addition and return of the result.
Applies the given fun
function to all elements of matrax
.
Converts given column index of %Matrax{}
to list.
Atomically compares the value at position
with expected
,
and if those are equal, sets value at position
to desired
.
Returns a %Matrax{}
struct with a new atomics reference
and positional values identical to the given matrax
.
Returns count of values (rows * columns).
Atomically replaces value at position
with value
and
returns the value it had before.
Returns value at position
from the given matrax.
Returns a position tuple for the given atomics index
.
Returns largest integer in matrax
.
Checks if value
exists within matrax
.
Returns smallest integer in matrax
.
Returns a new %Matrax{}
struct.
Returns atomics index corresponding to the position
tuple in the given %Matrax{}
struct.
Puts value
into matrax
at position
.
Converts given row index of %Matrax{}
to list.
Subtracts decr
from atomic at position
.
Atomic subtraction and return of the result.
Returns a submatrix.
Returns sum of integers in matrax
.
Converts %Matrax{}
to a flat list.
Converts %Matrax{}
to list of lists.
Only modifies the struct, it doesn't move or mutate data.
Link to this section Types
position()
View Sourceposition() :: {row :: non_neg_integer(), col :: non_neg_integer()}
t()
View Sourcet() :: %Matrax{ atomics: reference(), columns: pos_integer(), max: pos_integer(), min: integer(), rows: pos_integer(), signed: boolean(), transposed: boolean() }
Link to this section Functions
Adds incr
to atomic at position
.
Examples
iex> matrax = Matrax.new(10, 10)
iex> matrax |> Matrax.add({0, 0}, 2)
:ok
iex> matrax |> Matrax.add({0, 0}, 2)
:ok
iex> matrax |> Matrax.get({0, 0})
4
Atomic addition and return of the result.
Adds incr
to atomic at position
and returns result.
Examples
iex> matrax = Matrax.new(10, 10)
iex> matrax |> Matrax.add_get({0, 0}, 2)
2
iex> matrax |> Matrax.add_get({0, 0}, 2)
4
Applies the given fun
function to all elements of matrax
.
If arity of fun
is 1 it receives the integer as single argument.
If arity of fun
is 2 it receives the integer as first and
position tuple as the second argument.
Examples
iex> matrax = Matrax.new(10, 10)
iex> matrax |> Matrax.apply(fn int -> int + 2 end)
iex> matrax |> Matrax.get({0, 0})
2
iex> matrax = Matrax.new(10, 10)
iex> matrax |> Matrax.apply(fn _int, {row, col} -> row * col end)
iex> matrax |> Matrax.get({9, 9})
81
column_to_list(matrax, col)
View Sourcecolumn_to_list(t(), non_neg_integer()) :: [integer()]
Converts given column index of %Matrax{}
to list.
Examples
iex> matrax = Matrax.new(5, 5, seed_fun: fn _, {row, col} -> row * col end)
iex> matrax |> Matrax.column_to_list(2)
[0, 2, 4, 6, 8]
Atomically compares the value at position
with expected
,
and if those are equal, sets value at position
to desired
.
Returns :ok if desired
was written.
Returns the actual value at position
if it does not equal to desired
.
Examples
iex> matrax = Matrax.new(10, 10)
iex> matrax |> Matrax.compare_exchange({0, 0}, 0, -10)
:ok
iex> matrax |> Matrax.compare_exchange({0, 0}, 3, 10)
-10
Returns a %Matrax{}
struct with a new atomics reference
and positional values identical to the given matrax
.
The returned copy is always transposed: false
so this
can be used to finish the access-path only transpose
by the transpose/1
function.
iex> matrax = Matrax.new(10, 10)
iex> matrax |> Matrax.put({0, 0}, -9)
iex> matrax2 = Matrax.copy(matrax)
iex> Matrax.get(matrax2, {0, 0})
-9
Returns count of values (rows * columns).
Examples
iex> matrax = Matrax.new(5, 5)
iex> Matrax.count(matrax)
25
iex> Matrax.count(matrax) == :atomics.info(matrax.atomics).size
true
Atomically replaces value at position
with value
and
returns the value it had before.
Examples
iex> matrax = Matrax.new(10, 10)
iex> matrax |> Matrax.exchange({0, 0}, -10)
0
iex> matrax |> Matrax.exchange({0, 0}, -15)
-10
Returns value at position
from the given matrax.
Examples
iex> matrax = Matrax.new(10, 10, seed_fun: fn _ -> 3 end)
iex> matrax |> Matrax.get({0, 5})
3
index_to_position(matrax, index)
View Sourceindex_to_position(t(), pos_integer()) :: position()
Returns a position tuple for the given atomics index
.
Examples
iex> matrax = Matrax.new(10, 10)
iex> Matrax.index_to_position(matrax, 10)
{0, 9}
Returns largest integer in matrax
.
Examples
iex> matrax = Matrax.new(10, 10, seed_fun: fn _, {row, col} -> row * col end)
iex> matrax |> Matrax.max()
81
Checks if value
exists within matrax
.
iex> matrax = Matrax.new(5, 5, seed_fun: fn _, {row, col} -> row * col end)
iex> matrax |> Matrax.member?(6)
true
iex> matrax |> Matrax.member?(100)
false
Returns smallest integer in matrax
.
Examples
iex> matrax = Matrax.new(10, 10, seed_fun: fn _ -> 7 end)
iex> matrax |> Matrax.min()
7
new(rows, columns, options \\ [])
View Sourcenew(pos_integer(), pos_integer(), list()) :: t()
Returns a new %Matrax{}
struct.
Options
:seed_fun
- a function to seed all positions. Seeapply/2
for further information.:signed
- whether to have signed or unsigned 64bit integers
Examples
Matrax.new(10, 5) # 10 x 5 matrix
Matrax.new(10, 5, signed: false) # unsigned integers
Matrax.new(10, 5, seed_fun: fn _, {row, col} -> row * col end) # seed values
position_to_index(matrax, arg)
View Sourceposition_to_index(t(), position()) :: pos_integer()
Returns atomics index corresponding to the position
tuple in the given %Matrax{}
struct.
Examples
iex> matrax = Matrax.new(10, 10)
iex> matrax |> Matrax.position_to_index({1, 1})
12
iex> matrax |> Matrax.position_to_index({0, 4})
5
Puts value
into matrax
at position
.
Returns :ok
Examples
iex> matrax = Matrax.new(10, 10)
iex> matrax |> Matrax.put({1, 3}, 5)
:ok
row_to_list(matrax, row)
View Sourcerow_to_list(t(), non_neg_integer()) :: [integer()]
Converts given row index of %Matrax{}
to list.
Examples
iex> matrax = Matrax.new(5, 5, seed_fun: fn _, {row, col} -> row * col end)
iex> matrax |> Matrax.row_to_list(2)
[0, 2, 4, 6, 8]
Subtracts decr
from atomic at position
.
Examples
iex> matrax = Matrax.new(10, 10)
iex> matrax |> Matrax.sub({0, 0}, 1)
:ok
iex> matrax |> Matrax.sub({0, 0}, 1)
:ok
iex> matrax |> Matrax.get({0, 0})
-2
Atomic subtraction and return of the result.
Subtracts decr
from atomic at position
and returns result.
Examples
iex> matrax = Matrax.new(10, 10)
iex> matrax |> Matrax.sub_get({0, 0}, 2)
-2
iex> matrax |> Matrax.sub_get({0, 0}, 2)
-4
Returns a submatrix.
Creates a new :atomics
for the submatrix
and copies values over.
Ranges are inclusive and 0 based.
Examples
iex> matrax = Matrax.new(7, 4, seed_fun: fn _, {row, col} -> row + col end)
iex> matrax |> Matrax.to_list_of_lists()
[
[0, 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]
]
iex> matrax |> Matrax.submatrix(0..3, 0..3) |> Matrax.to_list_of_lists()
[
[0, 1, 2, 3],
[1, 2, 3, 4],
[2, 3, 4, 5],
[3, 4, 5, 6]
]
Returns sum of integers in matrax
.
Examples
iex> matrax = Matrax.new(10, 10, seed_fun: fn _, {row, col} -> row * col end)
iex> matrax |> Matrax.sum()
2025
Converts %Matrax{}
to a flat list.
Examples
iex> matrax = Matrax.new(3, 3, seed_fun: fn _, {row, col} -> row * col end)
iex> Matrax.to_list(matrax)
[0, 0, 0, 0, 1, 2, 0, 2, 4]
Converts %Matrax{}
to list of lists.
Examples
iex> matrax = Matrax.new(5, 5, seed_fun: fn _, {row, col} -> row * col end)
iex> Matrax.to_list_of_lists(matrax)
[
[0, 0, 0, 0, 0],
[0, 1, 2, 3, 4],
[0, 2, 4, 6, 8],
[0, 3, 6, 9, 12],
[0, 4, 8, 12, 16]
]
Only modifies the struct, it doesn't move or mutate data.
Given transposed: true
the access path to positions
will be modified during execution in position_to_index/2
.
For a real transposed matrix with data modification
you can first transpose/1
then copy/1
. copy/1
creates a
new %Matrax{}
struct based on the transposed matrax.
Examples
iex> matrax = Matrax.new(7, 4, seed_fun: fn _, {row, col} -> row + col end)
iex> matrax |> Matrax.to_list_of_lists()
[
[0, 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]
]
iex> matrax |> Matrax.transpose() |> Matrax.to_list_of_lists()
[
[0, 1, 2, 3, 4, 5, 6],
[1, 2, 3, 4, 5, 6, 7],
[2, 3, 4, 5, 6, 7, 8],
[3, 4, 5, 6, 7, 8, 9]
]