Class Matrix<T>

java.lang.Object
rocks.palaiologos.maja.matrix.Matrix<T>
Type Parameters:
T -
Direct Known Subclasses:
ComplexMatrix, DoubleMatrix

public class Matrix<T> extends Object
A class representing a two-dimensional matrix of arbitrary type. It is recommended that the type is immutable.
Author:
Palaiologos
  • Constructor Details

    • Matrix

      public Matrix(T[][] data)
      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

      public Matrix(List<List<T>> data)
      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

      public List<List<T>> 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

      public List<T> column(int i)
      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

      public List<List<T>> 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

      public List<T> row(int i)
      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

      public T get(int i, int j)
      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

      public T set(int i, int j, T element)
      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

      public Matrix<T> transpose()
      Transpose the matrix, i.e. swap its axes.
      Returns:
      The transposed matrix.
    • reduceLast

      public List<T> reduceLast(BiFunction<T,T,T> reductor)
      Reduce the matrix on the last axis with a specified reductor.
      Parameters:
      reductor -
      Returns:
    • reduceFirst

      public List<T> reduceFirst(BiFunction<T,T,T> reductor)
      Reduce the matrix on the first axis with a specified reductor.
      Parameters:
      reductor -
      Returns:
    • map

      public Matrix<T> map(Function<T,T> mapper)
      Map each cell of the matrix with a specified mapper.
      Parameters:
      mapper -
      Returns:
    • zipWith

      public Matrix<T> zipWith(Matrix<T> other, BiFunction<T,T,T> zipper)
      Zip two matrices together to produce a new matrix, using a specified zipper function.
      Parameters:
      other -
      zipper -
      Returns:
    • reverseFirst

      public Matrix<T> reverseFirst()
      Reverse the matrix on the first axis.
      Returns:
    • reverseLast

      public Matrix<T> reverseLast()
      Reverse the matrix on the last axis.
      Returns:
    • mapIdx

      public Matrix<T> mapIdx(BiFunction<Pair<Integer,Integer>,T,T> mapper)
      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

      public List<T> 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

      public boolean has(T element)
      Determine whether the matrix contains a specified element.
      Parameters:
      element -
      Returns:
    • dot

      public Matrix<T> dot(Matrix<T> another, BiFunction<T,T,T> scalar, BiFunction<T,T,T> vector)
      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

      public <R> Matrix<R> retype(Function<T,R> mapper)
    • zipWithRetype

      public <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. The zipper function may change the type of the matrix.
      Parameters:
      other -
      zipper -
      Returns:
    • copy

      public Matrix<T> copy()
      Copy the matrix.
      Returns:
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object