matrex v0.3.5 Matrex View Source

Performs fast operations on matrices using native C code and CBLAS library.

Link to this section Summary

Functions

Adds two matrices. NIF

Applies function to each element of the matrix

Applies function to elements of two matrices and returns matrix of function results

Returns zero-based index of the biggest element. NIF

Get element of a matrix at given one-based (row, column) position

Get column of matrix as matrix (vector) in matrex form. One-based

Get column of matrix as list of floats. One-based, NIF

Divides two matrices element-wise. NIF

Matrix multiplication. NIF, via cblas_sgemm()

Matrix multiplication with addition of thitd matrix. NIF, via cblas_sgemm()

Matrix multiplication where the second matrix needs to be transposed. NIF, via cblas_sgemm()

Matrix multiplication where the first matrix needs to be transposed. NIF, via cblas_sgemm()

Create eye square matrix of given size

Create square matrix filled with given value. Inlined

Create matrix filled with given value. NIF

Return first element of a matrix

Displays a visualization of the matrix

Creates “magic” n*n matrix, where sums of all dimensions are equal

Maximum element in a matrix. NIF

Elementwise multiplication of two matrices. NIF

Elementwise multiplication of a scalar. NIF

Creates new matrix from list of lists

Creates new matrix from list of lists, with number of rows and columns given

Create square matrix filled with ones

Create matrix filled with ones

Create square matrix of random floats

Create matrix of random floats in [0, 1] range. NIF

Get row of matrix as matrix (vector) in matrex form. One-based

Return matrix row as list by one-based index

Return size of matrix as {rows, cols}

Substracts two matrices element-wise. NIF

Substracts the second matrix from the first. Inlined

Sums all elements. NIF

Converts to flat list. NIF

Converts to list of lists

Transposes a matrix. NIF

Create square matrix of zeros. Inlined

Create matrix of zeros of the specified size. NIF, using memset()

Link to this section Types

Link to this type element() View Source
element() :: float()
Link to this type matrex() View Source
matrex() :: %Matrex{data: binary()}

Link to this section Functions

Link to this function add(matrex1, matrex2) View Source
add(matrex(), matrex()) :: matrex()

Adds two matrices. NIF.

Example

iex> Matrex.add(Matrex.new([[1,2,3],[4,5,6]]), Matrex.new([[7,8,9],[10,11,12]]))
#Matrex[2×3]
┌                         ┐
│     8.0    10.0    12.0 │
│    14.0    16.0    18.0 │
└                         ┘
Link to this function apply(matrix, function) View Source
apply(matrex(), atom()) :: matrex()
apply(matrex(), (element() -> element())) :: matrex()
apply(matrex(), (element(), index() -> element())) :: matrex()
apply(matrex(), (element(), index(), index() -> element())) :: matrex()

Applies function to each element of the matrix.

Zero-based index of element in the matix is passed to the function along with the element value.

Examples

iex> Matrex.ones(5) |> Matrex.apply(fn val, index -> val + index end)
#Matrex[5×5]
┌                                         ┐
│     2.0     3.0     4.0     5.0     6.0 │
│     7.0     8.0     9.0    10.0    11.0 │
│    12.0    13.0    14.0    15.0    16.0 │
│    17.0    18.0    19.0    20.0    21.0 │
│    22.0    23.0    24.0    25.0    26.0 │
└                                         ┘
Link to this function apply(matrex1, matrex2, function) View Source
apply(matrex(), matrex(), (element(), element() -> element())) :: matrex()

Applies function to elements of two matrices and returns matrix of function results.

Returns zero-based index of the biggest element. NIF.

Example

iex> m = Matrex.magic(3)
#Matrex[3×3]
┌                         ┐
│     8.0     1.0     6.0 │
│     3.0     5.0     7.0 │
│     4.0     9.0     2.0 │
└                         ┘
iex> Matrex.argmax(m)
7
Link to this function at(matrex, row, col) View Source
at(matrex(), index(), index()) :: element()

Get element of a matrix at given one-based (row, column) position.

Example

iex> m = Matrex.magic(3)
#Matrex[3×3]
┌                         ┐
│     8.0     1.0     6.0 │
│     3.0     5.0     7.0 │
│     4.0     9.0     2.0 │
└                         ┘
iex> Matrex.at(m, 3, 2)
9.0

You can use Access behaviour square brackets for the same purpose, but it will be slower:

iex> m[3][2]
9.0
Link to this function column(matrex, col) View Source
column(matrex(), index()) :: matrex()

Get column of matrix as matrix (vector) in matrex form. One-based.

Example

iex> m = Matrex.magic(3)
#Matrex[3×3]
┌                         ┐
│     8.0     1.0     6.0 │
│     3.0     5.0     7.0 │
│     4.0     9.0     2.0 │
└                         ┘
iex> Matrex.column(m, 2)
#Matrex[3×1]
┌         ┐
│     1.0 │
│     5.0 │
│     9.0 │
└         ┘
Link to this function column_to_list(matrex, column) View Source
column_to_list(matrex(), index()) :: [element()]

Get column of matrix as list of floats. One-based, NIF.

Example

iex> m = Matrex.magic(3)
#Matrex[3×3]
┌                         ┐
│     8.0     1.0     6.0 │
│     3.0     5.0     7.0 │
│     4.0     9.0     2.0 │
└                         ┘
iex> Matrex.column_to_list(m, 3)
[6.0, 7.0, 2.0]
Link to this function divide(matrex1, matrex2) View Source
divide(matrex(), matrex()) :: matrex()

Divides two matrices element-wise. NIF.

Raises ErlangError if matrices’ sizes do not match.

Example

iex> Matrex.new([[10, 20, 25], [8, 9, 4]])
...> |> Matrex.divide(Matrex.new([[5, 10, 5], [4, 3, 4]]))
#Matrex[2×3]
┌                         ┐
│     2.0     2.0     5.0 │
│     2.0     3.0     1.0 │
└                         ┘
Link to this function dot(matrex1, matrex2) View Source
dot(matrex(), matrex()) :: matrex()

Matrix multiplication. NIF, via cblas_sgemm().

Number of columns of the first matrix must be equal to the number of rows of the second matrix.

Raises ErlangError if matrices’ sizes do not match.

Example

iex> Matrex.new([[1, 2, 3], [4, 5, 6]]) |>
...> Matrex.dot(Matrex.new([[1, 2], [3, 4], [5, 6]]))
#Matrex[2×2]
┌                 ┐
│    22.0    28.0 │
│    49.0    64.0 │
└                 ┘
Link to this function dot_and_add(matrex1, matrex2, matrex3) View Source
dot_and_add(matrex(), matrex(), matrex()) :: matrex()

Matrix multiplication with addition of thitd matrix. NIF, via cblas_sgemm().

Raises ErlangError if matrices’ sizes do not match.

Example

iex> Matrex.new([[1, 2, 3], [4, 5, 6]]) |>
...> Matrex.dot_and_add(Matrex.new([[1, 2], [3, 4], [5, 6]]), Matrex.new([[1, 2], [3, 4]]))
#Matrex[2×2]
┌                 ┐
│    23.0    30.0 │
│    52.0    68.0 │
└                 ┘
Link to this function dot_nt(matrex1, matrex2) View Source
dot_nt(matrex(), matrex()) :: matrex()

Matrix multiplication where the second matrix needs to be transposed. NIF, via cblas_sgemm().

Raises ErlangError if matrices’ sizes do not match.

Example

iex> Matrex.new([[1, 2, 3], [4, 5, 6]]) |>
...> Matrex.dot_nt(Matrex.new([[1, 3, 5], [2, 4, 6]]))
#Matrex[2×2]
┌                 ┐
│    22.0    28.0 │
│    49.0    64.0 │
└                 ┘
Link to this function dot_tn(matrex1, matrex2) View Source
dot_tn(matrex(), matrex()) :: matrex()

Matrix multiplication where the first matrix needs to be transposed. NIF, via cblas_sgemm().

Raises ErlangError if matrices’ sizes do not match.

Example

iex> Matrex.new([[1, 4], [2, 5], [3, 6]]) |>
...> Matrex.dot_tn(Matrex.new([[1, 2], [3, 4], [5, 6]]))
#Matrex[2×2]
┌                 ┐
│    22.0    28.0 │
│    49.0    64.0 │
└                 ┘

Create eye square matrix of given size

Example

iex> Matrex.eye(3)
#Matrex[3×3]
┌                         ┐
│     1.0     0.0     0.0 │
│     0.0     1.0     0.0 │
│     0.0     0.0     1.0 │
└                         ┘
Link to this function fill(size, value) View Source
fill(index(), number()) :: matrex()

Create square matrix filled with given value. Inlined.

Example

iex> Matrex.fill(3, 55)
#Matrex[3×3]
┌                         ┐
│    33.0    33.0    33.0 │
│    33.0    33.0    33.0 │
│    33.0    33.0    33.0 │
└                         ┘
Link to this function fill(rows, cols, value) View Source
fill(index(), index(), number()) :: matrex()

Create matrix filled with given value. NIF.

Example

iex> Matrex.fill(4,3, 55)
#Matrex[4×3]
┌                         ┐
│    55.0    55.0    55.0 │
│    55.0    55.0    55.0 │
│    55.0    55.0    55.0 │
│    55.0    55.0    55.0 │
└                         ┘

Return first element of a matrix.

Example

iex> Matrex.new([[6,5,4],[3,2,1]]) |> Matrex.first()
6.0
Link to this function inspect(matrex, full \\ false) View Source
inspect(matrex(), boolean()) :: matrex()

Displays a visualization of the matrix.

Set the second parameter to true to show full numbers. Otherwise, they are truncated.

Creates “magic” n*n matrix, where sums of all dimensions are equal

Example

iex> Matrex.magic(5)
#Matrex[5×5]
┌                                         ┐
│    16.0    23.0     5.0     7.0    14.0 │
│    22.0     4.0     6.0    13.0    20.0 │
│     3.0    10.0    12.0    19.0    21.0 │
│     9.0    11.0    18.0    25.0     2.0 │
│    15.0    17.0    24.0     1.0     8.0 │
└                                         ┘

Maximum element in a matrix. NIF.

Example

iex> m = Matrex.magic(5)
#Matrex[5×5]
┌                                         ┐
│    16.0    23.0     5.0     7.0    14.0 │
│    22.0     4.0     6.0    13.0    20.0 │
│     3.0    10.0    12.0    19.0    21.0 │
│     9.0    11.0    18.0    25.0     2.0 │
│    15.0    17.0    24.0     1.0     8.0 │
└                                         ┘
iex> Matrex.max(m)
25.0
Link to this function multiply(matrex1, matrex2) View Source
multiply(matrex(), matrex()) :: matrex()

Elementwise multiplication of two matrices. NIF.

Raises ErlangError if matrices’ sizes do not match.

Example

iex> Matrex.new([[1, 2, 3], [4, 5, 6]]) |>
...> Matrex.multiply(Matrex.new([[5, 2, 1], [3, 4, 6]]))
#Matrex[2×3]
┌                         ┐
│     5.0     4.0     3.0 │
│    12.0    20.0    36.0 │
└                         ┘
Link to this function multiply_with_scalar(matrex, scalar) View Source
multiply_with_scalar(matrex(), number()) :: matrex()

Elementwise multiplication of a scalar. NIF.

Example

iex> Matrex.new([[1, 2, 3], [4, 5, 6]]) |> Matrex.multiply_with_scalar(2)
#Matrex[2×3]
┌                         ┐
│     2.0     4.0     6.0 │
│     8.0    10.0    12.0 │
└                         ┘
Link to this function new(list_of_lists) View Source
new([[element()]]) :: matrex()

Creates new matrix from list of lists.

Example

iex> Matrex.new([[1, 2, 3], [4, 5, 6]])
#Matrex[2×3]
┌                         ┐
│     1.0     2.0     3.0 │
│     4.0     5.0     6.0 │
└                         ┘
Link to this function new(rows, columns, function) View Source
new(index(), index(), (() -> element())) :: matrex()
new(index(), index(), (index(), index() -> element())) :: matrex()
new(index(), index(), [[element()]]) :: matrex()

Creates new matrix from list of lists, with number of rows and columns given.

Works faster, than new() without matrix size, but it will be noticeable only with big matrices.

Example

iex> Matrex.new(2, 3, [[1, 2, 3], [4, 5, 6]])
#Matrex[2×3]
┌                         ┐
│     1.0     2.0     3.0 │
│     4.0     5.0     6.0 │
└                         ┘

Create square matrix filled with ones.

Example

iex> Matrex.ones(3)
#Matrex[3×3]
┌                         ┐
│     1.0     1.0     1.0 │
│     1.0     1.0     1.0 │
│     1.0     1.0     1.0 │
└                         ┘
Link to this function ones(rows, cols) View Source
ones(index(), index()) :: matrex()

Create matrix filled with ones.

Create square matrix of random floats.

Example

iex> Matrex.random(3)
#Matrex[3×3]
┌                         ┐
│ 0.66438 0.31026 0.98602 │
│ 0.82127 0.04701 0.13278 │
│ 0.96935 0.70772 0.98738 │
└                         ┘
Link to this function random(rows, columns) View Source
random(index(), index()) :: matrex()

Create matrix of random floats in [0, 1] range. NIF.

Example

iex> Matrex.random(4,3)
#Matrex[4×3]
┌                         ┐
│ 0.32994 0.28736 0.88012 │
│ 0.51782 0.68608 0.29976 │
│ 0.52953  0.9071 0.26743 │
│ 0.82189 0.59311  0.8451 │
└                         ┘

Get row of matrix as matrix (vector) in matrex form. One-based.

Example

iex> m = Matrex.magic(5)
#Matrex[5×5]
┌                                         ┐
│    16.0    23.0     5.0     7.0    14.0 │
│    22.0     4.0     6.0    13.0    20.0 │
│     3.0    10.0    12.0    19.0    21.0 │
│     9.0    11.0    18.0    25.0     2.0 │
│    15.0    17.0    24.0     1.0     8.0 │
└                                         ┘
iex> Matrex.row(m, 4)
#Matrex[1×5]
┌                                         ┐
│     9.0    11.0    18.0    25.0     2.0 │
└                                         ┘
Link to this function row_to_list(matrex, row) View Source
row_to_list(matrex(), index()) :: [element()]

Return matrix row as list by one-based index.

Example

iex> m = Matrex.magic(5)
#Matrex[5×5]
┌                                         ┐
│    16.0    23.0     5.0     7.0    14.0 │
│    22.0     4.0     6.0    13.0    20.0 │
│     3.0    10.0    12.0    19.0    21.0 │
│     9.0    11.0    18.0    25.0     2.0 │
│    15.0    17.0    24.0     1.0     8.0 │
└                                         ┘
iex> Matrex.row_to_list(m, 3)
[3.0, 10.0, 12.0, 19.0, 21.0]

Return size of matrix as {rows, cols}

Example

iex> m = Matrex.random(2,3)
#Matrex[2×3]
┌                         ┐
│ 0.69745 0.23668 0.36376 │
│ 0.63423 0.29651 0.22844 │
└                         ┘
iex> Matrex.size(m)
{2, 3}
Link to this function substract(matrex1, matrex2) View Source
substract(matrex(), matrex()) :: matrex()

Substracts two matrices element-wise. NIF.

Raises ErlangError if matrices’ sizes do not match.

Example

iex> Matrex.new([[1, 2, 3], [4, 5, 6]]) |>
...> Matrex.substract(Matrex.new([[5, 2, 1], [3, 4, 6]]))
#Matrex[2×3]
┌                         ┐
│    -4.0     0.0     2.0 │
│     1.0     1.0     0.0 │
└                         ┘
Link to this function substract_inverse(first, second) View Source
substract_inverse(matrex(), matrex()) :: matrex()

Substracts the second matrix from the first. Inlined.

Raises ErlangError if matrices’ sizes do not match.

Example

iex> Matrex.new([[1, 2, 3], [4, 5, 6]]) |>
...> Matrex.substract_inverse(Matrex.new([[5, 2, 1], [3, 4, 6]]))
#Matrex[2×3]
┌                         ┐
│     4.0     0.0    -2.0 │
│    -1.0    -1.0     0.0 │
└                         ┘

Sums all elements. NIF.

Example

iex> m = Matrex.magic(3)
#Matrex[3×3]
┌                         ┐
│     8.0     1.0     6.0 │
│     3.0     5.0     7.0 │
│     4.0     9.0     2.0 │
└                         ┘
iex> Matrex.sum(m)
45.0
Link to this function to_list(matrex) View Source
to_list(matrex()) :: [element()]

Converts to flat list. NIF.

Example

iex> m = Matrex.magic(3)
#Matrex[3×3]
┌                         ┐
│     8.0     1.0     6.0 │
│     3.0     5.0     7.0 │
│     4.0     9.0     2.0 │
└                         ┘
iex> Matrex.to_list(m)
[8.0, 1.0, 6.0, 3.0, 5.0, 7.0, 4.0, 9.0, 2.0]
Link to this function to_list_of_lists(matrex) View Source
to_list_of_lists(matrex()) :: [[element()]]

Converts to list of lists

Example

iex> m = Matrex.magic(3)
#Matrex[3×3]
┌                         ┐
│     8.0     1.0     6.0 │
│     3.0     5.0     7.0 │
│     4.0     9.0     2.0 │
└                         ┘
iex> Matrex.to_list_of_lists(m)
[[8.0, 1.0, 6.0], [3.0, 5.0, 7.0], [4.0, 9.0, 2.0]]
Link to this function transpose(matrex) View Source
transpose(matrex()) :: matrex()

Transposes a matrix. NIF.

Example

iex> m = Matrex.new([[1,2,3],[4,5,6]])
#Matrex[2×3]
┌                         ┐
│     1.0     2.0     3.0 │
│     4.0     5.0     6.0 │
└                         ┘
iex> Matrex.transpose(m)
#Matrex[3×2]
┌                 ┐
│     1.0     4.0 │
│     2.0     5.0 │
│     3.0     6.0 │
└                 ┘

Create square matrix of zeros. Inlined.

Example

iex> Matrex.zeros(3)
#Matrex[3×3]
┌                         ┐
│     0.0     0.0     0.0 │
│     0.0     0.0     0.0 │
│     0.0     0.0     0.0 │
└                         ┘
Link to this function zeros(rows, cols) View Source
zeros(index(), index()) :: matrex()

Create matrix of zeros of the specified size. NIF, using memset().

Faster, than fill(rows, cols, 0).

Example

iex> Matrex.zeros(4,3)
#Matrex[4×3]
┌                         ┐
│     0.0     0.0     0.0 │
│     0.0     0.0     0.0 │
│     0.0     0.0     0.0 │
│     0.0     0.0     0.0 │
└                         ┘