Class Matrix2D
java.lang.Object
io.github.andreipunko.math.matrix.Matrix2D
- Direct Known Subclasses:
MatrixXY
Represents a two-dimensional matrix of real numbers with fixed dimensions.
This class provides efficient storage and manipulation of matrix data using
a single-dimensional array for better memory locality. It supports basic
matrix operations such as element access, row/column swapping, and finding
minimum/maximum values.
The matrix is stored internally as a single-dimensional array in row-major order, where the element at position (i,j) is stored at index i*n + j in the data array.
-
Constructor Summary
ConstructorsConstructorDescriptionMatrix2D(int m, int n) Creates a new matrix with specified dimensions, initialized with zeros. -
Method Summary
Modifier and TypeMethodDescriptionvoidfill(double d) Fills all elements of the matrix with the specified value.doubleget(int i, int j) Returns the value at the specified matrix position (i,j).double[]getRow(int i) Returns a copy of the specified row as an array.doublemax()Returns the maximum value stored in the matrix.doublemin()Returns the minimum value stored in the matrix.voidset(int i, int j, double value) Sets a value at the specified matrix position (i,j).voidsetRow(int i, double[] arr) Sets an entire row of the matrix using the provided array.voidswapCols(int n1, int n2) Swaps two columns of the matrix.voidswapRows(int m1, int m2) Swaps two rows of the matrix.
-
Constructor Details
-
Matrix2D
public Matrix2D(int m, int n) Creates a new matrix with specified dimensions, initialized with zeros.- Parameters:
m- number of rows (must be positive)n- number of columns (must be positive)- Throws:
IllegalArgumentException- if m <= 0 or n <= 0
-
-
Method Details
-
set
public void set(int i, int j, double value) Sets a value at the specified matrix position (i,j).- Parameters:
i- row index (0 <= i < m)j- column index (0 <= j < n)value- value to set- Throws:
IllegalArgumentException- if i < 0 or i >= m or j < 0 or j >= n
-
setRow
public void setRow(int i, double[] arr) Sets an entire row of the matrix using the provided array.- Parameters:
i- row index (0 <= i < m)arr- array of n values to set in the row- Throws:
IllegalArgumentException- if i < 0 or i >= m or arr.length != n
-
getRow
public double[] getRow(int i) Returns a copy of the specified row as an array.- Parameters:
i- row index (0 <= i < m)- Returns:
- array containing the row elements
- Throws:
IllegalArgumentException- if i < 0 or i >= m
-
get
public double get(int i, int j) Returns the value at the specified matrix position (i,j).- Parameters:
i- row index (0 <= i < m)j- column index (0 <= j < n)- Returns:
- value at position (i,j)
- Throws:
IllegalArgumentException- if i < 0 or i >= m or j < 0 or j >= n
-
min
public double min()Returns the minimum value stored in the matrix.- Returns:
- minimum value
-
max
public double max()Returns the maximum value stored in the matrix.- Returns:
- maximum value
-
fill
public void fill(double d) Fills all elements of the matrix with the specified value.- Parameters:
d- value to fill the matrix with
-
swapRows
public void swapRows(int m1, int m2) Swaps two rows of the matrix.- Parameters:
m1- index of the first row (0 <= m1 < m)m2- index of the second row (0 <= m2 < m)- Throws:
IllegalArgumentException- if m1 < 0 or m1 >= m or m2 < 0 or m2 >= m or m1 = m2
-
swapCols
public void swapCols(int n1, int n2) Swaps two columns of the matrix.- Parameters:
n1- index of the first column (0 <= n1 < n)n2- index of the second column (0 <= n2 < n)- Throws:
IllegalArgumentException- if n1 < 0 or n1 >= n or n2 < 0 or n2 >= n or n1 = n2
-