-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.java
More file actions
48 lines (41 loc) · 1.33 KB
/
Copy pathMatrix.java
File metadata and controls
48 lines (41 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package model;
public class Matrix {
private final double[][] data;
private final int rows;
private final int cols;
public Matrix(double[][] data) {
if (data == null || data.length == 0 || data[0].length == 0) {
throw new IllegalArgumentException("Matrix cannot be null or empty.");
}
// Check for jagged arrays
int expectedCols = data[0].length;
for (int i = 1; i < data.length; i++) {
if (data[i].length != expectedCols) {
throw new IllegalArgumentException(
"Matrix must be rectangular!"
);
}
}
this.rows = data.length;
this.cols = data[0].length;
this.data = new double[rows][cols];
for (int i = 0; i < rows; i++) {
System.arraycopy(data[i], 0, this.data[i], 0, cols);
}
}
public int getRows() {
return rows;
}
public int getCols() {
return cols;
}
public double get(int row, int col) {
if (row < 0 || row >= rows || col < 0 || col >= cols) {
throw new IndexOutOfBoundsException(
String.format("Invalid indices (%d, %d) for matrix of size %dx%d",
row, col, rows, cols)
);
}
return data[row][col];
}
}