[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()); |
---|
| 51 | assume (i > 0 && j > 0); |
---|
| 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 | { |
---|
[fe02b1] | 67 | assume (i > 0 && j > 0); |
---|
| 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 | { |
---|
[fe02b1] | 356 | assume (rows() > 0); |
---|
| 357 | assume (cols() > 0); |
---|
| 358 | |
---|
[9127cc] | 359 | StringSetS(""); |
---|
[fe02b1] | 360 | const int l = cols()*rows(); |
---|
| 361 | |
---|
| 362 | n_Write(v[0], basecoeffs()); |
---|
| 363 | for (int i = 1; i < l; i++) |
---|
[9127cc] | 364 | { |
---|
[fe02b1] | 365 | StringAppendS(","); n_Write(v[i], basecoeffs()); |
---|
[9127cc] | 366 | } |
---|
[fe02b1] | 367 | /* if (i != col*row-1) |
---|
| 368 | { |
---|
| 369 | StringAppendS(","); |
---|
| 370 | if ((i+1)%col == 0) |
---|
| 371 | StringAppendS("\n"); |
---|
| 372 | } */ |
---|
[9127cc] | 373 | return StringAppendS(""); |
---|
| 374 | } |
---|
| 375 | |
---|
[510dbc] | 376 | int intArrSum(int * a, int length) |
---|
| 377 | { |
---|
| 378 | int sum = 0; |
---|
| 379 | for (int i=0; i<length; i++) |
---|
| 380 | sum += a[i]; |
---|
| 381 | return sum; |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | int findLongest(int * a, int length) |
---|
[9127cc] | 385 | { |
---|
[510dbc] | 386 | int l = 0; |
---|
| 387 | int index; |
---|
| 388 | for (int i=0; i<length; i++) |
---|
[9127cc] | 389 | { |
---|
[510dbc] | 390 | if (a[i] > l) |
---|
[9127cc] | 391 | { |
---|
[510dbc] | 392 | l = a[i]; |
---|
| 393 | index = i; |
---|
| 394 | } |
---|
| 395 | } |
---|
| 396 | return index; |
---|
| 397 | } |
---|
| 398 | |
---|
| 399 | int getShorter (int * a, int l, int j, int cols, int rows) |
---|
| 400 | { |
---|
| 401 | int sndlong = 0; |
---|
| 402 | int min; |
---|
| 403 | for (int i=0; i<rows; i++) |
---|
| 404 | { |
---|
| 405 | int index = cols*i+j; |
---|
| 406 | if ((a[index] > sndlong) && (a[index] < l)) |
---|
| 407 | { |
---|
| 408 | min = floor(log10(cols))+floor(log10(rows))+5; |
---|
| 409 | if ((a[index] < min) && (min < l)) |
---|
| 410 | sndlong = min; |
---|
[9127cc] | 411 | else |
---|
[510dbc] | 412 | sndlong = a[index]; |
---|
[9127cc] | 413 | } |
---|
| 414 | } |
---|
[510dbc] | 415 | if (sndlong == 0) |
---|
| 416 | { |
---|
| 417 | min = floor(log10(cols))+floor(log10(rows))+5; |
---|
| 418 | if (min < l) |
---|
| 419 | sndlong = min; |
---|
| 420 | else |
---|
| 421 | sndlong = 1; |
---|
| 422 | } |
---|
| 423 | return sndlong; |
---|
| 424 | } |
---|
| 425 | |
---|
| 426 | |
---|
| 427 | int * bigintmat::getwid(int maxwid) |
---|
| 428 | { |
---|
| 429 | int const c = /*2**/(col-1)+1; |
---|
| 430 | if (col + c > maxwid-1) return NULL; |
---|
| 431 | int * wv = (int*)omAlloc(sizeof(int)*col*row); |
---|
| 432 | int * cwv = (int*)omAlloc(sizeof(int)*col); |
---|
| 433 | for (int j=0; j<col; j++) |
---|
| 434 | { |
---|
| 435 | cwv[j] = 0; |
---|
| 436 | for (int i=0; i<row; i++) |
---|
| 437 | { |
---|
| 438 | StringSetS(""); |
---|
| 439 | n_Write(v[col*i+j], basecoeffs()); |
---|
| 440 | char * tmp = StringAppendS(""); |
---|
| 441 | const int _nl = strlen(tmp); |
---|
| 442 | wv[col*i+j] = _nl; |
---|
| 443 | if (_nl > cwv[j]) |
---|
| 444 | cwv[j]=_nl; |
---|
| 445 | } |
---|
| 446 | } |
---|
| 447 | |
---|
| 448 | // Groesse verkleinern, bis < maxwid |
---|
| 449 | while (intArrSum(cwv, col)+c > maxwid) |
---|
| 450 | { |
---|
| 451 | int j = findLongest(cwv, col); |
---|
| 452 | cwv[j] = getShorter(wv, cwv[j], j, col, row); |
---|
| 453 | } |
---|
| 454 | omFree(wv); |
---|
| 455 | return cwv; |
---|
[9127cc] | 456 | } |
---|
| 457 | |
---|
| 458 | void bigintmat::pprint(int maxwid) |
---|
| 459 | { |
---|
| 460 | if ((col==0) || (row==0)) |
---|
| 461 | PrintS(""); |
---|
| 462 | else |
---|
| 463 | { |
---|
[510dbc] | 464 | int * colwid = getwid(maxwid); |
---|
| 465 | if (colwid == NULL) |
---|
| 466 | { |
---|
| 467 | WerrorS("not enough space to print bigintmat"); |
---|
| 468 | return; |
---|
| 469 | } |
---|
[9127cc] | 470 | char * ps; |
---|
[510dbc] | 471 | int slength = 0; |
---|
| 472 | for (int j=0; j<col; j++) |
---|
| 473 | slength += colwid[j]*row; |
---|
| 474 | slength += col*row+row; |
---|
| 475 | ps = (char*) omAlloc0(sizeof(char)*(slength)); |
---|
[9127cc] | 476 | int pos = 0; |
---|
| 477 | for (int i=0; i<col*row; i++) |
---|
| 478 | { |
---|
| 479 | StringSetS(""); |
---|
[fe02b1] | 480 | n_Write(v[i], basecoeffs()); |
---|
[9127cc] | 481 | char * temp = StringAppendS(""); |
---|
| 482 | char * ts = omStrDup(temp); |
---|
[fe02b1] | 483 | const int _nl = strlen(ts); |
---|
[510dbc] | 484 | int cj = i%col; |
---|
| 485 | if (_nl > colwid[cj]) |
---|
[9127cc] | 486 | { |
---|
| 487 | StringSetS(""); |
---|
| 488 | int ci = floor(i/col); |
---|
| 489 | StringAppend("[%d,%d]", ci+1, cj+1); |
---|
| 490 | char *tmp = StringAppendS(""); |
---|
| 491 | char * ph = omStrDup(tmp); |
---|
| 492 | int phl = strlen(ph); |
---|
[510dbc] | 493 | if (phl > colwid[cj]) |
---|
[9127cc] | 494 | { |
---|
[510dbc] | 495 | for (int j=0; j<colwid[cj]-1; j++) |
---|
| 496 | ps[pos+j] = ' '; |
---|
| 497 | ps[pos+colwid[cj]-1] = '*'; |
---|
[9127cc] | 498 | } |
---|
| 499 | else |
---|
| 500 | { |
---|
[510dbc] | 501 | for (int j=0; j<colwid[cj]-phl; j++) |
---|
[9127cc] | 502 | ps[pos+j] = ' '; |
---|
| 503 | for (int j=0; j<phl; j++) |
---|
[510dbc] | 504 | ps[pos+colwid[cj]-phl+j] = ph[j]; |
---|
[9127cc] | 505 | } |
---|
| 506 | omFree(ph); |
---|
| 507 | } |
---|
| 508 | else // Mit Leerzeichen auffÃŒllen und zahl reinschreiben |
---|
| 509 | { |
---|
[510dbc] | 510 | for (int j=0; j<colwid[cj]-_nl; j++) |
---|
[9127cc] | 511 | ps[pos+j] = ' '; |
---|
[fe02b1] | 512 | for (int j=0; j<_nl; j++) |
---|
[510dbc] | 513 | ps[pos+colwid[cj]-_nl+j] = ts[j]; |
---|
[9127cc] | 514 | } |
---|
[510dbc] | 515 | // ", " und (evtl) "\n" einfÃŒgen |
---|
[9127cc] | 516 | if ((i+1)%col == 0) |
---|
| 517 | { |
---|
| 518 | if (i != col*row-1) |
---|
| 519 | { |
---|
[510dbc] | 520 | ps[pos+colwid[cj]] = ','; |
---|
| 521 | ps[pos+colwid[cj]+1] = '\n'; |
---|
| 522 | pos += colwid[cj]+2; |
---|
[9127cc] | 523 | } |
---|
| 524 | } |
---|
| 525 | else |
---|
| 526 | { |
---|
[510dbc] | 527 | ps[pos+colwid[cj]] = ','; |
---|
| 528 | pos += colwid[cj]+1; |
---|
[9127cc] | 529 | } |
---|
[510dbc] | 530 | |
---|
| 531 | omFree(ts); // Hier ts zerstören |
---|
[9127cc] | 532 | } |
---|
| 533 | PrintS(ps); |
---|
[510dbc] | 534 | // omFree(ps); |
---|
[9127cc] | 535 | } |
---|
| 536 | } |
---|
| 537 | |
---|
| 538 | // Ungetestet |
---|
| 539 | static void bimRowContent(bigintmat *bimat, int rowpos, int colpos) |
---|
| 540 | { |
---|
[fe02b1] | 541 | const coeffs basecoeffs = bimat->basecoeffs(); |
---|
[75f10d] | 542 | |
---|
[9127cc] | 543 | number tgcd, m; |
---|
| 544 | int i=bimat->cols(); |
---|
| 545 | |
---|
| 546 | loop |
---|
| 547 | { |
---|
[fe02b1] | 548 | tgcd = n_Copy(BIMATELEM(*bimat,rowpos,i--), basecoeffs); |
---|
| 549 | if (! n_IsZero(tgcd, basecoeffs)) break; |
---|
[9127cc] | 550 | if (i<colpos) return; |
---|
| 551 | } |
---|
[fe02b1] | 552 | if ((! n_GreaterZero(tgcd, basecoeffs)) && (! n_IsZero(tgcd, basecoeffs))) tgcd = n_Neg(tgcd, basecoeffs); |
---|
| 553 | if ( n_IsOne(tgcd,basecoeffs)) return; |
---|
[9127cc] | 554 | loop |
---|
| 555 | { |
---|
[fe02b1] | 556 | m = n_Copy(BIMATELEM(*bimat,rowpos,i--), basecoeffs); |
---|
| 557 | if (! n_IsZero(m,basecoeffs)) |
---|
[9127cc] | 558 | { |
---|
[fe02b1] | 559 | number tp1 = n_Gcd(tgcd, m, basecoeffs); |
---|
| 560 | n_Delete(&tgcd, basecoeffs); |
---|
[9127cc] | 561 | tgcd = tp1; |
---|
| 562 | } |
---|
[fe02b1] | 563 | if ( n_IsOne(tgcd,basecoeffs)) return; |
---|
[9127cc] | 564 | if (i<colpos) break; |
---|
| 565 | } |
---|
| 566 | for (i=bimat->cols();i>=colpos;i--) |
---|
| 567 | { |
---|
[fe02b1] | 568 | number tp2 = n_Div(BIMATELEM(*bimat,rowpos,i), tgcd,basecoeffs); |
---|
| 569 | n_Delete(&BIMATELEM(*bimat,rowpos,i), basecoeffs); |
---|
[9127cc] | 570 | BIMATELEM(*bimat,rowpos,i) = tp2; |
---|
| 571 | } |
---|
[fe02b1] | 572 | n_Delete(&tgcd, basecoeffs); |
---|
| 573 | n_Delete(&m, basecoeffs); |
---|
[9127cc] | 574 | } |
---|
| 575 | |
---|
| 576 | static void bimReduce(bigintmat *bimat, int rpiv, int colpos, |
---|
[fe02b1] | 577 | int ready, int all) |
---|
[9127cc] | 578 | { |
---|
[fe02b1] | 579 | const coeffs basecoeffs = bimat->basecoeffs(); |
---|
| 580 | |
---|
[9127cc] | 581 | number tgcd, ce, m1, m2; |
---|
| 582 | int j, i; |
---|
| 583 | number piv = BIMATELEM(*bimat,rpiv,colpos); |
---|
| 584 | |
---|
| 585 | for (j=all;j>ready;j--) |
---|
| 586 | { |
---|
[fe02b1] | 587 | ce = n_Copy(BIMATELEM(*bimat,j,colpos),basecoeffs); |
---|
| 588 | if (! n_IsZero(ce, basecoeffs)) |
---|
[9127cc] | 589 | { |
---|
[fe02b1] | 590 | n_Delete(&BIMATELEM(*bimat,j,colpos), basecoeffs); |
---|
| 591 | BIMATELEM(*bimat,j,colpos) = n_Init(0, basecoeffs); |
---|
| 592 | m1 = n_Copy(piv,basecoeffs); |
---|
| 593 | m2 = n_Copy(ce,basecoeffs); |
---|
| 594 | tgcd = n_Gcd(m1, m2, basecoeffs); |
---|
| 595 | if (! n_IsOne(tgcd,basecoeffs)) |
---|
[9127cc] | 596 | { |
---|
[fe02b1] | 597 | number tp1 = n_Div(m1, tgcd,basecoeffs); |
---|
| 598 | number tp2 = n_Div(m2, tgcd,basecoeffs); |
---|
| 599 | n_Delete(&m1, basecoeffs); |
---|
| 600 | n_Delete(&m2, basecoeffs); |
---|
[9127cc] | 601 | m1 = tp1; |
---|
| 602 | m2 = tp2; |
---|
| 603 | } |
---|
| 604 | for (i=bimat->cols();i>colpos;i--) |
---|
| 605 | { |
---|
[fe02b1] | 606 | n_Delete(&BIMATELEM(*bimat,j,i), basecoeffs); |
---|
| 607 | number tp1 = n_Mult(BIMATELEM(*bimat,j,i), m1,basecoeffs); |
---|
| 608 | number tp2 = n_Mult(BIMATELEM(*bimat,rpiv,i), m2,basecoeffs); |
---|
| 609 | BIMATELEM(*bimat,j,i) = n_Sub(tp1, tp2,basecoeffs); |
---|
| 610 | n_Delete(&tp1, basecoeffs); |
---|
| 611 | n_Delete(&tp2, basecoeffs); |
---|
[9127cc] | 612 | } |
---|
| 613 | bimRowContent(bimat, j, colpos+1); |
---|
[fe02b1] | 614 | n_Delete(&m1, basecoeffs); |
---|
| 615 | n_Delete(&m2, basecoeffs); |
---|
[9127cc] | 616 | } |
---|
[fe02b1] | 617 | n_Delete(&ce, basecoeffs); |
---|
[9127cc] | 618 | } |
---|
| 619 | } |
---|
| 620 | |
---|
| 621 | |
---|
| 622 | |
---|
| 623 | |
---|
| 624 | |
---|