Class IntArray2D
java.lang.Object
smile.util.IntArray2D
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 SummaryConstructorsConstructorDescriptionIntArray2D(int[][] A) Constructor.IntArray2D(int nrow, int ncol) Constructor of all-zero matrix.IntArray2D(int nrow, int ncol, int value) Constructor.IntArray2D(int nrow, int ncol, int[] value) Constructor.
- 
Method SummaryModifier and TypeMethodDescriptionadd(int x) A += x.intadd(int i, int j, int x) A[i, j] += x.add(IntArray2D B) A += B.intapply(int i, int j) Returns A[i, j].div(int x) A /= x.intdiv(int i, int j, int x) A[i, j] /= x.div(IntArray2D B) A /= B.intget(int i, int j) Returns A[i, j].mul(int x) A *= x.intmul(int i, int j, int x) A[i, j] *= x.mul(IntArray2D B) A *= B.intncol()Returns the number of columns.intnrow()Returns the number of rows.voidset(int i, int j, int x) Sets A[i, j].sub(int x) A -= x.intsub(int i, int j, int x) A[i, j] -= x.sub(IntArray2D B) A -= B.longsum()Returns the sum of all elements.toString()toString(boolean full) Returns the string representation of matrix.toString(int m, int n) Returns the string representation of matrix.
- 
Constructor Details- 
IntArray2Dpublic IntArray2D(int[][] A) Constructor.- Parameters:
- A- the array of matrix.
 
- 
IntArray2Dpublic IntArray2D(int nrow, int ncol) Constructor of all-zero matrix.- Parameters:
- nrow- the number of rows.
- ncol- the number of columns.
 
- 
IntArray2Dpublic 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.
 
- 
IntArray2Dpublic 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- 
nrowpublic int nrow()Returns the number of rows.- Returns:
- the number of rows.
 
- 
ncolpublic int ncol()Returns the number of columns.- Returns:
- the number of columns.
 
- 
applypublic 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].
 
- 
getpublic int get(int i, int j) Returns A[i, j].- Parameters:
- i- the row index.
- j- the column index.
- Returns:
- A[i, j].
 
- 
setpublic void set(int i, int j, int x) Sets A[i, j].- Parameters:
- i- the row index.
- j- the column index.
- x- the value.
 
- 
addpublic 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.
 
- 
subpublic 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.
 
- 
mulpublic 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.
 
- 
divpublic 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
- 
sub
- 
mul
- 
div
- 
add
- 
sub
- 
mul
- 
div
- 
sumpublic long sum()Returns the sum of all elements.- Returns:
- the sum of all elements.
 
- 
toString
- 
toStringReturns 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.
 
- 
toStringReturns 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.
 
 
-