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