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 section Functions
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 │
└ ┘
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 │
└ ┘
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
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
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 │
└ ┘
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]
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 │
└ ┘
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 │
└ ┘
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 │
└ ┘
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 │
└ ┘
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 │
└ ┘
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 │
└ ┘
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 │
└ ┘
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
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 │
└ ┘
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 │
└ ┘
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 │
└ ┘
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 │
└ ┘
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 │
└ ┘
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 │
└ ┘
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}
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 │
└ ┘
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
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]
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]]
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 │
└ ┘