Package smile.util
Class Array2D
java.lang.Object
smile.util.Array2D
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 Summary
-
Method Summary
Modifier and TypeMethodDescriptionadd
(double x) A += x.double
add
(int i, int j, double x) A[i, j] += x.A += B.double
apply
(int i, int j) Returns A[i, j].div
(double x) A /= x.double
div
(int i, int j, double x) A[i, j] /= x.A /= B.double
get
(int i, int j) Returns A[i, j].mul
(double x) A *= x.double
mul
(int i, int j, double x) A[i, j] *= x.A *= B.int
ncol()
Returns the number of columns.int
nrow()
Returns the number of rows.replaceNaN
(double x) Replaces NaN values with x.void
set
(int i, int j, double x) Sets A[i, j].sub
(double x) A -= x.double
sub
(int i, int j, double x) A[i, j] -= x.A -= B.double
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
-
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
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.
-
replaceNaN
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
-
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.
-