-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.java
293 lines (253 loc) · 6.61 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
package algebra;
import java.io.*;
public class Matrix implements Cloneable{
private Double[][] matrix;
/*
* |˜a b˜|
* |_c d_|
* Creates a two by two matrix
*/
public Matrix(Double a, Double b, Double c, Double d){
matrix = new Double[][] {{a, b}, {c, d}};
}
//Creates an n by n matrix
public Matrix(int n) {
matrix = new Double[n][n];
}
//Creates an n by m matrix
public Matrix(int rows, int cols) {
matrix = new Double[rows][cols];
}
/*
*Initializes values for the Matrix
*Takes file name
*Format separate each value with ',' (do not include ') for each row separate rows by lines
*/
public void setValue(String file){
try(BufferedReader reader = new BufferedReader(new FileReader(file))) {
for(int i = 0; i < matrix.length; i++) {
String[] temp = reader.readLine().split(",");
if(temp.length != matrix[0].length) {
throw new NotEnoughException("Data in txt file does not match size of matrix");
}
else {
for(int j = 0; j < temp.length; j++) {
matrix[i][j] = Double.parseDouble(temp[j]);
}
}
}
}
catch(IOException e) {
e.printStackTrace();
}
catch(NotEnoughException e) {
e.printStackTrace();
}
}
//Sets a specific value in matrix
public void setValue(int row, int col, Double val) {
matrix[row][col] = val;
}
/*
* |˜a b c˜|
* |_d e f_|
* Creates a quick two by three matrix
*/
public void setMatrixL(Double a, Double b, Double c, Double d, double e, double f) throws NotEnoughException {
if(matrix.length == 2 && matrix[0].length == 3) {
matrix[0][0] = a;
matrix[0][1] = b;
matrix[0][2] = c;
matrix[1][0] = d;
matrix[1][1] = e;
matrix[1][2] = f;
}
else {
throw new NotEnoughException("Not a 2x3 matrix");
}
}
/*
* |˜a b˜|
* | c d |
* |_e f_|
* Creates a quick two by three matrix
*/
public void setMatrixT(Double a, Double b, Double c, Double d, double e, double f) throws NotEnoughException {
if(matrix.length == 3 && matrix[0].length == 2) {
matrix[0][0] = a;
matrix[0][1] = b;
matrix[1][0] = c;
matrix[1][1] = d;
matrix[2][0] = e;
matrix[2][1] = f;
}
else {
throw new NotEnoughException("Not a 2x3 matrix");
}
}
//Gets number of rows
public int getNumRows() {
return matrix.length;
}
//Gets number of columns
public int getNumCols() {
return matrix[0].length;
}
//Displays the matrix
public void display() {
System.out.print("|˜");
for(int i = 0; i < matrix.length; i++) {
if(i != 0 && i != (matrix.length-1)) {
System.out.printf("|");
}
if(i == (matrix.length - 1)) {
System.out.printf("|_");
}
for(int j = 0; j < matrix[i].length; j++) {
System.out.printf(" %.2f ", matrix[i][j]);
}
if(i != (matrix.length - 1) && i != 0) {
System.out.printf("|\n");
}
else if(i == 0) {
System.out.printf("˜|\n");
}
else {
System.out.printf("_|\n");
}
}
}
public Double getValue(int row, int col) {
return matrix[row][col];
}
//Multiplies two matrix operations
public static Matrix mulitply(Matrix A, Matrix B) throws NotEnoughException {
Matrix result = new Matrix(A.getNumRows(), B.getNumCols());
if(A.getNumRows() != B.getNumCols()) {
throw new NotEnoughException("Columns doesn't match rows");
}
else {
Double temp = 0.0;
int Amatrix = 0;
int Bmatrix = 0;
while(Amatrix != A.getNumRows()) {
for(int i = 0; i < A.getNumCols(); i++) {
temp += (A.getValue(Amatrix, i)*B.getValue(i, Bmatrix));
}
result.setValue(Amatrix, Bmatrix, temp);
temp = 0.0;
Bmatrix++;
if(Bmatrix == B.getNumCols()) {
Amatrix++;
Bmatrix = 0;
}
}
}
return result;
}
//Multiplies by matrix by a constant
public static Matrix multiply(Matrix A, Double b) {
Matrix result = A.clone();
for(int i = 0; i < A.getNumRows(); i++) {
for(int j = 0; j < A.getNumCols(); j++) {
result.setValue(i, j, b*A.getValue(i, j));
}
}
return result;
}
//Adds two matrices
public static Matrix add(Matrix A, Matrix B) throws MatrixException {
Matrix result = new Matrix(A.getNumRows(), A.getNumCols());
if(A.getNumRows() != B.getNumRows() && A.getNumCols() != B.getNumCols()) {
throw new MatrixException("Matrices not same size");
}
else {
for(int i = 0; i < A.getNumRows(); i++) {
for(int j = 0; j < A.getNumCols(); j++) {
result.setValue(i, j, A.getValue(i, j) + B.getValue(i, j));
}
}
return result;
}
}
//Subtracts two matrices
public static Matrix sub(Matrix A, Matrix B) throws MatrixException {
Matrix result = new Matrix(A.getNumRows(), A.getNumCols());
if(A.getNumRows() != B.getNumRows() && A.getNumCols() != B.getNumCols()) {
throw new MatrixException("Matrices not same size");
}
else {
for(int i = 0; i < A.getNumRows(); i++) {
for(int j = 0; j < A.getNumCols(); j++) {
result.setValue(i, j, A.getValue(i, j) - B.getValue(i, j));
}
}
return result;
}
}
//Gets determinate of n x n matrix
public static double det(Matrix A) throws MatrixException {
Double determinate = 0.0;
if(A.getNumCols() != A.getNumRows()) {
throw new MatrixException("Cannot take determinate of none n x n matrix");
}
else {
if(A.getNumCols() == 2 && A.getNumRows() == 2) {
return A.getValue(0, 0)*A.getValue(1, 1) - A.getValue(1, 0)*A.getValue(0, 1);
}
Matrix temp = new Matrix(A.getNumCols()-1);
for(int i = 0; i < A.getNumCols(); i++) {
int col;
for(int j = 1; j < A.getNumCols(); j++) {
col = 0;
for(int k = 0; k < A.getNumCols(); k++) {
if(k != i) {
temp.setValue(j-1, col, A.getValue(j, k));
col++;
}
}
}
if(i%2 == 1) {
determinate += det(temp)*-A.getValue(0, i);
}
else {
determinate += det(temp)*A.getValue(0, i);
}
}
return determinate;
}
}
//Checks if matrix is orthogonal
public static boolean isOrthogonal(Matrix A) throws MatrixException {
if(A.getNumRows() != A.getNumCols()) {
return false;
}
Double det = det(A);
if(det != 1.0 || det != -1.0) {
return false;
}
return true;
}
//Checks if matrix is special orthogonal
public static boolean isSpecialOrthogonal(Matrix A) throws MatrixException {
if(A.getNumRows() != A.getNumCols()) {
return false;
}
Double det = det(A);
if(det != 1.0) {
return false;
}
return true;
}
//Creates deep copy of matrix object
@Override
public Matrix clone() {
Matrix result = new Matrix(matrix.length, matrix[0].length);
for(int i = 0; i < matrix.length; i++) {
for(int j = 0; j < matrix[0].length; j++) {
result.setValue(i, j, matrix[i][j]);
}
}
return result;
}
}