1 | /***************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | *****************************************/ |
---|
4 | /* $Id: pcv.cc,v 1.20 1999-06-08 09:13:54 mschulze Exp $ */ |
---|
5 | /* |
---|
6 | * ABSTRACT: conversion between polys and coef vectors |
---|
7 | */ |
---|
8 | |
---|
9 | #include "mod2.h" |
---|
10 | |
---|
11 | #ifdef HAVE_PCV |
---|
12 | #if !defined(HAVE_DYNAMIC_LOADING) || defined(BUILD_MODULE) |
---|
13 | |
---|
14 | #include "tok.h" |
---|
15 | #include "ipid.h" |
---|
16 | #include "numbers.h" |
---|
17 | #include "polys.h" |
---|
18 | #include "lists.h" |
---|
19 | #include "matpol.h" |
---|
20 | #include "febase.h" |
---|
21 | #include "pcv.h" |
---|
22 | |
---|
23 | static int pcvMaxDegree; |
---|
24 | static int pcvTableSize; |
---|
25 | static int pcvIndexSize; |
---|
26 | static unsigned* pcvTable=NULL; |
---|
27 | static unsigned** pcvIndex=NULL; |
---|
28 | |
---|
29 | int pcvDeg(poly p) |
---|
30 | { |
---|
31 | int d=0; |
---|
32 | for(int i=pVariables;i>=1;i--) d+=pGetExp(p,i); |
---|
33 | return d; |
---|
34 | } |
---|
35 | |
---|
36 | int pcvMinDeg(poly p) |
---|
37 | { |
---|
38 | if(!p) return -1; |
---|
39 | int md=pcvDeg(p); |
---|
40 | pIter(p); |
---|
41 | while(p) |
---|
42 | { |
---|
43 | int d=pcvDeg(p); |
---|
44 | if(d<md) md=d; |
---|
45 | pIter(p); |
---|
46 | } |
---|
47 | return md; |
---|
48 | } |
---|
49 | |
---|
50 | int pcvMinDeg(matrix m) |
---|
51 | { |
---|
52 | int i,j,d; |
---|
53 | int md=-1; |
---|
54 | for(i=1;i<=MATROWS(m);i++) |
---|
55 | { |
---|
56 | for(j=1;j<=MATCOLS(m);j++) |
---|
57 | { |
---|
58 | d=pcvMinDeg(MATELEM(m,i,j)); |
---|
59 | if((d>=0&&md>d)||md==-1) md=d; |
---|
60 | } |
---|
61 | } |
---|
62 | return(md); |
---|
63 | } |
---|
64 | |
---|
65 | int pcvMaxDeg(poly p) |
---|
66 | { |
---|
67 | if(!p) return -1; |
---|
68 | int md=pcvDeg(p); |
---|
69 | pIter(p); |
---|
70 | while(p) |
---|
71 | { |
---|
72 | int d=pcvDeg(p); |
---|
73 | if(d>md) md=d; |
---|
74 | pIter(p); |
---|
75 | } |
---|
76 | return md; |
---|
77 | } |
---|
78 | |
---|
79 | int pcvMaxDeg(matrix m) |
---|
80 | { |
---|
81 | int i,j,d; |
---|
82 | int md=-1; |
---|
83 | for(i=1;i<=MATROWS(m);i++) |
---|
84 | { |
---|
85 | for(j=1;j<=MATCOLS(m);j++) |
---|
86 | { |
---|
87 | d=pcvMinDeg(MATELEM(m,i,j)); |
---|
88 | if((d>=0&&md<d)||md==-1) md=d; |
---|
89 | } |
---|
90 | } |
---|
91 | return(md); |
---|
92 | } |
---|
93 | |
---|
94 | BOOLEAN pcvMinDeg(leftv res,leftv h) |
---|
95 | { |
---|
96 | if(h) |
---|
97 | { |
---|
98 | if(h->Typ()==POLY_CMD) |
---|
99 | { |
---|
100 | res->rtyp=INT_CMD; |
---|
101 | res->data=(void*)pcvMinDeg((poly)h->Data()); |
---|
102 | return FALSE; |
---|
103 | } |
---|
104 | else |
---|
105 | if(h->Typ()==MATRIX_CMD) |
---|
106 | { |
---|
107 | res->rtyp=INT_CMD; |
---|
108 | res->data=(void*)pcvMinDeg((matrix)h->Data()); |
---|
109 | return FALSE; |
---|
110 | } |
---|
111 | } |
---|
112 | WerrorS("<poly> or <matrix> expected"); |
---|
113 | return TRUE; |
---|
114 | } |
---|
115 | |
---|
116 | BOOLEAN pcvMaxDeg(leftv res,leftv h) |
---|
117 | { |
---|
118 | if(h) |
---|
119 | { |
---|
120 | if(h->Typ()==POLY_CMD) |
---|
121 | { |
---|
122 | res->rtyp=INT_CMD; |
---|
123 | res->data=(void*)pcvMaxDeg((poly)h->Data()); |
---|
124 | return FALSE; |
---|
125 | } |
---|
126 | else |
---|
127 | if(h->Typ()==MATRIX_CMD) |
---|
128 | { |
---|
129 | res->rtyp=INT_CMD; |
---|
130 | res->data=(void*)pcvMaxDeg((matrix)h->Data()); |
---|
131 | return FALSE; |
---|
132 | } |
---|
133 | } |
---|
134 | WerrorS("<poly> or <matrix> expected"); |
---|
135 | return TRUE; |
---|
136 | } |
---|
137 | |
---|
138 | void pcvInit(int d) |
---|
139 | { |
---|
140 | if(d<0) d=0; |
---|
141 | pcvMaxDegree=d; |
---|
142 | pcvTableSize=pVariables*pcvMaxDegree*sizeof(unsigned); |
---|
143 | pcvTable=(unsigned*)Alloc0(pcvTableSize); |
---|
144 | pcvIndexSize=pVariables*sizeof(unsigned*); |
---|
145 | pcvIndex=(unsigned**)Alloc(pcvIndexSize); |
---|
146 | for(int i=0;i<pVariables;i++) |
---|
147 | pcvIndex[i]=pcvTable+i*pcvMaxDegree; |
---|
148 | for(int i=0;i<pcvMaxDegree;i++) |
---|
149 | pcvIndex[0][i]=i; |
---|
150 | for(int i=1;i<pVariables;i++) |
---|
151 | { |
---|
152 | unsigned x=0; |
---|
153 | for(int j=0;j<pcvMaxDegree;j++) |
---|
154 | { |
---|
155 | x+=pcvIndex[i-1][j]; |
---|
156 | pcvIndex[i][j]=x; |
---|
157 | } |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | void pcvClean() |
---|
162 | { |
---|
163 | if(pcvTable) |
---|
164 | { |
---|
165 | Free(pcvTable,pcvTableSize); |
---|
166 | pcvTable=NULL; |
---|
167 | } |
---|
168 | if(pcvIndex) |
---|
169 | { |
---|
170 | Free(pcvIndex,pcvIndexSize); |
---|
171 | pcvIndex=NULL; |
---|
172 | } |
---|
173 | } |
---|
174 | |
---|
175 | int pcvM2N(poly m) |
---|
176 | { |
---|
177 | unsigned n=0,d=0; |
---|
178 | for(int i=0;i<pVariables;i++) |
---|
179 | { |
---|
180 | d+=pGetExp(m,i+1); |
---|
181 | n+=pcvIndex[i][d]; |
---|
182 | } |
---|
183 | return n+1; |
---|
184 | } |
---|
185 | |
---|
186 | poly pcvN2M(int n) |
---|
187 | { |
---|
188 | n--; |
---|
189 | poly m=pOne(); |
---|
190 | int i,j,k; |
---|
191 | for(i=pVariables-1;i>=0;i--) |
---|
192 | { |
---|
193 | k=j; |
---|
194 | for(j=0;j<pcvMaxDegree&&pcvIndex[i][j]<=n;j++); |
---|
195 | j--; |
---|
196 | n-=pcvIndex[i][j]; |
---|
197 | if(i<pVariables-1) pSetExp(m,i+2,k-j); |
---|
198 | } |
---|
199 | if(n==0) |
---|
200 | { |
---|
201 | pSetExp(m,1,j); |
---|
202 | pSetm(m); |
---|
203 | return m; |
---|
204 | } |
---|
205 | else |
---|
206 | { |
---|
207 | pDelete1(&m); |
---|
208 | return NULL; |
---|
209 | } |
---|
210 | } |
---|
211 | |
---|
212 | poly pcvP2CV(poly p,int d0,int d1) |
---|
213 | { |
---|
214 | poly cv=NULL; |
---|
215 | while(p) |
---|
216 | { |
---|
217 | int d=pcvDeg(p); |
---|
218 | if(d0<=d&&d<d1) |
---|
219 | { |
---|
220 | poly c=pOne(); |
---|
221 | pSetComp(c,pcvM2N(p)); |
---|
222 | pSetCoeff(c,nCopy(pGetCoeff(p))); |
---|
223 | cv=pAdd(cv,c); |
---|
224 | } |
---|
225 | pIter(p); |
---|
226 | } |
---|
227 | return cv; |
---|
228 | } |
---|
229 | |
---|
230 | poly pcvCV2P(poly cv,int d0,int d1) |
---|
231 | { |
---|
232 | poly p=NULL; |
---|
233 | while(cv) |
---|
234 | { |
---|
235 | poly m=pcvN2M(pGetComp(cv)); |
---|
236 | if(m) |
---|
237 | { |
---|
238 | int d=pcvDeg(m); |
---|
239 | if(d0<=d&&d<d1) |
---|
240 | { |
---|
241 | pSetCoeff(m,nCopy(pGetCoeff(cv))); |
---|
242 | p=pAdd(p,m); |
---|
243 | } |
---|
244 | } |
---|
245 | pIter(cv); |
---|
246 | } |
---|
247 | return p; |
---|
248 | } |
---|
249 | |
---|
250 | lists pcvP2CV(lists pl,int d0,int d1) |
---|
251 | { |
---|
252 | lists cvl=(lists)Alloc(sizeof(slists)); |
---|
253 | cvl->Init(pl->nr+1); |
---|
254 | pcvInit(d1); |
---|
255 | for(int i=pl->nr;i>=0;i--) |
---|
256 | { |
---|
257 | if(pl->m[i].rtyp==POLY_CMD) |
---|
258 | { |
---|
259 | cvl->m[i].rtyp=VECTOR_CMD; |
---|
260 | cvl->m[i].data=pcvP2CV((poly)pl->m[i].data,d0,d1); |
---|
261 | } |
---|
262 | } |
---|
263 | pcvClean(); |
---|
264 | return cvl; |
---|
265 | } |
---|
266 | |
---|
267 | lists pcvCV2P(lists cvl,int d0,int d1) |
---|
268 | { |
---|
269 | lists pl=(lists)Alloc(sizeof(slists)); |
---|
270 | pl->Init(cvl->nr+1); |
---|
271 | pcvInit(d1); |
---|
272 | for(int i=cvl->nr;i>=0;i--) |
---|
273 | { |
---|
274 | if(cvl->m[i].rtyp==VECTOR_CMD) |
---|
275 | { |
---|
276 | pl->m[i].rtyp=POLY_CMD; |
---|
277 | pl->m[i].data=pcvCV2P((poly)cvl->m[i].data,d0,d1); |
---|
278 | } |
---|
279 | } |
---|
280 | pcvClean(); |
---|
281 | return pl; |
---|
282 | } |
---|
283 | |
---|
284 | BOOLEAN pcvP2CV(leftv res,leftv h) |
---|
285 | { |
---|
286 | if(currRingHdl) |
---|
287 | { |
---|
288 | if(h&&h->Typ()==LIST_CMD) |
---|
289 | { |
---|
290 | lists pl=(lists)h->Data(); |
---|
291 | h=h->next; |
---|
292 | if(h&&h->Typ()==INT_CMD) |
---|
293 | { |
---|
294 | int d0=(int)h->Data(); |
---|
295 | h=h->next; |
---|
296 | if(h&&h->Typ()==INT_CMD) |
---|
297 | { |
---|
298 | int d1=(int)h->Data(); |
---|
299 | res->rtyp=LIST_CMD; |
---|
300 | res->data=pcvP2CV(pl,d0,d1); |
---|
301 | return FALSE; |
---|
302 | } |
---|
303 | } |
---|
304 | } |
---|
305 | WerrorS("<list>,<int>,<int> expected"); |
---|
306 | return TRUE; |
---|
307 | } |
---|
308 | WerrorS("no ring active"); |
---|
309 | return TRUE; |
---|
310 | } |
---|
311 | |
---|
312 | BOOLEAN pcvCV2P(leftv res,leftv h) |
---|
313 | { |
---|
314 | if(currRingHdl) |
---|
315 | { |
---|
316 | if(h&&h->Typ()==LIST_CMD) |
---|
317 | { |
---|
318 | lists pl=(lists)h->Data(); |
---|
319 | h=h->next; |
---|
320 | if(h&&h->Typ()==INT_CMD) |
---|
321 | { |
---|
322 | int d0=(int)h->Data(); |
---|
323 | h=h->next; |
---|
324 | if(h&&h->Typ()==INT_CMD) |
---|
325 | { |
---|
326 | int d1=(int)h->Data(); |
---|
327 | res->rtyp=LIST_CMD; |
---|
328 | res->data=pcvCV2P(pl,d0,d1); |
---|
329 | return FALSE; |
---|
330 | } |
---|
331 | } |
---|
332 | } |
---|
333 | WerrorS("<list>,<int>,<int> expected"); |
---|
334 | return TRUE; |
---|
335 | } |
---|
336 | WerrorS("no ring active"); |
---|
337 | return TRUE; |
---|
338 | } |
---|
339 | |
---|
340 | int pcvDim(int d0,int d1) |
---|
341 | { |
---|
342 | if(d0<0) d0=0; |
---|
343 | if(d1<0) d1=0; |
---|
344 | pcvInit(d1+1); |
---|
345 | int d=pcvIndex[pVariables-1][d1]-pcvIndex[pVariables-1][d0]; |
---|
346 | pcvClean(); |
---|
347 | return d; |
---|
348 | } |
---|
349 | |
---|
350 | BOOLEAN pcvDim(leftv res,leftv h) |
---|
351 | { |
---|
352 | if(currRingHdl) |
---|
353 | { |
---|
354 | if(h&&h->Typ()==INT_CMD) |
---|
355 | { |
---|
356 | int d0=(int)h->Data(); |
---|
357 | h=h->next; |
---|
358 | if(h&&h->Typ()==INT_CMD) |
---|
359 | { |
---|
360 | int d1=(int)h->Data(); |
---|
361 | res->rtyp=INT_CMD; |
---|
362 | res->data=(void*)pcvDim(d0,d1); |
---|
363 | return FALSE; |
---|
364 | } |
---|
365 | } |
---|
366 | WerrorS("<int>,<int> expected"); |
---|
367 | return TRUE; |
---|
368 | } |
---|
369 | WerrorS("no ring active"); |
---|
370 | return TRUE; |
---|
371 | } |
---|
372 | |
---|
373 | int pcvBasis(lists b,int i,poly m,int d,int n) |
---|
374 | { |
---|
375 | if(n<pVariables) |
---|
376 | { |
---|
377 | for(int k=0,l=d;k<=l;k++,d--) |
---|
378 | { |
---|
379 | pSetExp(m,n,k); |
---|
380 | i=pcvBasis(b,i,m,d,n+1); |
---|
381 | } |
---|
382 | } |
---|
383 | else |
---|
384 | { |
---|
385 | pSetExp(m,n,d); |
---|
386 | pSetm(m); |
---|
387 | b->m[i].rtyp=POLY_CMD; |
---|
388 | b->m[i++].data=pCopy(m); |
---|
389 | } |
---|
390 | return i; |
---|
391 | } |
---|
392 | |
---|
393 | lists pcvBasis(int d0,int d1) |
---|
394 | { |
---|
395 | if(d0<0) d0=0; |
---|
396 | if(d1<0) d1=0; |
---|
397 | lists b=(lists)Alloc(sizeof(slists)); |
---|
398 | b->Init(pcvDim(d0,d1)); |
---|
399 | poly m=pOne(); |
---|
400 | for(int d=d0,i=0;d<d1;d++) |
---|
401 | i=pcvBasis(b,i,m,d,1); |
---|
402 | pDelete1(&m); |
---|
403 | return b; |
---|
404 | } |
---|
405 | |
---|
406 | BOOLEAN pcvBasis(leftv res,leftv h) |
---|
407 | { |
---|
408 | if(currRingHdl) |
---|
409 | { |
---|
410 | if(h&&h->Typ()==INT_CMD) |
---|
411 | { |
---|
412 | int d0=(int)h->Data(); |
---|
413 | h=h->next; |
---|
414 | if(h&&h->Typ()==INT_CMD) |
---|
415 | { |
---|
416 | int d1=(int)h->Data(); |
---|
417 | res->rtyp=LIST_CMD; |
---|
418 | res->data=pcvBasis(d0,d1); |
---|
419 | return FALSE; |
---|
420 | } |
---|
421 | } |
---|
422 | WerrorS("<int>,<int> expected"); |
---|
423 | return TRUE; |
---|
424 | } |
---|
425 | WerrorS("no ring active"); |
---|
426 | return TRUE; |
---|
427 | } |
---|
428 | |
---|
429 | #endif /* !defined(HAVE_DYNAMIC_LOADING) || defined(BUILD_MODULE) */ |
---|
430 | #endif /* HAVE_PCV */ |
---|