Package rocks.palaiologos.maja.matrix
Class Matrix<T>
java.lang.Object
rocks.palaiologos.maja.matrix.Matrix<T>
- Type Parameters:
T
-
- Direct Known Subclasses:
ComplexMatrix
,DoubleMatrix
A class representing a two-dimensional matrix of arbitrary type.
It is recommended that the type is immutable.
- Author:
- Palaiologos
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncolumn
(int i) Create a list view of a column in the matrix.columns()
Create a nested list view of all rows in the matrix.copy()
Copy the matrix.Compute the generalised dot product of the matrix with another matrix.boolean
get
(int i, int j) Pick the element at the specified position in the matrix.boolean
Determine whether the matrix contains a specified element.int
hashCode()
int
height()
Return the height of the matrix.Map each cell of the matrix with a specified mapper.Map each cell of the matrix with a specified mapper, which takes the index of the cell as an additional argument.ravel()
Compute the ravel of the matrix, i.e.reduceFirst
(BiFunction<T, T, T> reductor) Reduce the matrix on the first axis with a specified reductor.reduceLast
(BiFunction<T, T, T> reductor) Reduce the matrix on the last axis with a specified reductor.<R> Matrix<R>
Reverse the matrix on the first axis.Reverse the matrix on the last axis.row
(int i) Create a list view of a row in the matrix.rows()
Create a nested list view of all columns in the matrix.Set the element at the specified position in the matrix.void
swap
(int srcRow, int srcCol, int dstRow, int dstCol) Swap two elements in the matrix.toString()
Transpose the matrix, i.e.int
width()
Return the width of the matrix.Zip two matrices together to produce a new matrix, using a specified zipper function.<R,
U> Matrix<R> zipWithRetype
(Matrix<U> other, BiFunction<T, U, R> zipper) Zip two matrices together to produce a new matrix, using a specified zipper function.
-
Constructor Details
-
Matrix
Create a matrix out of a two-dimensional array. The array itself is copied, but the elements are not, meaning that if the elements are mutated, the change will be reflected in the matrix.- Parameters:
data
-
-
Matrix
public Matrix(int rows, int columns) Create an empty matrix with the specified dimensions.- Parameters:
rows
-columns
-
-
Matrix
Create a matrix out of a nested list. The list itself is copied, but the elements are not, meaning that if the elements are mutated, the change will be reflected in the matrix.- Parameters:
data
-
-
-
Method Details
-
columns
Create a nested list view of all rows in the matrix. The returned list is a view, meaning that if the matrix is mutated, the change will be reflected in the list. If the view is mutated, the change will be reflected in the matrix.- Returns:
-
column
Create a list view of a column in the matrix. The returned list is a view, meaning that if the matrix is mutated, the change will be reflected in the list. If the view is mutated, the change will be reflected in the matrix.- Parameters:
i
-- Returns:
-
width
public int width()Return the width of the matrix.- Returns:
-
height
public int height()Return the height of the matrix.- Returns:
-
rows
Create a nested list view of all columns in the matrix. The returned list is a view, meaning that if the matrix is mutated, the change will be reflected in the list. If the view is mutated, the change will be reflected in the matrix.- Returns:
-
row
Create a list view of a row in the matrix. The returned list is a view, meaning that if the matrix is mutated, the change will be reflected in the list. If the view is mutated, the change will be reflected in the matrix.- Parameters:
i
-- Returns:
-
get
Pick the element at the specified position in the matrix.- Parameters:
i
- The row index.j
- The column index.- Returns:
- The element at the specified position.
-
set
Set the element at the specified position in the matrix. The old element is returned.- Parameters:
i
- The row index.j
- The column index.element
- The new element.- Returns:
- The old element.
-
swap
public void swap(int srcRow, int srcCol, int dstRow, int dstCol) Swap two elements in the matrix.- Parameters:
srcRow
- The row index of the first element.srcCol
- The column index of the first element.dstRow
- The row index of the second element.dstCol
- The column index of the second element.
-
transpose
Transpose the matrix, i.e. swap its axes.- Returns:
- The transposed matrix.
-
reduceLast
Reduce the matrix on the last axis with a specified reductor.- Parameters:
reductor
-- Returns:
-
reduceFirst
Reduce the matrix on the first axis with a specified reductor.- Parameters:
reductor
-- Returns:
-
map
Map each cell of the matrix with a specified mapper.- Parameters:
mapper
-- Returns:
-
zipWith
Zip two matrices together to produce a new matrix, using a specified zipper function.- Parameters:
other
-zipper
-- Returns:
-
reverseFirst
Reverse the matrix on the first axis.- Returns:
-
reverseLast
Reverse the matrix on the last axis.- Returns:
-
mapIdx
Map each cell of the matrix with a specified mapper, which takes the index of the cell as an additional argument.- Parameters:
mapper
-- Returns:
-
ravel
Compute the ravel of the matrix, i.e. a list of all elements in it. The ravel follows a row-major order.- Returns:
-
has
Determine whether the matrix contains a specified element.- Parameters:
element
-- Returns:
-
dot
Compute the generalised dot product of the matrix with another matrix. The generalised dot product is defined as follows:(A o B) = sum_{i,j} (A_{i,j} o B_{i,j})
Where o is the *scalar* product and sum is the *vector* sum. For example, to multiply two matrices, one would use:
dot(A, B, (a, b) -> a * b, (a, b) -> a + b)
- Parameters:
another
- The other matrix.scalar
- The scalar product.vector
- The vector sum.- Returns:
- The generalised dot product.
- Throws:
IllegalArgumentException
- If the matrices are not aligned.
-
retype
-
zipWithRetype
Zip two matrices together to produce a new matrix, using a specified zipper function. The zipper function may change the type of the matrix.- Parameters:
other
-zipper
-- Returns:
-
copy
Copy the matrix.- Returns:
-
hashCode
public int hashCode() -
equals
-
toString
-