1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id: hutil.cc,v 1.3 2004-10-28 14:16:27 Singular Exp $ */ |
---|
5 | /* |
---|
6 | * ABSTRACT: Utilities for staircase operations |
---|
7 | */ |
---|
8 | |
---|
9 | #include "mod2.h" |
---|
10 | #include "structs.h" |
---|
11 | #include "febase.h" |
---|
12 | #include "omalloc.h" |
---|
13 | #include "ideals.h" |
---|
14 | #include "polys.h" |
---|
15 | #include "hutil.h" |
---|
16 | |
---|
17 | scfmon hexist, hstc, hrad, hwork; |
---|
18 | scmon hpure, hpur0; |
---|
19 | varset hvar, hsel; |
---|
20 | int hNexist, hNstc, hNrad, hNvar, hNpure; |
---|
21 | int hisModule; |
---|
22 | monf stcmem, radmem; |
---|
23 | |
---|
24 | // Making a global "security" copy of the allocated exponent vectors |
---|
25 | // is a dirty fix for correct memory disallocation: It would be |
---|
26 | // better, if either the fields of heist are never touched |
---|
27 | // (i.e. changed) except in hInit, or, if hInit would return the |
---|
28 | // "security" copy as well. But then, all the relevant data is held in |
---|
29 | // global variables, so we might do that here, as well. |
---|
30 | static int **hsecure= NULL; |
---|
31 | |
---|
32 | scfmon hInit(ideal S, ideal Q, int *Nexist, ring tailRing) |
---|
33 | { |
---|
34 | int sl, ql, i, k = 0; |
---|
35 | polyset si, qi, ss; |
---|
36 | scfmon ex, ek; |
---|
37 | if (tailRing != currRing) |
---|
38 | hisModule = idRankFreeModule(S, currRing, tailRing); |
---|
39 | else |
---|
40 | hisModule = idRankFreeModule(S); |
---|
41 | if (hisModule < 0) |
---|
42 | hisModule = 0; |
---|
43 | if (S) |
---|
44 | { |
---|
45 | si = S->m; |
---|
46 | sl = IDELEMS(S); |
---|
47 | } |
---|
48 | else |
---|
49 | { |
---|
50 | si = NULL; |
---|
51 | sl = 0; |
---|
52 | } |
---|
53 | if (Q) |
---|
54 | { |
---|
55 | qi = Q->m; |
---|
56 | ql = IDELEMS(Q); |
---|
57 | } |
---|
58 | else |
---|
59 | { |
---|
60 | qi = NULL; |
---|
61 | ql = 0; |
---|
62 | } |
---|
63 | if (!(sl + ql)) |
---|
64 | { |
---|
65 | *Nexist = 0; |
---|
66 | return NULL; |
---|
67 | } |
---|
68 | ss = si; |
---|
69 | for (i = sl; i; i--) |
---|
70 | { |
---|
71 | if (*ss!=0) |
---|
72 | k++; |
---|
73 | ss++; |
---|
74 | } |
---|
75 | ss = qi; |
---|
76 | for (i = ql; i; i--) |
---|
77 | { |
---|
78 | if (*ss!=0) |
---|
79 | k++; |
---|
80 | ss++; |
---|
81 | } |
---|
82 | *Nexist = k; |
---|
83 | if (!k) |
---|
84 | return NULL; |
---|
85 | ek = ex = (scfmon)omAlloc(k * sizeof(scmon)); |
---|
86 | hsecure = (int**) omAlloc(k * sizeof(scmon)); |
---|
87 | for (i = sl; i>0; i--) |
---|
88 | { |
---|
89 | if (*si!=NULL) |
---|
90 | { |
---|
91 | *ek = (int*) omAlloc((pVariables+1)*sizeof(int)); |
---|
92 | pGetExpV(*si, *ek); |
---|
93 | ek++; |
---|
94 | } |
---|
95 | si++; |
---|
96 | } |
---|
97 | for (i = ql; i; i--) |
---|
98 | { |
---|
99 | if (*qi!=NULL) |
---|
100 | { |
---|
101 | *ek = (int*) omAlloc((pVariables+1)*sizeof(int)); |
---|
102 | pGetExpV(*qi, *ek); |
---|
103 | ek++; |
---|
104 | } |
---|
105 | qi++; |
---|
106 | } |
---|
107 | memcpy(hsecure, ex, k * sizeof(scmon)); |
---|
108 | return ex; |
---|
109 | } |
---|
110 | |
---|
111 | void hWeight() |
---|
112 | { |
---|
113 | int i, k; |
---|
114 | int x; |
---|
115 | |
---|
116 | i = pVariables; |
---|
117 | loop |
---|
118 | { |
---|
119 | if (pWeight(i) != 1) break; |
---|
120 | i--; |
---|
121 | if (i == 0) return; |
---|
122 | } |
---|
123 | for (i=pVariables; i; i--) |
---|
124 | { |
---|
125 | x = pWeight(i); |
---|
126 | if (x != 1) |
---|
127 | { |
---|
128 | for (k=hNexist-1; k>=0; k--) |
---|
129 | { |
---|
130 | hexist[k][i] *= x; |
---|
131 | } |
---|
132 | } |
---|
133 | } |
---|
134 | } |
---|
135 | |
---|
136 | void hDelete(scfmon ev, int ev_length) |
---|
137 | { |
---|
138 | int i; |
---|
139 | |
---|
140 | if (ev_length>0) |
---|
141 | { |
---|
142 | for (i=ev_length-1;i>=0;i--) |
---|
143 | omFreeSize(hsecure[i],(pVariables+1)*sizeof(int)); |
---|
144 | omFreeSize(hsecure, ev_length*sizeof(scmon)); |
---|
145 | omFreeSize(ev, ev_length*sizeof(scmon)); |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|
149 | |
---|
150 | void hComp(scfmon exist, int Nexist, int ak, scfmon stc, int *Nstc) |
---|
151 | { |
---|
152 | int i = Nexist, k = 0; |
---|
153 | scfmon ex = exist, co = stc; |
---|
154 | for (; i; i--) |
---|
155 | { |
---|
156 | if (!(**ex) || ((**ex) == ak)) |
---|
157 | { |
---|
158 | *co = *ex; |
---|
159 | co++; |
---|
160 | k++; |
---|
161 | } |
---|
162 | ex++; |
---|
163 | } |
---|
164 | *Nstc = k; |
---|
165 | } |
---|
166 | |
---|
167 | |
---|
168 | void hSupp(scfmon stc, int Nstc, varset var, int *Nvar) |
---|
169 | { |
---|
170 | int nv, i0, i1, i, j; |
---|
171 | nv = i0 = *Nvar; |
---|
172 | i1 = 0; |
---|
173 | for (i = 1; i <= nv; i++) |
---|
174 | { |
---|
175 | j = 0; |
---|
176 | loop |
---|
177 | { |
---|
178 | if (stc[j][i]) |
---|
179 | { |
---|
180 | i1++; |
---|
181 | var[i1] = i; |
---|
182 | break; |
---|
183 | } |
---|
184 | j++; |
---|
185 | if (j == Nstc) |
---|
186 | { |
---|
187 | var[i0] = i; |
---|
188 | i0--; |
---|
189 | break; |
---|
190 | } |
---|
191 | } |
---|
192 | } |
---|
193 | *Nvar = i1; |
---|
194 | } |
---|
195 | |
---|
196 | void hOrdSupp(scfmon stc, int Nstc, varset var, int Nvar) |
---|
197 | { |
---|
198 | int i, i1, j, jj, k, l; |
---|
199 | int x; |
---|
200 | scmon temp, count; |
---|
201 | float o, h, g, *v1; |
---|
202 | |
---|
203 | v1 = (float *)omAlloc(Nvar * sizeof(float)); |
---|
204 | temp = (int *)omAlloc(Nstc * sizeof(int)); |
---|
205 | count = (int *)omAlloc(Nstc * sizeof(int)); |
---|
206 | for (i = 1; i <= Nvar; i++) |
---|
207 | { |
---|
208 | i1 = var[i]; |
---|
209 | *temp = stc[0][i1]; |
---|
210 | *count = 1; |
---|
211 | jj = 1; |
---|
212 | for (j = 1; j < Nstc; j++) |
---|
213 | { |
---|
214 | x = stc[j][i1]; |
---|
215 | k = 0; |
---|
216 | loop |
---|
217 | { |
---|
218 | if (x > temp[k]) |
---|
219 | { |
---|
220 | k++; |
---|
221 | if (k == jj) |
---|
222 | { |
---|
223 | temp[k] = x; |
---|
224 | count[k] = 1; |
---|
225 | jj++; |
---|
226 | break; |
---|
227 | } |
---|
228 | } |
---|
229 | else if (x < temp[k]) |
---|
230 | { |
---|
231 | for (l = jj; l > k; l--) |
---|
232 | { |
---|
233 | temp[l] = temp[l-1]; |
---|
234 | count[l] = count[l-1]; |
---|
235 | } |
---|
236 | temp[k] = x; |
---|
237 | count[k] = 1; |
---|
238 | jj++; |
---|
239 | break; |
---|
240 | } |
---|
241 | else |
---|
242 | { |
---|
243 | count[k]++; |
---|
244 | break; |
---|
245 | } |
---|
246 | } |
---|
247 | } |
---|
248 | h = 0.0; |
---|
249 | o = (float)Nstc/(float)jj; |
---|
250 | for(j = 0; j < jj; j++) |
---|
251 | { |
---|
252 | g = (float)count[j]; |
---|
253 | if (g > o) |
---|
254 | g -= o; |
---|
255 | else |
---|
256 | g = o - g; |
---|
257 | if (g > h) |
---|
258 | h = g; |
---|
259 | } |
---|
260 | v1[i-1] = h * (float)jj; |
---|
261 | } |
---|
262 | omFreeSize((ADDRESS)count, Nstc * sizeof(int)); |
---|
263 | omFreeSize((ADDRESS)temp, Nstc * sizeof(int)); |
---|
264 | for (i = 1; i < Nvar; i++) |
---|
265 | { |
---|
266 | i1 = var[i+1]; |
---|
267 | h = v1[i]; |
---|
268 | j = 0; |
---|
269 | loop |
---|
270 | { |
---|
271 | if (h > v1[j]) |
---|
272 | { |
---|
273 | for (l = i; l > j; l--) |
---|
274 | { |
---|
275 | v1[l] = v1[l-1]; |
---|
276 | var[l+1] = var[l]; |
---|
277 | } |
---|
278 | v1[j] = h; |
---|
279 | var[j+1] = i1; |
---|
280 | break; |
---|
281 | } |
---|
282 | j++; |
---|
283 | if (j == i) |
---|
284 | break; |
---|
285 | } |
---|
286 | } |
---|
287 | omFreeSize((ADDRESS)v1, Nvar * sizeof(float)); |
---|
288 | } |
---|
289 | |
---|
290 | |
---|
291 | static void hShrink(scfmon co, int a, int Nco) |
---|
292 | { |
---|
293 | while ((co[a]!=NULL) && (a<Nco)) a++; |
---|
294 | int i = a; |
---|
295 | int j; |
---|
296 | for (j = a; j < Nco; j++) |
---|
297 | { |
---|
298 | if (co[j]!=NULL) |
---|
299 | { |
---|
300 | co[i] = co[j]; |
---|
301 | i++; |
---|
302 | } |
---|
303 | } |
---|
304 | } |
---|
305 | |
---|
306 | |
---|
307 | void hStaircase(scfmon stc, int *Nstc, varset var, int Nvar) |
---|
308 | { |
---|
309 | int nc = *Nstc; |
---|
310 | if (nc < 2) |
---|
311 | return; |
---|
312 | int z = 0; |
---|
313 | int i = 0; |
---|
314 | int j = 1; |
---|
315 | scmon n = stc[1 /*j*/]; |
---|
316 | scmon o = stc[0]; |
---|
317 | int k = Nvar; |
---|
318 | loop |
---|
319 | { |
---|
320 | int k1 = var[k]; |
---|
321 | if (o[k1] > n[k1]) |
---|
322 | { |
---|
323 | loop |
---|
324 | { |
---|
325 | k--; |
---|
326 | if (k==0) |
---|
327 | { |
---|
328 | stc[i] = NULL; |
---|
329 | z++; |
---|
330 | break; |
---|
331 | } |
---|
332 | else |
---|
333 | { |
---|
334 | k1 = var[k]; |
---|
335 | if (o[k1] < n[k1]) |
---|
336 | break; |
---|
337 | } |
---|
338 | } |
---|
339 | k = Nvar; |
---|
340 | } |
---|
341 | else if (o[k1] < n[k1]) |
---|
342 | { |
---|
343 | loop |
---|
344 | { |
---|
345 | k--; |
---|
346 | if (k==0) |
---|
347 | { |
---|
348 | stc[j] = NULL; |
---|
349 | z++; |
---|
350 | break; |
---|
351 | } |
---|
352 | else |
---|
353 | { |
---|
354 | k1 = var[k]; |
---|
355 | if (o[k1] > n[k1]) |
---|
356 | break; |
---|
357 | } |
---|
358 | } |
---|
359 | k = Nvar; |
---|
360 | } |
---|
361 | else |
---|
362 | { |
---|
363 | k--; |
---|
364 | if (k==0) |
---|
365 | { |
---|
366 | stc[j] = NULL; |
---|
367 | z++; |
---|
368 | k = Nvar; |
---|
369 | } |
---|
370 | } |
---|
371 | if (k == Nvar) |
---|
372 | { |
---|
373 | if (stc[j]==NULL) |
---|
374 | i = j - 1; |
---|
375 | loop |
---|
376 | { |
---|
377 | i++; |
---|
378 | if (i == j) |
---|
379 | { |
---|
380 | i = -1; |
---|
381 | j++; |
---|
382 | if (j < nc) |
---|
383 | n = stc[j]; |
---|
384 | else |
---|
385 | { |
---|
386 | if (z!=0) |
---|
387 | { |
---|
388 | *Nstc -= z; |
---|
389 | hShrink(stc, 0, nc); |
---|
390 | } |
---|
391 | return; |
---|
392 | } |
---|
393 | } |
---|
394 | else if (stc[i]!=NULL) |
---|
395 | { |
---|
396 | o = stc[i]; |
---|
397 | break; |
---|
398 | } |
---|
399 | } |
---|
400 | } |
---|
401 | } |
---|
402 | } |
---|
403 | |
---|
404 | |
---|
405 | void hRadical(scfmon rad, int *Nrad, int Nvar) |
---|
406 | { |
---|
407 | int nc = *Nrad, z = 0, i, j, k; |
---|
408 | scmon n, o; |
---|
409 | if (nc < 2) |
---|
410 | return; |
---|
411 | i = 0; |
---|
412 | j = 1; |
---|
413 | n = rad[j]; |
---|
414 | o = rad[0]; |
---|
415 | k = Nvar; |
---|
416 | loop |
---|
417 | { |
---|
418 | if ((o[k]!=0) && (n[k]==0)) |
---|
419 | { |
---|
420 | loop |
---|
421 | { |
---|
422 | k--; |
---|
423 | if (k==0) |
---|
424 | { |
---|
425 | rad[i] = NULL; |
---|
426 | z++; |
---|
427 | break; |
---|
428 | } |
---|
429 | else |
---|
430 | { |
---|
431 | if ((o[k]==0) && (n[k]!=0)) |
---|
432 | break; |
---|
433 | } |
---|
434 | } |
---|
435 | k = Nvar; |
---|
436 | } |
---|
437 | else if (!o[k] && n[k]) |
---|
438 | { |
---|
439 | loop |
---|
440 | { |
---|
441 | k--; |
---|
442 | if (!k) |
---|
443 | { |
---|
444 | rad[j] = NULL; |
---|
445 | z++; |
---|
446 | break; |
---|
447 | } |
---|
448 | else |
---|
449 | { |
---|
450 | if (o[k] && !n[k]) |
---|
451 | break; |
---|
452 | } |
---|
453 | } |
---|
454 | k = Nvar; |
---|
455 | } |
---|
456 | else |
---|
457 | { |
---|
458 | k--; |
---|
459 | if (!k) |
---|
460 | { |
---|
461 | rad[j] = NULL; |
---|
462 | z++; |
---|
463 | k = Nvar; |
---|
464 | } |
---|
465 | } |
---|
466 | if (k == Nvar) |
---|
467 | { |
---|
468 | if (!rad[j]) |
---|
469 | i = j - 1; |
---|
470 | loop |
---|
471 | { |
---|
472 | i++; |
---|
473 | if (i == j) |
---|
474 | { |
---|
475 | i = -1; |
---|
476 | j++; |
---|
477 | if (j < nc) |
---|
478 | n = rad[j]; |
---|
479 | else |
---|
480 | { |
---|
481 | if (z) |
---|
482 | { |
---|
483 | *Nrad -= z; |
---|
484 | hShrink(rad, 0, nc); |
---|
485 | } |
---|
486 | return; |
---|
487 | } |
---|
488 | } |
---|
489 | else if (rad[i]) |
---|
490 | { |
---|
491 | o = rad[i]; |
---|
492 | break; |
---|
493 | } |
---|
494 | } |
---|
495 | } |
---|
496 | } |
---|
497 | } |
---|
498 | |
---|
499 | |
---|
500 | void hLexS(scfmon stc, int Nstc, varset var, int Nvar) |
---|
501 | { |
---|
502 | if (Nstc < 2) |
---|
503 | return; |
---|
504 | int j = 1, i = 0; |
---|
505 | scmon n = stc[j]; |
---|
506 | scmon o = stc[0]; |
---|
507 | int k = Nvar; |
---|
508 | loop |
---|
509 | { |
---|
510 | int k1 = var[k]; |
---|
511 | if (o[k1] < n[k1]) |
---|
512 | { |
---|
513 | i++; |
---|
514 | if (i < j) |
---|
515 | { |
---|
516 | o = stc[i]; |
---|
517 | k = Nvar; |
---|
518 | } |
---|
519 | else |
---|
520 | { |
---|
521 | j++; |
---|
522 | if (j < Nstc) |
---|
523 | { |
---|
524 | i = 0; |
---|
525 | o = stc[0]; |
---|
526 | n = stc[j]; |
---|
527 | k = Nvar; |
---|
528 | } |
---|
529 | else |
---|
530 | return; |
---|
531 | } |
---|
532 | } |
---|
533 | else if (o[k1] > n[k1]) |
---|
534 | { |
---|
535 | int tmp_k; |
---|
536 | for (tmp_k = j; tmp_k > i; tmp_k--) |
---|
537 | stc[tmp_k] = stc[tmp_k - 1]; |
---|
538 | stc[i] = n; |
---|
539 | j++; |
---|
540 | if (j < Nstc) |
---|
541 | { |
---|
542 | i = 0; |
---|
543 | o = stc[0]; |
---|
544 | n = stc[j]; |
---|
545 | k = Nvar; |
---|
546 | } |
---|
547 | else |
---|
548 | return; |
---|
549 | } |
---|
550 | else |
---|
551 | { |
---|
552 | k--; |
---|
553 | if (k<=0) return; |
---|
554 | } |
---|
555 | } |
---|
556 | } |
---|
557 | |
---|
558 | |
---|
559 | void hLexR(scfmon rad, int Nrad, varset var, int Nvar) |
---|
560 | { |
---|
561 | int j = 1, i = 0, k, k1; |
---|
562 | scmon n, o; |
---|
563 | if (Nrad < 2) |
---|
564 | return; |
---|
565 | n = rad[j]; |
---|
566 | o = rad[0]; |
---|
567 | k = Nvar; |
---|
568 | loop |
---|
569 | { |
---|
570 | k1 = var[k]; |
---|
571 | if (!o[k1] && n[k1]) |
---|
572 | { |
---|
573 | i++; |
---|
574 | if (i < j) |
---|
575 | { |
---|
576 | o = rad[i]; |
---|
577 | k = Nvar; |
---|
578 | } |
---|
579 | else |
---|
580 | { |
---|
581 | j++; |
---|
582 | if (j < Nrad) |
---|
583 | { |
---|
584 | i = 0; |
---|
585 | o = rad[0]; |
---|
586 | n = rad[j]; |
---|
587 | k = Nvar; |
---|
588 | } |
---|
589 | else |
---|
590 | return; |
---|
591 | } |
---|
592 | } |
---|
593 | else if (o[k1] && !n[k1]) |
---|
594 | { |
---|
595 | for (k = j; k > i; k--) |
---|
596 | rad[k] = rad[k - 1]; |
---|
597 | rad[i] = n; |
---|
598 | j++; |
---|
599 | if (j < Nrad) |
---|
600 | { |
---|
601 | i = 0; |
---|
602 | o = rad[0]; |
---|
603 | n = rad[j]; |
---|
604 | k = Nvar; |
---|
605 | } |
---|
606 | else |
---|
607 | return; |
---|
608 | } |
---|
609 | else |
---|
610 | k--; |
---|
611 | } |
---|
612 | } |
---|
613 | |
---|
614 | |
---|
615 | void hPure(scfmon stc, int a, int *Nstc, varset var, int Nvar, |
---|
616 | scmon pure, int *Npure) |
---|
617 | { |
---|
618 | int nc = *Nstc, np = 0, nq = 0, j, i, i1, c, l; |
---|
619 | scmon x; |
---|
620 | for (j = a; j < nc; j++) |
---|
621 | { |
---|
622 | x = stc[j]; |
---|
623 | i = Nvar; |
---|
624 | c = 2; |
---|
625 | l = 0; |
---|
626 | loop |
---|
627 | { |
---|
628 | i1 = var[i]; |
---|
629 | if (x[i1]) |
---|
630 | { |
---|
631 | c--; |
---|
632 | if (!c) |
---|
633 | { |
---|
634 | l = 0; |
---|
635 | break; |
---|
636 | } |
---|
637 | else if (c == 1) |
---|
638 | l = i1; |
---|
639 | } |
---|
640 | i--; |
---|
641 | if (!i) |
---|
642 | break; |
---|
643 | } |
---|
644 | if (l) |
---|
645 | { |
---|
646 | if (!pure[l]) |
---|
647 | { |
---|
648 | np++; |
---|
649 | pure[l] = x[l]; |
---|
650 | } |
---|
651 | else if (x[l] < pure[l]) |
---|
652 | pure[l] = x[l]; |
---|
653 | stc[j] = NULL; |
---|
654 | nq++; |
---|
655 | } |
---|
656 | } |
---|
657 | *Npure = np; |
---|
658 | if (nq!=0) |
---|
659 | { |
---|
660 | *Nstc -= nq; |
---|
661 | hShrink(stc, a, nc); |
---|
662 | } |
---|
663 | } |
---|
664 | |
---|
665 | |
---|
666 | void hElimS(scfmon stc, int *e1, int a2, int e2, varset var, int Nvar) |
---|
667 | { |
---|
668 | int nc = *e1, z = 0, i, j, k, k1; |
---|
669 | scmon n, o; |
---|
670 | if (!nc || (a2 == e2)) |
---|
671 | return; |
---|
672 | j = 0; |
---|
673 | i = a2; |
---|
674 | o = stc[i]; |
---|
675 | n = stc[0]; |
---|
676 | k = Nvar; |
---|
677 | loop |
---|
678 | { |
---|
679 | k1 = var[k]; |
---|
680 | if (o[k1] > n[k1]) |
---|
681 | { |
---|
682 | k = Nvar; |
---|
683 | i++; |
---|
684 | if (i < e2) |
---|
685 | o = stc[i]; |
---|
686 | else |
---|
687 | { |
---|
688 | j++; |
---|
689 | if (j < nc) |
---|
690 | { |
---|
691 | i = a2; |
---|
692 | o = stc[i]; |
---|
693 | n = stc[j]; |
---|
694 | } |
---|
695 | else |
---|
696 | { |
---|
697 | if (z!=0) |
---|
698 | { |
---|
699 | *e1 -= z; |
---|
700 | hShrink(stc, 0, nc); |
---|
701 | } |
---|
702 | return; |
---|
703 | } |
---|
704 | } |
---|
705 | } |
---|
706 | else |
---|
707 | { |
---|
708 | k--; |
---|
709 | if (k==0) |
---|
710 | { |
---|
711 | stc[j] = NULL; |
---|
712 | z++; |
---|
713 | j++; |
---|
714 | if (j < nc) |
---|
715 | { |
---|
716 | i = a2; |
---|
717 | o = stc[i]; |
---|
718 | n = stc[j]; |
---|
719 | k = Nvar; |
---|
720 | } |
---|
721 | else |
---|
722 | { |
---|
723 | if (z!=0) |
---|
724 | { |
---|
725 | *e1 -= z; |
---|
726 | hShrink(stc, 0, nc); |
---|
727 | } |
---|
728 | return; |
---|
729 | } |
---|
730 | } |
---|
731 | } |
---|
732 | } |
---|
733 | } |
---|
734 | |
---|
735 | |
---|
736 | void hElimR(scfmon rad, int *e1, int a2, int e2, varset var, int Nvar) |
---|
737 | { |
---|
738 | int nc = *e1, z = 0, i, j, k, k1; |
---|
739 | scmon n, o; |
---|
740 | if (!nc || (a2 == e2)) |
---|
741 | return; |
---|
742 | j = 0; |
---|
743 | i = a2; |
---|
744 | o = rad[i]; |
---|
745 | n = rad[0]; |
---|
746 | k = Nvar; |
---|
747 | loop |
---|
748 | { |
---|
749 | k1 = var[k]; |
---|
750 | if (o[k1] && !n[k1]) |
---|
751 | { |
---|
752 | k = Nvar; |
---|
753 | i++; |
---|
754 | if (i < e2) |
---|
755 | o = rad[i]; |
---|
756 | else |
---|
757 | { |
---|
758 | j++; |
---|
759 | if (j < nc) |
---|
760 | { |
---|
761 | i = a2; |
---|
762 | o = rad[i]; |
---|
763 | n = rad[j]; |
---|
764 | } |
---|
765 | else |
---|
766 | { |
---|
767 | if (z!=0) |
---|
768 | { |
---|
769 | *e1 -= z; |
---|
770 | hShrink(rad, 0, nc); |
---|
771 | } |
---|
772 | return; |
---|
773 | } |
---|
774 | } |
---|
775 | } |
---|
776 | else |
---|
777 | { |
---|
778 | k--; |
---|
779 | if (!k) |
---|
780 | { |
---|
781 | rad[j] = NULL; |
---|
782 | z++; |
---|
783 | j++; |
---|
784 | if (j < nc) |
---|
785 | { |
---|
786 | i = a2; |
---|
787 | o = rad[i]; |
---|
788 | n = rad[j]; |
---|
789 | k = Nvar; |
---|
790 | } |
---|
791 | else |
---|
792 | { |
---|
793 | if (z!=0) |
---|
794 | { |
---|
795 | *e1 -= z; |
---|
796 | hShrink(rad, 0, nc); |
---|
797 | } |
---|
798 | return; |
---|
799 | } |
---|
800 | } |
---|
801 | } |
---|
802 | } |
---|
803 | } |
---|
804 | |
---|
805 | |
---|
806 | void hLex2S(scfmon rad, int e1, int a2, int e2, varset var, |
---|
807 | int Nvar, scfmon w) |
---|
808 | { |
---|
809 | int j0 = 0, j = 0, i = a2, k, k1; |
---|
810 | scmon n, o; |
---|
811 | if (!e1) |
---|
812 | { |
---|
813 | for (; i < e2; i++) |
---|
814 | rad[i - a2] = rad[i]; |
---|
815 | return; |
---|
816 | } else if (i == e2) |
---|
817 | return; |
---|
818 | n = rad[j]; |
---|
819 | o = rad[i]; |
---|
820 | loop |
---|
821 | { |
---|
822 | k = Nvar; |
---|
823 | loop |
---|
824 | { |
---|
825 | k1 = var[k]; |
---|
826 | if (o[k1] < n[k1]) |
---|
827 | { |
---|
828 | w[j0] = o; |
---|
829 | j0++; |
---|
830 | i++; |
---|
831 | if (i < e2) |
---|
832 | { |
---|
833 | o = rad[i]; |
---|
834 | break; |
---|
835 | } |
---|
836 | else |
---|
837 | { |
---|
838 | for (; j < e1; j++) |
---|
839 | { |
---|
840 | w[j0] = rad[j]; |
---|
841 | j0++; |
---|
842 | } |
---|
843 | memcpy(rad, w, (e1 + e2 - a2) * sizeof(scmon)); |
---|
844 | return; |
---|
845 | } |
---|
846 | } |
---|
847 | else if (o[k1] > n[k1]) |
---|
848 | { |
---|
849 | w[j0] = n; |
---|
850 | j0++; |
---|
851 | j++; |
---|
852 | if (j < e1) |
---|
853 | { |
---|
854 | n = rad[j]; |
---|
855 | break; |
---|
856 | } |
---|
857 | else |
---|
858 | { |
---|
859 | for (; i < e2; i++) |
---|
860 | { |
---|
861 | w[j0] = rad[i]; |
---|
862 | j0++; |
---|
863 | } |
---|
864 | memcpy(rad, w, (e1 + e2 - a2) * sizeof(scmon)); |
---|
865 | return; |
---|
866 | } |
---|
867 | } |
---|
868 | k--; |
---|
869 | } |
---|
870 | } |
---|
871 | } |
---|
872 | |
---|
873 | |
---|
874 | void hLex2R(scfmon rad, int e1, int a2, int e2, varset var, |
---|
875 | int Nvar, scfmon w) |
---|
876 | { |
---|
877 | int j0 = 0, j = 0, i = a2, k, k1; |
---|
878 | scmon n, o; |
---|
879 | if (!e1) |
---|
880 | { |
---|
881 | for (; i < e2; i++) |
---|
882 | rad[i - a2] = rad[i]; |
---|
883 | return; |
---|
884 | } |
---|
885 | else if (i == e2) |
---|
886 | return; |
---|
887 | n = rad[j]; |
---|
888 | o = rad[i]; |
---|
889 | loop |
---|
890 | { |
---|
891 | k = Nvar; |
---|
892 | loop |
---|
893 | { |
---|
894 | k1 = var[k]; |
---|
895 | if (!o[k1] && n[k1]) |
---|
896 | { |
---|
897 | w[j0] = o; |
---|
898 | j0++; |
---|
899 | i++; |
---|
900 | if (i < e2) |
---|
901 | { |
---|
902 | o = rad[i]; |
---|
903 | break; |
---|
904 | } |
---|
905 | else |
---|
906 | { |
---|
907 | for (; j < e1; j++) |
---|
908 | { |
---|
909 | w[j0] = rad[j]; |
---|
910 | j0++; |
---|
911 | } |
---|
912 | memcpy(rad, w, (e1 + e2 - a2) * sizeof(scmon)); |
---|
913 | return; |
---|
914 | } |
---|
915 | } |
---|
916 | else if (o[k1] && !n[k1]) |
---|
917 | { |
---|
918 | w[j0] = n; |
---|
919 | j0++; |
---|
920 | j++; |
---|
921 | if (j < e1) |
---|
922 | { |
---|
923 | n = rad[j]; |
---|
924 | break; |
---|
925 | } |
---|
926 | else |
---|
927 | { |
---|
928 | for (; i < e2; i++) |
---|
929 | { |
---|
930 | w[j0] = rad[i]; |
---|
931 | j0++; |
---|
932 | } |
---|
933 | memcpy(rad, w, (e1 + e2 - a2) * sizeof(scmon)); |
---|
934 | return; |
---|
935 | } |
---|
936 | } |
---|
937 | k--; |
---|
938 | } |
---|
939 | } |
---|
940 | } |
---|
941 | |
---|
942 | |
---|
943 | void hStepS(scfmon stc, int Nstc, varset var, int Nvar, int *a, int *x) |
---|
944 | { |
---|
945 | int k1, i; |
---|
946 | int y; |
---|
947 | k1 = var[Nvar]; |
---|
948 | y = *x; |
---|
949 | i = *a; |
---|
950 | loop |
---|
951 | { |
---|
952 | if (y < stc[i][k1]) |
---|
953 | { |
---|
954 | *a = i; |
---|
955 | *x = stc[i][k1]; |
---|
956 | return; |
---|
957 | } |
---|
958 | i++; |
---|
959 | if (i == Nstc) |
---|
960 | { |
---|
961 | *a = i; |
---|
962 | return; |
---|
963 | } |
---|
964 | } |
---|
965 | } |
---|
966 | |
---|
967 | |
---|
968 | void hStepR(scfmon rad, int Nrad, varset var, int Nvar, int *a) |
---|
969 | { |
---|
970 | int k1, i; |
---|
971 | k1 = var[Nvar]; |
---|
972 | i = 0; |
---|
973 | loop |
---|
974 | { |
---|
975 | if (rad[i][k1]) |
---|
976 | { |
---|
977 | *a = i; |
---|
978 | return; |
---|
979 | } |
---|
980 | i++; |
---|
981 | if (i == Nrad) |
---|
982 | { |
---|
983 | *a = i; |
---|
984 | return; |
---|
985 | } |
---|
986 | } |
---|
987 | } |
---|
988 | |
---|
989 | |
---|
990 | monf hCreate(int Nvar) |
---|
991 | { |
---|
992 | monf xmem; |
---|
993 | int i; |
---|
994 | xmem = (monf)omAlloc((Nvar + 1) * sizeof(monp)); |
---|
995 | for (i = Nvar; i>0; i--) |
---|
996 | { |
---|
997 | xmem[i] = (monp)omAlloc(LEN_MON); |
---|
998 | xmem[i]->mo = NULL; |
---|
999 | } |
---|
1000 | return xmem; |
---|
1001 | } |
---|
1002 | |
---|
1003 | |
---|
1004 | void hKill(monf xmem, int Nvar) |
---|
1005 | { |
---|
1006 | int i; |
---|
1007 | for (i = Nvar; i!=0; i--) |
---|
1008 | { |
---|
1009 | if (xmem[i]->mo!=NULL) |
---|
1010 | omFreeSize((ADDRESS)xmem[i]->mo, xmem[i]->a * sizeof(scmon)); |
---|
1011 | omFreeSize((ADDRESS)xmem[i], LEN_MON); |
---|
1012 | } |
---|
1013 | omFreeSize((ADDRESS)xmem, (Nvar + 1) * sizeof(monp)); |
---|
1014 | } |
---|
1015 | |
---|
1016 | |
---|
1017 | scfmon hGetmem(int lm, scfmon old, monp monmem) |
---|
1018 | { |
---|
1019 | scfmon x = monmem->mo; |
---|
1020 | int lx = monmem->a; |
---|
1021 | if ((x==NULL) || (lm > lx)) |
---|
1022 | { |
---|
1023 | if ((x!=NULL)&&(lx>0)) omFreeSize((ADDRESS)x, lx * sizeof(scmon)); |
---|
1024 | monmem->mo = x = (scfmon)omAlloc(lm * sizeof(scmon)); |
---|
1025 | monmem->a = lm; |
---|
1026 | } |
---|
1027 | memcpy(x, old, lm * sizeof(scmon)); |
---|
1028 | return x; |
---|
1029 | } |
---|
1030 | |
---|
1031 | /* |
---|
1032 | * a bug in Metrowerks with "lifetime analysis" |
---|
1033 | *scmon hGetpure(scmon p) |
---|
1034 | *{ |
---|
1035 | * scmon p1, pn; |
---|
1036 | * p1 = p + 1; |
---|
1037 | * pn = p1 + pVariables; |
---|
1038 | * memcpy(pn, p1, pVariables * sizeof(int)); |
---|
1039 | * return pn - 1; |
---|
1040 | *} |
---|
1041 | */ |
---|
1042 | scmon hGetpure(scmon p) |
---|
1043 | { |
---|
1044 | scmon p1 = p; |
---|
1045 | scmon pn; |
---|
1046 | p1++; |
---|
1047 | pn = p1; |
---|
1048 | pn += pVariables; |
---|
1049 | memcpy(pn, p1, pVariables * sizeof(int)); |
---|
1050 | return pn - 1; |
---|
1051 | } |
---|
1052 | |
---|