ExMatrix

ExMatrix is an Elixir library implementing a parallel matrix multiplication algorithm and other utilitiy functions for working with matrices.

Multiplying matrices

iex> x = [[2,3], [3,5]]
[[2,3], [3,5]]
iex> y = [[1,2], [5,-1]]
[[1,2], [5,-1]]
iex> ExMatrix.pmultiply(x, y)
[[17, 1], [28, 1]]
iex> ExMatrix.multiply(x, y)
[[17, 1], [28, 1]]
Source

Summary

add(matrix_a, matrix_b)

Adds two matrices of the same dimensions, returning a new matrix of the same dimensions. If two non-matching matrices are provided an ArgumentError will be raised

add_rows(row_a, row_b)

Adds two rows together to return a new row

dot_product(row_a, row_b)

Calculates the dot-product for two rows of numbers

multiply(matrix_a, matrix_b)

Perform a sequential multiplication of the two matrices, matrix_a and matrix_b

new_matrix(rows, cols)

Creates a new matrix that has rows number of rows and cols columns. All cells are initially set to zero

pmultiply(matrix_a, matrix_b)

Perform a parallel multiplication of the two matrices, matrix_a and matrix_b

random_cells(rows, cols, max)
size(matrix)

Returns the size of the matrix as {rows, columns}

subtract(matrix_a, matrix_b)

Subtracts two matrices of the same dimensions, returning a new matrix of the same dimensions. If two non-matching matrices are provided an ArgumentError will be raised

subtract_rows(row_a, row_b)

Subtracts two rows to return a new row

transpose(cells)

Transposes the provided matrix so that a 3, 2 matrix would become a 2, 3 matrix. Once transposed the first row in the transposed matrix is actually the first column in the pre-transposed matrix

Functions

add(matrix_a, matrix_b)

Specs:

  • add([[number]], [[number]]) :: [[number]]

Adds two matrices of the same dimensions, returning a new matrix of the same dimensions. If two non-matching matrices are provided an ArgumentError will be raised.

Source
add_rows(row_a, row_b)

Specs:

  • add_rows([number], [number]) :: [number]

Adds two rows together to return a new row

Source
dot_product(row_a, row_b)

Specs:

  • dot_product([number], [number]) :: number

Calculates the dot-product for two rows of numbers

Source
multiply(matrix_a, matrix_b)

Specs:

  • multiply([[number]], [[number]]) :: [[number]]

Perform a sequential multiplication of the two matrices, matrix_a and matrix_b.

Source
new_matrix(rows, cols)

Specs:

  • new_matrix(number, number) :: [[number]]

Creates a new matrix that has rows number of rows and cols columns. All cells are initially set to zero.

Example

iex> ExMatrix.new_matrix(4, 5)
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
Source
pmultiply(matrix_a, matrix_b)

Specs:

  • pmultiply([[number]], [[number]]) :: [[number]]

Perform a parallel multiplication of the two matrices, matrix_a and matrix_b.

Source
random_cells(rows, cols, max)

Specs:

  • random_cells(integer, integer, integer) :: [[number]]
Source
size(matrix)

Specs:

  • size([[number]]) :: {number, number}

Returns the size of the matrix as {rows, columns}.

Source
subtract(matrix_a, matrix_b)

Specs:

  • subtract([[number]], [[number]]) :: [[number]]

Subtracts two matrices of the same dimensions, returning a new matrix of the same dimensions. If two non-matching matrices are provided an ArgumentError will be raised.

Source
subtract_rows(row_a, row_b)

Specs:

  • subtract_rows([number], [number]) :: [number]

Subtracts two rows to return a new row

Source
transpose(cells)

Specs:

  • transpose([[number]]) :: [[number]]

Transposes the provided matrix so that a 3, 2 matrix would become a 2, 3 matrix. Once transposed the first row in the transposed matrix is actually the first column in the pre-transposed matrix.

Example

iex> ExMatrix.transpose([[2, 4],[5, 6]])
[[2, 5], [4, 6]]
Source