1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id$ */ |
---|
5 | /* |
---|
6 | * ABSTRACT: gauss implementation for F4 |
---|
7 | */ |
---|
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 | int first; |
---|
228 | assume(row_cache[i]==mat->min_col_not_zero_in_row(i)); |
---|
229 | first=row_cache[i]; |
---|
230 | assume(first!=matcol); |
---|
231 | // if((!(mat->is_zero_entry(i,col)))&&(mat->non_zero_entries(i)<act_l)) |
---|
232 | int nz; |
---|
233 | if((row_cache[i]==col)&&((nz=nSize(mat->get(i,col))*mat->non_zero_entries(i))<act_l)) |
---|
234 | { |
---|
235 | found_in_row=i; |
---|
236 | act_l=nz; |
---|
237 | } |
---|
238 | |
---|
239 | } |
---|
240 | } |
---|
241 | mat->perm_rows(row,found_in_row); |
---|
242 | int h=row_cache[row]; |
---|
243 | row_cache[row]=row_cache[found_in_row]; |
---|
244 | row_cache[found_in_row]=h; |
---|
245 | |
---|
246 | if(!must_reduce) |
---|
247 | { |
---|
248 | row++; |
---|
249 | continue; |
---|
250 | } |
---|
251 | //reduction |
---|
252 | //must extract content and normalize here |
---|
253 | mat->row_content(row); |
---|
254 | mat->row_normalize(row); |
---|
255 | |
---|
256 | //for(i=row+1;i<pn;i++){ |
---|
257 | for(i=max_in_area;i>row;i--) |
---|
258 | { |
---|
259 | int col_area_index=col/bundle_size; |
---|
260 | assume(col_area_index<=max_area_index); |
---|
261 | assume(mat->min_col_not_zero_in_row(i)>=col); |
---|
262 | int first; |
---|
263 | assume(row_cache[i]==mat->min_col_not_zero_in_row(i)); |
---|
264 | first=row_cache[i]; |
---|
265 | assume(first!=matcol); |
---|
266 | if(row_cache[i]==col) |
---|
267 | { |
---|
268 | |
---|
269 | number c1=mat->get(i,col); |
---|
270 | number c2=mat->get(row,col); |
---|
271 | number n1=c1; |
---|
272 | number n2=c2; |
---|
273 | |
---|
274 | ksCheckCoeff(&n1,&n2,currRing->cf); |
---|
275 | //nDelete(&c1); |
---|
276 | n1=nNeg(n1); |
---|
277 | mat->mult_row(i,n2); |
---|
278 | mat->add_lambda_times_row(i,row,n1); |
---|
279 | nDelete(&n1); |
---|
280 | nDelete(&n2); |
---|
281 | assume(mat->is_zero_entry(i,col)); |
---|
282 | row_cache[i]=mat->min_col_not_zero_in_row(i); |
---|
283 | assume(mat->min_col_not_zero_in_row(i)>col); |
---|
284 | if(row_cache[i]==matcol) |
---|
285 | { |
---|
286 | int index; |
---|
287 | index=i; |
---|
288 | int last_in_area; |
---|
289 | int this_cai=col_area_index; |
---|
290 | while(this_cai<max_area_index) |
---|
291 | { |
---|
292 | last_in_area=area[this_cai+1]-1; |
---|
293 | int h_c=row_cache[last_in_area]; |
---|
294 | row_cache[last_in_area]=row_cache[index]; |
---|
295 | row_cache[index]=h_c; |
---|
296 | mat->perm_rows(index,last_in_area); |
---|
297 | index=last_in_area; |
---|
298 | this_cai++; |
---|
299 | area[this_cai]--; |
---|
300 | } |
---|
301 | mat->perm_rows(index,pn-1); |
---|
302 | row_cache[index]=row_cache[pn-1]; |
---|
303 | row_cache[pn-1]=matcol; |
---|
304 | pn--; |
---|
305 | } |
---|
306 | else |
---|
307 | { |
---|
308 | int index; |
---|
309 | index=i; |
---|
310 | int last_in_area; |
---|
311 | int this_cai=col_area_index; |
---|
312 | int final_cai=row_cache[index]/bundle_size; |
---|
313 | assume(final_cai<=max_area_index); |
---|
314 | while(this_cai<final_cai) |
---|
315 | { |
---|
316 | last_in_area=area[this_cai+1]-1; |
---|
317 | int h_c=row_cache[last_in_area]; |
---|
318 | row_cache[last_in_area]=row_cache[index]; |
---|
319 | row_cache[index]=h_c; |
---|
320 | mat->perm_rows(index,last_in_area); |
---|
321 | index=last_in_area; |
---|
322 | this_cai++; |
---|
323 | area[this_cai]--; |
---|
324 | } |
---|
325 | } |
---|
326 | } |
---|
327 | else |
---|
328 | assume(mat->min_col_not_zero_in_row(i)>col); |
---|
329 | } |
---|
330 | // for(i=row+1;i<pn;i++) |
---|
331 | // { |
---|
332 | // assume(mat->min_col_not_zero_in_row(i)==row_cache[i]); |
---|
333 | // // if(mat->zero_row(i)) |
---|
334 | // assume(matcol==mat->get_columns()); |
---|
335 | // if(row_cache[i]==matcol) |
---|
336 | // { |
---|
337 | // assume(mat->zero_row(i)); |
---|
338 | // mat->perm_rows(i,pn-1); |
---|
339 | // row_cache[i]=row_cache[pn-1]; |
---|
340 | // row_cache[pn-1]=matcol; |
---|
341 | // pn--; |
---|
342 | // if(i!=pn){i--;} |
---|
343 | // } |
---|
344 | // } |
---|
345 | #ifdef TGB_DEBUG |
---|
346 | { |
---|
347 | int last=-1; |
---|
348 | for(i=0;i<pn;i++) |
---|
349 | { |
---|
350 | int act=mat->min_col_not_zero_in_row(i); |
---|
351 | assume(act>last); |
---|
352 | } |
---|
353 | for(i=pn;i<mat->get_rows();i++) |
---|
354 | { |
---|
355 | assume(mat->zero_row(i)); |
---|
356 | } |
---|
357 | } |
---|
358 | #endif |
---|
359 | row++; |
---|
360 | } |
---|
361 | omfree(area); |
---|
362 | omfree(row_cache); |
---|
363 | } |
---|
364 | |
---|
365 | void simple_gauss2(tgb_matrix* mat) |
---|
366 | { |
---|
367 | int col, row; |
---|
368 | col=0; |
---|
369 | row=0; |
---|
370 | int i; |
---|
371 | int pn=mat->get_rows(); |
---|
372 | assume(pn>0); |
---|
373 | //first clear zeroes |
---|
374 | // for(i=0;i<pn;i++) |
---|
375 | // { |
---|
376 | // if(mat->zero_row(i)) |
---|
377 | // { |
---|
378 | // mat->perm_rows(i,pn-1); |
---|
379 | // pn--; |
---|
380 | // if(i!=pn){i--;} |
---|
381 | // } |
---|
382 | // } |
---|
383 | while((row<pn-1)&&(col<mat->get_columns())){ |
---|
384 | //row is the row where pivot should be |
---|
385 | // row== pn-1 means we have only to act on one row so no red nec. |
---|
386 | //we assume further all rows till the pn-1 row are non-zero |
---|
387 | |
---|
388 | //select column |
---|
389 | |
---|
390 | // col=mat->min_col_not_zero_in_row(row); |
---|
391 | assume(col!=mat->get_columns()); |
---|
392 | int found_in_row=-1; |
---|
393 | |
---|
394 | // found_in_row=row; |
---|
395 | assume(pn<=mat->get_rows()); |
---|
396 | for(i=row;i<pn;i++) |
---|
397 | { |
---|
398 | // int first=mat->min_col_not_zero_in_row(i); |
---|
399 | // if(first<col) |
---|
400 | if(!(mat->is_zero_entry(i,col))) |
---|
401 | { |
---|
402 | found_in_row=i; |
---|
403 | break; |
---|
404 | } |
---|
405 | } |
---|
406 | if(found_in_row!=-1) |
---|
407 | { |
---|
408 | //select pivot |
---|
409 | int act_l=mat->non_zero_entries(found_in_row); |
---|
410 | for(i=i+1;i<pn;i++) |
---|
411 | { |
---|
412 | int vgl; |
---|
413 | assume(mat->min_col_not_zero_in_row(i)>=col); |
---|
414 | if((!(mat->is_zero_entry(i,col))) |
---|
415 | &&((vgl=mat->non_zero_entries(i))<act_l)) |
---|
416 | { |
---|
417 | found_in_row=i; |
---|
418 | act_l=vgl; |
---|
419 | } |
---|
420 | |
---|
421 | } |
---|
422 | mat->perm_rows(row,found_in_row); |
---|
423 | |
---|
424 | //reduction |
---|
425 | for(i=row+1;i<pn;i++){ |
---|
426 | assume(mat->min_col_not_zero_in_row(i)>=col); |
---|
427 | if(!(mat->is_zero_entry(i,col))) |
---|
428 | { |
---|
429 | number c1=nNeg(nCopy(mat->get(i,col))); |
---|
430 | number c2=mat->get(row,col); |
---|
431 | number n1=c1; |
---|
432 | number n2=c2; |
---|
433 | |
---|
434 | ksCheckCoeff(&n1,&n2,currRing->cf); |
---|
435 | nDelete(&c1); |
---|
436 | mat->mult_row(i,n2); |
---|
437 | mat->add_lambda_times_row(i,row,n1); |
---|
438 | assume(mat->is_zero_entry(i,col)); |
---|
439 | } |
---|
440 | assume(mat->min_col_not_zero_in_row(i)>col); |
---|
441 | } |
---|
442 | row++; |
---|
443 | } |
---|
444 | col++; |
---|
445 | // for(i=row+1;i<pn;i++) |
---|
446 | // { |
---|
447 | // if(mat->zero_row(i)) |
---|
448 | // { |
---|
449 | // mat->perm_rows(i,pn-1); |
---|
450 | // pn--; |
---|
451 | // if(i!=pn){i--;} |
---|
452 | // } |
---|
453 | // } |
---|
454 | } |
---|
455 | } |
---|
456 | |
---|
457 | |
---|
458 | tgb_matrix::tgb_matrix(int i, int j) |
---|
459 | { |
---|
460 | n=(number**) omalloc(i*sizeof (number*));; |
---|
461 | int z; |
---|
462 | int z2; |
---|
463 | for(z=0;z<i;z++) |
---|
464 | { |
---|
465 | n[z]=(number*)omalloc(j*sizeof(number)); |
---|
466 | for(z2=0;z2<j;z2++) |
---|
467 | { |
---|
468 | n[z][z2]=nInit(0); |
---|
469 | } |
---|
470 | } |
---|
471 | columns=j; |
---|
472 | rows=i; |
---|
473 | free_numbers=FALSE; |
---|
474 | } |
---|
475 | |
---|
476 | tgb_matrix::~tgb_matrix() |
---|
477 | { |
---|
478 | int z; |
---|
479 | for(z=0;z<rows;z++) |
---|
480 | { |
---|
481 | if(n[z]) |
---|
482 | { |
---|
483 | if(free_numbers) |
---|
484 | { |
---|
485 | int z2; |
---|
486 | for(z2=0;z2<columns;z2++) |
---|
487 | { |
---|
488 | nDelete(&(n[z][z2])); |
---|
489 | } |
---|
490 | } |
---|
491 | omfree(n[z]); |
---|
492 | } |
---|
493 | } |
---|
494 | omfree(n); |
---|
495 | } |
---|
496 | |
---|
497 | void tgb_matrix::print() |
---|
498 | { |
---|
499 | int i; |
---|
500 | int j; |
---|
501 | PrintLn(); |
---|
502 | for(i=0;i<rows;i++) |
---|
503 | { |
---|
504 | PrintS("("); |
---|
505 | for(j=0;j<columns;j++) |
---|
506 | { |
---|
507 | StringSetS(""); |
---|
508 | n_Write(n[i][j],currRing); |
---|
509 | PrintS(StringAppendS("")); |
---|
510 | PrintS("\t"); |
---|
511 | } |
---|
512 | PrintS(")\n"); |
---|
513 | } |
---|
514 | } |
---|
515 | |
---|
516 | //transfers ownership of n to the matrix |
---|
517 | void tgb_matrix::set(int i, int j, number nn) |
---|
518 | { |
---|
519 | assume(i<rows); |
---|
520 | assume(j<columns); |
---|
521 | n[i][j]=nn; |
---|
522 | } |
---|
523 | |
---|
524 | int tgb_matrix::get_rows() |
---|
525 | { |
---|
526 | return rows; |
---|
527 | } |
---|
528 | |
---|
529 | int tgb_matrix::get_columns() |
---|
530 | { |
---|
531 | return columns; |
---|
532 | } |
---|
533 | |
---|
534 | number tgb_matrix::get(int i, int j) |
---|
535 | { |
---|
536 | assume(i<rows); |
---|
537 | assume(j<columns); |
---|
538 | return n[i][j]; |
---|
539 | } |
---|
540 | |
---|
541 | BOOLEAN tgb_matrix::is_zero_entry(int i, int j) |
---|
542 | { |
---|
543 | return (nIsZero(n[i][j])); |
---|
544 | } |
---|
545 | |
---|
546 | void tgb_matrix::perm_rows(int i, int j) |
---|
547 | { |
---|
548 | number* h; |
---|
549 | h=n[i]; |
---|
550 | n[i]=n[j]; |
---|
551 | n[j]=h; |
---|
552 | } |
---|
553 | |
---|
554 | int tgb_matrix::min_col_not_zero_in_row(int row) |
---|
555 | { |
---|
556 | int i; |
---|
557 | for(i=0;i<columns;i++) |
---|
558 | { |
---|
559 | if(!(nIsZero(n[row][i]))) |
---|
560 | return i; |
---|
561 | } |
---|
562 | return columns;//error code |
---|
563 | } |
---|
564 | |
---|
565 | int tgb_matrix::next_col_not_zero(int row,int pre) |
---|
566 | { |
---|
567 | int i; |
---|
568 | for(i=pre+1;i<columns;i++) |
---|
569 | { |
---|
570 | if(!(nIsZero(n[row][i]))) |
---|
571 | return i; |
---|
572 | } |
---|
573 | return columns;//error code |
---|
574 | } |
---|
575 | |
---|
576 | BOOLEAN tgb_matrix::zero_row(int row) |
---|
577 | { |
---|
578 | int i; |
---|
579 | for(i=0;i<columns;i++) |
---|
580 | { |
---|
581 | if(!(nIsZero(n[row][i]))) |
---|
582 | return FALSE; |
---|
583 | } |
---|
584 | return TRUE; |
---|
585 | } |
---|
586 | |
---|
587 | int tgb_matrix::non_zero_entries(int row) |
---|
588 | { |
---|
589 | int i; |
---|
590 | int z=0; |
---|
591 | for(i=0;i<columns;i++) |
---|
592 | { |
---|
593 | if(!(nIsZero(n[row][i]))) |
---|
594 | z++; |
---|
595 | } |
---|
596 | return z; |
---|
597 | } |
---|
598 | |
---|
599 | //row add_to=row add_to +row summand*factor |
---|
600 | void tgb_matrix::add_lambda_times_row(int add_to,int summand,number factor) |
---|
601 | { |
---|
602 | int i; |
---|
603 | for(i=0;i<columns;i++) |
---|
604 | { |
---|
605 | if(!(nIsZero(n[summand][i]))) |
---|
606 | { |
---|
607 | number n1=n[add_to][i]; |
---|
608 | number n2=nMult(factor,n[summand][i]); |
---|
609 | n[add_to][i]=nAdd(n1,n2); |
---|
610 | nDelete(&n1); |
---|
611 | nDelete(&n2); |
---|
612 | } |
---|
613 | } |
---|
614 | } |
---|
615 | |
---|
616 | void tgb_matrix::mult_row(int row,number factor) |
---|
617 | { |
---|
618 | if (nIsOne(factor)) |
---|
619 | return; |
---|
620 | int i; |
---|
621 | for(i=0;i<columns;i++) |
---|
622 | { |
---|
623 | if(!(nIsZero(n[row][i]))) |
---|
624 | { |
---|
625 | number n1=n[row][i]; |
---|
626 | n[row][i]=nMult(n1,factor); |
---|
627 | nDelete(&n1); |
---|
628 | } |
---|
629 | } |
---|
630 | } |
---|
631 | |
---|
632 | void tgb_matrix::free_row(int row, BOOLEAN free_non_zeros) |
---|
633 | { |
---|
634 | int i; |
---|
635 | for(i=0;i<columns;i++) |
---|
636 | if((free_non_zeros)||(!(nIsZero(n[row][i])))) |
---|
637 | nDelete(&(n[row][i])); |
---|
638 | omfree(n[row]); |
---|
639 | n[row]=NULL; |
---|
640 | } |
---|
641 | |
---|
642 | tgb_sparse_matrix::tgb_sparse_matrix(int i, int j, ring rarg) |
---|
643 | { |
---|
644 | mp=(mac_poly*) omalloc(i*sizeof (mac_poly));; |
---|
645 | int z; |
---|
646 | for(z=0;z<i;z++) |
---|
647 | { |
---|
648 | mp[z]=NULL; |
---|
649 | } |
---|
650 | columns=j; |
---|
651 | rows=i; |
---|
652 | free_numbers=FALSE; |
---|
653 | r=rarg; |
---|
654 | } |
---|
655 | |
---|
656 | tgb_sparse_matrix::~tgb_sparse_matrix() |
---|
657 | { |
---|
658 | int z; |
---|
659 | for(z=0;z<rows;z++) |
---|
660 | { |
---|
661 | if(mp[z]!=NULL) |
---|
662 | { |
---|
663 | if(free_numbers) |
---|
664 | { |
---|
665 | mac_destroy(mp[z]); |
---|
666 | } |
---|
667 | else { |
---|
668 | while(mp[z]!=NULL) |
---|
669 | { |
---|
670 | mac_poly next=mp[z]->next; |
---|
671 | delete mp[z]; |
---|
672 | mp[z]=next; |
---|
673 | } |
---|
674 | } |
---|
675 | } |
---|
676 | } |
---|
677 | omfree(mp); |
---|
678 | } |
---|
679 | |
---|
680 | static int row_cmp_gen(const void* a, const void* b) |
---|
681 | { |
---|
682 | const mac_poly ap= *((mac_poly*) a); |
---|
683 | const mac_poly bp=*((mac_poly*) b); |
---|
684 | if (ap==NULL) return 1; |
---|
685 | if (bp==NULL) return -1; |
---|
686 | if (ap->exp<bp->exp) return -1; |
---|
687 | return 1; |
---|
688 | } |
---|
689 | |
---|
690 | void tgb_sparse_matrix::sort_rows() |
---|
691 | { |
---|
692 | qsort(mp,rows,sizeof(mac_poly),row_cmp_gen); |
---|
693 | } |
---|
694 | |
---|
695 | void tgb_sparse_matrix::print() |
---|
696 | { |
---|
697 | int i; |
---|
698 | int j; |
---|
699 | PrintLn(); |
---|
700 | for(i=0;i<rows;i++) |
---|
701 | { |
---|
702 | PrintS("("); |
---|
703 | for(j=0;j<columns;j++) |
---|
704 | { |
---|
705 | StringSetS(""); |
---|
706 | number n=get(i,j); |
---|
707 | n_Write(n,currRing); |
---|
708 | PrintS(StringAppendS("")); |
---|
709 | PrintS("\t"); |
---|
710 | } |
---|
711 | PrintS(")\n"); |
---|
712 | } |
---|
713 | } |
---|
714 | |
---|
715 | //transfers ownership of n to the matrix |
---|
716 | void tgb_sparse_matrix::set(int i, int j, number n) |
---|
717 | { |
---|
718 | assume(i<rows); |
---|
719 | assume(j<columns); |
---|
720 | mac_poly* set_this=&mp[i]; |
---|
721 | // while(((*set_this)!=NULL)&&((*set_this)>exp<j)) |
---|
722 | while(((*set_this)!=NULL) && ((*set_this)->exp<j)) |
---|
723 | set_this=&((*set_this)->next); |
---|
724 | |
---|
725 | if (((*set_this)==NULL)||((*set_this)->exp>j)) |
---|
726 | { |
---|
727 | if (nIsZero(n)) return; |
---|
728 | mac_poly old=(*set_this); |
---|
729 | (*set_this)=new mac_poly_r(); |
---|
730 | (*set_this)->exp=j; |
---|
731 | (*set_this)->coef=n; |
---|
732 | (*set_this)->next=old; |
---|
733 | return; |
---|
734 | } |
---|
735 | assume((*set_this)->exp==j); |
---|
736 | if(!nIsZero(n)) |
---|
737 | { |
---|
738 | nDelete(&(*set_this)->coef); |
---|
739 | (*set_this)->coef=n; |
---|
740 | } |
---|
741 | else |
---|
742 | { |
---|
743 | nDelete(&(*set_this)->coef); |
---|
744 | mac_poly dt=(*set_this); |
---|
745 | (*set_this)=dt->next; |
---|
746 | delete dt; |
---|
747 | } |
---|
748 | return; |
---|
749 | } |
---|
750 | |
---|
751 | int tgb_sparse_matrix::get_rows() |
---|
752 | { |
---|
753 | return rows; |
---|
754 | } |
---|
755 | |
---|
756 | int tgb_sparse_matrix::get_columns() |
---|
757 | { |
---|
758 | return columns; |
---|
759 | } |
---|
760 | |
---|
761 | number tgb_sparse_matrix::get(int i, int j) |
---|
762 | { |
---|
763 | assume(i<rows); |
---|
764 | assume(j<columns); |
---|
765 | mac_poly rr=mp[i]; |
---|
766 | while((rr!=NULL)&&(rr->exp<j)) |
---|
767 | rr=rr->next; |
---|
768 | if ((rr==NULL)||(rr->exp>j)) |
---|
769 | { |
---|
770 | number n=nInit(0); |
---|
771 | return n; |
---|
772 | } |
---|
773 | assume(rr->exp==j); |
---|
774 | return rr->coef; |
---|
775 | } |
---|
776 | |
---|
777 | BOOLEAN tgb_sparse_matrix::is_zero_entry(int i, int j) |
---|
778 | { |
---|
779 | assume(i<rows); |
---|
780 | assume(j<columns); |
---|
781 | mac_poly rr=mp[i]; |
---|
782 | while((rr!=NULL)&&(rr->exp<j)) |
---|
783 | rr=rr->next; |
---|
784 | if ((rr==NULL)||(rr->exp>j)) |
---|
785 | { |
---|
786 | return TRUE; |
---|
787 | } |
---|
788 | assume(!nIsZero(rr->coef)); |
---|
789 | assume(rr->exp==j); |
---|
790 | return FALSE; |
---|
791 | } |
---|
792 | |
---|
793 | int tgb_sparse_matrix::min_col_not_zero_in_row(int row) |
---|
794 | { |
---|
795 | if(mp[row]!=NULL) |
---|
796 | { |
---|
797 | assume(!nIsZero(mp[row]->coef)); |
---|
798 | return mp[row]->exp; |
---|
799 | } |
---|
800 | else |
---|
801 | return columns;//error code |
---|
802 | } |
---|
803 | |
---|
804 | int tgb_sparse_matrix::next_col_not_zero(int row,int pre) |
---|
805 | { |
---|
806 | mac_poly rr=mp[row]; |
---|
807 | while((rr!=NULL)&&(rr->exp<=pre)) |
---|
808 | rr=rr->next; |
---|
809 | if(rr!=NULL) |
---|
810 | { |
---|
811 | assume(!nIsZero(rr->coef)); |
---|
812 | return rr->exp; |
---|
813 | } |
---|
814 | return columns;//error code |
---|
815 | } |
---|
816 | |
---|
817 | BOOLEAN tgb_sparse_matrix::zero_row(int row) |
---|
818 | { |
---|
819 | assume((mp[row]==NULL)||(!nIsZero(mp[row]->coef))); |
---|
820 | if (mp[row]==NULL) |
---|
821 | return TRUE; |
---|
822 | else |
---|
823 | return FALSE; |
---|
824 | } |
---|
825 | |
---|
826 | void tgb_sparse_matrix::row_normalize(int row) |
---|
827 | { |
---|
828 | if (!rField_has_simple_inverse(r)) /* Z/p, GF(p,n), R, long R/C */ |
---|
829 | { |
---|
830 | mac_poly m=mp[row]; |
---|
831 | while (m!=NULL) |
---|
832 | { |
---|
833 | #ifndef NDEBUG |
---|
834 | if (currRing==r) {nTest(m->coef);} |
---|
835 | #endif |
---|
836 | n_Normalize(m->coef,r); |
---|
837 | m=m->next; |
---|
838 | } |
---|
839 | } |
---|
840 | } |
---|
841 | |
---|
842 | void tgb_sparse_matrix::row_content(int row) |
---|
843 | { |
---|
844 | mac_poly ph=mp[row]; |
---|
845 | number h,d; |
---|
846 | mac_poly p; |
---|
847 | |
---|
848 | if(TEST_OPT_CONTENTSB) return; |
---|
849 | if(ph->next==NULL) |
---|
850 | { |
---|
851 | nDelete(&ph->coef); |
---|
852 | ph->coef=nInit(1); |
---|
853 | } |
---|
854 | else |
---|
855 | { |
---|
856 | nNormalize(ph->coef); |
---|
857 | if(!nGreaterZero(ph->coef)) |
---|
858 | { |
---|
859 | //ph = pNeg(ph); |
---|
860 | p=ph; |
---|
861 | while(p!=NULL) |
---|
862 | { |
---|
863 | p->coef=nNeg(p->coef); |
---|
864 | p=p->next; |
---|
865 | } |
---|
866 | } |
---|
867 | |
---|
868 | h=nCopy(ph->coef); |
---|
869 | p = ph->next; |
---|
870 | |
---|
871 | while (p!=NULL) |
---|
872 | { |
---|
873 | nNormalize(p->coef); |
---|
874 | d=nGcd(h,p->coef,currRing); |
---|
875 | nDelete(&h); |
---|
876 | h = d; |
---|
877 | if(nIsOne(h)) |
---|
878 | { |
---|
879 | break; |
---|
880 | } |
---|
881 | p=p->next; |
---|
882 | } |
---|
883 | p = ph; |
---|
884 | //number tmp; |
---|
885 | if(!nIsOne(h)) |
---|
886 | { |
---|
887 | while (p!=NULL) |
---|
888 | { |
---|
889 | d = nIntDiv(p->coef,h); |
---|
890 | nDelete(&p->coef); |
---|
891 | p->coef=d; |
---|
892 | p=p->next; |
---|
893 | } |
---|
894 | } |
---|
895 | nDelete(&h); |
---|
896 | } |
---|
897 | } |
---|
898 | int tgb_sparse_matrix::non_zero_entries(int row) |
---|
899 | { |
---|
900 | return mac_length(mp[row]); |
---|
901 | } |
---|
902 | |
---|
903 | //row add_to=row add_to +row summand*factor |
---|
904 | void tgb_sparse_matrix::add_lambda_times_row(int add_to,int summand,number factor) |
---|
905 | { |
---|
906 | mp[add_to]= mac_p_add_ff_qq(mp[add_to], factor,mp[summand]); |
---|
907 | } |
---|
908 | |
---|
909 | void tgb_sparse_matrix::mult_row(int row,number factor) |
---|
910 | { |
---|
911 | if (nIsZero(factor)) |
---|
912 | { |
---|
913 | mac_destroy(mp[row]); |
---|
914 | mp[row]=NULL; |
---|
915 | |
---|
916 | return; |
---|
917 | } |
---|
918 | if(nIsOne(factor)) |
---|
919 | return; |
---|
920 | mac_mult_cons(mp[row],factor); |
---|
921 | } |
---|
922 | |
---|
923 | void tgb_sparse_matrix::free_row(int row, BOOLEAN free_non_zeros) |
---|
924 | { |
---|
925 | if(free_non_zeros) |
---|
926 | mac_destroy(mp[row]); |
---|
927 | else |
---|
928 | { |
---|
929 | while(mp[row]!=NULL) |
---|
930 | { |
---|
931 | mac_poly next=mp[row]->next; |
---|
932 | delete mp[row]; |
---|
933 | mp[row]=next; |
---|
934 | } |
---|
935 | } |
---|
936 | mp[row]=NULL; |
---|
937 | } |
---|