-
Notifications
You must be signed in to change notification settings - Fork 0
/
SparseMatrix.java
267 lines (237 loc) · 7.08 KB
/
SparseMatrix.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
package com.company;
public class Main{
public static void main(String[] args){
double[][] mat1 = {{3,7,5},{0,5,4},{3,2,5}};
double[][] mat2 = {{1,3,3},{0,0,0},{1,2,5}};
SparseMatrix m1 = new SparseMatrix(mat1, 3,3);
SparseMatrix m2 = new SparseMatrix(mat2, 3,3);
SparseMatrix m3 = m1.add(m2);
System.out.println(m3);
}
}
class SparseMatrix{
private LinearProbingST<Integer, Double>[] matrix;
private int height, width;
public SparseMatrix(double[][] m, int h, int w){
matrix = (LinearProbingST<Integer, Double>[])new LinearProbingST[h];
for(int i = 0; i < h; ++i){
matrix[i] = new LinearProbingST<Integer, Double>();
}
for(int i = 0; i < h; ++i){
for(int j = 0; j < w; ++j){
if(m[i][j] != 0.0){
matrix[i].put(j, m[i][j]);
}
}
}
height = h;
width = w;
}
public SparseMatrix(int h, int w){
matrix = (LinearProbingST<Integer,Double>[])new LinearProbingST[h];
for(int i = 0; i < h; ++i){
matrix[i] = new LinearProbingST<Integer, Double>();
}
height = h;
width = w;
}
public int getHeight(){
return height;
}
public int getWidth(){
return width;
}
public double get(int row, int col){
if(matrix[row].contains(col)) return matrix[row].get(col);
return 0.0;
}
public void put(int row, int col, double val){
matrix[row].put(col, val);
}
public SparseMatrix multiply(SparseMatrix other){
assert this.width == other.height;
SparseMatrix newMatrix = new SparseMatrix(this.height, other.width);
for(int i = 0; i < this.height; ++i){
for(int j = 0; j < other.width; ++j){
for(int k = 0; k < other.height; ++k){
if((int)(1000 * this.get(i, k)) != 0 && (int)(1000 * other.get(k , j)) != 0) {
double sum = newMatrix.get(i, j) + this.get(i, k) * other.get(k, j);
if ((int) (sum * 1000) != 0)
newMatrix.put(i, j, sum);
}
}
}
}
return newMatrix;
}
public SparseMatrix add(SparseMatrix other){
assert this.height == other.height && this.width == other.width;
SparseMatrix newMatrix = new SparseMatrix(height, width);
for(int i = 0; i < height; ++i){
for(int j = 0; j < width; ++j){
double sum = this.get(i, j) + other.get(i, j);
if((int)(sum * 1000) != 0)
newMatrix.put(i, j, sum);
}
}
return newMatrix;
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
for(int i = 0; i < height; ++i){
for(int j = 0; j < width; ++j){
sb.append(String.format("%f, ", get(i, j)));
}
sb.append('\n');
}
return sb.toString();
}
}
class Matrix{
private double[][] matrix;
private int height, width;
public Matrix(int h, int w){
matrix = new double[h][w];
height = h;
width = w;
}
public Matrix(double[][] matrix, int h, int w){
this.matrix = matrix;
height = h;
width = w;
}
public double get(int row, int col){
return matrix[row][col];
}
public void put(int row, int col, double val){
matrix[row][col] = val;
}
public int getHeight(){
return height;
}
public int getWidth(){
return width;
}
public Matrix multiply(Matrix other){
assert this.width == other.height;
double[][] matrix2 = other.matrix;
int newHeight = this.height;
int newWidth = other.width;
double[][] newMatrix = new double[newHeight][newWidth];
//multiply
for(int i = 0; i < this.height; ++i){
for(int j = 0; j < other.width; ++j){
for(int k = 0; k < other.height; ++k){
newMatrix[i][j] += matrix[i][k] * matrix2[k][j];
}
}
}
return new Matrix(newMatrix, newHeight, newWidth);
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
for(int i = 0; i < height; ++i){
for(int j = 0; j < width; ++j){
sb.append(String.format("%f, ", matrix[i][j]));
}
sb.append('\n');
}
return sb.toString();
}
}
interface ST<Key, Value>{
void put(Key key, Value val);
void remove(Key key);
Value get(Key key);
int size();
boolean isEmpty();
boolean contains(Key key);
}
class LinearProbingST<Key, Value> implements ST<Key, Value>{
private int N;
private int M;
private Key[] keys;
private Value[] values;
public void printNM(){
System.out.println("N = " + N + ", M = " + M);
}
public LinearProbingST(){
keys = (Key[]) new Object[13];
values = (Value[]) new Object[13];
M = 13;
}
public LinearProbingST(int cap){
keys = (Key[]) new Object[cap];
values = (Value[]) new Object[cap];
M = cap;
}
@Override
public void put(Key key, Value val) {
if(N >= M / 2) resize(M * 2);
int i;
for(i = hash(key); keys[i] != null; i = (i + 1) % M){
if(key.equals(keys[i])){
values[i] = val; return;
}
}
keys[i] = key;
values[i] = val;
N++;
}
@Override
public void remove(Key key) {
if(!contains(key)) return;
int i = hash(key);
while(!keys[i].equals(key)) i = (i + 1) % M;
keys[i] = null; values[i] = null;
i = (i + 1) % M;
while(keys[i] != null){
Key tempKey = keys[i];
Value tempVal = values[i];
keys[i] = null;
values[i] = null;
i = (i + 1) % M;
N--;
put(tempKey, tempVal);
//System.out.println("Index : " + i);
}
N--;
if(N > 0 && N <= M / 8) resize(M / 2);
}
@Override
public Value get(Key key) {
for(int i = hash(key); keys[i] != null; i = (i + 1) % M){
if(key.equals(keys[i])) return values[i];
}
return null;
}
@Override
public int size() {
return N;
}
@Override
public boolean isEmpty() {
return N == 0;
}
@Override
public boolean contains(Key key) {
for(int i = hash(key); keys[i] != null; i = (i + 1) % M){
if(key.equals(keys[i])) return true;
}
return false;
}
private void resize(int size){
LinearProbingST<Key, Value> temp = new LinearProbingST<>(size);
for(int i = 0; i < M; ++i){
if(keys[i] != null) temp.put(keys[i], values[i]);
}
keys = temp.keys;
values = temp.values;
M = temp.M;
}
private int hash(Key key){
return (key.hashCode() & 0x7fffffff) % M;
}
}