1 | #include "mod2.h" |
---|
2 | #include "polys.h" |
---|
3 | #include "tok.h" |
---|
4 | #include "kstd1.h" |
---|
5 | #include "numbers.h" |
---|
6 | #include "ipid.h" |
---|
7 | #include "febase.h" |
---|
8 | #include "spolys0.h" |
---|
9 | #include "spolys.h" |
---|
10 | #include "modulop.h" |
---|
11 | #include "polys-comp.h" |
---|
12 | #include "kutil.h" |
---|
13 | #include "ring.h" |
---|
14 | #include "spSpolyLoop.h" |
---|
15 | |
---|
16 | |
---|
17 | #ifndef DO_DEEP_PROFILE |
---|
18 | // undefine to disable fast spoly loops |
---|
19 | #define FAST_SPOLY_LOOP |
---|
20 | #endif |
---|
21 | |
---|
22 | /*************************************************************** |
---|
23 | * |
---|
24 | * General spoly loop which always works |
---|
25 | * |
---|
26 | * assume monom = L(monom) |
---|
27 | * pNext(monom) = result = a2-a1*monom |
---|
28 | * do not destroy a1, but a2 |
---|
29 | * |
---|
30 | ***************************************************************/ |
---|
31 | void spSpolyLoop_General |
---|
32 | (poly a1, poly a2, poly monom, poly spNoether) |
---|
33 | { |
---|
34 | poly a = monom, // collects the result |
---|
35 | b = pNew(), // stores a1*monom |
---|
36 | c; // used for temporary storage |
---|
37 | number tm = pGetCoeff(monom), // coefficient of monom |
---|
38 | tneg = nNeg(nCopy(tm)), // - (coefficient of monom) |
---|
39 | tb; // used for tm*coeff(a1) |
---|
40 | |
---|
41 | |
---|
42 | if (a2==NULL) goto Finish; // we are done if a2 is 0 |
---|
43 | |
---|
44 | pCopyAddFast0(b, a1, monom); // now a2 != NULL -- set up b |
---|
45 | |
---|
46 | // MAIN LOOP: |
---|
47 | Top: // compare b = monom*a1 and a2 w.r.t. monomial ordering |
---|
48 | register long d; |
---|
49 | if ((d = pComp0(b, a2))) goto NotEqual; else goto Equal;; |
---|
50 | |
---|
51 | Equal: // b equals a2 |
---|
52 | tb = nMult(pGetCoeff(a1), tm); |
---|
53 | if (!nEqual(pGetCoeff(a2), tb)) |
---|
54 | { |
---|
55 | pSetCoeff0(a2,nSub(pGetCoeff(a2), tb)); // adjust coeff of a2 |
---|
56 | a = pNext(a) = a2; // append a2 to result and advance a2 |
---|
57 | pIter(a2); |
---|
58 | } |
---|
59 | else |
---|
60 | { // coeffs are equal, so their difference is 0: |
---|
61 | c = a2; // do not append anything to result: Delete a2 and advance |
---|
62 | pIter(a2); |
---|
63 | nDelete(&pGetCoeff(c)); |
---|
64 | pFree1(c); |
---|
65 | } |
---|
66 | nDelete(&tb); |
---|
67 | pIter(a1); |
---|
68 | if (a2 == NULL || a1 == NULL) goto Finish; // are we done ? |
---|
69 | pCopyAddFast0(b, a1, monom); // No! So, get new b = a1*monom |
---|
70 | goto Top; |
---|
71 | |
---|
72 | NotEqual: // b != a2 |
---|
73 | if (d < 0) // b < a2: |
---|
74 | { |
---|
75 | a = pNext(a) = a2;// append a2 to result and advance a2 |
---|
76 | pIter(a2); |
---|
77 | if (a2==NULL) goto Finish;; |
---|
78 | goto Top; |
---|
79 | } |
---|
80 | else // now d >= 0, i.e., b > a2 |
---|
81 | { |
---|
82 | pSetCoeff0(b,nMult(pGetCoeff(a1), tneg)); |
---|
83 | a = pNext(a) = b; // append b to result and advance a1 |
---|
84 | pIter(a1); |
---|
85 | b = pNew(); |
---|
86 | if (a1 == NULL) goto Finish; // are we done? |
---|
87 | pCopyAddFast0(b, a1, monom); // No! So, update b = a1*monom |
---|
88 | goto Top; |
---|
89 | } |
---|
90 | |
---|
91 | Finish: // a1 or a2 is NULL: Clean-up time |
---|
92 | if (a1 == NULL) // append rest of a2 to result |
---|
93 | pNext(a) = a2; |
---|
94 | else // append (- a1*monom) to result |
---|
95 | spGMultCopyX(a1, monom, a, tneg, spNoether); |
---|
96 | nDelete(&tneg); |
---|
97 | pFree1(b); |
---|
98 | } |
---|
99 | |
---|
100 | |
---|
101 | /*************************************************************** |
---|
102 | * |
---|
103 | * Fst spoly loops |
---|
104 | * |
---|
105 | ***************************************************************/ |
---|
106 | #ifdef FAST_SPOLY_LOOP |
---|
107 | |
---|
108 | #define NonZeroA(d, multiplier, actionE) \ |
---|
109 | { \ |
---|
110 | d ^= multiplier; \ |
---|
111 | actionE; \ |
---|
112 | } \ |
---|
113 | |
---|
114 | #define NonZeroTestA(d, multiplier, actionE) \ |
---|
115 | do \ |
---|
116 | { \ |
---|
117 | if (d) \ |
---|
118 | { \ |
---|
119 | d ^= multiplier; \ |
---|
120 | actionE; \ |
---|
121 | } \ |
---|
122 | } \ |
---|
123 | while(0) |
---|
124 | #include "spSpolyLoop.inc" // the loops and spGetSpolyLoop |
---|
125 | |
---|
126 | #endif // FAST_SPOLY_LOOP |
---|
127 | |
---|
128 | |
---|
129 | spSpolyLoopProc spGetSpolyLoop(ring r, rOrderType_t rot, BOOLEAN homog) |
---|
130 | { |
---|
131 | #ifdef FAST_SPOLY_LOOP |
---|
132 | Characteristics ch = chGEN; |
---|
133 | OrderingTypes ot = otGEN; |
---|
134 | Homogs hom = homGEN; |
---|
135 | NumWords nw = nwGEN; |
---|
136 | spSpolyLoopProc spolyloop; |
---|
137 | int Variables1W; |
---|
138 | |
---|
139 | // set characterisic |
---|
140 | if (r->ch > 0) ch = chMODP; |
---|
141 | |
---|
142 | // set Ordering Type |
---|
143 | switch (rot) |
---|
144 | { |
---|
145 | case rOrderType_Exp: |
---|
146 | ot = otEXP; |
---|
147 | break; |
---|
148 | |
---|
149 | case rOrderType_CompExp: |
---|
150 | ot = otCOMPEXP; |
---|
151 | break; |
---|
152 | |
---|
153 | case rOrderType_ExpComp: |
---|
154 | ot = otEXPCOMP; |
---|
155 | break; |
---|
156 | |
---|
157 | default: |
---|
158 | ot = otGEN; |
---|
159 | break; |
---|
160 | } |
---|
161 | |
---|
162 | // set homogenous |
---|
163 | if (homog) hom = homYES; |
---|
164 | |
---|
165 | // set NumWords |
---|
166 | if ((((r->N+1)*sizeof(Exponent_t)) % sizeof(void*)) == 0) |
---|
167 | Variables1W = (r->N+1)*sizeof(Exponent_t) / sizeof(void*); |
---|
168 | else |
---|
169 | Variables1W = ((r->N+1)*sizeof(Exponent_t) / sizeof(void*)) + 1; |
---|
170 | if (Variables1W > 2) |
---|
171 | { |
---|
172 | if (Variables1W & 1) nw = nwODD; |
---|
173 | else nw = nwEVEN; |
---|
174 | } |
---|
175 | else |
---|
176 | { |
---|
177 | if (Variables1W == 2) nw = nwTWO; |
---|
178 | else nw = nwONE; |
---|
179 | } |
---|
180 | |
---|
181 | // GetSpolyLoop |
---|
182 | spolyloop = spGetSpolyLoop(ch, ot, hom, nw); |
---|
183 | if (spolyloop != NULL) return spolyloop; |
---|
184 | // still here? -- no special spolyloop found, return the general loop |
---|
185 | #endif // FAST_SPOLY_LOOP |
---|
186 | return spSpolyLoop_General; |
---|
187 | } |
---|
188 | |
---|
189 | |
---|
190 | |
---|