-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgauss.cpp
106 lines (85 loc) · 2.01 KB
/
gauss.cpp
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
#include "gauss.hpp"
GaussianEliminator::GaussianEliminator(unsigned int v)
: rowsUnset(v)
{
vars = v;
rows = new Row[vars];
constTerms = new bool[vars];
}
bool
GaussianEliminator::addEquation(Equation eq, bool term)
{
Row nrow;
while(eq.size()) {
unsigned short int fp = eq[0];
if (rows[fp].set){
eq += rows[fp].eq;
term ^= constTerms[fp];
} else {
rows[fp].set = true;
rows[fp].eq = eq;
constTerms[fp] = term;
rowsUnset --;
break;
}
}
if (isSolvable())
return true;
return false;
}
std::vector <bool>
GaussianEliminator::getSolution()
{
std::vector <bool> sol(vars, false) ;
if (!isSolvable())
throw;
for (int k = vars-1; k >= 0; k --){
unsigned int size = rows[k].eq.size();
bool parity = false;
for (int j = size - 1; j > 0; j --)
parity ^= sol[rows[k].eq[j]];
sol[k] = parity ^ constTerms[k];
}
return sol;
}
void
GaussianEliminator::clear()
{
for (unsigned int i = 0; i < vars; i ++)
rows[i].set = false;
rowsUnset = vars;
return;
}
void
GaussianEliminator::printStats()
{
std::cout << "Rows Unset: " << rowsUnset << std::endl;
return;
}
#if 0
int main()
{
GaussianEliminator terminator(4);
unsigned short int r1[] = {0, 1, 2};
unsigned short int r2[] = {1, 3};
unsigned short int r3[] = {0, 1};
unsigned short int r4[] = {3};
Equation eq;
std::copy ( r1, r1 + 3, std::back_inserter ( eq ) );
terminator.addEquation(eq, true);
eq.clear();
std::copy ( r2, r2 + 2, std::back_inserter ( eq ) );
terminator.addEquation(eq, true);
eq.clear();
std::copy ( r3, r3 + 2, std::back_inserter ( eq ) );
terminator.addEquation(eq, false);
eq.clear();
std::copy ( r4, r4 + 1, std::back_inserter ( eq ) );
terminator.addEquation(eq, true);
std::vector <bool> sol = terminator.getSolution();
for (std::vector <bool>::iterator it = sol.begin();
it < sol.end(); it ++)
std::cout << *it << std::endl;
return 0;
}
#endif