1 | /***************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | *****************************************/ |
---|
4 | /* |
---|
5 | * ABSTRACT: class bigintmat: matrizes of big integers |
---|
6 | */ |
---|
7 | |
---|
8 | #ifdef HAVE_CONFIG_H |
---|
9 | #include "libpolysconfig.h" |
---|
10 | #endif /* HAVE_CONFIG_H */ |
---|
11 | |
---|
12 | #include "bigintmat.h" |
---|
13 | |
---|
14 | #include <misc/intvec.h> |
---|
15 | |
---|
16 | //#include <kernel/mod2.h> |
---|
17 | //#include <kernel/options.h> |
---|
18 | |
---|
19 | #include <math.h> |
---|
20 | #include <string.h> |
---|
21 | |
---|
22 | // Ungetestet |
---|
23 | static void bimRowContent(bigintmat *bimat, int rowpos, int colpos); |
---|
24 | static void bimReduce(bigintmat *bimat, int rpiv, int colpos, |
---|
25 | int ready, int all); |
---|
26 | |
---|
27 | |
---|
28 | |
---|
29 | //#define BIMATELEM(M,I,J) (M)[ (M).index(I,J) ] |
---|
30 | |
---|
31 | bigintmat * bigintmat::transpose() |
---|
32 | { |
---|
33 | bigintmat * t = new bigintmat(col, row, basecoeffs()); |
---|
34 | for (int i=1; i<=row; i++) |
---|
35 | { |
---|
36 | for (int j=1; j<=col; j++) |
---|
37 | { |
---|
38 | t->set(j, i, BIMATELEM(*this,i,j)); |
---|
39 | } |
---|
40 | } |
---|
41 | return t; |
---|
42 | } |
---|
43 | |
---|
44 | // Beginnt bei [0] |
---|
45 | void bigintmat::set(int i, number n, const coeffs C) |
---|
46 | { |
---|
47 | assume (C == NULL || C == basecoeffs()); |
---|
48 | |
---|
49 | rawset(i, n_Copy(n, basecoeffs()), basecoeffs()); |
---|
50 | } |
---|
51 | |
---|
52 | // Beginnt bei [1,1] |
---|
53 | void bigintmat::set(int i, int j, number n, const coeffs C) |
---|
54 | { |
---|
55 | assume (C == NULL || C == basecoeffs()); |
---|
56 | assume (i >= 0 && j >= 0); |
---|
57 | assume (i <= rows() && j <= cols()); |
---|
58 | |
---|
59 | set(index(i, j), n, C); |
---|
60 | } |
---|
61 | |
---|
62 | number bigintmat::get(int i) const |
---|
63 | { |
---|
64 | assume (i >= 0); |
---|
65 | assume (i<rows()*cols()); |
---|
66 | |
---|
67 | return n_Copy(v[i], basecoeffs()); |
---|
68 | } |
---|
69 | |
---|
70 | number bigintmat::get(int i, int j) const |
---|
71 | { |
---|
72 | assume (i >= 0 && j >= 0); |
---|
73 | assume (i <= rows() && j <= cols()); |
---|
74 | |
---|
75 | return get(index(i, j)); |
---|
76 | } |
---|
77 | |
---|
78 | // Ãberladener *=-Operator (fÃŒr int und bigint) |
---|
79 | // Frage hier: *= verwenden oder lieber = und * einzeln? |
---|
80 | void bigintmat::operator*=(int intop) |
---|
81 | { |
---|
82 | number iop = n_Init(intop, basecoeffs()); |
---|
83 | |
---|
84 | inpMult(iop, basecoeffs()); |
---|
85 | |
---|
86 | n_Delete(&iop, basecoeffs()); |
---|
87 | } |
---|
88 | |
---|
89 | void bigintmat::inpMult(number bintop, const coeffs C) |
---|
90 | { |
---|
91 | assume (C == NULL || C == basecoeffs()); |
---|
92 | |
---|
93 | const int l = rows() * cols(); |
---|
94 | |
---|
95 | for (int i=0; i < l; i++) |
---|
96 | n_InpMult(v[i], bintop, basecoeffs()); |
---|
97 | } |
---|
98 | |
---|
99 | // Stimmen Parameter? |
---|
100 | // Welche der beiden Methoden? |
---|
101 | // Oder lieber eine comp-Funktion? |
---|
102 | |
---|
103 | bool operator==(const bigintmat & lhr, const bigintmat & rhr) |
---|
104 | { |
---|
105 | if (&lhr == &rhr) { return true; } |
---|
106 | if (lhr.cols() != rhr.cols()) { return false; } |
---|
107 | if (lhr.rows() != rhr.rows()) { return false; } |
---|
108 | if (lhr.basecoeffs() != rhr.basecoeffs()) { return false; } |
---|
109 | |
---|
110 | const int l = (lhr.rows())*(lhr.cols()); |
---|
111 | |
---|
112 | for (int i=0; i < l; i++) |
---|
113 | { |
---|
114 | if (!n_Equal(lhr[i], rhr[i], lhr.basecoeffs())) { return false; } |
---|
115 | } |
---|
116 | |
---|
117 | return true; |
---|
118 | } |
---|
119 | |
---|
120 | bool operator!=(const bigintmat & lhr, const bigintmat & rhr) |
---|
121 | { |
---|
122 | return !(lhr==rhr); |
---|
123 | } |
---|
124 | |
---|
125 | // Matrix-Add/-Sub/-Mult so oder mit operator+/-/* ? |
---|
126 | bigintmat * bimAdd(bigintmat * a, bigintmat * b) |
---|
127 | { |
---|
128 | if (a->cols() != b->cols()) return NULL; |
---|
129 | if (a->rows() != b->rows()) return NULL; |
---|
130 | if (a->basecoeffs() != b->basecoeffs()) { return NULL; } |
---|
131 | |
---|
132 | const coeffs basecoeffs = a->basecoeffs(); |
---|
133 | |
---|
134 | int i; |
---|
135 | |
---|
136 | bigintmat * bim = new bigintmat(a->rows(), a->cols(), basecoeffs); |
---|
137 | |
---|
138 | for (i=a->rows()*a->cols()-1;i>=0; i--) |
---|
139 | bim->rawset(i, n_Add((*a)[i], (*b)[i], basecoeffs), basecoeffs); |
---|
140 | |
---|
141 | return bim; |
---|
142 | } |
---|
143 | bigintmat * bimAdd(bigintmat * a, int b) |
---|
144 | { |
---|
145 | |
---|
146 | const int mn = a->rows()*a->cols(); |
---|
147 | |
---|
148 | const coeffs basecoeffs = a->basecoeffs(); |
---|
149 | number bb=n_Init(b,basecoeffs); |
---|
150 | |
---|
151 | int i; |
---|
152 | |
---|
153 | bigintmat * bim = new bigintmat(a->rows(),a->cols() , basecoeffs); |
---|
154 | |
---|
155 | for (i=0; i<mn; i++) |
---|
156 | bim->rawset(i, n_Add((*a)[i], bb, basecoeffs), basecoeffs); |
---|
157 | |
---|
158 | n_Delete(&bb,basecoeffs); |
---|
159 | return bim; |
---|
160 | } |
---|
161 | |
---|
162 | bigintmat * bimSub(bigintmat * a, bigintmat * b) |
---|
163 | { |
---|
164 | if (a->cols() != b->cols()) return NULL; |
---|
165 | if (a->rows() != b->rows()) return NULL; |
---|
166 | if (a->basecoeffs() != b->basecoeffs()) { return NULL; } |
---|
167 | |
---|
168 | const coeffs basecoeffs = a->basecoeffs(); |
---|
169 | |
---|
170 | int i; |
---|
171 | |
---|
172 | bigintmat * bim = new bigintmat(a->rows(), a->cols(), basecoeffs); |
---|
173 | |
---|
174 | for (i=a->rows()*a->cols()-1;i>=0; i--) |
---|
175 | bim->rawset(i, n_Sub((*a)[i], (*b)[i], basecoeffs), basecoeffs); |
---|
176 | |
---|
177 | return bim; |
---|
178 | } |
---|
179 | |
---|
180 | bigintmat * bimSub(bigintmat * a, int b) |
---|
181 | { |
---|
182 | |
---|
183 | const int mn = a->rows()*a->cols(); |
---|
184 | |
---|
185 | const coeffs basecoeffs = a->basecoeffs(); |
---|
186 | number bb=n_Init(b,basecoeffs); |
---|
187 | |
---|
188 | int i; |
---|
189 | |
---|
190 | bigintmat * bim = new bigintmat(a->rows(),a->cols() , basecoeffs); |
---|
191 | |
---|
192 | for (i=0; i<mn; i++) |
---|
193 | bim->rawset(i, n_Sub((*a)[i], bb, basecoeffs), basecoeffs); |
---|
194 | |
---|
195 | n_Delete(&bb,basecoeffs); |
---|
196 | return bim; |
---|
197 | } |
---|
198 | |
---|
199 | bigintmat * bimMult(bigintmat * a, bigintmat * b) |
---|
200 | { |
---|
201 | const int ca = a->cols(); |
---|
202 | const int cb = b->cols(); |
---|
203 | |
---|
204 | const int ra = a->rows(); |
---|
205 | const int rb = b->rows(); |
---|
206 | |
---|
207 | if (ca != rb) |
---|
208 | { |
---|
209 | #ifndef NDEBUG |
---|
210 | Werror("wrong bigintmat sizes at multiplication a * b: acols: %d != brows: %d\n", ca, rb); |
---|
211 | #endif |
---|
212 | return NULL; |
---|
213 | } |
---|
214 | |
---|
215 | assume (ca == rb); |
---|
216 | |
---|
217 | if (a->basecoeffs() != b->basecoeffs()) { return NULL; } |
---|
218 | |
---|
219 | const coeffs basecoeffs = a->basecoeffs(); |
---|
220 | |
---|
221 | int i, j, k; |
---|
222 | |
---|
223 | number sum; |
---|
224 | |
---|
225 | bigintmat * bim = new bigintmat(ra, cb, basecoeffs); |
---|
226 | |
---|
227 | for (i=1; i<=ra; i++) |
---|
228 | for (j=1; j<=cb; j++) |
---|
229 | { |
---|
230 | sum = n_Init(0, basecoeffs); |
---|
231 | |
---|
232 | for (k=1; k<=ca; k++) |
---|
233 | { |
---|
234 | number prod = n_Mult( BIMATELEM(*a, i, k), BIMATELEM(*b, k, j), basecoeffs); |
---|
235 | |
---|
236 | number sum2 = n_Add(sum, prod, basecoeffs); // no inplace add :( |
---|
237 | |
---|
238 | n_Delete(&sum, basecoeffs); n_Delete(&prod, basecoeffs); |
---|
239 | |
---|
240 | sum = sum2; |
---|
241 | } |
---|
242 | bim->rawset(i, j, sum, basecoeffs); |
---|
243 | } |
---|
244 | return bim; |
---|
245 | } |
---|
246 | |
---|
247 | bigintmat * bimMult(bigintmat * a, int b) |
---|
248 | { |
---|
249 | |
---|
250 | const int mn = a->rows()*a->cols(); |
---|
251 | |
---|
252 | const coeffs basecoeffs = a->basecoeffs(); |
---|
253 | number bb=n_Init(b,basecoeffs); |
---|
254 | |
---|
255 | int i; |
---|
256 | |
---|
257 | bigintmat * bim = new bigintmat(a->rows(),a->cols() , basecoeffs); |
---|
258 | |
---|
259 | for (i=0; i<mn; i++) |
---|
260 | bim->rawset(i, n_Mult((*a)[i], bb, basecoeffs), basecoeffs); |
---|
261 | |
---|
262 | n_Delete(&bb,basecoeffs); |
---|
263 | return bim; |
---|
264 | } |
---|
265 | |
---|
266 | bigintmat * bimMult(bigintmat * a, number b, const coeffs cf) |
---|
267 | { |
---|
268 | if (cf!=a->basecoeffs()) return NULL; |
---|
269 | |
---|
270 | const int mn = a->rows()*a->cols(); |
---|
271 | |
---|
272 | const coeffs basecoeffs = a->basecoeffs(); |
---|
273 | |
---|
274 | int i; |
---|
275 | |
---|
276 | bigintmat * bim = new bigintmat(a->rows(),a->cols() , basecoeffs); |
---|
277 | |
---|
278 | for (i=0; i<mn; i++) |
---|
279 | bim->rawset(i, n_Mult((*a)[i], b, basecoeffs), basecoeffs); |
---|
280 | |
---|
281 | return bim; |
---|
282 | } |
---|
283 | |
---|
284 | // ----------------------------------------------------------------- // |
---|
285 | // Korrekt? |
---|
286 | |
---|
287 | intvec * bim2iv(bigintmat * b) |
---|
288 | { |
---|
289 | intvec * iv = new intvec(b->rows(), b->cols(), 0); |
---|
290 | for (int i=0; i<(b->rows())*(b->cols()); i++) |
---|
291 | (*iv)[i] = n_Int((*b)[i], b->basecoeffs()); // Geht das so? |
---|
292 | return iv; |
---|
293 | } |
---|
294 | |
---|
295 | bigintmat * iv2bim(intvec * b, const coeffs C) |
---|
296 | { |
---|
297 | const int l = (b->rows())*(b->cols()); |
---|
298 | bigintmat * bim = new bigintmat(b->rows(), b->cols(), C); |
---|
299 | |
---|
300 | for (int i=0; i < l; i++) |
---|
301 | bim->rawset(i, n_Init((*b)[i], C), C); |
---|
302 | |
---|
303 | return bim; |
---|
304 | } |
---|
305 | |
---|
306 | // ----------------------------------------------------------------- // |
---|
307 | |
---|
308 | int bigintmat::compare(const bigintmat* op) const |
---|
309 | { |
---|
310 | assume (basecoeffs() == op->basecoeffs() ); |
---|
311 | |
---|
312 | #ifndef NDEBUG |
---|
313 | if (basecoeffs() != op->basecoeffs() ) |
---|
314 | WerrorS("wrong bigintmat comparison: different basecoeffs!\n"); |
---|
315 | #endif |
---|
316 | |
---|
317 | if ((col!=1) ||(op->cols()!=1)) |
---|
318 | { |
---|
319 | if((col!=op->cols()) |
---|
320 | || (row!=op->rows())) |
---|
321 | return -2; |
---|
322 | } |
---|
323 | |
---|
324 | int i; |
---|
325 | for (i=0; i<si_min(row*col,op->rows()*op->cols()); i++) |
---|
326 | { |
---|
327 | if ( n_Greater(v[i], (*op)[i], basecoeffs()) ) |
---|
328 | return 1; |
---|
329 | else if (! n_Equal(v[i], (*op)[i], basecoeffs())) |
---|
330 | return -1; |
---|
331 | } |
---|
332 | |
---|
333 | for (; i<row; i++) |
---|
334 | { |
---|
335 | if ( n_GreaterZero(v[i], basecoeffs()) ) |
---|
336 | return 1; |
---|
337 | else if (! n_IsZero(v[i], basecoeffs()) ) |
---|
338 | return -1; |
---|
339 | } |
---|
340 | for (; i<op->rows(); i++) |
---|
341 | { |
---|
342 | if ( n_GreaterZero((*op)[i], basecoeffs()) ) |
---|
343 | return -1; |
---|
344 | else if (! n_IsZero((*op)[i], basecoeffs()) ) |
---|
345 | return 1; |
---|
346 | } |
---|
347 | return 0; |
---|
348 | } |
---|
349 | |
---|
350 | |
---|
351 | bigintmat * bimCopy(const bigintmat * b) |
---|
352 | { |
---|
353 | if (b == NULL) |
---|
354 | return NULL; |
---|
355 | |
---|
356 | return new bigintmat(b); |
---|
357 | } |
---|
358 | |
---|
359 | char* bigintmat::String() |
---|
360 | { |
---|
361 | StringSetS(""); |
---|
362 | const int l = cols()*rows(); |
---|
363 | |
---|
364 | n_Write(v[0], basecoeffs()); |
---|
365 | for (int i = 1; i < l; i++) |
---|
366 | { |
---|
367 | StringAppendS(","); n_Write(v[i], basecoeffs()); |
---|
368 | } |
---|
369 | /* if (i != col*row-1) |
---|
370 | { |
---|
371 | StringAppendS(","); |
---|
372 | if ((i+1)%col == 0) |
---|
373 | StringAppendS("\n"); |
---|
374 | } */ |
---|
375 | return StringEndS(); |
---|
376 | } |
---|
377 | |
---|
378 | char* bigintmat::StringAsPrinted() |
---|
379 | { |
---|
380 | if ((col==0) || (row==0)) |
---|
381 | return NULL; |
---|
382 | else |
---|
383 | { |
---|
384 | int * colwid = getwid(80); |
---|
385 | if (colwid == NULL) |
---|
386 | { |
---|
387 | WerrorS("not enough space to print bigintmat"); |
---|
388 | WerrorS("try string(...) for a unformatted output"); |
---|
389 | return NULL; |
---|
390 | } |
---|
391 | char * ps; |
---|
392 | int slength = 0; |
---|
393 | for (int j=0; j<col; j++) |
---|
394 | slength += colwid[j]*row; |
---|
395 | slength += col*row+row; |
---|
396 | ps = (char*) omAlloc0(sizeof(char)*(slength)); |
---|
397 | int pos = 0; |
---|
398 | for (int i=0; i<col*row; i++) |
---|
399 | { |
---|
400 | StringSetS(""); |
---|
401 | n_Write(v[i], basecoeffs()); |
---|
402 | char * ts = StringEndS(); |
---|
403 | const int _nl = strlen(ts); |
---|
404 | int cj = i%col; |
---|
405 | if (_nl > colwid[cj]) |
---|
406 | { |
---|
407 | StringSetS(""); |
---|
408 | int ci = i/col; |
---|
409 | StringAppend("[%d,%d]", ci+1, cj+1); |
---|
410 | char * ph = StringEndS(); |
---|
411 | int phl = strlen(ph); |
---|
412 | if (phl > colwid[cj]) |
---|
413 | { |
---|
414 | for (int j=0; j<colwid[cj]-1; j++) |
---|
415 | ps[pos+j] = ' '; |
---|
416 | ps[pos+colwid[cj]-1] = '*'; |
---|
417 | } |
---|
418 | else |
---|
419 | { |
---|
420 | for (int j=0; j<colwid[cj]-phl; j++) |
---|
421 | ps[pos+j] = ' '; |
---|
422 | for (int j=0; j<phl; j++) |
---|
423 | ps[pos+colwid[cj]-phl+j] = ph[j]; |
---|
424 | } |
---|
425 | omFree(ph); |
---|
426 | } |
---|
427 | else // Mit Leerzeichen auffÃŒllen und zahl reinschreiben |
---|
428 | { |
---|
429 | for (int j=0; j<colwid[cj]-_nl; j++) |
---|
430 | ps[pos+j] = ' '; |
---|
431 | for (int j=0; j<_nl; j++) |
---|
432 | ps[pos+colwid[cj]-_nl+j] = ts[j]; |
---|
433 | } |
---|
434 | // ", " und (evtl) "\n" einfÃŒgen |
---|
435 | if ((i+1)%col == 0) |
---|
436 | { |
---|
437 | if (i != col*row-1) |
---|
438 | { |
---|
439 | ps[pos+colwid[cj]] = ','; |
---|
440 | ps[pos+colwid[cj]+1] = '\n'; |
---|
441 | pos += colwid[cj]+2; |
---|
442 | } |
---|
443 | } |
---|
444 | else |
---|
445 | { |
---|
446 | ps[pos+colwid[cj]] = ','; |
---|
447 | pos += colwid[cj]+1; |
---|
448 | } |
---|
449 | |
---|
450 | omFree(ts); // Hier ts zerstören |
---|
451 | } |
---|
452 | return(ps); |
---|
453 | // omFree(ps); |
---|
454 | } |
---|
455 | // if ((col==0) || (row==0)) |
---|
456 | // return 0; |
---|
457 | // int * colwid = getwid(80); |
---|
458 | // if (colwid == NULL) |
---|
459 | // { |
---|
460 | // WerrorS("not enough space to print bigintmat"); |
---|
461 | // WerrorS("try string(...) for a unformatted output"); |
---|
462 | // return 0; |
---|
463 | // } |
---|
464 | // char * ps; |
---|
465 | // int slength = 0; |
---|
466 | // for (int j=0; j<col; j++) |
---|
467 | // slength += colwid[j]*row; |
---|
468 | // slength += 2*(col-1)*row+2*row-1; |
---|
469 | // ps = (char*) omAlloc0(sizeof(char)*(slength)); |
---|
470 | // int pos = 0; |
---|
471 | // for (int i=0; i<col*row; i++) |
---|
472 | // { |
---|
473 | // StringSetS(""); |
---|
474 | // n_Write(v[i], basecoeffs()); |
---|
475 | // char * temp = StringAppendS(""); |
---|
476 | // char * ts = omStrDup(temp); |
---|
477 | // int nl = strlen(ts); |
---|
478 | // int cj = i%col; |
---|
479 | // if (nl > colwid[cj]) |
---|
480 | // { |
---|
481 | // StringSetS(""); |
---|
482 | // int ci = floor(i/col); |
---|
483 | // StringAppend("[%d,%d]", ci+1, cj+1); |
---|
484 | // char *tmp = StringAppendS(""); |
---|
485 | // char * ph = omStrDup(tmp); |
---|
486 | // int phl = strlen(ph); |
---|
487 | // if (phl > colwid[cj]) |
---|
488 | // { |
---|
489 | // for (int j=0; j<colwid[cj]; j++) |
---|
490 | // ps[pos+j] = '*'; |
---|
491 | // } |
---|
492 | // else |
---|
493 | // { |
---|
494 | // for (int j=0; j<colwid[cj]-phl; j++) |
---|
495 | // ps[pos+j] = ' '; |
---|
496 | // for (int j=0; j<phl; j++) |
---|
497 | // ps[pos+colwid[cj]-phl+j] = ph[j]; |
---|
498 | // } |
---|
499 | // omFree(ph); |
---|
500 | // } |
---|
501 | // else // Mit Leerzeichen auffÃŒllen und Zahl reinschreiben |
---|
502 | // { |
---|
503 | // for (int j=0; j<colwid[cj]-nl; j++) |
---|
504 | // ps[pos+j] = ' '; |
---|
505 | // for (int j=0; j<nl; j++) |
---|
506 | // ps[pos+colwid[cj]-nl+j] = ts[j]; |
---|
507 | // } |
---|
508 | // // ", " oder "\n" einfÃŒgen |
---|
509 | // if ((i+1)%col == 0) |
---|
510 | // { |
---|
511 | // if (i != col*row-1) |
---|
512 | // { |
---|
513 | // ps[pos+colwid[cj]] = ','; |
---|
514 | // ps[pos+colwid[cj]+1] = '\n'; |
---|
515 | // pos += colwid[cj]+2; |
---|
516 | // } |
---|
517 | // } |
---|
518 | // else |
---|
519 | // { |
---|
520 | // ps[pos+colwid[cj]] = ','; |
---|
521 | // ps[pos+colwid[cj]+1] = ' '; |
---|
522 | // pos += colwid[cj]+2; |
---|
523 | // } |
---|
524 | // omFree(ts); |
---|
525 | // } |
---|
526 | // return ps; |
---|
527 | } |
---|
528 | |
---|
529 | int intArrSum(int * a, int length) |
---|
530 | { |
---|
531 | int sum = 0; |
---|
532 | for (int i=0; i<length; i++) |
---|
533 | sum += a[i]; |
---|
534 | return sum; |
---|
535 | } |
---|
536 | |
---|
537 | int findLongest(int * a, int length) |
---|
538 | { |
---|
539 | int l = 0; |
---|
540 | int index; |
---|
541 | for (int i=0; i<length; i++) |
---|
542 | { |
---|
543 | if (a[i] > l) |
---|
544 | { |
---|
545 | l = a[i]; |
---|
546 | index = i; |
---|
547 | } |
---|
548 | } |
---|
549 | return index; |
---|
550 | } |
---|
551 | |
---|
552 | int getShorter (int * a, int l, int j, int cols, int rows) |
---|
553 | { |
---|
554 | int sndlong = 0; |
---|
555 | int min; |
---|
556 | for (int i=0; i<rows; i++) |
---|
557 | { |
---|
558 | int index = cols*i+j; |
---|
559 | if ((a[index] > sndlong) && (a[index] < l)) |
---|
560 | { |
---|
561 | min = floor(log10((double)cols))+floor(log10((double)rows))+5; |
---|
562 | if ((a[index] < min) && (min < l)) |
---|
563 | sndlong = min; |
---|
564 | else |
---|
565 | sndlong = a[index]; |
---|
566 | } |
---|
567 | } |
---|
568 | if (sndlong == 0) |
---|
569 | { |
---|
570 | min = floor(log10((double)cols))+floor(log10((double)rows))+5; |
---|
571 | if (min < l) |
---|
572 | sndlong = min; |
---|
573 | else |
---|
574 | sndlong = 1; |
---|
575 | } |
---|
576 | return sndlong; |
---|
577 | } |
---|
578 | |
---|
579 | |
---|
580 | int * bigintmat::getwid(int maxwid) |
---|
581 | { |
---|
582 | int const c = /*2**/(col-1)+1; |
---|
583 | if (col + c > maxwid-1) return NULL; |
---|
584 | int * wv = (int*)omAlloc(sizeof(int)*col*row); |
---|
585 | int * cwv = (int*)omAlloc(sizeof(int)*col); |
---|
586 | for (int j=0; j<col; j++) |
---|
587 | { |
---|
588 | cwv[j] = 0; |
---|
589 | for (int i=0; i<row; i++) |
---|
590 | { |
---|
591 | StringSetS(""); |
---|
592 | n_Write(v[col*i+j], basecoeffs()); |
---|
593 | char * tmp = StringEndS(); |
---|
594 | const int _nl = strlen(tmp); |
---|
595 | wv[col*i+j] = _nl; |
---|
596 | if (_nl > cwv[j]) |
---|
597 | cwv[j]=_nl; |
---|
598 | omFree(tmp); |
---|
599 | } |
---|
600 | } |
---|
601 | |
---|
602 | // Groesse verkleinern, bis < maxwid |
---|
603 | while (intArrSum(cwv, col)+c > maxwid) |
---|
604 | { |
---|
605 | int j = findLongest(cwv, col); |
---|
606 | cwv[j] = getShorter(wv, cwv[j], j, col, row); |
---|
607 | } |
---|
608 | omFree(wv); |
---|
609 | return cwv; |
---|
610 | } |
---|
611 | |
---|
612 | void bigintmat::pprint(int maxwid) |
---|
613 | { |
---|
614 | if ((col==0) || (row==0)) |
---|
615 | PrintS(""); |
---|
616 | else |
---|
617 | { |
---|
618 | int * colwid = getwid(maxwid); |
---|
619 | if (colwid == NULL) |
---|
620 | { |
---|
621 | WerrorS("not enough space to print bigintmat"); |
---|
622 | return; |
---|
623 | } |
---|
624 | char * ps; |
---|
625 | int slength = 0; |
---|
626 | for (int j=0; j<col; j++) |
---|
627 | slength += colwid[j]*row; |
---|
628 | slength += col*row+row; |
---|
629 | ps = (char*) omAlloc0(sizeof(char)*(slength)); |
---|
630 | int pos = 0; |
---|
631 | for (int i=0; i<col*row; i++) |
---|
632 | { |
---|
633 | StringSetS(""); |
---|
634 | n_Write(v[i], basecoeffs()); |
---|
635 | char * ts = StringEndS(); |
---|
636 | const int _nl = strlen(ts); |
---|
637 | int cj = i%col; |
---|
638 | if (_nl > colwid[cj]) |
---|
639 | { |
---|
640 | StringSetS(""); |
---|
641 | int ci = i/col; |
---|
642 | StringAppend("[%d,%d]", ci+1, cj+1); |
---|
643 | char * ph = StringEndS(); |
---|
644 | int phl = strlen(ph); |
---|
645 | if (phl > colwid[cj]) |
---|
646 | { |
---|
647 | for (int j=0; j<colwid[cj]-1; j++) |
---|
648 | ps[pos+j] = ' '; |
---|
649 | ps[pos+colwid[cj]-1] = '*'; |
---|
650 | } |
---|
651 | else |
---|
652 | { |
---|
653 | for (int j=0; j<colwid[cj]-phl; j++) |
---|
654 | ps[pos+j] = ' '; |
---|
655 | for (int j=0; j<phl; j++) |
---|
656 | ps[pos+colwid[cj]-phl+j] = ph[j]; |
---|
657 | } |
---|
658 | omFree(ph); |
---|
659 | } |
---|
660 | else // Mit Leerzeichen auffÃŒllen und zahl reinschreiben |
---|
661 | { |
---|
662 | for (int j=0; j<colwid[cj]-_nl; j++) |
---|
663 | ps[pos+j] = ' '; |
---|
664 | for (int j=0; j<_nl; j++) |
---|
665 | ps[pos+colwid[cj]-_nl+j] = ts[j]; |
---|
666 | } |
---|
667 | // ", " und (evtl) "\n" einfÃŒgen |
---|
668 | if ((i+1)%col == 0) |
---|
669 | { |
---|
670 | if (i != col*row-1) |
---|
671 | { |
---|
672 | ps[pos+colwid[cj]] = ','; |
---|
673 | ps[pos+colwid[cj]+1] = '\n'; |
---|
674 | pos += colwid[cj]+2; |
---|
675 | } |
---|
676 | } |
---|
677 | else |
---|
678 | { |
---|
679 | ps[pos+colwid[cj]] = ','; |
---|
680 | pos += colwid[cj]+1; |
---|
681 | } |
---|
682 | |
---|
683 | omFree(ts); // Hier ts zerstören |
---|
684 | } |
---|
685 | PrintS(ps); |
---|
686 | // omFree(ps); |
---|
687 | } |
---|
688 | } |
---|
689 | |
---|
690 | // Ungetestet |
---|
691 | // (According to C. Fieker) Seems to have a lot of memory leaks (pure adaptation of the |
---|
692 | // corresponding method for intmat) due to return statements |
---|
693 | static void bimRowContent(bigintmat *bimat, int rowpos, int colpos) |
---|
694 | { |
---|
695 | const coeffs basecoeffs = bimat->basecoeffs(); |
---|
696 | |
---|
697 | number tgcd, m; |
---|
698 | int i=bimat->cols(); |
---|
699 | |
---|
700 | loop |
---|
701 | { |
---|
702 | tgcd = n_Copy(BIMATELEM(*bimat,rowpos,i--), basecoeffs); |
---|
703 | if (! n_IsZero(tgcd, basecoeffs)) break; |
---|
704 | if (i<colpos) return; |
---|
705 | } |
---|
706 | if ((! n_GreaterZero(tgcd, basecoeffs)) && (! n_IsZero(tgcd, basecoeffs))) tgcd = n_Neg(tgcd, basecoeffs); |
---|
707 | if ( n_IsOne(tgcd,basecoeffs)) return; |
---|
708 | loop |
---|
709 | { |
---|
710 | m = n_Copy(BIMATELEM(*bimat,rowpos,i--), basecoeffs); |
---|
711 | if (! n_IsZero(m,basecoeffs)) |
---|
712 | { |
---|
713 | number tp1 = n_Gcd(tgcd, m, basecoeffs); |
---|
714 | n_Delete(&tgcd, basecoeffs); |
---|
715 | tgcd = tp1; |
---|
716 | } |
---|
717 | if ( n_IsOne(tgcd,basecoeffs)) return; |
---|
718 | if (i<colpos) break; |
---|
719 | } |
---|
720 | for (i=bimat->cols();i>=colpos;i--) |
---|
721 | { |
---|
722 | number tp2 = n_Div(BIMATELEM(*bimat,rowpos,i), tgcd,basecoeffs); |
---|
723 | n_Delete(&BIMATELEM(*bimat,rowpos,i), basecoeffs); |
---|
724 | BIMATELEM(*bimat,rowpos,i) = tp2; |
---|
725 | } |
---|
726 | n_Delete(&tgcd, basecoeffs); |
---|
727 | n_Delete(&m, basecoeffs); |
---|
728 | } |
---|
729 | |
---|
730 | static void bimReduce(bigintmat *bimat, int rpiv, int colpos, |
---|
731 | int ready, int all) |
---|
732 | { |
---|
733 | const coeffs basecoeffs = bimat->basecoeffs(); |
---|
734 | |
---|
735 | number tgcd, ce, m1, m2; |
---|
736 | int j, i; |
---|
737 | number piv = BIMATELEM(*bimat,rpiv,colpos); |
---|
738 | |
---|
739 | for (j=all;j>ready;j--) |
---|
740 | { |
---|
741 | ce = n_Copy(BIMATELEM(*bimat,j,colpos),basecoeffs); |
---|
742 | if (! n_IsZero(ce, basecoeffs)) |
---|
743 | { |
---|
744 | n_Delete(&BIMATELEM(*bimat,j,colpos), basecoeffs); |
---|
745 | BIMATELEM(*bimat,j,colpos) = n_Init(0, basecoeffs); |
---|
746 | m1 = n_Copy(piv,basecoeffs); |
---|
747 | m2 = n_Copy(ce,basecoeffs); |
---|
748 | tgcd = n_Gcd(m1, m2, basecoeffs); |
---|
749 | if (! n_IsOne(tgcd,basecoeffs)) |
---|
750 | { |
---|
751 | number tp1 = n_Div(m1, tgcd,basecoeffs); |
---|
752 | number tp2 = n_Div(m2, tgcd,basecoeffs); |
---|
753 | n_Delete(&m1, basecoeffs); |
---|
754 | n_Delete(&m2, basecoeffs); |
---|
755 | m1 = tp1; |
---|
756 | m2 = tp2; |
---|
757 | } |
---|
758 | for (i=bimat->cols();i>colpos;i--) |
---|
759 | { |
---|
760 | n_Delete(&BIMATELEM(*bimat,j,i), basecoeffs); |
---|
761 | number tp1 = n_Mult(BIMATELEM(*bimat,j,i), m1,basecoeffs); |
---|
762 | number tp2 = n_Mult(BIMATELEM(*bimat,rpiv,i), m2,basecoeffs); |
---|
763 | BIMATELEM(*bimat,j,i) = n_Sub(tp1, tp2,basecoeffs); |
---|
764 | n_Delete(&tp1, basecoeffs); |
---|
765 | n_Delete(&tp2, basecoeffs); |
---|
766 | } |
---|
767 | bimRowContent(bimat, j, colpos+1); |
---|
768 | n_Delete(&m1, basecoeffs); |
---|
769 | n_Delete(&m2, basecoeffs); |
---|
770 | } |
---|
771 | n_Delete(&ce, basecoeffs); |
---|
772 | } |
---|
773 | } |
---|
774 | |
---|
775 | // columnwise concatination of two bigintmats |
---|
776 | bigintmat * bimConcat(bigintmat * a, bigintmat * b, const coeffs cf) |
---|
777 | { |
---|
778 | int ac=a->cols(); |
---|
779 | int r=si_max(a->rows(),b->rows()); |
---|
780 | bigintmat * ab = new bigintmat(r,ac + b->cols(),cf); |
---|
781 | |
---|
782 | int i,j; |
---|
783 | for (i=1; i<=a->rows(); i++) |
---|
784 | { |
---|
785 | for (j=1; j<=ac; j++) |
---|
786 | { |
---|
787 | n_Delete(&(BIMATELEM(*ab,i,j)),cf); |
---|
788 | BIMATELEM(*ab,i,j)=n_Copy(BIMATELEM(*a,i,j),cf); |
---|
789 | } |
---|
790 | } |
---|
791 | |
---|
792 | for (i=1; i<=b->rows(); i++) |
---|
793 | { |
---|
794 | for (j=1; j<=b->cols(); j++) |
---|
795 | { |
---|
796 | n_Delete(&(BIMATELEM(*ab,i,j+ac)),cf); |
---|
797 | BIMATELEM(*ab,i,j+ac)=n_Copy(BIMATELEM(*b,i,j),cf); |
---|
798 | } |
---|
799 | } |
---|
800 | |
---|
801 | return ab; |
---|
802 | } |
---|
803 | |
---|