matrix_reloaded v1.0.2 MatrixReloaded.Matrix

Provides a set of functions to work with matrices.

Don't forget, numbering of row and column starts from 0 and goes to m - 1 and n - 1 where {m, n} is dimension (size) of matrix.

Link to this section Summary

Functions

Summation of two matrices. Sizes (dimensions) of both matrices must be same. Otherwise you get an error message

Concatenate matrices vertically. Both matrices must have same a column dimension

Concatenate matrices horizontally. Both matrices must have same a row dimension

Creates a square diagonal matrix with the elements of vector on the main diagonal or on lower/upper bidiagonal if diagonal number k is k < 0 or 0 < k. This number k must be integer

Drops the column or list of columns from the matrix. The column number (or column numbers) must be positive integer

Drops the row or list of rows from the matrix. The row number (or row numbers) must be positive integer

Flip columns of matrix in the left-right direction (i.e. about a vertical axis)

Flip rows of matrix in the up-down direction (i.e. about a horizontal axis)

Gets a whole column from the matrix. By column number you can select the column which you want

Gets a part column from the matrix. By index and positive number you can select the column and elements which you want

Gets an element from the matrix. By index you can select an element

Gets a whole row from the matrix. By row number you can select the row which you want

Gets a part row from the matrix. By index and positive number you can select the row and elements which you want

Gets a submatrix from the matrix. By index you can select a submatrix. Dimension of submatrix is given by positive number (result then will be a square matrix) or tuple of two positive numbers (you get then a rectangular matrix)

Creates a new matrix of the specified size. In case of positive number you get a squared matrix, for tuple {m, n} you get a rectangular matrix. For negative values you get an error message. All elements of the matrix are filled with the default value 0. This value can be changed

Product of two matrices. If matrix A has a size n × p and matrix B has a size p × m then their matrix product A*B is matrix of size n × m. Otherwise you get an error message

Schur product (or the Hadamard product) of two matrices. It produces another matrix where each element i, j is the product of elements i, j of the original two matrices. Sizes (dimensions) of both matrices must be same. Otherwise you get an error message

The size (dimensions) of the matrix

Subtraction of two matrices. Sizes (dimensions) of both matrices must be same. Otherwise you get an error message

Transpose of matrix

Updates the matrix by given a submatrix. The position of submatrix inside matrix is given by index {row_num, col_num} and dimension of submatrix. Size of submatrix must be less than or equal to size of matrix. Otherwise you get an error message. The values of indices start from 0 to matrix row size - 1. Similarly for col size

Updates column in the matrix by given a column vector. The column which you want to change is given by tuple {row_num, col_num}. Both values are non negative integers

Updates the matrix by given a number. The position of element in matrix which you want to change is given by tuple {row_num, col_num}

Updates the matrix by given a submatrices. The positions (or locations) of these submatrices are given by list of indices. Index of the individual submatrices is tuple of two numbers. These two numbers are number row and number column of matrix where the submatrices will be located. All submatrices must have same size (dimension)

Updates row in the matrix by given a row vector (list) of numbers. The row which you want to change is given by tuple {row_num, col_num}. Both values are non negative integers

Link to this section Types

Link to this section Functions

Link to this function

add(matrix1, matrix2)
add(t(), t()) :: Result.t(String.t(), t())

Summation of two matrices. Sizes (dimensions) of both matrices must be same. Otherwise you get an error message.

Returns result, it means either tuple of {:ok, matrix} or {:error, "msg"}.

Examples

iex> mat1 = {:ok, [[1, 2, 3], [4, 5, 6], [7, 8, 9]]}
iex> mat2 = MatrixReloaded.Matrix.new(3,1)
iex> Result.and_then_x([mat1, mat2], &MatrixReloaded.Matrix.add(&1, &2))
{:ok,
  [
    [2, 3, 4],
    [5, 6, 7],
    [8, 9, 10]
  ]
}
Link to this function

concat_col(matrix1, matrix2)
concat_col(t(), t()) :: Result.t(String.t(), t())

Concatenate matrices vertically. Both matrices must have same a column dimension.

Returns result, it means either tuple of {:ok, matrix} or {:error, "msg"}.

Example:

iex> mat1 = MatrixReloaded.Matrix.diag([1, 1, 1])
iex> mat2 = MatrixReloaded.Matrix.diag([2, 2, 2])
iex> Result.and_then_x([mat1, mat2], &MatrixReloaded.Matrix.concat_col(&1, &2))
{:ok,
  [
    [1, 0, 0],
    [0, 1, 0],
    [0, 0, 1],
    [2, 0, 0],
    [0, 2, 0],
    [0, 0, 2]
  ]
}
Link to this function

concat_row(matrix1, matrix2)
concat_row(t(), t()) :: Result.t(String.t(), t())

Concatenate matrices horizontally. Both matrices must have same a row dimension.

Returns result, it means either tuple of {:ok, matrix} or {:error, "msg"}.

Example:

iex> mat1 = MatrixReloaded.Matrix.diag([1, 1, 1])
iex> mat2 = MatrixReloaded.Matrix.diag([2, 2, 2])
iex> Result.and_then_x([mat1, mat2], &MatrixReloaded.Matrix.concat_row(&1, &2))
{:ok,
  [
    [1, 0, 0, 2, 0, 0],
    [0, 1, 0, 0, 2, 0],
    [0, 0, 1, 0, 0, 2]
  ]
}

Creates a square diagonal matrix with the elements of vector on the main diagonal or on lower/upper bidiagonal if diagonal number k is k < 0 or 0 < k. This number k must be integer.

Returns result, it means either tuple of {:ok, matrix} or {:error, "msg"}.

Example:

iex> MatrixReloaded.Matrix.diag([1, 2, 3])
{:ok,
  [
    [1, 0, 0],
    [0, 2, 0],
    [0, 0, 3]
  ]
}
iex> MatrixReloaded.Matrix.diag([1, 2, 3], 1)
{:ok,
  [
    [0, 1, 0, 0],
    [0, 0, 2, 0],
    [0, 0, 0, 3],
    [0, 0, 0, 0]
  ]
}
Link to this function

drop_col(matrix, cols)
drop_col(t(), pos_integer() | MatrixReloaded.Vector.t()) ::
  Result.t(String.t(), t())

Drops the column or list of columns from the matrix. The column number (or column numbers) must be positive integer.

Returns result, it means either tuple of {:ok, matrix} or {:error, "msg"}.

Example:

iex> mat = {:ok, [[0, 0, 0, 0], [0, 0, 1, 2], [0, 0, 3, 4], [0, 0, 0, 0]]}
iex> mat |> Result.and_then(&MatrixReloaded.Matrix.drop_col(&1, 2))
{:ok,
  [
    [0, 0, 0],
    [0, 0, 2],
    [0, 0, 4],
    [0, 0, 0]
  ]
}

iex> mat = {:ok, [[0, 0, 0, 0], [0, 0, 1, 2], [0, 0, 3, 4], [0, 0, 0, 0]]}
iex> mat |> Result.and_then(&MatrixReloaded.Matrix.drop_col(&1, [0, 1]))
{:ok,
  [
    [0, 0],
    [1, 2],
    [3, 4],
    [0, 0]
  ]
}
Link to this function

drop_row(matrix, rows)
drop_row(t(), pos_integer() | MatrixReloaded.Vector.t()) ::
  Result.t(String.t(), t())

Drops the row or list of rows from the matrix. The row number (or row numbers) must be positive integer.

Returns result, it means either tuple of {:ok, matrix} or {:error, "msg"}.

Example:

iex> mat = {:ok, [[0, 0, 0, 0], [0, 0, 1, 2], [0, 0, 3, 4], [0, 0, 0, 0]]}
iex> mat |> Result.and_then(&MatrixReloaded.Matrix.drop_row(&1, 2))
{:ok,
  [
    [0, 0, 0, 0],
    [0, 0, 1, 2],
    [0, 0, 0, 0]
  ]
}

iex> mat = {:ok, [[0, 0, 0, 0], [0, 0, 1, 2], [0, 0, 3, 4], [0, 0, 0, 0]]}
iex> mat |> Result.and_then(&MatrixReloaded.Matrix.drop_row(&1, [0, 3]))
{:ok,
  [
    [0, 0, 1, 2],
    [0, 0, 3, 4]
  ]
}
Link to this function

flip_lr(matrix)
flip_lr(t()) :: Result.t(String.t(), t())

Flip columns of matrix in the left-right direction (i.e. about a vertical axis).

Returns result, it means either tuple of {:ok, matrix} or {:error, "msg"}.

Example:

iex> mat = {:ok, [[1,2,3], [4,5,6], [7,8,9]]}
iex> mat |> Result.and_then(&MatrixReloaded.Matrix.flip_lr(&1))
{:ok,
  [
    [3, 2, 1],
    [6, 5, 4],
    [9, 8, 7]
  ]
}
Link to this function

flip_ud(matrix)
flip_ud(t()) :: Result.t(String.t(), t())

Flip rows of matrix in the up-down direction (i.e. about a horizontal axis).

Returns result, it means either tuple of {:ok, matrix} or {:error, "msg"}.

Example:

iex> mat = {:ok, [[1,2,3], [4,5,6], [7,8,9]]}
iex> mat |> Result.and_then(&MatrixReloaded.Matrix.flip_ud(&1))
{:ok,
  [
    [7, 8, 9],
    [4, 5, 6],
    [1, 2, 3]
  ]
}
Link to this function

get_col(matrix, col_num)
get_col(t(), non_neg_integer()) :: Result.t(String.t(), t())

Gets a whole column from the matrix. By column number you can select the column which you want.

Returns result, it means either tuple of {:ok, number} or {:error, "msg"}.

Example:

iex> mat = {:ok, [[0, 0, 0, 0], [0, 0, 1, 2], [0, 0, 3, 4], [0, 0, 0, 0]]}
iex> mat |> Result.and_then(&MatrixReloaded.Matrix.get_col(&1, 3))
{:ok, [[0], [2], [4], [0]]}
Link to this function

get_col(matrix, index, num_of_el)
get_col(t(), index(), non_neg_integer()) :: Result.t(String.t(), t())

Gets a part column from the matrix. By index and positive number you can select the column and elements which you want.

Returns result, it means either tuple of {:ok, matrix} or {:error, "msg"}.

Example:

iex> mat = {:ok, [[0, 0, 0, 0], [0, 0, 1, 2], [0, 0, 3, 4], [0, 0, 0, 0]]}
iex> mat |> Result.and_then(&MatrixReloaded.Matrix.get_col(&1, {1, 2}, 2))
{:ok, [[1], [3]]}
Link to this function

get_element(matrix, index)
get_element(t(), index()) :: Result.t(String.t(), number())

Gets an element from the matrix. By index you can select an element.

Returns result, it means either tuple of {:ok, number} or {:error, "msg"}.

Example:

iex> mat = {:ok, [[0, 0, 0, 0], [0, 0, 1, 2], [0, 0, 3, 4], [0, 0, 0, 0]]}
iex> mat |> Result.and_then(&MatrixReloaded.Matrix.get_element(&1, {2, 2}))
{:ok, 3}
Link to this function

get_row(matrix, row_num)

Gets a whole row from the matrix. By row number you can select the row which you want.

Returns result, it means either tuple of {:ok, number} or {:error, "msg"}.

Example:

iex> mat = {:ok, [[0, 0, 0, 0], [0, 0, 1, 2], [0, 0, 3, 4], [0, 0, 0, 0]]}
iex> mat |> Result.and_then(&MatrixReloaded.Matrix.get_row(&1, 1))
{:ok, [0, 0, 1, 2]}
Link to this function

get_row(matrix, index, num_of_el)

Gets a part row from the matrix. By index and positive number you can select the row and elements which you want.

Returns result, it means either tuple of {:ok, number} or {:error, "msg"}.

Example:

iex> mat = {:ok, [[0, 0, 0, 0], [0, 0, 1, 2], [0, 0, 3, 4], [0, 0, 0, 0]]}
iex> mat |> Result.and_then(&MatrixReloaded.Matrix.get_row(&1, {2, 1}, 2))
{:ok, [0, 3]}
Link to this function

get_submatrix(matrix, index, dimension)
get_submatrix(t(), index(), dimension()) :: Result.t(String.t(), t())

Gets a submatrix from the matrix. By index you can select a submatrix. Dimension of submatrix is given by positive number (result then will be a square matrix) or tuple of two positive numbers (you get then a rectangular matrix).

Returns result, it means either tuple of {:ok, matrix} or {:error, "msg"}.

Example:

iex> mat = {:ok, [[0, 0, 0, 0], [0, 0, 1, 2], [0, 0, 3, 4], [0, 0, 0, 0]]}
iex> mat |> Result.and_then(&MatrixReloaded.Matrix.get_submatrix(&1, {1, 2}, 2))
{:ok,
  [
    [1, 2],
    [3, 4]
  ]
}

iex> mat = {:ok, [[0, 0, 0, 0], [0, 0, 0, 0], [0, 1, 2, 3], [0, 4, 5, 6]]}
iex> mat |> Result.and_then(&MatrixReloaded.Matrix.get_submatrix(&1, {2, 1}, {3, 3}))
{:ok,
  [
    [1, 2, 3],
    [4, 5, 6]
  ]
}
Link to this function

new(dimension, val \\ 0)
new(dimension(), number()) :: Result.t(String.t(), t())

Creates a new matrix of the specified size. In case of positive number you get a squared matrix, for tuple {m, n} you get a rectangular matrix. For negative values you get an error message. All elements of the matrix are filled with the default value 0. This value can be changed.

Returns result, it means either tuple of {:ok, matrix} or {:error, "msg"}.

Examples

iex> MatrixReloaded.Matrix.new(3)
{:ok, [[0, 0, 0], [0, 0, 0], [0, 0, 0]]}

iex> MatrixReloaded.Matrix.new({2, 3}, -10)
{:ok, [[-10, -10, -10], [-10, -10, -10]]}
Link to this function

product(matrix1, matrix2)
product(t(), t()) :: Result.t(String.t(), t())

Product of two matrices. If matrix A has a size n × p and matrix B has a size p × m then their matrix product A*B is matrix of size n × m. Otherwise you get an error message.

Returns result, it means either tuple of {:ok, matrix} or {:error, "msg"}.

Examples

iex> mat1 = {:ok, [[1, 2], [3, 4], [5, 6], [7, 8]]}
iex> mat2 = {:ok, [[1, 2 ,3], [4, 5, 6]]}
iex> Result.and_then_x([mat1, mat2], &MatrixReloaded.Matrix.product(&1, &2))
{:ok,
  [
    [9, 12, 15],
    [19, 26, 33],
    [29, 40, 51],
    [39, 54, 69]
  ]
}
Link to this function

schur_product(matrix1, matrix2)
schur_product(t(), t()) :: Result.t(String.t(), t())

Schur product (or the Hadamard product) of two matrices. It produces another matrix where each element i, j is the product of elements i, j of the original two matrices. Sizes (dimensions) of both matrices must be same. Otherwise you get an error message.

Returns result, it means either tuple of {:ok, matrix} or {:error, "msg"}.

Examples

iex> mat1 = {:ok, [[1, 2, 3], [5, 6, 7]]}
iex> mat2 = {:ok, [[1, 2 ,3], [4, 5, 6]]}
iex> Result.and_then_x([mat1, mat2], &MatrixReloaded.Matrix.schur_product(&1, &2))
{:ok,
  [
    [1, 4, 9],
    [20, 30, 42]
  ]
}
Link to this function

size(matrix)
size(t()) :: {pos_integer(), pos_integer()}

The size (dimensions) of the matrix.

Returns tuple of {row_size, col_size}.

Example:

iex> MatrixReloaded.Matrix.new({3,4}) |> Result.and_then(&MatrixReloaded.Matrix.size(&1))
{3, 4}
Link to this function

sub(matrix1, matrix2)
sub(t(), t()) :: Result.t(String.t(), t())

Subtraction of two matrices. Sizes (dimensions) of both matrices must be same. Otherwise you get an error message.

Returns result, it means either tuple of {:ok, matrix} or {:error, "msg"}.

Examples

iex> mat1 = {:ok, [[1, 2, 3], [4, 5, 6], [7, 8, 9]]}
iex> mat2 = MatrixReloaded.Matrix.new(3,1)
iex> Result.and_then_x([mat1, mat2], &MatrixReloaded.Matrix.sub(&1, &2))
{:ok,
  [
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8]
  ]
}
Link to this function

transpose(matrix)
transpose(t()) :: Result.t(String.t(), t())

Transpose of matrix.

Returns result, it means either tuple of {:ok, matrix} or {:error, "msg"}.

Example:

iex> mat = {:ok, [[1,2,3], [4,5,6], [7,8,9]]}
iex> mat |> Result.and_then(&MatrixReloaded.Matrix.transpose(&1))
{:ok,
  [
    [1, 4, 7],
    [2, 5, 8],
    [3, 6, 9]
  ]
}
Link to this function

update(matrix, submatrix, index)
update(t(), submatrix(), index()) :: Result.t(String.t(), t())

Updates the matrix by given a submatrix. The position of submatrix inside matrix is given by index {row_num, col_num} and dimension of submatrix. Size of submatrix must be less than or equal to size of matrix. Otherwise you get an error message. The values of indices start from 0 to matrix row size - 1. Similarly for col size.

Returns result, it means either tuple of {:ok, matrix} or {:error, "msg"}.

Example:

iex> mat = MatrixReloaded.Matrix.new(4)
iex> mat |> Result.and_then(&MatrixReloaded.Matrix.update(&1, [[1,2],[3,4]], {1,2}))
{:ok,
  [
    [0, 0, 0, 0],
    [0, 0, 1, 2],
    [0, 0, 3, 4],
    [0, 0, 0, 0]
  ]
}
Link to this function

update_col(matrix, submatrix, index)
update_col(t(), t(), index()) :: Result.t(String.t(), t())

Updates column in the matrix by given a column vector. The column which you want to change is given by tuple {row_num, col_num}. Both values are non negative integers.

Returns result, it means either tuple of {:ok, matrix} or {:error, "msg"}.

Example:

iex> {:ok, mat} = MatrixReloaded.Matrix.new(4)
iex> MatrixReloaded.Matrix.update_col(mat, [[1], [2], [3]], {0, 1})
{:ok,
  [
    [0, 1, 0, 0],
    [0, 2, 0, 0],
    [0, 3, 0, 0],
    [0, 0, 0, 0]
  ]
}
Link to this function

update_element(matrix, el, index)
update_element(t(), number(), index()) :: Result.t(String.t(), t())

Updates the matrix by given a number. The position of element in matrix which you want to change is given by tuple {row_num, col_num}.

Returns result, it means either tuple of {:ok, matrix} or {:error, "msg"}.

Example:

iex> mat = MatrixReloaded.Matrix.new(3)
iex> mat |> Result.and_then(&MatrixReloaded.Matrix.update_element(&1, -1, {1, 1}))
{:ok,
  [
    [0, 0, 0],
    [0, -1, 0],
    [0, 0, 0]
  ]
}
Link to this function

update_map(matrix, submatrix, position_indices)
update_map(t(), submatrix(), [index()]) :: Result.t(String.t(), t())

Updates the matrix by given a submatrices. The positions (or locations) of these submatrices are given by list of indices. Index of the individual submatrices is tuple of two numbers. These two numbers are number row and number column of matrix where the submatrices will be located. All submatrices must have same size (dimension).

Returns result, it means either tuple of {:ok, matrix} or {:error, "msg"}.

Example:

iex> mat = MatrixReloaded.Matrix.new(5)
iex> sub_mat = MatrixReloaded.Matrix.new(2,1)
iex> positions = [{0,0}, {3, 3}]
iex> MatrixReloaded.Matrix.update_map(mat, sub_mat, positions)
{:ok,
  [
    [1, 1, 0, 0, 0],
    [1, 1, 0, 0, 0],
    [0, 0, 0, 0, 0],
    [0, 0, 0, 1, 1],
    [0, 0, 0, 1, 1]
  ]
}
Link to this function

update_row(matrix, row, index)
update_row(t(), MatrixReloaded.Vector.t(), index()) ::
  Result.t(String.t(), t())

Updates row in the matrix by given a row vector (list) of numbers. The row which you want to change is given by tuple {row_num, col_num}. Both values are non negative integers.

Returns result, it means either tuple of {:ok, matrix} or {:error, "msg"}.

Example:

iex> {:ok, mat} = MatrixReloaded.Matrix.new(4)
iex> MatrixReloaded.Matrix.update_row(mat, [1, 2, 3], {3, 1})
{:ok,
  [
    [0, 0, 0, 0],
    [0, 0, 0, 0],
    [0, 0, 0, 0],
    [0, 1, 2, 3]
  ]
}