-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGaussEliminator.java
More file actions
327 lines (255 loc) · 7.22 KB
/
Copy pathGaussEliminator.java
File metadata and controls
327 lines (255 loc) · 7.22 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
import java.math.BigInteger;
import java.util.*;
//import java.math.*;
/**
* Factorizes an integer
* @author Kristoffer Hallqvist
*/
public class GaussEliminator {
//Fields:
//no fields atm
//Methods
/**
* Constructor
*/
public GaussEliminator() {}
private void run() {
Scanner sc = new Scanner(System.in);
System.err.println("Enter a binary matrix:");
int rows, columns;
rows = sc.nextInt();
columns = sc.nextInt();
BitSet[] matrix = new BitSet[rows];
int curBit = 0;
boolean boolCurBit = false;
//System.err.println("nr of columns (BitSet length): " + columns);
for (int i = 0;i<rows;i++) {
matrix[i] = new BitSet(columns);
//System.err.println("Created BitSet of size: " + matrix[i].size());
for (int j = 0;j<columns;j++) {
try {
curBit = sc.nextInt();
curBit = curBit % 2;
if (curBit == 0) {
boolCurBit = false;
} else {
boolCurBit = true;
}
} catch (Exception e) {
boolCurBit = false;
}
matrix[i].set(j,boolCurBit);
}
//System.err.println("length: " + matrix[i].length());
}
System.err.println("You entered the matrix:");
printMatrix(matrix, rows, columns);
/*
matrix = transpose(matrix, rows, columns);
System.err.println("After transposition:");
printMatrix(matrix, columns, rows);
matrix = gaussEliminate(matrix, columns, rows);
System.err.println("After Gauss elimination");
printMatrix(matrix, columns, rows);
BitSet freeVar = getFreeVariables(matrix, columns, rows);
System.err.println("Free variables: ");
printBitSet(freeVar, rows);
BitSet nullspace = null;
while (true) {
nullspace = calcNullSpace(matrix, columns, rows, freeVar, nullspace);
System.err.println("Null space vector: ");
printBitSet(nullspace, columns);
if (nullspace.isEmpty()) {
break;
}
}
//System.err.println("Null space vector: ");
//printBitSet(nullspace, columns);
*/
matrix = gaussEliminate(matrix, rows, columns);
System.err.println("After Gauss elimination");
printMatrix(matrix, rows, columns);
BitSet freeVar = getFreeVariables(matrix, rows, columns);
System.err.println("Free variables: ");
printBitSet(freeVar, columns);
BitSet nullspace = null;
while (true) {
nullspace = calcNullSpace(matrix, rows, columns, freeVar, nullspace);
System.err.println("Null space vector: ");
printBitSet(nullspace, columns);
if (nullspace.isEmpty()) {
break;
}
}
//System.err.println("Null space vector: ");
//printBitSet(nullspace, columns);
}
/**
* Main method
*/
public static void main(String[] args) {
new GaussEliminator().run();
}
public BitSet getFreeVariables(BitSet[] matrix, int r, int c) {
if (c > r) {
System.err.println("Matrix is wide...");
return null;
}
BitSet res = new BitSet(c);
//Warning. We assume that the matrix is "high" or square. Might crash otherwise.
for(int i = c-1; i >= 0; i--) { //Lower rows should be all 0 anyway
boolean bitValue = matrix[i].get(i);
if (bitValue) { //Bound
//System.err.println("Bound!");
res.clear(i);
} else { //Free
//System.err.println("Free!");
res.set(i);
}
}
return res;
}
public BitSet[] transpose(final BitSet[] src, int r, int c) {
BitSet[] res = new BitSet[c];
for (int i = 0;i<c;i++) {
res[i] = new BitSet(r);
}
for (int i = 0;i<c;i++) {
for (int j = 0;j<r;j++) {
res[i].set(j, src[j].get(i));
}
}
return res;
}
//Prev denotes the solution given by the previous call to this function
//Free denotes the free variables, calculated by getFreeVariables();
public BitSet calcNullSpace(BitSet[] matrix, int r, int c, final BitSet free, final BitSet prev) {
if (free == null || free.isEmpty()) { //Return zero-vector
BitSet ret = new BitSet(c);
ret.clear();
return ret;
}
BitSet variables;
int lastRow;
BitSet lr;
if (prev == null || prev.isEmpty()) {
lastRow = c-1;
variables = new BitSet(c);
variables.set(0,c);
} else {
lr = (BitSet)prev.clone();
lr.and(free);
//lastRow = lr.length()-1;
lastRow = lr.nextSetBit(0);
/*
if (lastRow == -1) {
System.err.println("lastRow is negative!");
System.err.println("prev is: " + prev.toString());
printBitSet(prev, prev.size());
System.err.println("prev length is " + prev.length());
System.err.println("lr is " + lr.toString());
printBitSet(lr, lr.size());
System.err.println("lr length is " + lr.length());
System.err.println("Bits 0 and 1 of lr are:");
System.err.println("" + lr.get(0));
System.err.println("" + lr.get(1));
System.err.println("free is " + free.toString());
printBitSet(free, free.size());
System.err.println("free length is " + free.length());
}
*/
if (lastRow == -1) {
lastRow++;
}
variables = (BitSet)prev.clone();
//if (lastRow > 0) {
variables.set(0,lastRow);
variables.clear(lastRow);
//}
//System.err.println("variables is now:");
//printBitSet(variables, c);
lastRow--;
}
BitSet bsTmp = new BitSet(c);
//System.err.println("lastRow is: " + lastRow);
//Warning. We assume that the matrix is "high" or square. Might crash otherwise.
for(int i = lastRow; i >= 0; i--) { //Lower rows should be all 0 anyway
bsTmp = (BitSet)matrix[i].clone();
//Set the variables
bsTmp.and(variables);
if (!free.get(i)) { //Bound
//System.err.println("Bound!" + " i is " + i);
//Determine what we need to set it to
int bitVal = bsTmp.cardinality();
//System.err.println("bitVal is: " + bitVal);
bitVal = (bitVal+1) % 2;
if (bitVal == 0) {
variables.clear(i);
} else {
variables.set(i);
}
} else { //Free, set to 1 as default for now
//System.err.println("Free!");
variables.set(i);
}
}
return variables;
}
public BitSet[] gaussEliminate(final BitSet[] src, int r, int c) {
BitSet[] res;
res = (BitSet[])src.clone(); //Use this if you don't want to modify input
//res = src; //Use this if you want to modify the input instead
BitSet tmp;
int cNoBit = 0;
int x = -1;
for (int j = 0;j<c;j++) {
x = -1;
for (int i = j-cNoBit;i<r;i++) {
if (res[i].get(j) == true) {
if (x < 0) {
x = i;
} else {
res[i].xor(res[x]);
}
}
}
if (x > -1) {
tmp = (BitSet)res[j-cNoBit].clone();
res[j-cNoBit] = (BitSet)res[x].clone();
res[x] = (BitSet)tmp.clone();
} else {
cNoBit++;
}
}
//Rearrange for a nicer diagonal
for (int j = c-1;j>=0;j--) {
if (res[j].get(j) == true) { //No rearranging needed.
break;
}
if (res[j].isEmpty()) { //0-row. Just continue
continue;
}
int bitPos = res[j].nextSetBit(0);
tmp = (BitSet)res[j].clone();
res[j] = new BitSet(c);
res[j].clear();
res[bitPos] = (BitSet)tmp.clone();
}
return res;
}
public void printMatrix(BitSet[] matrix, int r, int c) {
for (int i = 0;i<r;i++) {
printBitSet(matrix[i], c);
}
}
public void printBitSet(BitSet bs, int c) {
if (bs != null) {
for (int j = 0;j<c;j++) {
System.err.print(" " + (bs.get(j) == true ? 1 : 0));
}
System.err.println();
} else {
System.err.println("null");
}
}
}