-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSparse.java
More file actions
200 lines (109 loc) · 4.28 KB
/
Copy pathSparse.java
File metadata and controls
200 lines (109 loc) · 4.28 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
//Name: Joshua Rony
//CruzID: jrony
//Class: CMPS101
//Assignment Name: pa3
import java.util.*;
import java.lang.*;
import java.io.*;
public class Sparse {
public Scanner s;
public PrintWriter printWrite;
private boolean flag = false;
private int marker;
public int lineCount = 0;
private String str;
public Matrix A;
public Matrix B;
public void openInputFile(String str){
try {
s = new Scanner(new File(str));
} catch (Exception e) {
System.out.println("Could not find file." + System.getProperty("user.dir"));
}
}
public void openOutputFile(String str){
try{
printWrite = new PrintWriter(new FileWriter(str));
}
catch(Exception e){
System.out.println("Could not find file." + System.getProperty("user.dir"));
}
}
public void writeString(String str){
printWrite.print(str);
}
public void writeMatrices(Matrix M){
for(int x = 0; x < M.getSize(); x++){
if(M.arrayOfLists[x].length() > 0) {
//printWrite.println(" ");
printWrite.print((x + 1) + ":");
M.arrayOfLists[x].moveFront();
for (int y = 0; y < M.arrayOfLists[x].length(); y++) {
String input = M.arrayOfLists[x].get().toString();
printWrite.print(input);
M.arrayOfLists[x].moveNext();
}
printWrite.println("\r\n");
}
}
printWrite.println("\r\n");
}
public void closeInputFile(){
s.close();
}
public void closeOutputFile(){
printWrite.close();
}
public void createMatrices() {
while (s.hasNextLine()) {
String a = s.nextLine();
if(lineCount > 1 && a.isEmpty()){
flag = true;
marker = lineCount;
}
if (lineCount == 0) {
str = a.substring(0, a.indexOf(" "));
int matrixSize = Integer.parseInt(str);
A = new Matrix(matrixSize);
B = new Matrix(matrixSize);
}
if(lineCount > 1 && flag == false) {
String[] params = a.split(" ");//params[0] = row; params[1] = column; params[2] = value
A.changeEntry(Integer.parseInt(params[0]), Integer.parseInt(params[1]), Double.parseDouble(params[2]));
}
if(lineCount > marker && flag == true){
String[] params = a.split(" ");
B.changeEntry(Integer.parseInt(params[0]), Integer.parseInt(params[1]), Double.parseDouble(params[2]));
}
lineCount++;
}
}
public static void main(String[] args) {
Sparse connect = new Sparse();
connect.openInputFile(args[0]);
connect.createMatrices();
connect.closeInputFile();
connect.openOutputFile(args[1]);
connect.writeString("A has " + connect.A.getNNZ() + " non-zero entries:\r\n");
connect.writeMatrices(connect.A);
connect.writeString("B has " + connect.B.getNNZ() + " non-zero entries:\r\n");
connect.writeMatrices(connect.B);
connect.writeString("(1.5)*A =\r\n");
connect.writeMatrices(connect.A.scalarMult(1.5));
connect.writeString("A+B =\r\n");
connect.writeMatrices(connect.A.add(connect.B));
connect.writeString("A+A =\r\n");
connect.writeMatrices(connect.A.add(connect.A));
connect.writeString("B-A =\r\n");
connect.writeMatrices(connect.B.sub(connect.A));
connect.writeString("A-A =\r\n");
connect.writeMatrices(connect.A.sub(connect.A));
connect.writeString("Transpose(A) =\r\n");
connect.writeMatrices(connect.A.transpose());
connect.writeString("A*B =\r\n");
connect.writeMatrices(connect.A.mult(connect.B));
connect.writeString("B*B =\r\n");
connect.writeMatrices(connect.B.mult(connect.B));
connect.closeOutputFile();
}
}