1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* |
---|
5 | * ABSTRACT: gauss implementation for F4 |
---|
6 | */ |
---|
7 | #include "config.h" |
---|
8 | #include <kernel/mod2.h> |
---|
9 | #include <misc/options.h> |
---|
10 | #include <kernel/tgbgauss.h> |
---|
11 | #include <omalloc/omalloc.h> |
---|
12 | #include <stdlib.h> |
---|
13 | #include <kernel/kutil.h> |
---|
14 | #include <kernel/febase.h> |
---|
15 | #include <kernel/polys.h> |
---|
16 | static const int bundle_size=100; |
---|
17 | |
---|
18 | mac_poly mac_p_add_ff_qq(mac_poly a, number f,mac_poly b) |
---|
19 | { |
---|
20 | mac_poly erg; |
---|
21 | mac_poly* set_this; |
---|
22 | set_this=&erg; |
---|
23 | while((a!=NULL) &&(b!=NULL)) |
---|
24 | { |
---|
25 | if (a->exp<b->exp) |
---|
26 | { |
---|
27 | (*set_this)=a; |
---|
28 | a=a->next; |
---|
29 | set_this= &((*set_this)->next); |
---|
30 | } |
---|
31 | else |
---|
32 | { |
---|
33 | if (a->exp>b->exp) |
---|
34 | { |
---|
35 | mac_poly in =new mac_poly_r(); |
---|
36 | in->exp=b->exp; |
---|
37 | in->coef=nMult(b->coef,f); |
---|
38 | (*set_this)=in; |
---|
39 | b=b->next; |
---|
40 | set_this= &((*set_this)->next); |
---|
41 | } |
---|
42 | else |
---|
43 | { |
---|
44 | //a->exp==b->ecp |
---|
45 | number n=nMult(b->coef,f); |
---|
46 | number n2=nAdd(a->coef,n); |
---|
47 | nDelete(&n); |
---|
48 | nDelete(&(a->coef)); |
---|
49 | if (nIsZero(n2)) |
---|
50 | { |
---|
51 | nDelete(&n2); |
---|
52 | mac_poly ao=a; |
---|
53 | a=a->next; |
---|
54 | delete ao; |
---|
55 | b=b->next; |
---|
56 | } |
---|
57 | else |
---|
58 | { |
---|
59 | a->coef=n2; |
---|
60 | b=b->next; |
---|
61 | (*set_this)=a; |
---|
62 | a=a->next; |
---|
63 | set_this= &((*set_this)->next); |
---|
64 | } |
---|
65 | } |
---|
66 | } |
---|
67 | } |
---|
68 | if((a==NULL)&&(b==NULL)) |
---|
69 | { |
---|
70 | (*set_this)=NULL; |
---|
71 | return erg; |
---|
72 | } |
---|
73 | if (b==NULL) |
---|
74 | { |
---|
75 | (*set_this=a); |
---|
76 | return erg; |
---|
77 | } |
---|
78 | |
---|
79 | //a==NULL |
---|
80 | while(b!=NULL) |
---|
81 | { |
---|
82 | mac_poly mp= new mac_poly_r(); |
---|
83 | mp->exp=b->exp; |
---|
84 | mp->coef=nMult(f,b->coef); |
---|
85 | (*set_this)=mp; |
---|
86 | set_this=&(mp->next); |
---|
87 | b=b->next; |
---|
88 | } |
---|
89 | (*set_this)=NULL; |
---|
90 | return erg; |
---|
91 | } |
---|
92 | |
---|
93 | void mac_mult_cons(mac_poly p,number c) |
---|
94 | { |
---|
95 | while(p) |
---|
96 | { |
---|
97 | number m=nMult(p->coef,c); |
---|
98 | nDelete(&(p->coef)); |
---|
99 | p->coef=m; |
---|
100 | p=p->next; |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | int mac_length(mac_poly p) |
---|
105 | { |
---|
106 | int l=0; |
---|
107 | while(p){ |
---|
108 | l++; |
---|
109 | p=p->next; |
---|
110 | } |
---|
111 | return l; |
---|
112 | } |
---|
113 | |
---|
114 | //contrary to delete on the mac_poly_r, the coefficients are also destroyed here |
---|
115 | void mac_destroy(mac_poly p) |
---|
116 | { |
---|
117 | mac_poly iter=p; |
---|
118 | while(iter) |
---|
119 | { |
---|
120 | mac_poly next=iter->next; |
---|
121 | nDelete(&iter->coef); |
---|
122 | delete iter; |
---|
123 | iter=next; |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | void simple_gauss(tgb_sparse_matrix* mat, slimgb_alg* /*c*/) |
---|
128 | { |
---|
129 | int col, row; |
---|
130 | int* row_cache=(int*) omAlloc(mat->get_rows()*sizeof(int)); |
---|
131 | col=0; |
---|
132 | row=0; |
---|
133 | int i; |
---|
134 | int pn=mat->get_rows(); |
---|
135 | int matcol=mat->get_columns(); |
---|
136 | int* area=(int*) omAlloc(sizeof(int)*((matcol-1)/bundle_size+1)); |
---|
137 | const int max_area_index=(matcol-1)/bundle_size; |
---|
138 | //rows are divided in areas |
---|
139 | //if row begins with columns col, it is located in [area[col/bundle_size],area[col/bundle_size+1]-1] |
---|
140 | assume(pn>0); |
---|
141 | //first clear zeroes |
---|
142 | for(i=0;i<pn;i++) |
---|
143 | { |
---|
144 | if(mat->zero_row(i)) |
---|
145 | { |
---|
146 | mat->perm_rows(i,pn-1); |
---|
147 | pn--; |
---|
148 | if(i!=pn){i--;} |
---|
149 | } |
---|
150 | |
---|
151 | } |
---|
152 | mat->sort_rows(); |
---|
153 | for(i=0;i<pn;i++) |
---|
154 | { |
---|
155 | row_cache[i]=mat->min_col_not_zero_in_row(i); |
---|
156 | // Print("row_cache:%d\n",row_cache[i]); |
---|
157 | } |
---|
158 | int last_area=-1; |
---|
159 | for(i=0;i<pn;i++) |
---|
160 | { |
---|
161 | int this_area=row_cache[i]/bundle_size; |
---|
162 | assume(this_area>=last_area); |
---|
163 | if(this_area>last_area) |
---|
164 | { |
---|
165 | int j; |
---|
166 | for(j=last_area+1;j<=this_area;j++) |
---|
167 | area[j]=i; |
---|
168 | last_area=this_area; |
---|
169 | } |
---|
170 | } |
---|
171 | for(i=last_area+1;i<=max_area_index;i++) |
---|
172 | { |
---|
173 | area[i]=pn; |
---|
174 | } |
---|
175 | while(row<pn-1) |
---|
176 | { |
---|
177 | //row is the row where pivot should be |
---|
178 | // row== pn-1 means we have only to act on one row so no red nec. |
---|
179 | //we assume further all rows till the pn-1 row are non-zero |
---|
180 | |
---|
181 | //select column |
---|
182 | |
---|
183 | //col=mat->min_col_not_zero_in_row(row); |
---|
184 | int max_in_area; |
---|
185 | { |
---|
186 | int tai=row_cache[row]/bundle_size; |
---|
187 | assume(tai<=max_area_index); |
---|
188 | if(tai==max_area_index) |
---|
189 | max_in_area=pn-1; |
---|
190 | else |
---|
191 | max_in_area=area[tai+1]-1; |
---|
192 | } |
---|
193 | assume(row_cache[row]==mat->min_col_not_zero_in_row(row)); |
---|
194 | col=row_cache[row]; |
---|
195 | |
---|
196 | assume(col!=matcol); |
---|
197 | int found_in_row; |
---|
198 | |
---|
199 | found_in_row=row; |
---|
200 | BOOLEAN must_reduce=FALSE; |
---|
201 | assume(pn<=mat->get_rows()); |
---|
202 | for(i=row+1;i<=max_in_area;i++) |
---|
203 | { |
---|
204 | int first;//=mat->min_col_not_zero_in_row(i); |
---|
205 | assume(row_cache[i]==mat->min_col_not_zero_in_row(i)); |
---|
206 | first=row_cache[i]; |
---|
207 | assume(first!=matcol); |
---|
208 | if(first<col) |
---|
209 | { |
---|
210 | col=first; |
---|
211 | found_in_row=i; |
---|
212 | must_reduce=FALSE; |
---|
213 | } |
---|
214 | else |
---|
215 | { |
---|
216 | if(first==col) |
---|
217 | must_reduce=TRUE; |
---|
218 | } |
---|
219 | } |
---|
220 | //select pivot |
---|
221 | int act_l=nSize(mat->get(found_in_row,col))*mat->non_zero_entries(found_in_row); |
---|
222 | if(must_reduce) |
---|
223 | { |
---|
224 | for(i=found_in_row+1;i<=max_in_area;i++) |
---|
225 | { |
---|
226 | assume(mat->min_col_not_zero_in_row(i)>=col); |
---|
227 | assume(row_cache[i]==mat->min_col_not_zero_in_row(i)); |
---|
228 | #ifndef NDEBUG |
---|
229 | int first=row_cache[i]; |
---|
230 | assume(first!=matcol); |
---|
231 | #endif |
---|
232 | // if((!(mat->is_zero_entry(i,col)))&&(mat->non_zero_entries(i)<act_l)) |
---|
233 | int nz; |
---|
234 | if((row_cache[i]==col)&&((nz=nSize(mat->get(i,col))*mat->non_zero_entries(i))<act_l)) |
---|
235 | { |
---|
236 | found_in_row=i; |
---|
237 | act_l=nz; |
---|
238 | } |
---|
239 | |
---|
240 | } |
---|
241 | } |
---|
242 | mat->perm_rows(row,found_in_row); |
---|
243 | int h=row_cache[row]; |
---|
244 | row_cache[row]=row_cache[found_in_row]; |
---|
245 | row_cache[found_in_row]=h; |
---|
246 | |
---|
247 | if(!must_reduce) |
---|
248 | { |
---|
249 | row++; |
---|
250 | continue; |
---|
251 | } |
---|
252 | //reduction |
---|
253 | //must extract content and normalize here |
---|
254 | mat->row_content(row); |
---|
255 | mat->row_normalize(row); |
---|
256 | |
---|
257 | //for(i=row+1;i<pn;i++){ |
---|
258 | for(i=max_in_area;i>row;i--) |
---|
259 | { |
---|
260 | int col_area_index=col/bundle_size; |
---|
261 | assume(col_area_index<=max_area_index); |
---|
262 | assume(mat->min_col_not_zero_in_row(i)>=col); |
---|
263 | assume(row_cache[i]==mat->min_col_not_zero_in_row(i)); |
---|
264 | #ifndef NDEBUG |
---|
265 | int first=row_cache[i]; |
---|
266 | assume(first!=matcol); |
---|
267 | #endif |
---|
268 | if(row_cache[i]==col) |
---|
269 | { |
---|
270 | |
---|
271 | number c1=mat->get(i,col); |
---|
272 | number c2=mat->get(row,col); |
---|
273 | number n1=c1; |
---|
274 | number n2=c2; |
---|
275 | |
---|
276 | ksCheckCoeff(&n1,&n2,currRing->cf); |
---|
277 | //nDelete(&c1); |
---|
278 | n1=nNeg(n1); |
---|
279 | mat->mult_row(i,n2); |
---|
280 | mat->add_lambda_times_row(i,row,n1); |
---|
281 | nDelete(&n1); |
---|
282 | nDelete(&n2); |
---|
283 | assume(mat->is_zero_entry(i,col)); |
---|
284 | row_cache[i]=mat->min_col_not_zero_in_row(i); |
---|
285 | assume(mat->min_col_not_zero_in_row(i)>col); |
---|
286 | if(row_cache[i]==matcol) |
---|
287 | { |
---|
288 | int index; |
---|
289 | index=i; |
---|
290 | int last_in_area; |
---|
291 | int this_cai=col_area_index; |
---|
292 | while(this_cai<max_area_index) |
---|
293 | { |
---|
294 | last_in_area=area[this_cai+1]-1; |
---|
295 | int h_c=row_cache[last_in_area]; |
---|
296 | row_cache[last_in_area]=row_cache[index]; |
---|
297 | row_cache[index]=h_c; |
---|
298 | mat->perm_rows(index,last_in_area); |
---|
299 | index=last_in_area; |
---|
300 | this_cai++; |
---|
301 | area[this_cai]--; |
---|
302 | } |
---|
303 | mat->perm_rows(index,pn-1); |
---|
304 | row_cache[index]=row_cache[pn-1]; |
---|
305 | row_cache[pn-1]=matcol; |
---|
306 | pn--; |
---|
307 | } |
---|
308 | else |
---|
309 | { |
---|
310 | int index; |
---|
311 | index=i; |
---|
312 | int last_in_area; |
---|
313 | int this_cai=col_area_index; |
---|
314 | int final_cai=row_cache[index]/bundle_size; |
---|
315 | assume(final_cai<=max_area_index); |
---|
316 | while(this_cai<final_cai) |
---|
317 | { |
---|
318 | last_in_area=area[this_cai+1]-1; |
---|
319 | int h_c=row_cache[last_in_area]; |
---|
320 | row_cache[last_in_area]=row_cache[index]; |
---|
321 | row_cache[index]=h_c; |
---|
322 | mat->perm_rows(index,last_in_area); |
---|
323 | index=last_in_area; |
---|
324 | this_cai++; |
---|
325 | area[this_cai]--; |
---|
326 | } |
---|
327 | } |
---|
328 | } |
---|
329 | else |
---|
330 | assume(mat->min_col_not_zero_in_row(i)>col); |
---|
331 | } |
---|
332 | // for(i=row+1;i<pn;i++) |
---|
333 | // { |
---|
334 | // assume(mat->min_col_not_zero_in_row(i)==row_cache[i]); |
---|
335 | // // if(mat->zero_row(i)) |
---|
336 | // assume(matcol==mat->get_columns()); |
---|
337 | // if(row_cache[i]==matcol) |
---|
338 | // { |
---|
339 | // assume(mat->zero_row(i)); |
---|
340 | // mat->perm_rows(i,pn-1); |
---|
341 | // row_cache[i]=row_cache[pn-1]; |
---|
342 | // row_cache[pn-1]=matcol; |
---|
343 | // pn--; |
---|
344 | // if(i!=pn){i--;} |
---|
345 | // } |
---|
346 | // } |
---|
347 | #ifdef TGB_DEBUG |
---|
348 | { |
---|
349 | int last=-1; |
---|
350 | for(i=0;i<pn;i++) |
---|
351 | { |
---|
352 | int act=mat->min_col_not_zero_in_row(i); |
---|
353 | assume(act>last); |
---|
354 | } |
---|
355 | for(i=pn;i<mat->get_rows();i++) |
---|
356 | { |
---|
357 | assume(mat->zero_row(i)); |
---|
358 | } |
---|
359 | } |
---|
360 | #endif |
---|
361 | row++; |
---|
362 | } |
---|
363 | omFree(area); |
---|
364 | omFree(row_cache); |
---|
365 | } |
---|
366 | |
---|
367 | void simple_gauss2(tgb_matrix* mat) |
---|
368 | { |
---|
369 | int col, row; |
---|
370 | col=0; |
---|
371 | row=0; |
---|
372 | int i; |
---|
373 | int pn=mat->get_rows(); |
---|
374 | assume(pn>0); |
---|
375 | //first clear zeroes |
---|
376 | // for(i=0;i<pn;i++) |
---|
377 | // { |
---|
378 | // if(mat->zero_row(i)) |
---|
379 | // { |
---|
380 | // mat->perm_rows(i,pn-1); |
---|
381 | // pn--; |
---|
382 | // if(i!=pn){i--;} |
---|
383 | // } |
---|
384 | // } |
---|
385 | while((row<pn-1)&&(col<mat->get_columns())){ |
---|
386 | //row is the row where pivot should be |
---|
387 | // row== pn-1 means we have only to act on one row so no red nec. |
---|
388 | //we assume further all rows till the pn-1 row are non-zero |
---|
389 | |
---|
390 | //select column |
---|
391 | |
---|
392 | // col=mat->min_col_not_zero_in_row(row); |
---|
393 | assume(col!=mat->get_columns()); |
---|
394 | int found_in_row=-1; |
---|
395 | |
---|
396 | // found_in_row=row; |
---|
397 | assume(pn<=mat->get_rows()); |
---|
398 | for(i=row;i<pn;i++) |
---|
399 | { |
---|
400 | // int first=mat->min_col_not_zero_in_row(i); |
---|
401 | // if(first<col) |
---|
402 | if(!(mat->is_zero_entry(i,col))) |
---|
403 | { |
---|
404 | found_in_row=i; |
---|
405 | break; |
---|
406 | } |
---|
407 | } |
---|
408 | if(found_in_row!=-1) |
---|
409 | { |
---|
410 | //select pivot |
---|
411 | int act_l=mat->non_zero_entries(found_in_row); |
---|
412 | for(i=i+1;i<pn;i++) |
---|
413 | { |
---|
414 | int vgl; |
---|
415 | assume(mat->min_col_not_zero_in_row(i)>=col); |
---|
416 | if((!(mat->is_zero_entry(i,col))) |
---|
417 | &&((vgl=mat->non_zero_entries(i))<act_l)) |
---|
418 | { |
---|
419 | found_in_row=i; |
---|
420 | act_l=vgl; |
---|
421 | } |
---|
422 | |
---|
423 | } |
---|
424 | mat->perm_rows(row,found_in_row); |
---|
425 | |
---|
426 | //reduction |
---|
427 | for(i=row+1;i<pn;i++){ |
---|
428 | assume(mat->min_col_not_zero_in_row(i)>=col); |
---|
429 | if(!(mat->is_zero_entry(i,col))) |
---|
430 | { |
---|
431 | number c1=nNeg(nCopy(mat->get(i,col))); |
---|
432 | number c2=mat->get(row,col); |
---|
433 | number n1=c1; |
---|
434 | number n2=c2; |
---|
435 | |
---|
436 | ksCheckCoeff(&n1,&n2,currRing->cf); |
---|
437 | nDelete(&c1); |
---|
438 | mat->mult_row(i,n2); |
---|
439 | mat->add_lambda_times_row(i,row,n1); |
---|
440 | assume(mat->is_zero_entry(i,col)); |
---|
441 | } |
---|
442 | assume(mat->min_col_not_zero_in_row(i)>col); |
---|
443 | } |
---|
444 | row++; |
---|
445 | } |
---|
446 | col++; |
---|
447 | // for(i=row+1;i<pn;i++) |
---|
448 | // { |
---|
449 | // if(mat->zero_row(i)) |
---|
450 | // { |
---|
451 | // mat->perm_rows(i,pn-1); |
---|
452 | // pn--; |
---|
453 | // if(i!=pn){i--;} |
---|
454 | // } |
---|
455 | // } |
---|
456 | } |
---|
457 | } |
---|
458 | |
---|
459 | |
---|
460 | tgb_matrix::tgb_matrix(int i, int j) |
---|
461 | { |
---|
462 | n=(number**) omAlloc(i*sizeof (number*));; |
---|
463 | int z; |
---|
464 | int z2; |
---|
465 | for(z=0;z<i;z++) |
---|
466 | { |
---|
467 | n[z]=(number*)omAlloc(j*sizeof(number)); |
---|
468 | for(z2=0;z2<j;z2++) |
---|
469 | { |
---|
470 | n[z][z2]=nInit(0); |
---|
471 | } |
---|
472 | } |
---|
473 | columns=j; |
---|
474 | rows=i; |
---|
475 | free_numbers=FALSE; |
---|
476 | } |
---|
477 | |
---|
478 | tgb_matrix::~tgb_matrix() |
---|
479 | { |
---|
480 | int z; |
---|
481 | for(z=0;z<rows;z++) |
---|
482 | { |
---|
483 | if(n[z]) |
---|
484 | { |
---|
485 | if(free_numbers) |
---|
486 | { |
---|
487 | int z2; |
---|
488 | for(z2=0;z2<columns;z2++) |
---|
489 | { |
---|
490 | nDelete(&(n[z][z2])); |
---|
491 | } |
---|
492 | } |
---|
493 | omFree(n[z]); |
---|
494 | } |
---|
495 | } |
---|
496 | omfree(n); |
---|
497 | } |
---|
498 | |
---|
499 | void tgb_matrix::print() |
---|
500 | { |
---|
501 | int i; |
---|
502 | int j; |
---|
503 | PrintLn(); |
---|
504 | for(i=0;i<rows;i++) |
---|
505 | { |
---|
506 | PrintS("("); |
---|
507 | for(j=0;j<columns;j++) |
---|
508 | { |
---|
509 | StringSetS(""); |
---|
510 | n_Write(n[i][j],currRing); |
---|
511 | char *s=StringEndS(); |
---|
512 | PrintS(s); |
---|
513 | omFree(s); |
---|
514 | PrintS("\t"); |
---|
515 | } |
---|
516 | PrintS(")\n"); |
---|
517 | } |
---|
518 | } |
---|
519 | |
---|
520 | //transfers ownership of n to the matrix |
---|
521 | void tgb_matrix::set(int i, int j, number nn) |
---|
522 | { |
---|
523 | assume(i<rows); |
---|
524 | assume(j<columns); |
---|
525 | n[i][j]=nn; |
---|
526 | } |
---|
527 | |
---|
528 | int tgb_matrix::get_rows() |
---|
529 | { |
---|
530 | return rows; |
---|
531 | } |
---|
532 | |
---|
533 | int tgb_matrix::get_columns() |
---|
534 | { |
---|
535 | return columns; |
---|
536 | } |
---|
537 | |
---|
538 | number tgb_matrix::get(int i, int j) |
---|
539 | { |
---|
540 | assume(i<rows); |
---|
541 | assume(j<columns); |
---|
542 | return n[i][j]; |
---|
543 | } |
---|
544 | |
---|
545 | BOOLEAN tgb_matrix::is_zero_entry(int i, int j) |
---|
546 | { |
---|
547 | return (nIsZero(n[i][j])); |
---|
548 | } |
---|
549 | |
---|
550 | void tgb_matrix::perm_rows(int i, int j) |
---|
551 | { |
---|
552 | number* h; |
---|
553 | h=n[i]; |
---|
554 | n[i]=n[j]; |
---|
555 | n[j]=h; |
---|
556 | } |
---|
557 | |
---|
558 | int tgb_matrix::min_col_not_zero_in_row(int row) |
---|
559 | { |
---|
560 | int i; |
---|
561 | for(i=0;i<columns;i++) |
---|
562 | { |
---|
563 | if(!(nIsZero(n[row][i]))) |
---|
564 | return i; |
---|
565 | } |
---|
566 | return columns;//error code |
---|
567 | } |
---|
568 | |
---|
569 | int tgb_matrix::next_col_not_zero(int row,int pre) |
---|
570 | { |
---|
571 | int i; |
---|
572 | for(i=pre+1;i<columns;i++) |
---|
573 | { |
---|
574 | if(!(nIsZero(n[row][i]))) |
---|
575 | return i; |
---|
576 | } |
---|
577 | return columns;//error code |
---|
578 | } |
---|
579 | |
---|
580 | BOOLEAN tgb_matrix::zero_row(int row) |
---|
581 | { |
---|
582 | int i; |
---|
583 | for(i=0;i<columns;i++) |
---|
584 | { |
---|
585 | if(!(nIsZero(n[row][i]))) |
---|
586 | return FALSE; |
---|
587 | } |
---|
588 | return TRUE; |
---|
589 | } |
---|
590 | |
---|
591 | int tgb_matrix::non_zero_entries(int row) |
---|
592 | { |
---|
593 | int i; |
---|
594 | int z=0; |
---|
595 | for(i=0;i<columns;i++) |
---|
596 | { |
---|
597 | if(!(nIsZero(n[row][i]))) |
---|
598 | z++; |
---|
599 | } |
---|
600 | return z; |
---|
601 | } |
---|
602 | |
---|
603 | //row add_to=row add_to +row summand*factor |
---|
604 | void tgb_matrix::add_lambda_times_row(int add_to,int summand,number factor) |
---|
605 | { |
---|
606 | int i; |
---|
607 | for(i=0;i<columns;i++) |
---|
608 | { |
---|
609 | if(!(nIsZero(n[summand][i]))) |
---|
610 | { |
---|
611 | number n1=n[add_to][i]; |
---|
612 | number n2=nMult(factor,n[summand][i]); |
---|
613 | n[add_to][i]=nAdd(n1,n2); |
---|
614 | nDelete(&n1); |
---|
615 | nDelete(&n2); |
---|
616 | } |
---|
617 | } |
---|
618 | } |
---|
619 | |
---|
620 | void tgb_matrix::mult_row(int row,number factor) |
---|
621 | { |
---|
622 | if (nIsOne(factor)) |
---|
623 | return; |
---|
624 | int i; |
---|
625 | for(i=0;i<columns;i++) |
---|
626 | { |
---|
627 | if(!(nIsZero(n[row][i]))) |
---|
628 | { |
---|
629 | number n1=n[row][i]; |
---|
630 | n[row][i]=nMult(n1,factor); |
---|
631 | nDelete(&n1); |
---|
632 | } |
---|
633 | } |
---|
634 | } |
---|
635 | |
---|
636 | void tgb_matrix::free_row(int row, BOOLEAN free_non_zeros) |
---|
637 | { |
---|
638 | int i; |
---|
639 | for(i=0;i<columns;i++) |
---|
640 | if((free_non_zeros)||(!(nIsZero(n[row][i])))) |
---|
641 | nDelete(&(n[row][i])); |
---|
642 | omFree(n[row]); |
---|
643 | n[row]=NULL; |
---|
644 | } |
---|
645 | |
---|
646 | tgb_sparse_matrix::tgb_sparse_matrix(int i, int j, ring rarg) |
---|
647 | { |
---|
648 | mp=(mac_poly*) omAlloc(i*sizeof (mac_poly));; |
---|
649 | int z; |
---|
650 | for(z=0;z<i;z++) |
---|
651 | { |
---|
652 | mp[z]=NULL; |
---|
653 | } |
---|
654 | columns=j; |
---|
655 | rows=i; |
---|
656 | free_numbers=FALSE; |
---|
657 | r=rarg; |
---|
658 | } |
---|
659 | |
---|
660 | tgb_sparse_matrix::~tgb_sparse_matrix() |
---|
661 | { |
---|
662 | int z; |
---|
663 | for(z=0;z<rows;z++) |
---|
664 | { |
---|
665 | if(mp[z]!=NULL) |
---|
666 | { |
---|
667 | if(free_numbers) |
---|
668 | { |
---|
669 | mac_destroy(mp[z]); |
---|
670 | } |
---|
671 | else { |
---|
672 | while(mp[z]!=NULL) |
---|
673 | { |
---|
674 | mac_poly next=mp[z]->next; |
---|
675 | delete mp[z]; |
---|
676 | mp[z]=next; |
---|
677 | } |
---|
678 | } |
---|
679 | } |
---|
680 | } |
---|
681 | omfree(mp); |
---|
682 | } |
---|
683 | |
---|
684 | static int row_cmp_gen(const void* a, const void* b) |
---|
685 | { |
---|
686 | const mac_poly ap= *((mac_poly*) a); |
---|
687 | const mac_poly bp=*((mac_poly*) b); |
---|
688 | if (ap==NULL) return 1; |
---|
689 | if (bp==NULL) return -1; |
---|
690 | if (ap->exp<bp->exp) return -1; |
---|
691 | return 1; |
---|
692 | } |
---|
693 | |
---|
694 | void tgb_sparse_matrix::sort_rows() |
---|
695 | { |
---|
696 | qsort(mp,rows,sizeof(mac_poly),row_cmp_gen); |
---|
697 | } |
---|
698 | |
---|
699 | void tgb_sparse_matrix::print() |
---|
700 | { |
---|
701 | int i; |
---|
702 | int j; |
---|
703 | PrintLn(); |
---|
704 | for(i=0;i<rows;i++) |
---|
705 | { |
---|
706 | PrintS("("); |
---|
707 | for(j=0;j<columns;j++) |
---|
708 | { |
---|
709 | StringSetS(""); |
---|
710 | number n=get(i,j); |
---|
711 | n_Write(n,currRing); |
---|
712 | char *s=StringEndS(); |
---|
713 | PrintS(s); |
---|
714 | omFree(s); |
---|
715 | PrintS("\t"); |
---|
716 | } |
---|
717 | PrintS(")\n"); |
---|
718 | } |
---|
719 | } |
---|
720 | |
---|
721 | //transfers ownership of n to the matrix |
---|
722 | void tgb_sparse_matrix::set(int i, int j, number n) |
---|
723 | { |
---|
724 | assume(i<rows); |
---|
725 | assume(j<columns); |
---|
726 | mac_poly* set_this=&mp[i]; |
---|
727 | // while(((*set_this)!=NULL)&&((*set_this)>exp<j)) |
---|
728 | while(((*set_this)!=NULL) && ((*set_this)->exp<j)) |
---|
729 | set_this=&((*set_this)->next); |
---|
730 | |
---|
731 | if (((*set_this)==NULL)||((*set_this)->exp>j)) |
---|
732 | { |
---|
733 | if (nIsZero(n)) return; |
---|
734 | mac_poly old=(*set_this); |
---|
735 | (*set_this)=new mac_poly_r(); |
---|
736 | (*set_this)->exp=j; |
---|
737 | (*set_this)->coef=n; |
---|
738 | (*set_this)->next=old; |
---|
739 | return; |
---|
740 | } |
---|
741 | assume((*set_this)->exp==j); |
---|
742 | if(!nIsZero(n)) |
---|
743 | { |
---|
744 | nDelete(&(*set_this)->coef); |
---|
745 | (*set_this)->coef=n; |
---|
746 | } |
---|
747 | else |
---|
748 | { |
---|
749 | nDelete(&(*set_this)->coef); |
---|
750 | mac_poly dt=(*set_this); |
---|
751 | (*set_this)=dt->next; |
---|
752 | delete dt; |
---|
753 | } |
---|
754 | return; |
---|
755 | } |
---|
756 | |
---|
757 | int tgb_sparse_matrix::get_rows() |
---|
758 | { |
---|
759 | return rows; |
---|
760 | } |
---|
761 | |
---|
762 | int tgb_sparse_matrix::get_columns() |
---|
763 | { |
---|
764 | return columns; |
---|
765 | } |
---|
766 | |
---|
767 | number tgb_sparse_matrix::get(int i, int j) |
---|
768 | { |
---|
769 | assume(i<rows); |
---|
770 | assume(j<columns); |
---|
771 | mac_poly rr=mp[i]; |
---|
772 | while((rr!=NULL)&&(rr->exp<j)) |
---|
773 | rr=rr->next; |
---|
774 | if ((rr==NULL)||(rr->exp>j)) |
---|
775 | { |
---|
776 | number n=nInit(0); |
---|
777 | return n; |
---|
778 | } |
---|
779 | assume(rr->exp==j); |
---|
780 | return rr->coef; |
---|
781 | } |
---|
782 | |
---|
783 | BOOLEAN tgb_sparse_matrix::is_zero_entry(int i, int j) |
---|
784 | { |
---|
785 | assume(i<rows); |
---|
786 | assume(j<columns); |
---|
787 | mac_poly rr=mp[i]; |
---|
788 | while((rr!=NULL)&&(rr->exp<j)) |
---|
789 | rr=rr->next; |
---|
790 | if ((rr==NULL)||(rr->exp>j)) |
---|
791 | { |
---|
792 | return TRUE; |
---|
793 | } |
---|
794 | assume(!nIsZero(rr->coef)); |
---|
795 | assume(rr->exp==j); |
---|
796 | return FALSE; |
---|
797 | } |
---|
798 | |
---|
799 | int tgb_sparse_matrix::min_col_not_zero_in_row(int row) |
---|
800 | { |
---|
801 | if(mp[row]!=NULL) |
---|
802 | { |
---|
803 | assume(!nIsZero(mp[row]->coef)); |
---|
804 | return mp[row]->exp; |
---|
805 | } |
---|
806 | else |
---|
807 | return columns;//error code |
---|
808 | } |
---|
809 | |
---|
810 | int tgb_sparse_matrix::next_col_not_zero(int row,int pre) |
---|
811 | { |
---|
812 | mac_poly rr=mp[row]; |
---|
813 | while((rr!=NULL)&&(rr->exp<=pre)) |
---|
814 | rr=rr->next; |
---|
815 | if(rr!=NULL) |
---|
816 | { |
---|
817 | assume(!nIsZero(rr->coef)); |
---|
818 | return rr->exp; |
---|
819 | } |
---|
820 | return columns;//error code |
---|
821 | } |
---|
822 | |
---|
823 | BOOLEAN tgb_sparse_matrix::zero_row(int row) |
---|
824 | { |
---|
825 | assume((mp[row]==NULL)||(!nIsZero(mp[row]->coef))); |
---|
826 | if (mp[row]==NULL) |
---|
827 | return TRUE; |
---|
828 | else |
---|
829 | return FALSE; |
---|
830 | } |
---|
831 | |
---|
832 | void tgb_sparse_matrix::row_normalize(int row) |
---|
833 | { |
---|
834 | if (!rField_has_simple_inverse(r)) /* Z/p, GF(p,n), R, long R/C */ |
---|
835 | { |
---|
836 | mac_poly m=mp[row]; |
---|
837 | while (m!=NULL) |
---|
838 | { |
---|
839 | #ifndef NDEBUG |
---|
840 | if (currRing==r) {nTest(m->coef);} |
---|
841 | #endif |
---|
842 | n_Normalize(m->coef,r); |
---|
843 | m=m->next; |
---|
844 | } |
---|
845 | } |
---|
846 | } |
---|
847 | |
---|
848 | void tgb_sparse_matrix::row_content(int row) |
---|
849 | { |
---|
850 | mac_poly ph=mp[row]; |
---|
851 | number h,d; |
---|
852 | mac_poly p; |
---|
853 | |
---|
854 | if(TEST_OPT_CONTENTSB) return; |
---|
855 | if(ph->next==NULL) |
---|
856 | { |
---|
857 | nDelete(&ph->coef); |
---|
858 | ph->coef=nInit(1); |
---|
859 | } |
---|
860 | else |
---|
861 | { |
---|
862 | nNormalize(ph->coef); |
---|
863 | if(!nGreaterZero(ph->coef)) |
---|
864 | { |
---|
865 | //ph = pNeg(ph); |
---|
866 | p=ph; |
---|
867 | while(p!=NULL) |
---|
868 | { |
---|
869 | p->coef=nNeg(p->coef); |
---|
870 | p=p->next; |
---|
871 | } |
---|
872 | } |
---|
873 | |
---|
874 | h=nCopy(ph->coef); |
---|
875 | p = ph->next; |
---|
876 | |
---|
877 | while (p!=NULL) |
---|
878 | { |
---|
879 | nNormalize(p->coef); |
---|
880 | d=nGcd(h,p->coef,currRing); |
---|
881 | nDelete(&h); |
---|
882 | h = d; |
---|
883 | if(nIsOne(h)) |
---|
884 | { |
---|
885 | break; |
---|
886 | } |
---|
887 | p=p->next; |
---|
888 | } |
---|
889 | p = ph; |
---|
890 | //number tmp; |
---|
891 | if(!nIsOne(h)) |
---|
892 | { |
---|
893 | while (p!=NULL) |
---|
894 | { |
---|
895 | d = nIntDiv(p->coef,h); |
---|
896 | nDelete(&p->coef); |
---|
897 | p->coef=d; |
---|
898 | p=p->next; |
---|
899 | } |
---|
900 | } |
---|
901 | nDelete(&h); |
---|
902 | } |
---|
903 | } |
---|
904 | int tgb_sparse_matrix::non_zero_entries(int row) |
---|
905 | { |
---|
906 | return mac_length(mp[row]); |
---|
907 | } |
---|
908 | |
---|
909 | //row add_to=row add_to +row summand*factor |
---|
910 | void tgb_sparse_matrix::add_lambda_times_row(int add_to,int summand,number factor) |
---|
911 | { |
---|
912 | mp[add_to]= mac_p_add_ff_qq(mp[add_to], factor,mp[summand]); |
---|
913 | } |
---|
914 | |
---|
915 | void tgb_sparse_matrix::mult_row(int row,number factor) |
---|
916 | { |
---|
917 | if (nIsZero(factor)) |
---|
918 | { |
---|
919 | mac_destroy(mp[row]); |
---|
920 | mp[row]=NULL; |
---|
921 | |
---|
922 | return; |
---|
923 | } |
---|
924 | if(nIsOne(factor)) |
---|
925 | return; |
---|
926 | mac_mult_cons(mp[row],factor); |
---|
927 | } |
---|
928 | |
---|
929 | void tgb_sparse_matrix::free_row(int row, BOOLEAN free_non_zeros) |
---|
930 | { |
---|
931 | if(free_non_zeros) |
---|
932 | mac_destroy(mp[row]); |
---|
933 | else |
---|
934 | { |
---|
935 | while(mp[row]!=NULL) |
---|
936 | { |
---|
937 | mac_poly next=mp[row]->next; |
---|
938 | delete mp[row]; |
---|
939 | mp[row]=next; |
---|
940 | } |
---|
941 | } |
---|
942 | mp[row]=NULL; |
---|
943 | } |
---|