max v0.1.3 Max View Source

A matrix library in pure Elixir based on :array.

Erlang array documentation

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

Link to this type

position()

View Source
position() :: {row :: non_neg_integer(), col :: non_neg_integer()}
Link to this type

t()

View Source
t() :: %Max{array: tuple(), columns: pos_integer(), rows: pos_integer()}

Link to this section Functions

Link to this function

add(left, max)

View Source
add(t(), t()) :: t()

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]
]
Link to this function

argmax(matrix)

View Source
argmax(t()) :: any()

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}
Link to this function

argmin(matrix)

View Source
argmin(t()) :: any()

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}
Link to this function

column(matrix, col)

View Source
column(t(), non_neg_integer()) :: t()

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]]
Link to this function

column_to_list(matrix, col)

View Source
column_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]
Link to this function

concat(list, concat_type, options \\ [])

View Source
concat([t(), ...], :rows | :columns, list()) :: t() | no_return()

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 to 0.

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]
]
Link to this function

default(max)

View Source
default(t()) :: any()

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"
Link to this function

diagonal(matrix)

View Source
diagonal(t()) :: t()

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]]
Link to this function

dot(left, right)

View Source
dot(t(), t()) :: t()

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]
]
Link to this function

drop_column(matrix, column_index)

View Source
drop_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]
]
Link to this function

drop_row(matrix, row_index)

View Source
drop_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]
]
Link to this function

find(matrix, term)

View Source
find(t(), any()) :: position() | nil

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
Link to this function

flip_lr(matrix)

View Source
flip_lr(t()) :: t()

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]
]
Link to this function

flip_ud(matrix)

View Source
flip_ud(t()) :: t()

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]
]
Link to this function

foldl(max, fun, acc)

View Source
foldl(t(), function(), any()) :: any()

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
Link to this function

foldr(max, fun, acc)

View Source
foldr(t(), function(), any()) :: any()

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
Link to this function

from_list(list, rows, columns, options \\ [])

View Source
from_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 to 0.

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]]
Link to this function

from_list_of_lists(list, options \\ [])

View Source
from_list_of_lists([[...], ...], list()) :: t()

Converts a list of lists matrix to a new %Max{} struct.

Options

  • :default - (term) the default value of the matrix. Defaults to 0.

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]]
Link to this function

get(matrix, position)

View Source
get(t(), position()) :: any()

Returns value at position from the given matrix.

Examples

iex> matrix = Max.identity(5)
iex> matrix |> Max.get({1, 1})
1
Link to this function

identity(size, options \\ [])

View Source

Create identity square matrix of given size.

Options

  • :default - (term) the default value of the matrix. Defaults to 0.

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]
]
Link to this function

index_to_position(max, index)

View Source
index_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
Link to this function

map(matrix, fun)

View Source
map(t(), (... -> any())) :: t()

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
Link to this function

member?(matrix, term)

View Source
member?(t(), any()) :: boolean()

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
Link to this function

multiply(left, max)

View Source
multiply(t(), t()) :: t()

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]
]
Link to this function

new(rows, columns, options \\ [])

View Source
new(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 to 0.

Examples

 Max.new(10, 5) # 10 x 5 matrix
 Max.new(10, 5, default: 70) # 70 as a default value
Link to this function

position_to_index(max, arg)

View Source
position_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
Link to this function

reset(matrix, position)

View Source
reset(t(), position()) :: t()

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
Link to this function

reshape(matrix, rows, columns)

View Source
reshape(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]]
Link to this function

row_to_list(matrix, row)

View Source
row_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]
Link to this function

set(matrix, position, value)

View Source
set(t(), position(), any()) :: t()

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
Link to this function

set_column(matrix, column_index, column_matrix)

View Source
set_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],
]
Link to this function

set_row(matrix, row_index, row_matrix)

View Source
set_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
Link to this function

sparse_foldl(max, fun, acc)

View Source
sparse_foldl(t(), function(), any()) :: any()

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
Link to this function

sparse_foldr(max, fun, acc)

View Source
sparse_foldr(t(), function(), any()) :: any()

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
Link to this function

sparse_map(matrix, fun)

View Source
sparse_map(t(), (... -> any())) :: t()

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
Link to this function

sparse_size(max)

View Source
sparse_size(t()) :: pos_integer()

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
Link to this function

submatrix(matrix, row_range, col_range, options \\ [])

View Source

Returns a submatrix from the given matrix. Ranges are inclusive.

Options

  • :default - (term) the default value of the matrix. Defaults to 0.

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
Link to this function

to_list(max)

View Source
to_list(t()) :: list()

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]
Link to this function

to_list_of_lists(matrix)

View Source
to_list_of_lists(t()) :: list()

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],
]
Link to this function

trace(matrix)

View Source
trace(t()) :: number()

Trace of matrix (sum of all diagonal elements).

Examples

iex> matrix = Max.new(3, 3, default: 1)
iex> matrix |> Max.trace()
3
Link to this function

transpose(matrix)

View Source
transpose(t()) :: t()

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]
]