Package smile.util

Class IntArray2D

java.lang.Object
smile.util.IntArray2D

public class IntArray2D extends Object
2-dimensional array of integers. Java doesn't have real multidimensional arrays. They are just array of arrays, which are not friendly to modern CPU due to frequent cache miss. The extra (multiple times) array index checking also significantly reduces the performance. This class solves these pain points by storing data in a single 1D array of int in row major order. Note that this class is simple by design, as a replacement of int[][] in case of row by row access. For advanced matrix computation, please use Matrix.
See Also:
  • Constructor Details

    • IntArray2D

      public IntArray2D(int[][] A)
      Constructor.
      Parameters:
      A - the array of matrix.
    • IntArray2D

      public IntArray2D(int nrow, int ncol)
      Constructor of all-zero matrix.
      Parameters:
      nrow - the number of rows.
      ncol - the number of columns.
    • IntArray2D

      public IntArray2D(int nrow, int ncol, int value)
      Constructor. Fill the matrix with given value.
      Parameters:
      nrow - the number of rows.
      ncol - the number of columns.
      value - the initial value.
    • IntArray2D

      public IntArray2D(int nrow, int ncol, int[] value)
      Constructor.
      Parameters:
      nrow - the number of rows.
      ncol - the number of columns.
      value - the array of matrix values arranged in row major format.
  • Method Details

    • nrow

      public int nrow()
      Returns the number of rows.
      Returns:
      the number of rows.
    • ncol

      public int ncol()
      Returns the number of columns.
      Returns:
      the number of columns.
    • apply

      public int apply(int i, int j)
      Returns A[i, j]. Useful in Scala.
      Parameters:
      i - the row index.
      j - the column index.
      Returns:
      A[i, j].
    • get

      public int get(int i, int j)
      Returns A[i, j].
      Parameters:
      i - the row index.
      j - the column index.
      Returns:
      A[i, j].
    • set

      public void set(int i, int j, int x)
      Sets A[i, j].
      Parameters:
      i - the row index.
      j - the column index.
      x - the value.
    • add

      public int add(int i, int j, int x)
      A[i, j] += x.
      Parameters:
      i - the row index.
      j - the column index.
      x - the operand.
      Returns:
      the updated cell value.
    • sub

      public int sub(int i, int j, int x)
      A[i, j] -= x.
      Parameters:
      i - the row index.
      j - the column index.
      x - the operand.
      Returns:
      the updated cell value.
    • mul

      public int mul(int i, int j, int x)
      A[i, j] *= x.
      Parameters:
      i - the row index.
      j - the column index.
      x - the operand.
      Returns:
      the updated cell value.
    • div

      public int div(int i, int j, int x)
      A[i, j] /= x.
      Parameters:
      i - the row index.
      j - the column index.
      x - the operand.
      Returns:
      the updated cell value.
    • add

      public IntArray2D add(IntArray2D B)
      A += B.
      Parameters:
      B - the operand.
      Returns:
      this object.
    • sub

      public IntArray2D sub(IntArray2D B)
      A -= B.
      Parameters:
      B - the operand.
      Returns:
      this object.
    • mul

      public IntArray2D mul(IntArray2D B)
      A *= B.
      Parameters:
      B - the operand.
      Returns:
      this object.
    • div

      public IntArray2D div(IntArray2D B)
      A /= B.
      Parameters:
      B - the operand.
      Returns:
      this object.
    • add

      public IntArray2D add(int x)
      A += x.
      Parameters:
      x - the operand.
      Returns:
      this object.
    • sub

      public IntArray2D sub(int x)
      A -= x.
      Parameters:
      x - the operand.
      Returns:
      this object.
    • mul

      public IntArray2D mul(int x)
      A *= x.
      Parameters:
      x - the operand.
      Returns:
      this object.
    • div

      public IntArray2D div(int x)
      A /= x.
      Parameters:
      x - the operand.
      Returns:
      this object.
    • sum

      public long sum()
      Returns the sum of all elements.
      Returns:
      the sum of all elements.
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • toString

      public String toString(boolean full)
      Returns the string representation of matrix.
      Parameters:
      full - Print the full matrix if true. Otherwise, print only top left 7 x 7 submatrix.
      Returns:
      the string representation of matrix.
    • toString

      public String toString(int m, int n)
      Returns the string representation of matrix.
      Parameters:
      m - the number of rows to print.
      n - the number of columns to print.
      Returns:
      the string representation of matrix.