-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSymbolicMatrix.cpp
336 lines (259 loc) · 8.05 KB
/
SymbolicMatrix.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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include "SymbolicMatrix.h"
SymbolicMatrix::SymbolicMatrix()
{
}
void SymbolicMatrix::setDims(unsigned int cols, unsigned int rows){
A.resize(cols);
for(unsigned int i=0;i<cols;i++){
A[i].resize(rows);
}
}
SymbolicMatrix SymbolicMatrix::operator+(const SymbolicMatrix& a) const{
SymbolicMatrix res(*this);
res+=a;
return res;
}
SymbolicMatrix SymbolicMatrix::operator*(const SymbolicMatrix& a) const{
SymbolicMatrix res(*this);
res*=a;
return res;
}
void SymbolicMatrix::operator+=(const SymbolicMatrix &b){
int resultNumberOfCols = (*this).getNumberOfRows();
int resultNumberOfRows = b.getNumberOfCols();
SymbolicMatrix result;
result.setDims(resultNumberOfCols,resultNumberOfRows);
for(unsigned i=0;i<result.getNumberOfCols();i++){
for(unsigned j=0;j<result.getNumberOfRows();j++){
PolynomialFraction temp(A[i][j]+ b.getEntry(i,j));
result.setEntry(i,j,temp);
}
}
(*this) = result;
}
void SymbolicMatrix::operator*=(const SymbolicMatrix &b){
int resultNumberOfCols = (*this).getNumberOfRows();
int resultNumberOfRows = b.getNumberOfCols();
SymbolicMatrix result;
result.setDims(resultNumberOfCols,resultNumberOfRows);
for(unsigned i=0;i<result.getNumberOfCols();i++){
for(unsigned j=0;j<result.getNumberOfRows();j++){
PolynomialFraction temp(to_ZZ_p(0));
for(unsigned k=0;k<getNumberOfCols();k++){
temp+= A[i][k]* b.getEntry(k,j);
}
result.setEntry(i,j,temp);
}
}
(*this) = result;
}
void SymbolicMatrix::setEntry(unsigned int col,unsigned int row, const PolynomialFraction &elem){
A[col][row] = elem;
}
SymbolicMatrix inv(const SymbolicMatrix& a){
unsigned n = a.getNumberOfRows();
if(a.getNumberOfCols() != n){
cout<<"inv: nonsquare matrix"<<endl;
return a;
}
SymbolicMatrix result;
result.setDims(n,n);
if(n==1){
result.setEntry(0,0,-a.getEntry(0,0));
return result;
}
if(n==0){
cout<<"You are trying to invert an empty matrix!"<<endl;
result.setEntry(0,0,PolynomialFraction(to_ZZ_p(0)));
return result;
}
PolynomialFraction determinant = a.determinant();
SymbolicMatrix adj = a.adjugate();
for(unsigned int i=0;i<n;i++){
for(unsigned j=0;j<n;j++){
result.setEntry(i,j,adj.getEntry(i,j)/determinant);
}
}
return result;
/* Victor's Variant:
unsigned i,j,k,pos;
vector <vector <PolynomialFraction> > m;
m.resize(n);
for(i = 0; i < n; i++){
m[i].resize(2*n);
for (j = 0; j < n; j++) {
m[i][j].setEntry(a.getEntry(i,j));
m[i][n+j].setEntry(PolynomialFraction(to_ZZ_p(0)));
}
m[i][n+i].setEntry(PolynomialFraction(to_ZZ_p(1)));
}
PolynomialFraction &det = PolynomialFraction(to_ZZ_p(1));
for (k = 0; k < n; k++) {
pos = -1;
for (i = k; i < n; i++) {
if (pos == -1 && !IsZero(t1)) {
pos = i;
}
}
}
/*
void inv(ZZ_p& d, mat_ZZ_p& X, const mat_ZZ_p& A)
{
long n = A.NumRows();
if (A.NumCols() != n)
Error("inv: nonsquare matrix");
if (n == 0) {
set(d);
X.SetDims(0, 0);
return;
}
long i, j, k, pos;
ZZ t1, t2;
ZZ *x, *y;
const ZZ& p = ZZ_p::modulus();
vec_ZZVec M;
sqr(t1, p);//p=t^2
mul(t1, t1, n);//t1*=n
M.SetLength(n);
for (i = 0; i < n; i++) {
M[i].SetSize(2*n, t1.size());
for (j = 0; j < n; j++) {
M[i][j] = rep(A[i][j]);
clear(M[i][n+j]);
}
set(M[i][n+i]);
}
ZZ det;
set(det);//det=1
for (k = 0; k < n; k++) {
pos = -1;
for (i = k; i < n; i++) {
rem(t1, M[i][k], p);//t1=M[i][k] mod p
M[i][k] = t1;
if (pos == -1 && !IsZero(t1)) {
pos = i;
}
}
if (pos != -1) {
if (k != pos) {
swap(M[pos], M[k]);
NegateMod(det, det, p);
}
MulMod(det, det, M[k][k], p);
// make M[k, k] == -1 mod p, and make row k reduced
InvMod(t1, M[k][k], p);
NegateMod(t1, t1, p);
for (j = k+1; j < 2*n; j++) {
rem(t2, M[k][j], p);
MulMod(M[k][j], t2, t1, p);
}
for (i = k+1; i < n; i++) {
// M[i] = M[i] + M[k]*M[i,k]
t1 = M[i][k]; // this is already reduced
x = M[i].elts() + (k+1);
y = M[k].elts() + (k+1);
for (j = k+1; j < 2*n; j++, x++, y++) {
// *x = *x + (*y)*t1
mul(t2, *y, t1);
add(*x, *x, t2);
}
}
}
else {
clear(d);
return;
}
}
X.SetDims(n, n);
for (k = 0; k < n; k++) {
for (i = n-1; i >= 0; i--) {
clear(t1);
for (j = i+1; j < n; j++) {
mul(t2, rep(X[j][k]), M[i][j]);
add(t1, t1, t2);
}
sub(t1, t1, M[i][n+k]);
conv(X[i][k], t1);
}
}
conv(d, det);
}*/
}
SymbolicMatrix transpose(const SymbolicMatrix& a){
SymbolicMatrix res;
res.setDims(a.getNumberOfRows(),a.getNumberOfCols());
for(int i=0;i<a.getNumberOfCols();i++){
for(int j=0;j<a.getNumberOfRows();j++){
res.setEntry(j,i,a.getEntry(i,j));
}
}
return res;
}
SymbolicMatrix SymbolicMatrix::adjugate() const{
unsigned n = getNumberOfRows();
if(getNumberOfCols() != n){
cout<<"inv: nonsquare matrix"<<endl;
return SymbolicMatrix(*this);//PolynomialFraction(to_ZZ_p(0));
}
SymbolicMatrix result;
result.setDims(n,n);
for(unsigned int i=0;i<n;i++){
for(unsigned int j=0;j<n;j++){
PolynomialFraction currentEntry(PolynomialFraction(to_ZZ_p(static_cast<int>(pow(-1.0,(int)(i+j))))) * (submatrix(i, j)).determinant());
result.setEntry(j,i,currentEntry);
}
}
return result;
}
PolynomialFraction SymbolicMatrix::determinant() const {
unsigned n = getNumberOfRows();
if(getNumberOfCols() != n){
cout<<"inv: nonsquare matrix"<<endl;
return PolynomialFraction(to_ZZ_p(0));
}
if(n==1){
return A[0][0];//output the only matrix entry...
}
if(n==0){
cout<<"Ypu are trying to evaluate the determinant of an empty matrix!"<<endl;
return PolynomialFraction(to_ZZ_p(0));//output the only matrix entry...
}
PolynomialFraction res(to_ZZ_p(0));
for(unsigned int i=0;i<n;i++){
//cout<<submatrix(i, 0);
res+= PolynomialFraction(to_ZZ_p(static_cast<int>(pow(-1.0,(int)i)))) * A[i][0] * (submatrix(i, 0)).determinant();
}
return res;
}
SymbolicMatrix SymbolicMatrix::submatrix(int i, int j) const {
SymbolicMatrix res;
res.setDims(getNumberOfCols()-1,getNumberOfRows()-1);
int a = 0;
for(unsigned int ii=0;ii<getNumberOfCols();ii++){
if(ii==i) continue; //Skip ith row
//res[a] = new int[n - 1];
int b = 0;
for(int jj = 0; jj< getNumberOfRows(); jj++) {
if(jj==j) continue; //Skip jth column
res.setEntry(a,b,A[ii][jj]);
b++;
}
a++; //Increment row index
}
return res;
}
bool SymbolicMatrix::unitTest(unsigned int maxDimension){
bool result = true;
//SymbolicMatrix testMatrix;
for(unsigned int i=1;i<=maxDimension;i++){
setDims(i,i);
//fill the matrix
fillSymbolicMatrix("m", 0);
//find inverse and multiply them
SymbolicMatrix testMatrix (*this);
testMatrix = testMatrix* inv(testMatrix);
//we need to obtain Identity matrix
//if not then cout i, matrix and return false
}
return result;
}