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