Class Matrix<T>

java.lang.Object
io.openml.gearbox.Matrix<T>
Type Parameters:
T - The type of cell value in the tensor

public class Matrix<T> extends Object
A 2D tensor.
  • Constructor Details

    • Matrix

      public Matrix(T[][] matrix)
      Constructor.
      Parameters:
      matrix - The tensor data. A shallow defensive copy is made. Changes to the original matrix structure will not affect this object, but changes to its mutable elements will be reflected.
      Throws:
      IllegalArgumentException - if matrix is null, empty, have no columns, or non-rectangular
  • Method Details

    • getNumRows

      public int getNumRows()
      Returns the number of rows within this Matrix.
      Returns:
      the height of the matrix
    • getNumCols

      public int getNumCols()
      Returns the number of columns in this Matrix.
      Returns:
      the width of the matrix
    • get

      public T get(int row, int col)
      Returns the data stored at specified row and column in this matrix.
      Parameters:
      row - The row index
      col - The column index
      Returns:
      the data
      Throws:
      IllegalArgumentException - if row or col is out-of-bound
    • set

      public void set(int row, int col, T value)
      Mutates the data stored at specified row and column in this matrix.
      Parameters:
      row - The row index
      col - The column index
      value - The new value
      Throws:
      IllegalArgumentException - if row or col is out-of-bound
    • traverseBfs

      public void traverseBfs(int startRow, int startCol, BiConsumer<Integer,Integer> processCurrentCell, BiPredicate<Integer,Integer> shouldEnqueue)
      Traversing all cells of this matrix using the BFS (Breadth-First search) algorithm.

      In this 2D version, the neighbors are defined as adjacent cells (up, down, left, right); diagonals are not considered as adjacent and therefore not neighbors.

       Create a queue to store the cells to be visited.
      
       While the queue is not empty:
           Dequeue a cell (current row, current column).
           Process the current cell (e.g., print its value, check if it's the target).
           For each unvisited neighbor of the current cell:
               Check if the neighbor's coordinates are within the grid boundaries.
               Check if the neighbor is a valid cell to visit (e.g., not an obstacle).
               If valid and unvisited
                   mark the neighbor as visited and enqueue its coordinates.
       
      An example application on "number of island" problem could be something along these lines (written in Groovy)
       
       int LAND = 1
       int WATER = 0
       Integer[][] map = data
       Matrix<Integer> matrix = new Matrix<>(map)
       int numLands = 0
      
       for (int i = 0; i < map.length; i++) {
           for (int j = 0; j < map[0].length; j++) {
               if (matrix.get(i, j) == LAND) {
                   numLands++
                   matrix.traverseBfs(i, j, (x, y) -> matrix.set(x, y, WATER), (x, y) -> matrix.get(x, y) == LAND)
               }
           }
       }
       
      
       System.out.println(numLands)
       
      Parameters:
      startRow - The starting row index of the traversal
      startCol - The starting column index of the traversal
      processCurrentCell - The logic for processing the current cell
      shouldEnqueue - An extra check after the "valid and unvisited" passed
      Throws:
      IllegalArgumentException - if startRow or startCol is out of bounds