max v0.1.3 Max View Source
A matrix library in pure Elixir based on :array
.
Examples
iex> matrix = Max.new(5, 5, default: 2) # 5x5 matrix with default value 2
iex> Max.get(matrix, {0, 0})
2
iex> matrix = Max.set(matrix, {0, 0}, 8)
iex> Max.get(matrix, {0, 0})
8
Enumberable protocol
Max
implements the Enumerable protocol, so all Enum functions can be used:
iex> matrix = Max.new(10, 10, default: 8)
iex> Enum.max(matrix)
8
iex> Enum.member?(matrix, 7)
false
Link to this section Summary
Functions
Adds two matrices. Size of matrices must match.
Returns position tuple of largest value.
Returns position tuple of smallest value.
Reduces matrix to only one column at given col
index.
Converts column at given column index of matrix to list.
Concatenates a list of matrices.
Returns the default value for matrix.
Returns diagonal of matrix.
Returns matrix product of the two given matrices.
Drops column of matrix at given column_index
.
Drops row of matrix at given row_index
.
Returns position of the first occurence of the given value
or nil
if nothing was found.
Flips columns of matrix in the left-right direction (vertical axis).
Flip rows of matrix in the up-down direction (horizontal axis).
Folds the elements using the specified function and initial accumulator value. The elements are visited in order from the lowest index to the highest.
Folds the elements right-to-left using the specified function and initial accumulator value. The elements are visited in order from the highest index to the lowest.
Converts a flat list to a new %Max{}
struct with the given rows
& columns
size.
Converts a list of lists matrix to a new %Max{}
struct.
Returns value at position
from the given matrix
.
Create identity square matrix of given size
.
Returns array index corresponding to the position tuple.
Maps each element to the result of the a given fun
.
Returns largest value in matrix using Kernel.max/2
.
Checks for membership of given term
.
Returns true
if member, false
otherwise.
Returns smallest value in matrix using Kernel.min/2
.
Elementwise multiplication of two matrices.
Returns a new %Max{}
struct with the given rows
and columns
size.
Returns a position tuple for the given index.
Resets element at position to the default value.
Reshapes matrix
to the given rows
& columns
.
Reduces matrix to only one row at given row
index.
Converts row at given row index of matrix to list.
Sets value
at position
in matrix
.
Set column of a matrix at column_index
to the values from the given 1-column matrix.
Set row of a matrix at row_index
to the values from the given 1-row matrix.
Returns the size of matrix. (rows * columns)
Folds the elements using the specified function and initial accumulator value, skipping default-valued entries. The elements are visited in order from the lowest index to the highest.
Folds the array elements right-to-left using the specified function and initial accumulator value, skipping default-valued entries. The elements are visited in order from the highest index to the lowest.
Same as map/2
except it skips default valued elements.
Returns the sparse size of the :array
.
Returns a submatrix from the given matrix
.
Ranges are inclusive.
Returns sum of integers in matrix.
Converts matrix to a flat list.
Converts matrix to list of lists.
Trace of matrix (sum of all diagonal elements).
Returns transpose of given matrix
.
Link to this section Types
position()
View Sourceposition() :: {row :: non_neg_integer(), col :: non_neg_integer()}
t()
View Sourcet() :: %Max{array: tuple(), columns: pos_integer(), rows: pos_integer()}
Link to this section Functions
Adds two matrices. Size of matrices must match.
Examples
iex> matrix = Max.from_list_of_lists([
...> [0, 0, 0, 0],
...> [1, 1, 1, 1],
...> [2, 2, 2, 2]
...>])
iex> Max.add(matrix, matrix) |> Max.to_list_of_lists()
[
[0, 0, 0, 0],
[2, 2, 2, 2],
[4, 4, 4, 4]
]
Returns position tuple of largest value.
Examples
iex> matrix = Max.new(5, 5, default: 8)
iex> matrix |> Max.argmax()
{0, 0}
iex> matrix = matrix |> Max.set({1, 1}, 10)
iex> matrix |> Max.argmax()
{1, 1}
Returns position tuple of smallest value.
Examples
iex> matrix = Max.new(5, 5, default: 8)
iex> matrix |> Max.argmin()
{0, 0}
iex> matrix = matrix |> Max.set({1, 1}, 7)
iex> matrix |> Max.argmin()
{1, 1}
Reduces matrix to only one column at given col
index.
Examples
iex> matrix = Max.new(5, 5, default: 3)
iex> matrix |> Max.column(4) |> Max.to_list_of_lists
[[3], [3], [3], [3], [3]]
column_to_list(matrix, col)
View Sourcecolumn_to_list(t(), non_neg_integer()) :: list()
Converts column at given column index of matrix to list.
Examples
iex> matrix = Max.identity(5)
iex> matrix |> Max.column_to_list(0)
[1, 0, 0, 0, 0]
Concatenates a list of matrices.
Returns a new %Max{}
struct with a new array containing all values
of matrices from list
.
Options
:default
- (term) the default value of the matrix. Defaults to0
.
Examples
iex> matrix = Max.new(3, 3) |> Max.map(fn i, _v -> i end)
iex> matrix |> Max.to_list_of_lists()
[
[0, 1, 2],
[3, 4, 5],
[6, 7, 8]
]
iex> Max.concat([matrix, matrix], :rows) |> Max.to_list_of_lists()
[
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 1, 2],
[3, 4, 5],
[6, 7, 8]
]
iex> Max.concat([matrix, matrix], :columns) |> Max.to_list_of_lists()
[
[0, 1, 2, 0, 1, 2],
[3, 4, 5, 3, 4, 5],
[6, 7, 8, 6, 7, 8]
]
Returns the default value for matrix.
Examples
iex> matrix = Max.from_list_of_lists([[1,2], [3,4]])
iex> matrix |> Max.default()
0
iex> matrix = Max.new(5, 5, default: "preciz")
iex> matrix |> Max.default()
"preciz"
Returns diagonal of matrix.
Examples
iex> matrix = Max.identity(3)
iex> matrix |> Max.to_list_of_lists()
[
[1, 0, 0],
[0, 1, 0],
[0, 0, 1]
]
iex> matrix |> Max.diagonal() |> Max.to_list_of_lists()
[[1, 1, 1]]
Returns matrix product of the two given matrices.
Number of columns of the first matrix must be equal to the number of rows of the second matrix.
Examples
iex> matrix_a = Max.from_list_of_lists([
...> [-2, -2, -2, -2],
...> [8, 8, 8, 8],
...> [2, 2, 2, 2]
...>])
iex> matrix_b = Max.from_list_of_lists([
...> [0, 0, 0, 0],
...> [1, 1, 1, 1],
...> [2, 2, 2, 2],
...> [3, 3, 3, 3]
...>])
iex> Max.dot(matrix_a, matrix_b) |> Max.to_list_of_lists()
[
[-12, -12, -12, -12],
[48, 48, 48, 48],
[12, 12, 12, 12]
]
drop_column(matrix, column_index)
View Sourcedrop_column(t(), non_neg_integer()) :: t()
Drops column of matrix at given column_index
.
Examples
iex> matrix = Max.from_list_of_lists([
...> [0, 1, 2, 3, 4],
...> [0, 1, 2, 3, 4],
...> [0, 1, 2, 3, 4],
...> [0, 1, 2, 3, 4]
...> ])
iex> matrix |> Max.drop_column(1) |> Max.to_list_of_lists()
[
[0, 2, 3, 4],
[0, 2, 3, 4],
[0, 2, 3, 4],
[0, 2, 3, 4]
]
drop_row(matrix, row_index)
View Sourcedrop_row(t(), non_neg_integer()) :: t()
Drops row of matrix at given row_index
.
Examples
iex> matrix = Max.new(3, 3) |> Max.map(fn i, _v -> i + 1 end)
iex> matrix |> Max.to_list_of_lists()
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
iex> matrix |> Max.drop_row(1) |> Max.to_list_of_lists()
[
[1, 2, 3],
[7, 8, 9]
]
Returns position of the first occurence of the given value
or nil
if nothing was found.
Examples
iex> Max.new(5, 5) |> Max.find(0)
{0, 0}
iex> matrix = Max.new(5, 5) |> Max.map(fn i, _v -> i end)
iex> matrix |> Max.find(16)
{3, 1}
iex> matrix |> Max.find(42)
nil
Flips columns of matrix in the left-right direction (vertical axis).
Examples
iex> matrix = Max.from_list_of_lists([
...> [0, 1, 2, 3],
...> [0, 1, 2, 3],
...> [0, 1, 2, 3]
...>])
iex> matrix |> Max.flip_lr() |> Max.to_list_of_lists()
[
[3, 2, 1, 0],
[3, 2, 1, 0],
[3, 2, 1, 0]
]
Flip rows of matrix in the up-down direction (horizontal axis).
Examples
iex> matrix = Max.from_list_of_lists([
...> [0, 0, 0, 0],
...> [1, 1, 1, 1],
...> [2, 2, 2, 2]
...>])
iex> matrix |> Max.flip_ud() |> Max.to_list_of_lists()
[
[2, 2, 2, 2],
[1, 1, 1, 1],
[0, 0, 0, 0]
]
Folds the elements using the specified function and initial accumulator value. The elements are visited in order from the lowest index to the highest.
Examples
iex> matrix = Max.new(5, 5, default: 1)
iex> matrix |> Max.foldl(fn _index, value, acc -> value + acc end, 0)
25
Folds the elements right-to-left using the specified function and initial accumulator value. The elements are visited in order from the highest index to the lowest.
Examples
iex> matrix = Max.new(5, 5, default: 1)
iex> matrix |> Max.foldr(fn _index, value, acc -> value + acc end, 0)
25
from_list(list, rows, columns, options \\ [])
View Sourcefrom_list([...], pos_integer(), pos_integer(), list()) :: t()
Converts a flat list to a new %Max{}
struct with the given rows
& columns
size.
Options
:default
- (term) the default value of the matrix. Defaults to0
.
Examples
iex> matrix = Max.from_list([1,2,3,4,5,6], 2, 3)
iex> matrix |> Max.to_list_of_lists
[[1,2,3], [4, 5, 6]]
Converts a list of lists matrix to a new %Max{}
struct.
Options
:default
- (term) the default value of the matrix. Defaults to0
.
Examples
iex> matrix = %Max{rows: 2, columns: 3} = Max.from_list_of_lists([[1,2,3], [4, 5, 6]])
iex> matrix |> Max.to_list_of_lists
[[1,2,3], [4, 5, 6]]
Returns value at position
from the given matrix
.
Examples
iex> matrix = Max.identity(5)
iex> matrix |> Max.get({1, 1})
1
Create identity square matrix of given size
.
Options
:default
- (term) the default value of the matrix. Defaults to0
.
Examples
iex> Max.identity(5) |> Max.to_list_of_lists()
[
[1, 0, 0, 0, 0],
[0, 1, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 1, 0],
[0, 0, 0, 0, 1]
]
index_to_position(max, index)
View Sourceindex_to_position(t(), non_neg_integer()) :: position()
Returns array index corresponding to the position tuple.
Examples
iex> matrix = Max.new(10, 10)
iex> matrix |> Max.position_to_index({1, 1})
11
iex> matrix |> Max.position_to_index({0, 4})
4
Maps each element to the result of the a given fun
.
The given fun
receives the index as first and
value as the second argument.
To convert index to position use index_to_position/2
.
Examples
iex> matrix = Max.new(10, 10, default: 2)
iex> matrix = Max.map(matrix, fn _index, value -> value + 2 end)
iex> matrix |> Max.get({0, 0})
4
Returns largest value in matrix using Kernel.max/2
.
Examples
iex> matrix = Max.new(10, 10) |> Max.map(fn index, _ -> index end)
iex> matrix |> Max.max()
99
Checks for membership of given term
.
Returns true
if member, false
otherwise.
Examples
iex> matrix = Max.new(5, 5) |> Max.map(fn i, _ -> i end)
iex> matrix |> Max.member?(6)
true
iex> matrix |> Max.member?(100)
false
Returns smallest value in matrix using Kernel.min/2
.
Examples
iex> matrix = Max.new(10, 10, default: 7)
iex> matrix |> Max.min()
7
Elementwise multiplication of two matrices.
Examples
iex> matrix = Max.from_list_of_lists([
...> [0, 0, 0, 0],
...> [1, 1, 1, 1],
...> [2, 2, 2, 2]
...>])
iex> Max.multiply(matrix, matrix) |> Max.to_list_of_lists()
[
[0, 0, 0, 0],
[1, 1, 1, 1],
[4, 4, 4, 4]
]
new(rows, columns, options \\ [])
View Sourcenew(pos_integer(), pos_integer(), list()) :: t()
Returns a new %Max{}
struct with the given rows
and columns
size.
Options
:default
- (term) the default value of the matrix. Defaults to0
.
Examples
Max.new(10, 5) # 10 x 5 matrix
Max.new(10, 5, default: 70) # 70 as a default value
position_to_index(max, arg)
View Sourceposition_to_index(t(), position()) :: pos_integer()
Returns a position tuple for the given index.
:array
indices are 0 based.
Examples
iex> matrix = Max.new(5, 5)
iex> matrix |> Max.position_to_index({0, 0})
0
iex> matrix |> Max.position_to_index({1, 0})
5
Resets element at position to the default value.
Examples
iex> matrix = Max.new(5, 5, default: 1) |> Max.map(fn _,_ -> 7 end)
iex> matrix |> Max.get({0, 0})
7
iex> matrix |> Max.reset({0, 0}) |> Max.get({0, 0})
1
reshape(matrix, rows, columns)
View Sourcereshape(t(), pos_integer(), pos_integer()) :: t()
Reshapes matrix
to the given rows
& columns
.
Examples
iex> matrix = Max.identity(4)
iex> matrix |> Max.to_list_of_lists()
[
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]
]
iex> matrix |> Max.reshape(2, 8) |> Max.to_list_of_lists()
[
[1, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 1]
]
Reduces matrix to only one row at given row
index.
Examples
iex> matrix = Max.new(5, 5, default: 3)
iex> matrix |> Max.row(4) |> Max.to_list_of_lists
[[3, 3, 3, 3, 3]]
row_to_list(matrix, row)
View Sourcerow_to_list(t(), non_neg_integer()) :: list()
Converts row at given row index of matrix to list.
Examples
iex> matrix = Max.identity(5)
iex> matrix |> Max.row_to_list(2)
[0, 0, 1, 0, 0]
Sets value
at position
in matrix
.
Returns %Max{}
struct.
Examples
iex> matrix = Max.new(10, 10)
iex> matrix = matrix |> Max.set({1, 3}, 5)
iex> matrix |> Max.get({1, 3})
5
set_column(matrix, column_index, column_matrix)
View Sourceset_column(t(), non_neg_integer(), t()) :: t()
Set column of a matrix at column_index
to the values from the given 1-column matrix.
Examples
iex> matrix = Max.new(5, 5, default: 1)
iex> column_matrix = Max.new(5, 1, default: 3)
iex> Max.set_column(matrix, 2, column_matrix) |> Max.to_list_of_lists
[
[1, 1, 3, 1, 1],
[1, 1, 3, 1, 1],
[1, 1, 3, 1, 1],
[1, 1, 3, 1, 1],
[1, 1, 3, 1, 1],
]
set_row(matrix, row_index, row_matrix)
View Sourceset_row(t(), non_neg_integer(), t()) :: t()
Set row of a matrix at row_index
to the values from the given 1-row matrix.
Examples
iex> matrix = Max.new(5, 5, default: 1)
iex> row_matrix = Max.new(1, 5, default: 3)
iex> Max.set_row(matrix, 2, row_matrix) |> Max.to_list_of_lists()
[
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
[3, 3, 3, 3, 3],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1],
]
Returns the size of matrix. (rows * columns)
Examples
iex> matrix = Max.new(5, 5)
iex> Max.size(matrix)
25
Folds the elements using the specified function and initial accumulator value, skipping default-valued entries. The elements are visited in order from the lowest index to the highest.
Examples
iex> matrix = Max.new(5, 5, default: 1)
iex> matrix |> Max.sparse_foldl(fn _index, value, acc -> value + acc end, 0)
0
Folds the array elements right-to-left using the specified function and initial accumulator value, skipping default-valued entries. The elements are visited in order from the highest index to the lowest.
Examples
iex> matrix = Max.new(5, 5, default: 1)
iex> matrix |> Max.sparse_foldr(fn _index, value, acc -> value + acc end, 0)
0
Same as map/2
except it skips default valued elements.
Examples
iex> matrix = Max.new(10, 10, default: 2)
iex> matrix = Max.sparse_map(matrix, fn _index, value -> value + 2 end)
iex> matrix |> Max.get({0, 0}) # value stays at 2 because it was at default
2
Returns the sparse size of the :array
.
Erlang array docs: "Gets the number of entries in the array up until the last non-default-valued entry. That is, returns I+1 if I is the last non-default-valued entry in the array, or zero if no such entry exists."
Examples
iex> matrix = Max.new(5, 5)
iex> Max.sparse_size(matrix)
0
Returns a submatrix from the given matrix
.
Ranges are inclusive.
Options
:default
- (term) the default value of the matrix. Defaults to0
.
Examples
iex> matrix = Max.new(2, 4) |> Max.map(fn i, _v -> i end)
iex> matrix |> Max.to_list_of_lists()
[
[0, 1, 2, 3],
[4, 5, 6, 7],
]
iex> matrix |> Max.submatrix(0..1, 1..3) |> Max.to_list_of_lists()
[
[1, 2, 3],
[5, 6, 7]
]
Returns sum of integers in matrix.
Examples
iex> matrix = Max.new(3, 3, default: 1)
iex> matrix |> Max.sum()
9
Converts matrix to a flat list.
Examples
iex> matrix = Max.new(3, 3) |> Max.map(fn index, _val -> index end)
iex> Max.to_list(matrix)
[0, 1, 2, 3, 4, 5, 6, 7, 8]
Converts matrix to list of lists.
Examples
iex> matrix = Max.new(5, 5) |> Max.map(fn i, _v -> i + 1 end)
iex> Max.to_list_of_lists(matrix)
[
[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
]
Trace of matrix (sum of all diagonal elements).
Examples
iex> matrix = Max.new(3, 3, default: 1)
iex> matrix |> Max.trace()
3
Returns transpose of given matrix
.
Examples
iex> matrix = Max.new(2, 3) |> Max.map(fn i, _v -> i end)
iex> matrix |> Max.to_list_of_lists()
[
[0, 1, 2],
[3, 4, 5]
]
iex> matrix |> Max.transpose() |> Max.to_list_of_lists()
[
[0, 3],
[1, 4],
[2, 5]
]