-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMatrix.java
167 lines (139 loc) · 4.92 KB
/
Matrix.java
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
public class Matrix {
final int rows;
final int columns;
final double[][] data;
public Matrix(int rows, int columns) {
this.rows = rows;
this.columns = columns;
this.data = new double[rows][columns];
}
public Matrix(double[][] data) {
this.rows = data.length;
this.columns = data[0].length;
this.data = data;
}
public Matrix(double[] data) {
this.rows = data.length;
this.columns = 1;
this.data = new double[rows][columns];
for (int i = 0; i < rows; i++) {
this.data[i] = new double[] { data[i] };
}
}
public static Matrix id(int n) {
double[][] result_A = new double[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
result_A[i][j] = (i == j ? 1 : 0);
}
}
return new Matrix(result_A);
}
public static Matrix Rz(float rad) {
return new Matrix(
new double[][] { { Math.cos(rad), -Math.sin(rad), 0 }, { Math.sin(rad), Math.cos(rad), 0 }, { 0, 0, 1 } });
}
public static Matrix add(Matrix matrix1, Matrix matrix2) {
if (matrix1.rows != matrix2.rows || matrix1.columns != matrix2.columns) {
throw new IllegalArgumentException("Matrices must have the same dimensions for addition.");
}
double[][] resultData = new double[matrix1.rows][matrix1.columns];
for (int i = 0; i < matrix1.rows; i++) {
for (int j = 0; j < matrix1.columns; j++) {
resultData[i][j] = matrix1.data[i][j] + matrix2.data[i][j];
}
}
return new Matrix(resultData);
}
public static Matrix sub(Matrix matrix1, Matrix matrix2) {
return add(matrix1, multiply(-1, matrix2));
}
public static double dot(Matrix matrix1, Matrix matrix2) {
return multiply(transpose(matrix1), matrix2).getElement(0, 0);
}
public double getElement(int row, int column) {
if (row < 0 || row >= rows || column < 0 || column >= columns) {
throw new IllegalArgumentException("Invalid row or column index.");
}
return data[row][column];
}
public static Matrix crossProduct(Matrix vector1, Matrix vector2) {
if (vector1.getRows() != 3 || vector1.getColumns() != 1 ||
vector2.getRows() != 3 || vector2.getColumns() != 1) {
throw new IllegalArgumentException("Cross product is defined for 3D vectors only.");
}
double resultDataX = vector1.getElement(1, 0) * vector2.getElement(2, 0)
- vector1.getElement(2, 0) * vector2.getElement(1, 0);
double resultDataY = vector1.getElement(2, 0) * vector2.getElement(0, 0)
- vector1.getElement(0, 0) * vector2.getElement(2, 0);
double resultDataZ = vector1.getElement(0, 0) * vector2.getElement(1, 0)
- vector1.getElement(1, 0) * vector2.getElement(0, 0);
return new Matrix(new double[][] { { resultDataX }, { resultDataY }, { resultDataZ } });
}
public static Matrix multiply(Matrix matrix1, Matrix matrix2) {
if (matrix1.columns != matrix2.rows) {
throw new IllegalArgumentException(
"Number of columns in the first matrix must be equal to the number of rows in the second matrix for multiplication.");
}
double[][] resultData = new double[matrix1.rows][matrix2.columns];
for (int i = 0; i < matrix1.rows; i++) {
for (int j = 0; j < matrix2.columns; j++) {
for (int k = 0; k < matrix1.columns; k++) {
resultData[i][j] += matrix1.data[i][k] * matrix2.data[k][j];
}
}
}
return new Matrix(resultData);
}
public static Matrix multiply(double scalar, Matrix matrix) {
double[][] resultData = new double[matrix.rows][matrix.columns];
for (int i = 0; i < matrix.rows; i++) {
for (int j = 0; j < matrix.columns; j++) {
resultData[i][j] = scalar * matrix.data[i][j];
}
}
return new Matrix(resultData);
}
public static Matrix transpose(Matrix matrix) {
double[][] transposedData = new double[matrix.columns][matrix.rows];
for (int i = 0; i < matrix.rows; i++) {
for (int j = 0; j < matrix.columns; j++) {
transposedData[j][i] = matrix.data[i][j];
}
}
return new Matrix(transposedData);
}
public Matrix getColumn(int columnIndex) {
if (columnIndex < 0 || columnIndex >= columns) {
throw new IllegalArgumentException("Invalid column index.");
}
double[] column = new double[rows];
for (int i = 0; i < rows; i++) {
column[i] = data[i][columnIndex];
}
return new Matrix(column);
}
public Matrix getRow(int rowIndex) {
if (rowIndex < 0 || rowIndex >= rows) {
throw new IllegalArgumentException("Invalid row index.");
}
return new Matrix(new double[][] { data[rowIndex].clone() });
}
public int getRows() {
return rows;
}
public int getColumns() {
return columns;
}
public void print() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < columns; j++) {
System.out.print(data[i][j] + " ");
}
System.out.println();
}
}
public double[][] toArrays() {
return data;
}
}