forked from ccrowley96/Matrix-Calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.java
More file actions
365 lines (321 loc) · 10.4 KB
/
Matrix.java
File metadata and controls
365 lines (321 loc) · 10.4 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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Matrix {
//Private Class Variable Definitions -- belongs to each object
private int m_rows;
private int n_cols;
private double[][] arr;
// -------------------Constructors--------------------------------
//Construct[1] given m & n as parameters
public Matrix(int m, int n){
this.m_rows = m;
this.n_cols = n;
this.arr = new double[m][n];
for(int i = 0; i< this.m_rows; i++){
for(int j = 0; j < this.n_cols; j++){
this.arr[i][j] = 0;
}
}
}
//Construct [2] with console input -- no parameters (Overloaded)
public Matrix(){
Scanner in = new Scanner(System.in);//Create scanner
System.out.println("Enter the number of array [rows] then [columns] seperated by a space");
String input = in.nextLine();//Get user input line as string
StringTokenizer st = new StringTokenizer(input," ");//Store input in string tokenizer -- delimit tokens by ','
this.m_rows = Integer.parseInt(st.nextToken().trim());//store rows
this.n_cols = Integer.parseInt(st.nextToken().trim());//store cols
//Initialize Array
this.arr = new double[this.m_rows][this.n_cols];
//Prompt user to enter array data
System.out.println("Enter the array in format:");
//Show how array input should be formatted
for(int i = 0; i< this.m_rows; i++){
for(int j = 0; j < this.n_cols; j++){
System.out.print("X ");
//System.out.print(this.arr[i][j]);
}
System.out.println();
}
System.out.println("Column elements seperated by spaces.\nRow elements seperated by return");
//Get array input
StringTokenizer st2;
int token;
for(int i = 0; i < this.m_rows; i++){
input = in.nextLine();
st2 = new StringTokenizer(input," ");
//System.out.println("st = "+st2.countTokens());
for(int j = 0; j < this.n_cols; j++ ){
token = Integer.parseInt(st2.nextToken().trim());
//System.out.print(" " + token + " ");
//System.out.print("i = "+i +", j = "+j + " ");
this.arr[i][j] = token;
}
}
}//end construct [2]
//Construct[3] -- Constructor using file input
public Matrix(String filename) throws FileNotFoundException{
Scanner inFile = new Scanner(new FileReader(filename));
//Initialize array using first line of file -- size parameters
String arrSize = inFile.nextLine();
StringTokenizer st = new StringTokenizer(arrSize,",");
this.m_rows = Integer.parseInt(st.nextToken());
this.n_cols = Integer.parseInt(st.nextToken());
this.arr = new double[m_rows][n_cols];
//Fill array with data
int i = 0;//row variable
String rowData;//String of row data
StringTokenizer st2;//Create new stringtokenizer to store row data
while(inFile.hasNextLine()){
//Fill array with data
rowData = inFile.nextLine();
st2 = new StringTokenizer(rowData,",");
for(int j = 0; j < this.n_cols; j++){
this.arr[i][j] = Double.parseDouble(st2.nextToken());
}
i++;
}
}//End construct[3]
//-----------------------------End Constructors----------------------------
//----------------------------Operations--------------------------------
//ADD MATRIX
public Matrix add(Matrix m){//add 'this' matrix to matrix m
Matrix newMat = new Matrix(this.m_rows,this.n_cols);
for(int i = 0; i < this.m_rows; i++){
for(int j = 0; j < this.n_cols; j++){
newMat.arr[i][j] = this.arr[i][j] + m.arr[i][j];
}
}
return newMat;
}
//SUBTRACT MATRIX
public Matrix subtract(Matrix m){//add 'this' matrix to matrix m
Matrix newMat = new Matrix(this.m_rows,this.n_cols);
for(int i = 0; i < this.m_rows; i++){
for(int j = 0; j < this.n_cols; j++){
newMat.arr[i][j] = this.arr[i][j] - m.arr[i][j];
}
}
return newMat;
}
//MULTIPLY MATRIX -- by another matrix
public Matrix multiply(Matrix m){
Matrix newMat = new Matrix(this.m_rows,m.n_cols);//Create new matrix with correct dimensions
//Matrices must have the same inner dimensions 2x3 * 3*2
/*
* |1 2 3| * |3 2| |20 <- 1*3 + 2*1 + 3 *5 |
* |4 5 6| |1 2| = | |
* |5 4|
* 2x3 * 3x2
*/
if(this.n_cols == m.m_rows){
for(int row = 0; row < this.m_rows; row++){
for(int col = 0; col < m.n_cols; col++){
for(int k = 0; k < this.n_cols; k++)
newMat.arr[row][col] += this.arr[row][k] * m.arr[k][col];
}
}
}else{
System.out.println("Arrays can't be multiplied");
return newMat;
}
return newMat;
}
//MULTIPLY MATRIX -- by scalar double
public Matrix multiply(double x){
Matrix newMat = new Matrix(this.m_rows,this.n_cols);
for(int i = 0; i < this.m_rows; i++){
for(int j = 0; j < this.n_cols; j++){
newMat.arr[i][j] = this.arr[i][j] *x;
}
}
return newMat;
}
//DETERMINANT MATRIX -- return determinant of square matrix
public double determinant(){
double result = -1;
if(this.m_rows > 3){
System.out.println("Matrix too large -- Cannot find determinant");
return -1;
}
if(this.m_rows == 1){
return this.arr[0][0];
}
else if(this.m_rows == 2)
{
result = (this.arr[0][0]*this.arr[1][1])-(this.arr[1][0]*this.arr[0][1]);
}
else if(this.m_rows == 3){
result = (this.arr[0][0]*this.det_2x2(0)) - (this.arr[1][0]*this.det_2x2(1)) + (this.arr[2][0]*this.det_2x2(2));
}
return result;//error case
}
//Helper method for determinant method
private double det_2x2(int rowX){
//Assuming determinant column is always 0!
if(rowX == 0)
return (this.arr[1][1]*this.arr[2][2])-(this.arr[2][1]*this.arr[1][2]);
else if(rowX == 1)
return (this.arr[0][1]*this.arr[2][2])-(this.arr[2][1]*this.arr[0][2]);
else if(rowX == 2)
return (this.arr[0][1]*this.arr[1][2])-(this.arr[1][1]*this.arr[0][2]);
else
return -1;//Error case
}
//Checks if matrix is square
public boolean isSquare(){
if(this.m_rows == this.n_cols)
return true;
else
return false;
}
//Transpose Matrix -- Switch rows and columns
public Matrix transpose(){
Matrix result = new Matrix(this.m_rows,this.n_cols);
for(int i = 0; i < this.m_rows; i++){
for(int j = 0; j < this.n_cols; j++){
result.arr[i][j] = this.arr[j][i];
}
}
return result;
}//end transpose
//Inverse Matrix -- Return inverse of matrix 1x1,2x2,3x3
public Matrix inverse(){
Matrix newMat = new Matrix(this.m_rows,this.n_cols);
if(this.isSquare()){//check if square matrix
if(this.m_rows == 1){//1x1 inverse
newMat.arr[0][0] = 1/this.arr[0][0];
}
else if(this.m_rows == 2){//2x2 inverse
double det = this.determinant();
det = 1/det;
newMat.arr[0][0] = det*this.arr[1][1];
newMat.arr[1][1] = det*this.arr[0][0];
newMat.arr[0][1] = -1*det*this.arr[0][1];
newMat.arr[1][0] = -1*det*this.arr[1][0];
}
else if(this.m_rows == 3){
double det = this.determinant();
if(det == 0){
System.out.println("Determinant = 0, cannot find inverse");
return newMat;
}
this.cofactorAndminor();
Matrix temp = this.transpose();
//temp.printConsole("Intermediate step printing temp");
Matrix temp2 = temp.multiply(1/det);
return temp2;//Still need to code 3x3 matrix inverse
}
else{
System.out.println("Problem encountered finding inverse");
return newMat;
}
}else{
System.out.println("Cannot find inverse of non-square matrix");
return newMat;
}
//default return
return newMat;
}
public void cofactorAndminor() {
double[][] save = new double[this.m_rows][this.n_cols];
for (int i = 0; i < this.m_rows; i++){
for (int j = 0; j < this.n_cols; j++){
save[i][j] = this.arr[i][j];
}
}
//Calculates determinant wrt each array element --> calculating determinant of 2x2 array after blocking out row/column chosen
//Simultaneosly finds the 'minor' of the matrix +-+ sign change in rows and columns
this.arr[0][0] = (save[1][1]*save[2][2]) - (save[2][1]*save[1][2]);
this.arr[0][1] = -1*((save[1][0]*save[2][2]) - (save[2][0]*save[1][2]));
this.arr[0][2] = (save[1][0]*save[2][1]) - (save[2][0]*save[1][1]);
this.arr[1][0] = -1*((save[0][1]*save[2][2]) - (save[2][1]*save[0][2]));
this.arr[1][1] = (save[0][0]*save[2][2]) - (save[2][0]*save[0][2]);
this.arr[1][2] = -1*((save[0][0]*save[2][1]) - (save[2][0]*save[0][1]));
this.arr[2][0] = (save[0][1]*save[1][2]) - (save[1][1]*save[0][2]);
this.arr[2][1] = -1*((save[0][0]*save[1][2]) - (save[1][0]*save[0][2]));
this.arr[2][2] = (save[0][0]*save[1][1]) - (save[1][0]*save[0][1]);
}
public Matrix divide(Matrix m){
Matrix newMat = null;
if(m.m_rows == m.n_cols){//Check if divisor is square matrix
Matrix inverse = m.inverse();
if(this.n_cols == inverse.m_rows){//check if inner dimensions equal
newMat = this.multiply(inverse);
}else{
System.out.println("Inner dimensions do not match...\nCannot multiply by inverse");
}
}
else{
System.out.println("Matrix divisor is not a square matrix...\nCannot divide.");
}
return newMat;
}//end divide
//--------------------------End Operations--------------------------------
//----------------------------Getters--------------------------------
public int getM(){
return this.m_rows;
}
public int getN(){
return this.n_cols;
}
public double getElement(int i, int j){
return this.arr[i][j];
}
//----------------------------End Getters--------------------------------
//--------------------------Other Classes---------------------------------
//Print Array to Console
void printConsole(String message){
System.out.println("----"+message+"----");
for(int i = 0; i < this.m_rows; i++){
for(int j = 0; j < this.n_cols; j++){
System.out.print(this.arr[i][j]+" ");
}
System.out.println();
}
System.out.println("--End Array--");
}
//turn matrix into file-format string
public String toString(){
String result = "";
result += this.m_rows +"," + this.n_cols+"\n";
for(int i = 0; i < m_rows; i++){
for(int j = 0; j < n_cols; j++){
result += this.arr[i][j];
if(j < n_cols - 1)
result +=",";
result+=" ";
}
result += "\n";
}
return result;
}
//Write matrix to specified file
public void print(String filename) throws IOException {
String data = this.toString();
FileWriter fw = new FileWriter(filename);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(data);
bw.flush();
bw.close();
}
//Static Method to create identity matrix
static Matrix identity(int squareSize){
Matrix id = new Matrix(squareSize,squareSize);
for(int i = 0; i < squareSize; i++){
for(int j= 0; i< squareSize; i++){
if(i == j)
id.arr[i][j] = 1;
}
}
return id;
}
//----------------------- End Other Classes---------------------------------
}//end class