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 = NULL, // 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 | tc; // used as intermediate number |
---|
41 | |
---|
42 | if (a2==NULL) goto Finish; // we are done if a2 is 0 |
---|
43 | |
---|
44 | b = pNew(); |
---|
45 | pCopyAddFast0(b, a1, monom); // now a2 != NULL -- set up b |
---|
46 | |
---|
47 | // MAIN LOOP: |
---|
48 | Top: // compare b = monom*a1 and a2 w.r.t. monomial ordering |
---|
49 | register long d; |
---|
50 | if ((d = pComp0(b, a2))) goto NotEqual; else goto Equal;; |
---|
51 | |
---|
52 | Equal: // b equals a2 |
---|
53 | tb = nMult(pGetCoeff(a1), tm); |
---|
54 | tc = pGetCoeff(a2); |
---|
55 | if (!nEqual(tc, tb)) |
---|
56 | { |
---|
57 | tc = nSub(tc, tb); |
---|
58 | pSetCoeff(a2,tc); // adjust coeff of a2 |
---|
59 | a = pNext(a) = a2; // append a2 to result and advance a2 |
---|
60 | pIter(a2); |
---|
61 | } |
---|
62 | else |
---|
63 | { // coeffs are equal, so their difference is 0: |
---|
64 | c = a2; // do not append anything to result: Delete a2 and advance |
---|
65 | pIter(a2); |
---|
66 | nDelete(&tc); |
---|
67 | pFree1(c); |
---|
68 | } |
---|
69 | nDelete(&tb); |
---|
70 | pIter(a1); |
---|
71 | if (a1 == NULL || a2 == NULL) goto Finish; // are we done ? |
---|
72 | pCopyAddFast0(b, a1, monom); // No! So, get new b = a1*monom |
---|
73 | goto Top; |
---|
74 | |
---|
75 | NotEqual: // b != a2 |
---|
76 | if (d < 0) // b < a2: |
---|
77 | { |
---|
78 | a = pNext(a) = a2;// append a2 to result and advance a2 |
---|
79 | pIter(a2); |
---|
80 | if (a2==NULL) goto Finish;; |
---|
81 | goto Top; |
---|
82 | } |
---|
83 | else // now d >= 0, i.e., b > a2 |
---|
84 | { |
---|
85 | pSetCoeff0(b,nMult(pGetCoeff(a1), tneg)); |
---|
86 | a = pNext(a) = b; // append b to result and advance a1 |
---|
87 | pIter(a1); |
---|
88 | if (a1 == NULL) // are we done? |
---|
89 | { |
---|
90 | b = NULL; |
---|
91 | goto Finish; |
---|
92 | } |
---|
93 | b = pNew(); |
---|
94 | pCopyAddFast0(b, a1, monom); // No! So, update b = a1*monom |
---|
95 | goto Top; |
---|
96 | } |
---|
97 | |
---|
98 | Finish: // a1 or a2 is NULL: Clean-up time |
---|
99 | if (a1 == NULL) // append rest of a2 to result |
---|
100 | pNext(a) = a2; |
---|
101 | else // append (- a1*monom) to result |
---|
102 | spGMultCopyX(a1, monom, a, tneg, spNoether); |
---|
103 | nDelete(&tneg); |
---|
104 | if (b != NULL) pFree1(b); |
---|
105 | } |
---|
106 | |
---|
107 | /*************************************************************** |
---|
108 | * |
---|
109 | * Fst spoly loops |
---|
110 | * |
---|
111 | ***************************************************************/ |
---|
112 | #ifdef FAST_SPOLY_LOOP |
---|
113 | |
---|
114 | #define NonZeroA(d, multiplier, actionE) \ |
---|
115 | { \ |
---|
116 | d ^= multiplier; \ |
---|
117 | actionE; \ |
---|
118 | } \ |
---|
119 | |
---|
120 | #define NonZeroTestA(d, multiplier, actionE) \ |
---|
121 | do \ |
---|
122 | { \ |
---|
123 | if (d) \ |
---|
124 | { \ |
---|
125 | d ^= multiplier; \ |
---|
126 | actionE; \ |
---|
127 | } \ |
---|
128 | } \ |
---|
129 | while(0) |
---|
130 | #include "spSpolyLoop.inc" // the loops and spGetSpolyLoop |
---|
131 | |
---|
132 | #endif // FAST_SPOLY_LOOP |
---|
133 | |
---|
134 | |
---|
135 | spSpolyLoopProc spGetSpolyLoop(ring r, rOrderType_t rot, BOOLEAN homog) |
---|
136 | { |
---|
137 | #ifdef FAST_SPOLY_LOOP |
---|
138 | Characteristics ch = chGEN; |
---|
139 | OrderingTypes ot = otGEN; |
---|
140 | Homogs hom = homGEN; |
---|
141 | NumWords nw = nwGEN; |
---|
142 | spSpolyLoopProc spolyloop; |
---|
143 | int Variables1W; |
---|
144 | |
---|
145 | // set characterisic |
---|
146 | if (rField_is_Zp(r)) ch = chMODP; |
---|
147 | |
---|
148 | // set Ordering Type |
---|
149 | switch (rot) |
---|
150 | { |
---|
151 | case rOrderType_Exp: |
---|
152 | ot = otEXP; |
---|
153 | break; |
---|
154 | |
---|
155 | case rOrderType_CompExp: |
---|
156 | ot = otCOMPEXP; |
---|
157 | break; |
---|
158 | |
---|
159 | case rOrderType_ExpComp: |
---|
160 | ot = otEXPCOMP; |
---|
161 | break; |
---|
162 | |
---|
163 | default: |
---|
164 | ot = otGEN; |
---|
165 | break; |
---|
166 | } |
---|
167 | |
---|
168 | // set homogenous |
---|
169 | if (homog) hom = homYES; |
---|
170 | |
---|
171 | // set NumWords |
---|
172 | if ((((r->N+1)*sizeof(Exponent_t)) % sizeof(void*)) == 0) |
---|
173 | Variables1W = (r->N+1)*sizeof(Exponent_t) / sizeof(void*); |
---|
174 | else |
---|
175 | Variables1W = ((r->N+1)*sizeof(Exponent_t) / sizeof(void*)) + 1; |
---|
176 | if (Variables1W > 2) |
---|
177 | { |
---|
178 | if (Variables1W & 1) nw = nwODD; |
---|
179 | else nw = nwEVEN; |
---|
180 | } |
---|
181 | else |
---|
182 | { |
---|
183 | if (Variables1W == 2) nw = nwTWO; |
---|
184 | else nw = nwONE; |
---|
185 | } |
---|
186 | |
---|
187 | // GetSpolyLoop |
---|
188 | spolyloop = spGetSpolyLoop(ch, ot, hom, nw); |
---|
189 | if (spolyloop != NULL) return spolyloop; |
---|
190 | // still here? -- no special spolyloop found, return the general loop |
---|
191 | #endif // FAST_SPOLY_LOOP |
---|
192 | return spSpolyLoop_General; |
---|
193 | } |
---|
194 | |
---|
195 | |
---|
196 | |
---|