matrix_reloaded v2.2.1 MatrixReloaded.Matrix View Source
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
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
Link to this section Types
dimension()
View Source
dimension() :: {pos_integer(), pos_integer()} | pos_integer()
dimension() :: {pos_integer(), pos_integer()} | pos_integer()
index()
View Source
index() :: {non_neg_integer(), non_neg_integer()}
index() :: {non_neg_integer(), non_neg_integer()}
submatrix()
View Source
submatrix() :: number() | MatrixReloaded.Vector.t() | t()
submatrix() :: number() | MatrixReloaded.Vector.t() | t()
t()
View Source
t() :: [MatrixReloaded.Vector.t()]
t() :: [MatrixReloaded.Vector.t()]
Link to this section Functions
add(matrix1, matrix2) View Source
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]
]
}
concat_col(matrix1, matrix2) View Source
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]
]
}
concat_row(matrix1, matrix2) View Source
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]
]
}
diag(vector, k \\ 0)
View Source
diag(MatrixReloaded.Vector.t(), integer()) :: Result.t(String.t(), t())
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:
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]
]
}
drop_col(matrix, cols)
View Source
drop_col(t(), non_neg_integer() | [non_neg_integer()]) ::
Result.t(String.t(), t())
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:
iex> mat = [[0, 0, 0, 0], [0, 0, 1, 2], [0, 0, 3, 4], [0, 0, 0, 0]]
iex> MatrixReloaded.Matrix.drop_col(mat, 2)
{: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]]
iex> MatrixReloaded.Matrix.drop_col(mat, [0, 1])
{:ok,
[
[0, 0],
[1, 2],
[3, 4],
[0, 0]
]
}
drop_row(matrix, rows)
View Source
drop_row(t(), non_neg_integer() | [non_neg_integer()]) ::
Result.t(String.t(), t())
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:
iex> mat = [[0, 0, 0, 0], [0, 0, 1, 2], [0, 0, 3, 4], [0, 0, 0, 0]]
iex> MatrixReloaded.Matrix.drop_row(mat, 2)
{: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]]
iex> MatrixReloaded.Matrix.drop_row(mat, [0, 3])
{:ok,
[
[0, 0, 1, 2],
[0, 0, 3, 4]
]
}
flip_lr(matrix) View Source
Flip columns of matrix in the left-right direction (i.e. about a vertical axis).
Example:
iex> mat = [[1,2,3], [4,5,6], [7,8,9]]
iex> MatrixReloaded.Matrix.flip_lr(mat)
[
[3, 2, 1],
[6, 5, 4],
[9, 8, 7]
]
flip_ud(matrix) View Source
Flip rows of matrix in the up-down direction (i.e. about a horizontal axis).
Example:
iex> mat = [[1,2,3], [4,5,6], [7,8,9]]
iex> MatrixReloaded.Matrix.flip_ud(mat)
[
[7, 8, 9],
[4, 5, 6],
[1, 2, 3]
]
get_col(matrix, col_num)
View Source
get_col(t(), non_neg_integer()) ::
Result.t(String.t(), MatrixReloaded.Vector.column())
get_col(t(), non_neg_integer()) :: Result.t(String.t(), MatrixReloaded.Vector.column())
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 = [[0, 0, 0, 0], [0, 0, 1, 2], [0, 0, 3, 4], [0, 0, 0, 0]]
iex> MatrixReloaded.Matrix.get_col(mat, 3)
{:ok, [[0], [2], [4], [0]]}
get_col(matrix, index, num_of_el)
View Source
get_col(t(), index(), non_neg_integer()) ::
Result.t(String.t(), MatrixReloaded.Vector.column())
get_col(t(), index(), non_neg_integer()) :: Result.t(String.t(), MatrixReloaded.Vector.column())
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 = [[0, 0, 0, 0], [0, 0, 1, 2], [0, 0, 3, 4], [0, 0, 0, 0]]
iex> MatrixReloaded.Matrix.get_col(mat, {1, 2}, 2)
{:ok, [[1], [3]]}
get_element(matrix, index) View Source
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 = [[0, 0, 0, 0], [0, 0, 1, 2], [0, 0, 3, 4], [0, 0, 0, 0]]
iex> MatrixReloaded.Matrix.get_element(mat, {2, 2})
{:ok, 3}
get_row(matrix, row_num)
View Source
get_row(t(), non_neg_integer()) ::
Result.t(String.t(), MatrixReloaded.Vector.t())
get_row(t(), non_neg_integer()) :: Result.t(String.t(), MatrixReloaded.Vector.t())
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 = [[0, 0, 0, 0], [0, 0, 1, 2], [0, 0, 3, 4], [0, 0, 0, 0]]
iex> MatrixReloaded.Matrix.get_row(mat, 1)
{:ok, [0, 0, 1, 2]}
get_row(matrix, index, num_of_el)
View Source
get_row(t(), index(), non_neg_integer()) ::
Result.t(String.t(), MatrixReloaded.Vector.t())
get_row(t(), index(), non_neg_integer()) :: Result.t(String.t(), MatrixReloaded.Vector.t())
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 = [[0, 0, 0, 0], [0, 0, 1, 2], [0, 0, 3, 4], [0, 0, 0, 0]]
iex> MatrixReloaded.Matrix.get_row(mat, {2, 1}, 2)
{:ok, [0, 3]}
get_submatrix(matrix, index, dimension) View Source
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 = [[0, 0, 0, 0], [0, 0, 1, 2], [0, 0, 3, 4], [0, 0, 0, 0]]
iex> MatrixReloaded.Matrix.get_submatrix(mat, {1, 2}, 2)
{: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
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]]}
product(matrix1, matrix2) View Source
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]
]
}
reshape(vector, row, col)
View Source
reshape(MatrixReloaded.Vector.t() | t(), pos_integer(), pos_integer()) ::
Result.t(String.t(), MatrixReloaded.Vector.t()) | Result.t(String.t(), t())
reshape(MatrixReloaded.Vector.t() | t(), pos_integer(), pos_integer()) :: Result.t(String.t(), MatrixReloaded.Vector.t()) | Result.t(String.t(), t())
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:
iex> 1..10 |> Enum.to_list |> MatrixReloaded.Matrix.reshape(5, 2)
{:ok,
[
[1, 2],
[3, 4],
[5, 6],
[7, 8],
[9, 10]
]
}
iex> MatrixReloaded.Matrix.new({3,4}) |> Result.map(&MatrixReloaded.Matrix.reshape(&1, 2, 6))
{:ok,
[
[0, 0, 0, 0, 0, 0,],
[0, 0, 0, 0, 0, 0,]
]
}
schur_product(matrix1, matrix2) View Source
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]
]
}
size(matrix)
View Source
size(t()) :: {pos_integer(), pos_integer()}
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.map(&MatrixReloaded.Matrix.size(&1))
{:ok, {3, 4}}
sub(matrix1, matrix2) View Source
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]
]
}
transpose(matrix) View Source
Transpose of matrix.
Example:
iex> mat = [[1,2,3], [4,5,6], [7,8,9]]
iex> MatrixReloaded.Matrix.transpose(mat)
[
[1, 4, 7],
[2, 5, 8],
[3, 6, 9]
]
update(matrix, submatrix, index) View Source
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]
]
}
update_col(matrix, submatrix, index) View Source
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]
]
}
update_element(matrix, el, index) View Source
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]
]
}
update_map(matrix, submatrix, position_indices) View Source
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> [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
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]
]
}