-
Notifications
You must be signed in to change notification settings - Fork 4
/
Matrix.java
313 lines (284 loc) · 9.08 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/*This program tries to solve all the Matrix calculation including element by element multiplication, minus, plus, division and linear algebraic matrix multiplication.
Other things such as dimension detection, matrix copy and transform between double array
type and Matrix type object are also included.
*/
import java.text.NumberFormat;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;
import java.text.FieldPosition;
import java.io.PrintWriter;
import java.io.BufferedReader;
import java.io.StreamTokenizer;
class Matrix{
private double[][] A;
private int m, n;
//constructors
public Matrix (int m, int n) {
this.m = m;
this.n = n;
A = new double[m][n];
}
public Matrix (int m, int n, double num) {
this.m = m;
this.n = n;
A = new double[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
A[i][j] = num;
}
}
}
public Matrix (double[][] A, int m, int n) {
this.A = A;
this.m = m;
this.n = n;
}
public Matrix (double value[], int m) {
this.m = m;
n = (m != 0 ? value.length/m : 0);
//if m != 0 is ture, n = value.length/m, else n = 0
if (m * n != value.length) {
throw new IllegalArgumentException("Array length must be a multiple of m.");
}
A = new double[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
A[i][j] = value[i + j * m];
}
}
}
//error detection
public Matrix (double[][] A) {
m = A.length;
n = A[0].length;
for (int i = 0; i < m; i++) {
if (A[i].length != n) {
throw new IllegalArgumentException("All Row length should be the same");
}
}
this.A = A;
}
//check whether two matrixes can be performed arithmetic computing with each other.
private void checkMatrixDimensions (Matrix B) {
if (B.m != m || B.n != n) {
throw new IllegalArgumentException("Matrix dimensions must agree.");
}
}
//public Matrix Calculation Methods
//copy Matrix type and return Matrix type object
public static Matrix constructWithCopy(double[][] A) {
int m = A.length;
int n = A[0].length;
Matrix X = new Matrix(m,n);
double[][] C = X.getArray();
for (int i = 0; i < m; i++) {
if (A[i].length != n) {
throw new IllegalArgumentException
("All rows must have the same length.");
}
for (int j = 0; j < n; j++) {
C[i][j] = A[i][j];
}
}
return X;
}
//Make a deep copy of a matrix
public Matrix copy () {
Matrix X = new Matrix(m,n);
double[][] C = X.getArray();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
C[i][j] = A[i][j];
}
}
return X;
}
//copy double array type and return double array type
public double[][] getArrayCopy () {
double[][] C = new double[m][n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
C[i][j] = A[i][j];
}
}
return C;
}
//return the double array stored in matrix object
public double[][] getArray () {
return A;
}
//Make a one-dimensional column packed copy of the internal array.
public double[] getColumnPackedCopy () {
double[] value = new double[m * n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
value[i + j * m] = A[i][j];
}
}
return value;
}
//Make a one-dimensional row packed copy of the internal array.
public double[] getRowPackedCopy () {
double[] value = new double[m * n];
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
value[i * n + j] = A[i][j];
}
}
return value;
}
//get the dimensions of array, or any single element in the array
public int getRowDimension () {
return m;
}
public int getColumnDimension () {
return n;
}
public double get (int i, int j) {
return A[i][j];
}
//set a single element in a double array
public void set (int i, int j, double value) {
A[i][j] = value;
}
//get the transpose of a Matrix
public Matrix transpose () {
Matrix X = new Matrix(n,m);
double[][] C = X.getArray();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
C[j][i] = A[i][j];
}
}
return X;
}
//return the result of the element by element plus, the result is assigned to another variable.
public Matrix plus (Matrix B) {
checkMatrixDimensions(B);
Matrix X = new Matrix(m,n);
double[][] C = X.getArray();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
C[i][j] = A[i][j] + B.A[i][j];
}
}
return X;
}
//return the result of the element by element plus, the result is assigned to original matrix.
public Matrix plusEquals (Matrix B) {
checkMatrixDimensions(B);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
A[i][j] = A[i][j] + B.A[i][j];
}
}
return this;
}
//return the result of the element by element minus, the result is assigned to another variable.
public Matrix minus (Matrix B) {
checkMatrixDimensions(B);
Matrix X = new Matrix(m,n);
double[][] C = X.getArray();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
C[i][j] = A[i][j] - B.A[i][j];
}
}
return X;
}
//return the result of the element by element minus, the result is assigned to original matrix.
public Matrix minusEquals (Matrix B) {
checkMatrixDimensions(B);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
A[i][j] = A[i][j] - B.A[i][j];
}
}
return this;
}
//return the result of the element by element multiplication, the result is assigned to original matrix or another variable.
public Matrix arrayTimes (Matrix B) {
checkMatrixDimensions(B);
Matrix X = new Matrix(m,n);
double[][] C = X.getArray();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
C[i][j] = A[i][j] * B.A[i][j];
}
}
return X;
}
public Matrix arrayTimesEquals (Matrix B) {
checkMatrixDimensions(B);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
A[i][j] = A[i][j] * B.A[i][j];
}
}
return this;
}
//return the result of the element by element minus, the result is assigned to original matrix or another variable.
public Matrix arrayRightDivide (Matrix B) {
checkMatrixDimensions(B);
Matrix X = new Matrix(m,n);
double[][] C = X.getArray();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
C[i][j] = A[i][j] / B.A[i][j];
}
}
return X;
}
public Matrix arrayRightDivideEquals (Matrix B) {
checkMatrixDimensions(B);
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
A[i][j] = A[i][j] / B.A[i][j];
}
}
return this;
}
//Multiply a matrix by a scalar, C = s*A or A = s*A
public Matrix times (double s) {
Matrix X = new Matrix(m,n);
double[][] C = X.getArray();
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
C[i][j] = s * A[i][j];
}
}
return X;
}
public Matrix timesEquals (double s) {
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
A[i][j] = s * A[i][j];
}
}
return this;
}
//Linear algebraic matrix multiplication, A * B
public Matrix times (Matrix B) {
if (B.m != n) {
throw new IllegalArgumentException("Matrix inner dimensions must agree.");
}
Matrix X = new Matrix(m,B.n);
double[][] C = X.getArray();
double[] Bcolj = new double[n];
for (int j = 0; j < B.n; j++) {
for (int k = 0; k < n; k++) {
Bcolj[k] = B.A[k][j];
}
for (int i = 0; i < m; i++) {
double[] Arowi = A[i];
double value = 0;
for (int k = 0; k < n; k++) {
value += Arowi[k]*Bcolj[k];
}
C[i][j] = value;
}
}
return X;
}
}