1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id: prCopy.cc,v 1.4 2000-08-29 14:10:30 obachman Exp $ */ |
---|
5 | /* |
---|
6 | * ABSTRACT - implementation of functions for Copy/Move/Delete for Polys |
---|
7 | */ |
---|
8 | |
---|
9 | #include "mod2.h" |
---|
10 | #include <omalloc.h> |
---|
11 | #include "polys.h" |
---|
12 | #include "numbers.h" |
---|
13 | #include "tok.h" |
---|
14 | #include "ring.h" |
---|
15 | #include "ideals.h" |
---|
16 | #include "polys-comp.h" |
---|
17 | |
---|
18 | |
---|
19 | ///////////////////////////////////////////////////////////////////////// |
---|
20 | // prSort prMerge |
---|
21 | |
---|
22 | poly prMergeR(poly p1, poly p2, ring r) |
---|
23 | { |
---|
24 | if (p1 == NULL) return p2; |
---|
25 | if (p2 == NULL) return p1; |
---|
26 | |
---|
27 | spolyrec rp; |
---|
28 | poly p = &rp; |
---|
29 | |
---|
30 | Top: |
---|
31 | _prMonCmp(p1, p2, r, goto Equal, goto Greater, goto Smaller); |
---|
32 | |
---|
33 | Equal: |
---|
34 | // should never get here |
---|
35 | assume(0); |
---|
36 | |
---|
37 | Greater: |
---|
38 | pNext(p) = p1; |
---|
39 | if (pNext(p1) != NULL) |
---|
40 | { |
---|
41 | pIter(p); |
---|
42 | pIter(p1); |
---|
43 | goto Top; |
---|
44 | } |
---|
45 | pNext(p1) = p2; |
---|
46 | goto Finish; |
---|
47 | |
---|
48 | Smaller: |
---|
49 | pNext(p) = p2; |
---|
50 | if (pNext(p2) != NULL) |
---|
51 | { |
---|
52 | pIter(p); |
---|
53 | pIter(p2); |
---|
54 | goto Top; |
---|
55 | } |
---|
56 | pNext(p2) = p1; |
---|
57 | goto Finish; |
---|
58 | |
---|
59 | Finish: |
---|
60 | assume(prTest(pNext(&rp), r)); |
---|
61 | return pNext(&rp); |
---|
62 | } |
---|
63 | |
---|
64 | // Sorts poly, reverts it first -- this way, almost sorted polys's |
---|
65 | // don't have n^2 sorting property |
---|
66 | poly prSortR(poly p, ring r, BOOLEAN revert) |
---|
67 | { |
---|
68 | if (p == NULL) return NULL; |
---|
69 | poly qq, result = NULL; |
---|
70 | int *lp = NULL, lq = 0; |
---|
71 | |
---|
72 | if (revert) |
---|
73 | { |
---|
74 | // there must be a bug here: consider m1->m2 |
---|
75 | while (p != NULL) |
---|
76 | { |
---|
77 | qq = p; |
---|
78 | pIter(p); |
---|
79 | qq->next = result; |
---|
80 | result = qq; |
---|
81 | } |
---|
82 | p = result; |
---|
83 | } |
---|
84 | |
---|
85 | result = p; |
---|
86 | pIter(p); |
---|
87 | pNext(result) = NULL; |
---|
88 | while (p != NULL) |
---|
89 | { |
---|
90 | qq = p; |
---|
91 | pIter(p); |
---|
92 | qq->next = NULL; |
---|
93 | result = prMergeR(result, qq, r); |
---|
94 | } |
---|
95 | prTest(result, r); |
---|
96 | return result; |
---|
97 | } |
---|
98 | |
---|
99 | static inline void |
---|
100 | prCopyEvector(poly dest, ring dest_r, poly src, ring src_r,int max) |
---|
101 | { |
---|
102 | assume(dest_r == currRing); |
---|
103 | int i=0; |
---|
104 | for (i=max; i; i--) |
---|
105 | { |
---|
106 | pRingSetExp(dest_r, dest, i, pRingGetExp(src_r, src, i)); |
---|
107 | } |
---|
108 | pSetComp(dest, pRingGetComp(src_r, src)); |
---|
109 | pSetm(dest); |
---|
110 | } |
---|
111 | |
---|
112 | #include "prCopy.inc" |
---|
113 | |
---|
114 | ///////////////////////////////////////////////////////////////////////// |
---|
115 | // prCopy |
---|
116 | poly prCopy(poly p) |
---|
117 | { |
---|
118 | poly res; |
---|
119 | |
---|
120 | if (rField_has_simple_Alloc(currRing)) |
---|
121 | res = pr_Copy_REqual_NSimple_NoSort(p, currRing, currRing); |
---|
122 | else |
---|
123 | res = pr_Copy_REqual_NoNSimple_NoSort(p, currRing, currRing); |
---|
124 | assume(pTest(res)); |
---|
125 | return res; |
---|
126 | } |
---|
127 | |
---|
128 | poly prCopyR(poly p, ring src_r) |
---|
129 | { |
---|
130 | poly res; |
---|
131 | if (rField_has_simple_Alloc(currRing)) |
---|
132 | res = pr_Copy_NoREqual_NSimple_Sort(p, src_r, currRing); |
---|
133 | else |
---|
134 | res = pr_Copy_NoREqual_NoNSimple_Sort(p, src_r, currRing); |
---|
135 | assume(pTest(res)); |
---|
136 | return res; |
---|
137 | } |
---|
138 | |
---|
139 | poly prCopyR_NoSort(poly p, ring src_r) |
---|
140 | { |
---|
141 | poly res; |
---|
142 | if (rField_has_simple_Alloc(currRing)) |
---|
143 | res = pr_Copy_NoREqual_NSimple_NoSort(p, src_r, currRing); |
---|
144 | else |
---|
145 | res = pr_Copy_NoREqual_NoNSimple_NoSort(p, src_r, currRing); |
---|
146 | assume(pTest(res)); |
---|
147 | return res; |
---|
148 | } |
---|
149 | |
---|
150 | ///////////////////////////////////////////////////////////////////////// |
---|
151 | // prMove |
---|
152 | poly prMoveR(poly &p, ring src_r) |
---|
153 | { |
---|
154 | poly res; |
---|
155 | if (rField_has_simple_Alloc(currRing)) |
---|
156 | res = pr_Move_NoREqual_NSimple_Sort(p, src_r, currRing); |
---|
157 | else |
---|
158 | res = pr_Move_NoREqual_NoNSimple_Sort(p, src_r, currRing); |
---|
159 | assume(pTest(res)); |
---|
160 | return res; |
---|
161 | } |
---|
162 | |
---|
163 | poly prMoveR_NoSort(poly &p, ring src_r) |
---|
164 | { |
---|
165 | poly res; |
---|
166 | if (rField_has_simple_Alloc(currRing)) |
---|
167 | res = pr_Move_NoREqual_NSimple_NoSort(p, src_r, currRing); |
---|
168 | else |
---|
169 | res = pr_Move_NoREqual_NoNSimple_NoSort(p, src_r, currRing); |
---|
170 | assume(pTest(res)); |
---|
171 | return res; |
---|
172 | } |
---|
173 | |
---|
174 | ///////////////////////////////////////////////////////////////////////// |
---|
175 | // prHead |
---|
176 | typedef poly (*prCopyProc_t)(poly &src_p, ring src_r, ring dest_r); |
---|
177 | |
---|
178 | poly prHeadR(poly p, ring src_r, prCopyProc_t prproc) |
---|
179 | { |
---|
180 | if (p == NULL) return NULL; |
---|
181 | poly tail, head, q = p; |
---|
182 | tail = pNext(p); |
---|
183 | pNext(p) = NULL; |
---|
184 | head = prproc(q, src_r, currRing); |
---|
185 | pNext(p) = tail; |
---|
186 | return head; |
---|
187 | } |
---|
188 | |
---|
189 | poly prHeadR(poly p, ring src_r) |
---|
190 | { |
---|
191 | prCopyProc_t prproc; |
---|
192 | if (rField_has_simple_Alloc(currRing)) |
---|
193 | prproc = pr_Copy_NoREqual_NSimple_NoSort; |
---|
194 | else |
---|
195 | prproc = pr_Copy_NoREqual_NoNSimple_NoSort; |
---|
196 | |
---|
197 | return prHeadR(p, src_r, prproc); |
---|
198 | } |
---|
199 | |
---|
200 | ///////////////////////////////////////////////////////////////////////// |
---|
201 | // idrCopy |
---|
202 | static inline ideal |
---|
203 | idrCopy(ideal id, ring src_r, ring dest_r, prCopyProc_t prproc) |
---|
204 | { |
---|
205 | if (id == NULL) return NULL; |
---|
206 | poly p; |
---|
207 | ideal res = idInit(IDELEMS(id), id->rank); |
---|
208 | int i; |
---|
209 | |
---|
210 | for (i=IDELEMS(id)-1; i>=0; i--) |
---|
211 | { |
---|
212 | p = id->m[i]; |
---|
213 | res->m[i] = prproc(p, src_r, dest_r); |
---|
214 | assume(pTest(res->m[i])); |
---|
215 | } |
---|
216 | return res; |
---|
217 | } |
---|
218 | |
---|
219 | ideal idrCopy(ideal id) |
---|
220 | { |
---|
221 | ideal res; |
---|
222 | prCopyProc_t prproc; |
---|
223 | if (rField_has_simple_Alloc(currRing)) |
---|
224 | prproc = pr_Copy_REqual_NSimple_NoSort; |
---|
225 | else |
---|
226 | prproc = pr_Copy_REqual_NoNSimple_NoSort; |
---|
227 | res = idrCopy(id, currRing, currRing, prproc); |
---|
228 | return res; |
---|
229 | } |
---|
230 | |
---|
231 | ideal idrCopyR(ideal id, ring src_r) |
---|
232 | { |
---|
233 | ideal res; |
---|
234 | prCopyProc_t prproc; |
---|
235 | if (rField_has_simple_Alloc(currRing)) |
---|
236 | prproc = pr_Copy_NoREqual_NSimple_Sort; |
---|
237 | else |
---|
238 | prproc = pr_Copy_NoREqual_NoNSimple_Sort; |
---|
239 | res = idrCopy(id, src_r, currRing, prproc); |
---|
240 | return res; |
---|
241 | } |
---|
242 | |
---|
243 | ideal idrCopyR_NoSort(ideal id, ring src_r) |
---|
244 | { |
---|
245 | ideal res; |
---|
246 | prCopyProc_t prproc; |
---|
247 | if (rField_has_simple_Alloc(currRing)) |
---|
248 | prproc = pr_Copy_NoREqual_NSimple_NoSort; |
---|
249 | else |
---|
250 | prproc = pr_Copy_NoREqual_NoNSimple_NoSort; |
---|
251 | res = idrCopy(id, src_r, currRing, prproc); |
---|
252 | return res; |
---|
253 | } |
---|
254 | |
---|
255 | ///////////////////////////////////////////////////////////////////////// |
---|
256 | // idrMove |
---|
257 | static inline ideal |
---|
258 | idrMove(ideal &id, ring src_r, ring dest_r, prCopyProc_t prproc) |
---|
259 | { |
---|
260 | if (id == NULL) return NULL; |
---|
261 | ideal res = id; |
---|
262 | |
---|
263 | int i; |
---|
264 | for (i=IDELEMS(id)-1; i>=0; i--) |
---|
265 | res->m[i] = prproc(id->m[i], src_r, dest_r); |
---|
266 | id = NULL; |
---|
267 | return res; |
---|
268 | } |
---|
269 | |
---|
270 | ideal idrMoveR(ideal &id, ring src_r) |
---|
271 | { |
---|
272 | prCopyProc_t prproc; |
---|
273 | ideal res; |
---|
274 | |
---|
275 | if (rField_has_simple_Alloc(currRing)) |
---|
276 | prproc = pr_Move_NoREqual_NSimple_Sort; |
---|
277 | else |
---|
278 | prproc = pr_Move_NoREqual_NoNSimple_Sort; |
---|
279 | res = idrMove(id, src_r, currRing, prproc); |
---|
280 | return res; |
---|
281 | } |
---|
282 | |
---|
283 | ideal idrMoveR_NoSort(ideal &id, ring src_r) |
---|
284 | { |
---|
285 | prCopyProc_t prproc; |
---|
286 | ideal res; |
---|
287 | |
---|
288 | if (rField_has_simple_Alloc(currRing)) |
---|
289 | prproc = pr_Move_NoREqual_NSimple_NoSort; |
---|
290 | else |
---|
291 | prproc = pr_Move_NoREqual_NoNSimple_NoSort; |
---|
292 | res = idrMove(id, src_r, currRing, prproc); |
---|
293 | return res; |
---|
294 | } |
---|
295 | |
---|
296 | ///////////////////////////////////////////////////////////////////////// |
---|
297 | // prDelete |
---|
298 | typedef void (*prDeleteProc_t)(poly &src_p, ring src_r); |
---|
299 | |
---|
300 | static inline void prDeleteR_NSimple(poly &p, ring r) |
---|
301 | { |
---|
302 | poly next; |
---|
303 | |
---|
304 | while (p != NULL) |
---|
305 | { |
---|
306 | next = pNext(p); |
---|
307 | omFreeBin(p, r->PolyBin); |
---|
308 | p = next; |
---|
309 | } |
---|
310 | } |
---|
311 | |
---|
312 | static inline void prDeleteR_NoNSimple(poly &p, ring r) |
---|
313 | { |
---|
314 | poly next; |
---|
315 | |
---|
316 | while (p != NULL) |
---|
317 | { |
---|
318 | next = pNext(p); |
---|
319 | nDelete(&pGetCoeff(p)); |
---|
320 | omFreeBin(p, r->PolyBin); |
---|
321 | p = next; |
---|
322 | } |
---|
323 | } |
---|
324 | |
---|
325 | void prDelete(poly &p) |
---|
326 | { |
---|
327 | if (rField_has_simple_Alloc(currRing)) |
---|
328 | prDeleteR_NSimple(p, currRing); |
---|
329 | else |
---|
330 | prDeleteR_NoNSimple(p, currRing); |
---|
331 | } |
---|
332 | |
---|
333 | void prDeleteR(poly &p, ring r) |
---|
334 | { |
---|
335 | if (rField_has_simple_Alloc(r)) |
---|
336 | prDeleteR_NSimple(p, r); |
---|
337 | else |
---|
338 | prDeleteR_NoNSimple(p, r); |
---|
339 | } |
---|
340 | |
---|
341 | |
---|
342 | static inline void idrDelete(ideal &id, ring r, prDeleteProc_t prproc) |
---|
343 | { |
---|
344 | if (id == NULL) return; |
---|
345 | int i = IDELEMS(id); |
---|
346 | |
---|
347 | for (; i>=0; i--) |
---|
348 | prproc(id->m[i], r); |
---|
349 | |
---|
350 | omFreeSize(id->m, IDELEMS(id)*sizeof(poly)); |
---|
351 | omFreeBin(id, sip_sideal_bin); |
---|
352 | id = NULL; |
---|
353 | } |
---|
354 | |
---|
355 | void idrDelete(ideal &id) |
---|
356 | { |
---|
357 | prDeleteProc_t prproc; |
---|
358 | if (rField_has_simple_Alloc(currRing)) |
---|
359 | prproc = prDeleteR_NSimple; |
---|
360 | else |
---|
361 | prproc = prDeleteR_NoNSimple; |
---|
362 | |
---|
363 | idrDelete(id, currRing, prproc); |
---|
364 | } |
---|
365 | |
---|
366 | void idrDeleteR(ideal &id, ring r) |
---|
367 | { |
---|
368 | prDeleteProc_t prproc; |
---|
369 | if (rField_has_simple_Alloc(r)) |
---|
370 | prproc = prDeleteR_NSimple; |
---|
371 | else |
---|
372 | prproc = prDeleteR_NoNSimple; |
---|
373 | |
---|
374 | idrDelete(id, r, prproc); |
---|
375 | } |
---|
376 | |
---|
377 | |
---|
378 | |
---|
379 | |
---|