Package smile.util

Class Array2D

java.lang.Object
smile.util.Array2D

public class Array2D extends Object
2-dimensional array of doubles. 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 doubles in row major order. Note that this class is simple by design, as a replacement of double[][] in case of row by row access. For advanced matrix computation, please use Matrix.
See Also:
  • Constructor Details

    • Array2D

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

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

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

      public Array2D(int nrow, int ncol, double[] 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 double 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 double 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, double x)
      Sets A[i, j].
      Parameters:
      i - the row index.
      j - the column index.
      x - the value.
    • add

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

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

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

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

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

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

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

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

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

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

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

      public Array2D div(double x)
      A /= x.
      Parameters:
      x - the operand.
      Returns:
      this object.
    • replaceNaN

      public Array2D replaceNaN(double x)
      Replaces NaN values with x.
      Parameters:
      x - the value.
      Returns:
      this object.
    • sum

      public double 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.