1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /*************************************************************** |
---|
5 | * File: pInline1.h |
---|
6 | * Purpose: implementation of poly procs which iter over ExpVector |
---|
7 | * Author: obachman (Olaf Bachmann) |
---|
8 | * Created: 8/00 |
---|
9 | * Version: $Id: pInline1.h,v 1.1 2000-09-12 16:01:04 obachman Exp $ |
---|
10 | *******************************************************************/ |
---|
11 | #ifndef PINLINE1_H |
---|
12 | #define PINLINE1_H |
---|
13 | |
---|
14 | // define to enable debugging/statistics of pShortDivisibleBy |
---|
15 | #undef PDIV_DEBUG |
---|
16 | |
---|
17 | #include "mod2.h" |
---|
18 | #include "structs.h" |
---|
19 | #include "p_MemCmp.h" |
---|
20 | #include "polys-impl.h" |
---|
21 | |
---|
22 | #if PDEBUG > 0 || defined(NO_PINLINE1) |
---|
23 | |
---|
24 | #define _p_LmCmpAction(p, q, r, actionE, actionG, actionS) \ |
---|
25 | do \ |
---|
26 | { \ |
---|
27 | int _cmp = p_LmCmp(p,q,r); \ |
---|
28 | if (_cmp == 0) actionE; \ |
---|
29 | if (_cmp == 1) actionG; \ |
---|
30 | actionS; \ |
---|
31 | } \ |
---|
32 | while(0) |
---|
33 | |
---|
34 | #else |
---|
35 | |
---|
36 | #define _p_LmCmpAction(p, q, r, actionE, actionG, actionS) \ |
---|
37 | p_MemCmp_LengthGeneral_OrdGeneral(p->exp, q->exp, r->pCompLSize, r->ordsgn, \ |
---|
38 | actionE, actionG, actionS) |
---|
39 | |
---|
40 | #endif |
---|
41 | |
---|
42 | #ifdef PDIV_DEBUG |
---|
43 | BOOLEAN pDebugShortDivisibleBy(poly p1, unsigned long sev_1, ring r_1, |
---|
44 | poly p2, unsigned long not_sev_2, ring r_2); |
---|
45 | #endif |
---|
46 | |
---|
47 | #if !defined(NO_PINLINE1) || defined(PINLINE1_CC) |
---|
48 | |
---|
49 | #include "omalloc.h" |
---|
50 | #include "numbers.h" |
---|
51 | #include "pInline2.h" |
---|
52 | #include "p_MemAdd.h" |
---|
53 | #include "p_MemCopy.h" |
---|
54 | #include "polys.h" |
---|
55 | |
---|
56 | /*************************************************************** |
---|
57 | * |
---|
58 | * Allocation/Initalization/Deletion |
---|
59 | * |
---|
60 | ***************************************************************/ |
---|
61 | PINLINE1 poly p_Init(ring r) |
---|
62 | { |
---|
63 | p_CheckRing(r); |
---|
64 | poly p; |
---|
65 | omTypeAlloc0Bin(poly, p, r->PolyBin); |
---|
66 | p_SetRingOfPoly(p, r); |
---|
67 | return p; |
---|
68 | } |
---|
69 | PINLINE1 poly p_Init(poly p, ring r) |
---|
70 | { |
---|
71 | p_CheckPolyRing(p, r); |
---|
72 | poly np; |
---|
73 | omTypeAllocBin(poly, np, r->PolyBin); |
---|
74 | p_ExpVectorCopy(np, p, r); |
---|
75 | _pNext(np) = NULL; |
---|
76 | _pSetCoeff0(np, NULL); |
---|
77 | return np; |
---|
78 | } |
---|
79 | PINLINE1 poly pInit() |
---|
80 | { |
---|
81 | return p_Init(currRing); |
---|
82 | } |
---|
83 | PINLINE1 poly pInit(poly p) |
---|
84 | { |
---|
85 | return p_Init(p, currRing); |
---|
86 | } |
---|
87 | PINLINE1 poly p_Head(poly p, ring r) |
---|
88 | { |
---|
89 | if (p == NULL) return NULL; |
---|
90 | p_CheckPolyRing(p, r); |
---|
91 | poly np; |
---|
92 | omTypeAllocBin(poly, np, r->PolyBin); |
---|
93 | p_ExpVectorCopy(np, p, r); |
---|
94 | _pNext(np) = NULL; |
---|
95 | _pSetCoeff0(np, p_nCopy(_pGetCoeff(p), r)); |
---|
96 | return np; |
---|
97 | } |
---|
98 | |
---|
99 | /*************************************************************** |
---|
100 | * |
---|
101 | * Operation on ExpVectors |
---|
102 | * |
---|
103 | ***************************************************************/ |
---|
104 | // ExpVextor(d_p) = ExpVector(s_p) |
---|
105 | PINLINE1 void p_ExpVectorCopy(poly d_p, poly s_p, ring r) |
---|
106 | { |
---|
107 | p_CheckPolyRing(d_p, r); |
---|
108 | p_CheckPolyRing(s_p, r); |
---|
109 | p_MemCopy_LengthGeneral(d_p->exp, s_p->exp, r->ExpLSize); |
---|
110 | } |
---|
111 | // ExpVector(p1) += ExpVector(p2) |
---|
112 | PINLINE1 void p_ExpVectorAdd(poly p1, poly p2, ring r) |
---|
113 | { |
---|
114 | p_CheckPolyRing(p1, r); |
---|
115 | p_CheckPolyRing(p2, r); |
---|
116 | #if PDEBUG >= 2 |
---|
117 | for (int i=1; i<=r->N; i++) |
---|
118 | passume2((unsigned long) (p_GetExp(p1, i, r) + p_GetExp(p2, i, r)) <= r->bitmask); |
---|
119 | passume2(!rRing_has_Comp(r) || p_GetComp(p1, r) == 0 || p_GetComp(p2, r) == 0); |
---|
120 | #endif |
---|
121 | |
---|
122 | p_MemAdd_LengthGeneral(p1->exp, p2->exp, r->ExpLSize); |
---|
123 | } |
---|
124 | // ExpVector(p1) -= ExpVector(p2) |
---|
125 | PINLINE1 void p_ExpVectorSub(poly p1, poly p2, ring r) |
---|
126 | { |
---|
127 | p_CheckPolyRing(p1, r); |
---|
128 | p_CheckPolyRing(p2, r); |
---|
129 | #if PDEBUG >= 2 |
---|
130 | for (int i=1; i<=r->N; i++) |
---|
131 | passume2(p_GetExp(p1, i, r) >= p_GetExp(p2, i, r)); |
---|
132 | passume2(!rRing_has_Comp(r) || p_GetComp(p1, r) == p_GetComp(p2, r)); |
---|
133 | #endif |
---|
134 | |
---|
135 | p_MemSub_LengthGeneral(p1->exp, p2->exp, r->ExpLSize); |
---|
136 | } |
---|
137 | // ExpVector(pr) = ExpVector(p1) + ExpVector(p2) |
---|
138 | PINLINE1 void p_ExpVectorSum(poly pr, poly p1, poly p2, ring r) |
---|
139 | { |
---|
140 | p_CheckPolyRing(p1, r); |
---|
141 | p_CheckPolyRing(p2, r); |
---|
142 | p_CheckPolyRing(pr, r); |
---|
143 | #if PDEBUG >= 2 |
---|
144 | for (int i=1; i<=r->N; i++) |
---|
145 | passume2((unsigned long) (p_GetExp(p1, i, r) + p_GetExp(p2, i, r)) <= r->bitmask); |
---|
146 | passume2(!rRing_has_Comp(r) || p_GetComp(p1, r) == 0 || p_GetComp(p2, r) == 0); |
---|
147 | #endif |
---|
148 | |
---|
149 | p_MemSum_LengthGeneral(pr->exp, p1->exp, p2->exp, r->ExpLSize); |
---|
150 | } |
---|
151 | // ExpVector(pr) = ExpVector(p1) - ExpVector(p2) |
---|
152 | PINLINE1 void p_ExpVectorDiff(poly pr, poly p1, poly p2, ring r) |
---|
153 | { |
---|
154 | p_CheckPolyRing(p1, r); |
---|
155 | p_CheckPolyRing(p2, r); |
---|
156 | p_CheckPolyRing(pr, r); |
---|
157 | #if PDEBUG >= 2 |
---|
158 | for (int i=1; i<=r->N; i++) |
---|
159 | passume2(p_GetExp(p1, i, r) >= p_GetExp(p2, i, r)); |
---|
160 | passume2(!rRing_has_Comp(r) || p_GetComp(p1, r) == p_GetComp(p2, r)); |
---|
161 | #endif |
---|
162 | |
---|
163 | p_MemDiff_LengthGeneral(pr->exp, p1->exp, p2->exp, r->ExpLSize); |
---|
164 | } |
---|
165 | |
---|
166 | PINLINE1 BOOLEAN p_ExpVectorEqual(poly p1, poly p2, ring r) |
---|
167 | { |
---|
168 | p_CheckPolyRing(p1, r); |
---|
169 | p_CheckPolyRing(p2, r); |
---|
170 | |
---|
171 | int i = r->ExpLSize; |
---|
172 | unsigned long *ep = p1->exp; |
---|
173 | unsigned long *eq = p2->exp; |
---|
174 | |
---|
175 | do |
---|
176 | { |
---|
177 | i--; |
---|
178 | if (ep[i] != eq[i]) return FALSE; |
---|
179 | } |
---|
180 | while (i); |
---|
181 | return TRUE; |
---|
182 | } |
---|
183 | |
---|
184 | PINLINE1 unsigned long p_ExpVectorQuerSum(poly p, ring r) |
---|
185 | { |
---|
186 | p_CheckPolyRing(p, r); |
---|
187 | unsigned long s = 0; |
---|
188 | unsigned long i = r->N; |
---|
189 | |
---|
190 | do |
---|
191 | { |
---|
192 | s += p_GetExp(p, i, r); |
---|
193 | i--; |
---|
194 | } |
---|
195 | while (i); |
---|
196 | return s; |
---|
197 | } |
---|
198 | |
---|
199 | PINLINE1 void p_GetExpV(poly p, Exponent_t *ev, ring r) |
---|
200 | { |
---|
201 | p_CheckPolyRing(p, r); |
---|
202 | for (int j = r->N; j; j--) |
---|
203 | ev[j] = p_GetExp(p, j, r); |
---|
204 | |
---|
205 | ev[0] = _p_GetComp(p, r); |
---|
206 | } |
---|
207 | PINLINE1 void p_SetExpV(poly p, Exponent_t *ev, ring r) |
---|
208 | { |
---|
209 | p_CheckPolyRing(p, r); |
---|
210 | for (int j = r->N; j; j--) |
---|
211 | p_SetExp(p, j, ev[j], r); |
---|
212 | |
---|
213 | p_SetComp(p, ev[0],r); |
---|
214 | p_Setm(p, r); |
---|
215 | } |
---|
216 | |
---|
217 | /*************************************************************** |
---|
218 | * |
---|
219 | * Comparison w.r.t. monomial ordering |
---|
220 | * |
---|
221 | ***************************************************************/ |
---|
222 | PINLINE1 int p_LmCmp(poly p, poly q, ring r) |
---|
223 | { |
---|
224 | p_CheckPolyRing(p, r); |
---|
225 | p_CheckPolyRing(q, r); |
---|
226 | |
---|
227 | p_MemCmp_LengthGeneral_OrdGeneral(p->exp, q->exp, r->pCompLSize, r->ordsgn, |
---|
228 | return 0, return 1, return -1); |
---|
229 | } |
---|
230 | |
---|
231 | PINLINE1 BOOLEAN p_LmEqual(poly p, poly q, ring r) |
---|
232 | { |
---|
233 | p_CheckPolyRing(p, r); |
---|
234 | p_CheckPolyRing(q, r); |
---|
235 | int i = r->ExpLSize; |
---|
236 | unsigned long *ep = p->exp; |
---|
237 | unsigned long *eq = q->exp; |
---|
238 | |
---|
239 | do |
---|
240 | { |
---|
241 | i--; |
---|
242 | if (ep[i] != eq[i]) return FALSE; |
---|
243 | } |
---|
244 | while (i); |
---|
245 | return p_nEqual(pGetCoeff(p), pGetCoeff(q), r); |
---|
246 | } |
---|
247 | |
---|
248 | /*************************************************************** |
---|
249 | * |
---|
250 | * divisibility |
---|
251 | * |
---|
252 | ***************************************************************/ |
---|
253 | static inline BOOLEAN _p_DivisibleBy2(poly a, poly b, ring r) |
---|
254 | { |
---|
255 | int i=r->N; |
---|
256 | |
---|
257 | do |
---|
258 | { |
---|
259 | if (p_GetExp(a,i,r) > p_GetExp(b,i,r)) |
---|
260 | return FALSE; |
---|
261 | i--; |
---|
262 | } |
---|
263 | while (i); |
---|
264 | return TRUE; |
---|
265 | } |
---|
266 | static inline BOOLEAN _p_DivisibleBy2(poly a, ring r_a, poly b, ring r_b) |
---|
267 | { |
---|
268 | int i=r_a->N; |
---|
269 | passume1(r_a->N == r_b->N); |
---|
270 | |
---|
271 | do |
---|
272 | { |
---|
273 | if (p_GetExp(a,i,r_a) > p_GetExp(b,i,r_b)) |
---|
274 | return FALSE; |
---|
275 | i--; |
---|
276 | } |
---|
277 | while (i); |
---|
278 | return TRUE; |
---|
279 | } |
---|
280 | PINLINE1 BOOLEAN p_DivisibleBy2(poly a, poly b, ring r) |
---|
281 | { |
---|
282 | p_CheckPolyRing(a, r); |
---|
283 | p_CheckPolyRing(b, r); |
---|
284 | return _p_DivisibleBy2(a, b, r); |
---|
285 | } |
---|
286 | PINLINE1 BOOLEAN p_DivisibleBy1(poly a, poly b, ring r) |
---|
287 | { |
---|
288 | p_CheckPolyRing(b, r); |
---|
289 | p_CheckIf(a != NULL, p_CheckPolyRing(b, r)); |
---|
290 | if (p_GetComp(a, r) == 0 || p_GetComp(a,r) == p_GetComp(b,r)) |
---|
291 | return _p_DivisibleBy2(a, b, r); |
---|
292 | return FALSE; |
---|
293 | } |
---|
294 | PINLINE1 BOOLEAN p_DivisibleBy(poly a, poly b, ring r) |
---|
295 | { |
---|
296 | p_CheckPolyRing(b, r); |
---|
297 | p_CheckIf(a!=NULL, p_CheckPolyRing(a, r)); |
---|
298 | |
---|
299 | if (a != NULL && (p_GetComp(a, r) == 0 || p_GetComp(a,r) == p_GetComp(b,r))) |
---|
300 | return _p_DivisibleBy2(a,b,r); |
---|
301 | return FALSE; |
---|
302 | } |
---|
303 | |
---|
304 | PINLINE1 BOOLEAN p_ShortDivisibleBy(poly a, unsigned long sev_a, |
---|
305 | poly b, unsigned long not_sev_b, ring r) |
---|
306 | { |
---|
307 | p_CheckPolyRing(a, r); |
---|
308 | p_CheckPolyRing(b, r); |
---|
309 | #ifndef PDIV_DEBUG |
---|
310 | passume2(p_GetShortExpVector(a, r) == sev_a); |
---|
311 | passume2(p_GetShortExpVector(b, r) == ~ not_sev_b); |
---|
312 | |
---|
313 | if (sev_a & not_sev_b) |
---|
314 | { |
---|
315 | passume2(p_DivisibleBy2(a, b, r) == FALSE); |
---|
316 | return FALSE; |
---|
317 | } |
---|
318 | return p_DivisibleBy1(a, b, r); |
---|
319 | #else |
---|
320 | return pDebugShortDivisibleBy(a, sev_a, r, b, not_sev_b, r); |
---|
321 | #endif |
---|
322 | } |
---|
323 | |
---|
324 | #endif // !defined(NO_PINLINE1) || defined(PINLINE1_CC) |
---|
325 | #endif // PINLINE1_CC |
---|
326 | |
---|
327 | #if 0 |
---|
328 | #include <stdio.h> |
---|
329 | |
---|
330 | const unsigned long mask = 0x01010101; |
---|
331 | int old_test(unsigned long l1,unsigned long l2) |
---|
332 | { |
---|
333 | unsigned char* c1 = &l1; |
---|
334 | unsigned char* c2 = &l2; |
---|
335 | |
---|
336 | if (c1[0] < c2[0] || c1[1] < c2[1] || c1[2] < c2[2] || c1[3] < c2[3]) |
---|
337 | return 0; |
---|
338 | return 1; |
---|
339 | } |
---|
340 | |
---|
341 | int new_test(unsigned long l1,unsigned long l2) |
---|
342 | { |
---|
343 | |
---|
344 | if ( (l2 > l1) || ((l1 & mask) ^ (l2 & mask)) != ((l1 - l2) & mask)) |
---|
345 | return 0; |
---|
346 | return 1; |
---|
347 | } |
---|
348 | |
---|
349 | print_it(unsigned long l1,unsigned long l2) |
---|
350 | { |
---|
351 | printf("%d:%d\n", old_test(l1, l2), new_test(l1, l2)); |
---|
352 | fflush(NULL); |
---|
353 | } |
---|
354 | |
---|
355 | |
---|
356 | void main() |
---|
357 | { |
---|
358 | unsigned long mask = 0x01010101; |
---|
359 | unsigned long l1, l2; |
---|
360 | char *c1, *c2; |
---|
361 | unsigned long seed = time(NULL); |
---|
362 | |
---|
363 | for (;;) |
---|
364 | { |
---|
365 | l1 = random(); |
---|
366 | l2 = random(); |
---|
367 | |
---|
368 | if (old_test(l1,l2) != new_test(l1, l2)) |
---|
369 | { |
---|
370 | print_it(l1, l2); |
---|
371 | } |
---|
372 | } |
---|
373 | } |
---|
374 | |
---|
375 | #endif |
---|