-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.cpp
159 lines (134 loc) · 4.17 KB
/
Main.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
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
#include <iostream>
using namespace std;
bool is_prime(uint32_t a)
{
if(a == 1 or a == 0)
return false;
for(uint32_t i=2; i<=a/2; i++)
if(a % i == 0) return false;
return true;
}
uint32_t fast_pow(uint32_t a, uint32_t power, uint32_t order)
{
if(power == 0)
return 1;
if(power % 2 == 1)
return fast_pow(a, power-1, order) * a % order;
int b = fast_pow(a, power/2, order);
return b*b % order;
}
struct GFElement // element of Galious field
{
uint32_t value;
uint32_t order; // order of field
GFElement() : value(0), order(0) {};
explicit GFElement(uint32_t p_order, uint32_t p_value=0)
{
/*if(!is_prime(p_order))
throw "Constructor error(!!!): this program can't \
\n work with not prime-order finite fields";*/
value = p_value;
order = p_order;
}
void checkOrders(GFElement b) { if(b.order != order) throw "wrong orders"; }
GFElement &operator=(GFElement b)
{
order = b.order;
value = b.value;
return *this;
}
GFElement &operator+=(GFElement b)
{ return *this = GFElement{ order, (value+b.value) % order }; }
GFElement &operator-=(GFElement b)
{ return *this = GFElement{ order, (order + value - b.value) % order };; }
GFElement &operator*=(GFElement b)
{ return *this = GFElement{ order, (value*b.value) % order }; }
GFElement inverse() { return GFElement{order, fast_pow(value, order-2, order)}; }
GFElement &operator/=(GFElement b) { return *this *= b.inverse(); }
GFElement operator+(GFElement b)
{ GFElement result{*this}; return result+=b; }
GFElement operator*(GFElement b)
{ GFElement result{*this}; return result*=b; }
bool operator==(GFElement b) { checkOrders(b); return value == b.value; }
bool operator==(int b) { return value == b % order; }
bool operator!=(GFElement b) { return !(*this == b); }
bool operator!=(int b) { return !(*this == b); }
};
istream &operator>>(istream &is, GFElement &a)
{
is >> a.value;
a.value = a.value % a.order;
return is;
}
ostream &operator<<(ostream &os, GFElement a)
{
os << a.value;
return os;
}
void print_(GFElement **A, GFElement *B, uint32_t COUNT_OF_EQS, uint32_t COUNT_OF_VARS)
{
for(uint32_t i=0; i<COUNT_OF_EQS; i++) {
for(uint32_t j=0; j<COUNT_OF_VARS; j++)
cout << A[i][j] << " ";
cout << "| " << B[i] << endl;
}
}
void gauss(GFElement **A, GFElement *B, uint32_t COUNT_OF_EQS, uint32_t COUNT_OF_VARS)
{
for(uint32_t step=0; step<COUNT_OF_EQS; step++) {
print_(A, B, COUNT_OF_EQS, COUNT_OF_VARS);
cout << endl;
if(A[step][step] == 0) {
for(uint32_t j=0; j<COUNT_OF_EQS; j++) {
}
}
if(A[step][step] != 1) {
GFElement k = A[step][step].inverse();
cout << "Multiply " << step << " row by " << k << ":" << endl;
for(uint32_t i=step; i<COUNT_OF_VARS; i++)
A[step][i] *= k;
B[step] *= k;
print_(A, B, COUNT_OF_EQS, COUNT_OF_VARS);
cout << endl;
}
for(uint32_t i=0; i<COUNT_OF_EQS; i++) {
if(i == step) continue;
GFElement k = A[i][step];
for(uint32_t j=step; j<COUNT_OF_VARS; j++)
A[i][j] -= A[step][j] * k;
B[i] -= B[step] * k;
}
}
}
int main()
{
uint32_t ORDER;
uint32_t COUNT_OF_VARS;
uint32_t COUNT_OF_EQS;
cout << "Order of field: ";
cin >> ORDER;
if(!is_prime(ORDER)) {
cout << "My program still unable to work with non-prime order finite fields" << endl;
return 1;
}
cout << "Count of variables: ";
cin >> COUNT_OF_VARS;
cout << "Count of equations: ";
cin >> COUNT_OF_EQS;
GFElement **A = new GFElement*[COUNT_OF_EQS];
GFElement *B = new GFElement[COUNT_OF_EQS];
for(uint32_t i=0; i<COUNT_OF_EQS; i++) {
A[i] = new GFElement[COUNT_OF_VARS];
for(uint32_t j=0; j<COUNT_OF_VARS; j++)
A[i][j] = GFElement{ ORDER };
B[i] = GFElement{ ORDER };
}
cout << "Type matrix A[" << COUNT_OF_EQS << "x" << COUNT_OF_VARS << "] of SLE:" << endl;
for(uint32_t i=0; i<COUNT_OF_EQS; i++)
for(uint32_t j=0; j<COUNT_OF_VARS; j++)
cin >> A[i][j];
cout << "Type vector B[" << COUNT_OF_EQS << "] - vector of right hand side:" << endl;
for(uint32_t i=0; i<COUNT_OF_EQS; i++)
cin >> B[i];
gauss(A, B, COUNT_OF_EQS, COUNT_OF_VARS);
}