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.

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.

Reshape vector or matrix. The `row` and `col` numbers must be positive number. By the `row` or `col` number you can change shape of matrix, respectively create new from vector.

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.

`@type dimension() :: {pos_integer(), pos_integer()} | pos_integer()`
`@type index() :: {non_neg_integer(), non_neg_integer()}`
`@type submatrix() :: number() | MatrixReloaded.Vector.t() | t()`
`@type t() :: [MatrixReloaded.Vector.t()]`

View Source
`@spec 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 Examples

``````iex> mat1 = {:ok, [[1, 2, 3], [4, 5, 6], [7, 8, 9]]}
{:ok,
[
[2, 3, 4],
[5, 6, 7],
[8, 9, 10]
]
}``````

concat_col(matrix1, matrix2)

View Source
`@spec 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 Example:

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

concat_row(matrix1, matrix2)

View Source
`@spec 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 Example:

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

diag(vector, k \\ 0)

View Source
`@spec diag(MatrixReloaded.Vector.t(), integer()) :: Result.t(String.t(), t())`

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 Example:

``````iex> MatrixReloaded.Matrix.diag([1, 2, 3])
{:ok,
[
[1, 0, 0],
[0, 2, 0],
[0, 0, 3]
]
}
{:ok,
[
[0, 1, 0, 0],
[0, 0, 2, 0],
[0, 0, 0, 3],
[0, 0, 0, 0]
]
}``````

drop_col(matrix, cols)

View Source
```@spec drop_col(t(), non_neg_integer() | [non_neg_integer()]) ::
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 Example:

``````iex> mat = [[0, 0, 0, 0], [0, 0, 1, 2], [0, 0, 3, 4], [0, 0, 0, 0]]
{:ok,
[
[0, 0, 0],
[0, 0, 2],
[0, 0, 4],
[0, 0, 0]
]
}

iex> mat = [[0, 0, 0, 0], [0, 0, 1, 2], [0, 0, 3, 4], [0, 0, 0, 0]]
{:ok,
[
[0, 0],
[1, 2],
[3, 4],
[0, 0]
]
}``````

drop_row(matrix, rows)

View Source
```@spec drop_row(t(), non_neg_integer() | [non_neg_integer()]) ::
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 matrix.

example Example:

``````iex> mat = [[0, 0, 0, 0], [0, 0, 1, 2], [0, 0, 3, 4], [0, 0, 0, 0]]
{:ok,
[
[0, 0, 0, 0],
[0, 0, 1, 2],
[0, 0, 0, 0]
]
}

iex> mat = [[0, 0, 0, 0], [0, 0, 1, 2], [0, 0, 3, 4], [0, 0, 0, 0]]
{:ok,
[
[0, 0, 1, 2],
[0, 0, 3, 4]
]
}``````

flip_lr(matrix)

View Source
`@spec flip_lr(t()) :: t()`

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

example Example:

``````iex> mat = [[1,2,3], [4,5,6], [7,8,9]]
[
[3, 2, 1],
[6, 5, 4],
[9, 8, 7]
]``````

flip_ud(matrix)

View Source
`@spec flip_ud(t()) :: t()`

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

example Example:

``````iex> mat = [[1,2,3], [4,5,6], [7,8,9]]
[
[7, 8, 9],
[4, 5, 6],
[1, 2, 3]
]``````

get_col(matrix, col_num)

View Source
```@spec get_col(t(), non_neg_integer()) ::

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 Example:

``````iex> mat = [[0, 0, 0, 0], [0, 0, 1, 2], [0, 0, 3, 4], [0, 0, 0, 0]]
{:ok, [[0], [2], [4], [0]]}``````

get_col(matrix, index, num_of_el)

View Source
```@spec get_col(t(), index(), non_neg_integer()) ::

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 Example:

``````iex> mat = [[0, 0, 0, 0], [0, 0, 1, 2], [0, 0, 3, 4], [0, 0, 0, 0]]
{:ok, [[1], [3]]}``````

get_element(matrix, index)

View Source
`@spec 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 Example:

``````iex> mat = [[0, 0, 0, 0], [0, 0, 1, 2], [0, 0, 3, 4], [0, 0, 0, 0]]
{:ok, 3}``````

get_row(matrix, row_num)

View Source
```@spec get_row(t(), non_neg_integer()) ::

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 Example:

``````iex> mat = [[0, 0, 0, 0], [0, 0, 1, 2], [0, 0, 3, 4], [0, 0, 0, 0]]
{:ok, [0, 0, 1, 2]}``````

get_row(matrix, index, num_of_el)

View Source
```@spec get_row(t(), index(), non_neg_integer()) ::

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 Example:

``````iex> mat = [[0, 0, 0, 0], [0, 0, 1, 2], [0, 0, 3, 4], [0, 0, 0, 0]]
{:ok, [0, 3]}``````

get_submatrix(matrix, index, dimension)

View Source
`@spec 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 Example:

``````iex> mat = [[0, 0, 0, 0], [0, 0, 1, 2], [0, 0, 3, 4], [0, 0, 0, 0]]
{:ok,
[
[1, 2],
[3, 4]
]
}

iex> mat = [[0, 0, 0, 0], [0, 0, 0, 0], [0, 1, 2, 3], [0, 4, 5, 6]]
iex> MatrixReloaded.Matrix.get_submatrix(mat, {2, 1}, {3, 3})
{:ok,
[
[1, 2, 3],
[4, 5, 6]
]
}``````

new(dimension, val \\ 0)

View Source
`@spec 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 Examples

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

{:ok, [[-10, -10, -10], [-10, -10, -10]]}``````

product(matrix1, matrix2)

View Source
`@spec 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 Examples

``````iex> mat1 = {:ok, [[1, 2], [3, 4], [5, 6], [7, 8]]}
iex> mat2 = {:ok, [[1, 2 ,3], [4, 5, 6]]}
{:ok,
[
[9, 12, 15],
[19, 26, 33],
[29, 40, 51],
[39, 54, 69]
]
}``````

reshape(vector, row, col)

View Source
```@spec reshape(MatrixReloaded.Vector.t() | t(), pos_integer(), pos_integer()) ::

Reshape vector or matrix. The `row` and `col` numbers must be positive number. By the `row` or `col` number you can change shape of matrix, respectively create new from vector.

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

example Example:

``````iex> 1..10 |> Enum.to_list |> MatrixReloaded.Matrix.reshape(5, 2)
{:ok,
[
[1, 2],
[3, 4],
[5, 6],
[7, 8],
[9, 10]
]
}

{:ok,
[
[0, 0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0, 0,]
]
}``````

schur_product(matrix1, matrix2)

View Source
`@spec 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 Examples

``````iex> mat1 = {:ok, [[1, 2, 3], [5, 6, 7]]}
iex> mat2 = {:ok, [[1, 2 ,3], [4, 5, 6]]}
{:ok,
[
[1, 4, 9],
[20, 30, 42]
]
}``````
`@spec size(t()) :: {pos_integer(), pos_integer()}`

The size (dimensions) of the matrix.

Returns tuple of {row_size, col_size}.

example Example:

``````iex> MatrixReloaded.Matrix.new({3,4}) |> Result.map(&MatrixReloaded.Matrix.size(&1))
{:ok, {3, 4}}``````

sub(matrix1, matrix2)

View Source
`@spec 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 Examples

``````iex> mat1 = {:ok, [[1, 2, 3], [4, 5, 6], [7, 8, 9]]}
{:ok,
[
[0, 1, 2],
[3, 4, 5],
[6, 7, 8]
]
}``````

transpose(matrix)

View Source
`@spec transpose(t()) :: t()`

Transpose of matrix.

example Example:

``````iex> mat = [[1,2,3], [4,5,6], [7,8,9]]
[
[1, 4, 7],
[2, 5, 8],
[3, 6, 9]
]``````

update(matrix, submatrix, index)

View Source
`@spec 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 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]
]
}``````

update_col(matrix, submatrix, index)

View Source
```@spec update_col(t(), MatrixReloaded.Vector.column(), 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 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]
]
}``````

update_element(matrix, el, index)

View Source
`@spec 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 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]
]
}``````

update_map(matrix, submatrix, position_indices)

View Source
`@spec 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 Example:

``````iex> mat = MatrixReloaded.Matrix.new(5)
iex> positions = [{0,0}, {3, 3}]
iex> [mat, sub_mat] |> Result.and_then_x(&MatrixReloaded.Matrix.update_map(&1, &2, 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]
]
}``````

update_row(matrix, row, index)

View Source
`@spec 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 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]
]
}``````