Package smile.util
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 Summary
ConstructorDescriptionIntArray2D
(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 Summary
Modifier and TypeMethodDescriptionadd
(int x) A += x.int
add
(int i, int j, int x) A[i, j] += x.add
(IntArray2D B) A += B.int
apply
(int i, int j) Returns A[i, j].div
(int x) A /= x.int
div
(int i, int j, int x) A[i, j] /= x.div
(IntArray2D B) A /= B.int
get
(int i, int j) Returns A[i, j].mul
(int x) A *= x.int
mul
(int i, int j, int x) A[i, j] *= x.mul
(IntArray2D B) A *= B.int
ncol()
Returns the number of columns.int
nrow()
Returns the number of rows.void
set
(int i, int j, int x) Sets A[i, j].sub
(int x) A -= x.int
sub
(int i, int j, int x) A[i, j] -= x.sub
(IntArray2D B) A -= B.long
sum()
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
-
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
A += B.- Parameters:
B
- the operand.- Returns:
- this object.
-
sub
A -= B.- Parameters:
B
- the operand.- Returns:
- this object.
-
mul
A *= B.- Parameters:
B
- the operand.- Returns:
- this object.
-
div
A /= B.- Parameters:
B
- the operand.- Returns:
- this object.
-
add
A += x.- Parameters:
x
- the operand.- Returns:
- this object.
-
sub
A -= x.- Parameters:
x
- the operand.- Returns:
- this object.
-
mul
A *= x.- Parameters:
x
- the operand.- Returns:
- this object.
-
div
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
-
toString
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
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.
-