1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* |
---|
5 | * ABSTRACT: handling of the list type |
---|
6 | */ |
---|
7 | // to produce a non-inline version from lists.h |
---|
8 | #define LISTS_CC |
---|
9 | |
---|
10 | #include "config.h" |
---|
11 | #include <kernel/mod2.h> |
---|
12 | #include <Singular/tok.h> |
---|
13 | #include <kernel/febase.h> |
---|
14 | //#include "ipid.h" |
---|
15 | #include <kernel/polys.h> |
---|
16 | #include <kernel/ideals.h> |
---|
17 | #include <Singular/attrib.h> |
---|
18 | #include <Singular/ipshell.h> |
---|
19 | #include <misc/intvec.h> |
---|
20 | #include <Singular/lists.h> |
---|
21 | |
---|
22 | omBin slists_bin = omGetSpecBin(sizeof(slists)); |
---|
23 | |
---|
24 | int lSize(lists L) |
---|
25 | { |
---|
26 | int n=L->nr; |
---|
27 | while ((n>=0)&&((L->m[n].rtyp==DEF_CMD)||(L->m[n].rtyp==0))) n--; |
---|
28 | return n; |
---|
29 | } |
---|
30 | |
---|
31 | lists lCopy(lists L) |
---|
32 | { |
---|
33 | lists N=(lists)omAlloc0Bin(slists_bin); |
---|
34 | int n=L->nr; |
---|
35 | if (L->nr>=0) |
---|
36 | N->Init(n+1); |
---|
37 | else |
---|
38 | N->Init(); |
---|
39 | for(;n>=0;n--) |
---|
40 | { |
---|
41 | N->m[n].Copy(&L->m[n]); |
---|
42 | } |
---|
43 | //Print("copy list with %d -> %d elems\n",L->nr,N->nr); |
---|
44 | return N; |
---|
45 | } |
---|
46 | |
---|
47 | /*2 |
---|
48 | * concat 2 lists |
---|
49 | */ |
---|
50 | BOOLEAN lAdd(leftv res, leftv u, leftv v) |
---|
51 | { |
---|
52 | lists l=(lists) omAllocBin(slists_bin); |
---|
53 | lists ul=(lists)u->CopyD(); |
---|
54 | lists vl=(lists)v->CopyD(); |
---|
55 | l->Init(ul->nr+vl->nr+2); |
---|
56 | int i; |
---|
57 | |
---|
58 | for(i=0;i<=ul->nr;i++) |
---|
59 | { |
---|
60 | //Print("u[%d]->r[%d]\n",i,i); |
---|
61 | l->m[i].rtyp=ul->m[i].rtyp; |
---|
62 | l->m[i].data=ul->m[i].data; |
---|
63 | } |
---|
64 | for(i=0;i<=vl->nr;i++) |
---|
65 | { |
---|
66 | //Print("v[%d]->r[%d]\n",i,i+ul->nr+1); |
---|
67 | l->m[i+ul->nr+1].rtyp=vl->m[i].rtyp; |
---|
68 | l->m[i+ul->nr+1].data=vl->m[i].data; |
---|
69 | } |
---|
70 | if (ul->m != NULL) |
---|
71 | omFreeSize((ADDRESS)ul->m,(ul->nr+1)*sizeof(sleftv)); |
---|
72 | omFreeBin((ADDRESS)ul, slists_bin); |
---|
73 | if (vl->m != NULL) |
---|
74 | omFreeSize((ADDRESS)vl->m,(vl->nr+1)*sizeof(sleftv)); |
---|
75 | omFreeBin((ADDRESS)vl, slists_bin); |
---|
76 | memset(u,0,sizeof(*u)); |
---|
77 | memset(v,0,sizeof(*v)); |
---|
78 | res->data = (char *)l; |
---|
79 | //res->Print(); |
---|
80 | return FALSE; |
---|
81 | } |
---|
82 | |
---|
83 | /*2 |
---|
84 | * insert v into list ul, destroys u |
---|
85 | */ |
---|
86 | lists lInsert0(lists ul, leftv v, int pos) |
---|
87 | { |
---|
88 | if ((pos<0)||(v->rtyp==NONE)) |
---|
89 | return NULL; |
---|
90 | lists l=(lists) omAllocBin(slists_bin); |
---|
91 | l->Init(si_max(ul->nr+2,pos+1)); |
---|
92 | int i,j; |
---|
93 | |
---|
94 | for(i=j=0;i<=ul->nr;i++,j++) |
---|
95 | { |
---|
96 | if(j==pos) j++; |
---|
97 | l->m[j]=ul->m[i]; |
---|
98 | } |
---|
99 | for(j=ul->nr+1;j<pos;j++) |
---|
100 | l->m[j].rtyp=DEF_CMD; |
---|
101 | // memset(&(l->m[pos]),0,sizeof(sleftv)); - done by Init |
---|
102 | l->m[pos].rtyp=v->Typ(); |
---|
103 | l->m[pos].data=v->CopyD(); |
---|
104 | l->m[pos].flag=v->flag; |
---|
105 | attr *a=v->Attribute(); |
---|
106 | if (a!=NULL) |
---|
107 | { |
---|
108 | l->m[pos].attribute=(*a)->Copy(); |
---|
109 | } |
---|
110 | if (ul->m != NULL) |
---|
111 | omFreeSize((ADDRESS)ul->m,(ul->nr+1)*sizeof(sleftv)); |
---|
112 | omFreeBin((ADDRESS)ul, slists_bin); |
---|
113 | return l; |
---|
114 | } |
---|
115 | |
---|
116 | /*2 |
---|
117 | * insert v into list u, at the beginning |
---|
118 | */ |
---|
119 | BOOLEAN lInsert(leftv res, leftv u, leftv v) |
---|
120 | { |
---|
121 | lists ul=(lists)u->CopyD(); |
---|
122 | res->data=(char *)lInsert0(ul,v,0); |
---|
123 | if (res->data==NULL) |
---|
124 | { |
---|
125 | Werror("cannot insert type `%s`",Tok2Cmdname(v->Typ())); |
---|
126 | return TRUE; |
---|
127 | } |
---|
128 | return FALSE; |
---|
129 | } |
---|
130 | |
---|
131 | /*2 |
---|
132 | * insert v into list u at pos w |
---|
133 | */ |
---|
134 | BOOLEAN lInsert3(leftv res, leftv u, leftv v, leftv w) |
---|
135 | { |
---|
136 | lists ul=(lists)u->CopyD(); |
---|
137 | res->data=(char *)lInsert0(ul,v,(int)(long)w->Data()); |
---|
138 | if (res->data==NULL) |
---|
139 | { |
---|
140 | Werror("cannot insert type `%s` at pos. %d", |
---|
141 | Tok2Cmdname(v->Typ()),(int)(long)w->Data()); |
---|
142 | return TRUE; |
---|
143 | } |
---|
144 | return FALSE; |
---|
145 | } |
---|
146 | |
---|
147 | /*2 |
---|
148 | * append v to list u |
---|
149 | */ |
---|
150 | BOOLEAN lAppend(leftv res, leftv u, leftv v) |
---|
151 | { |
---|
152 | lists ul=(lists)u->CopyD(); |
---|
153 | res->data=(char *)lInsert0(ul,v,ul->nr+1); |
---|
154 | return (res->data==NULL); |
---|
155 | } |
---|
156 | |
---|
157 | /*2 |
---|
158 | * delete v-th element from list u |
---|
159 | */ |
---|
160 | BOOLEAN lDelete(leftv res, leftv u, leftv v) |
---|
161 | { |
---|
162 | lists ul=(lists)u->Data(); |
---|
163 | int VIndex=(int)(long)v->Data()-1; |
---|
164 | int EndIndex=lSize(ul); |
---|
165 | |
---|
166 | if((0<=VIndex)&&(VIndex<=ul->nr)) |
---|
167 | { |
---|
168 | ul=(lists)u->CopyD(); |
---|
169 | int i,j; |
---|
170 | lists l=(lists) omAllocBin(slists_bin); |
---|
171 | l->Init(EndIndex+(VIndex>EndIndex)); |
---|
172 | |
---|
173 | for(i=j=0;i<=EndIndex;i++,j++) |
---|
174 | { |
---|
175 | if (i!=VIndex) |
---|
176 | { |
---|
177 | l->m[j]=ul->m[i]; |
---|
178 | memset(&ul->m[i],0,sizeof(ul->m[i])); |
---|
179 | } |
---|
180 | else |
---|
181 | { |
---|
182 | j--; |
---|
183 | ul->m[i].CleanUp(); |
---|
184 | } |
---|
185 | } |
---|
186 | omFreeSize((ADDRESS)ul->m,(ul->nr+1)*sizeof(sleftv)); |
---|
187 | omFreeBin((ADDRESS)ul, slists_bin); |
---|
188 | res->data = (char *)l; |
---|
189 | return FALSE; |
---|
190 | } |
---|
191 | Werror("wrong index %d in list(%d)",VIndex+1,ul->nr+1); |
---|
192 | return TRUE; |
---|
193 | } |
---|
194 | |
---|
195 | /*2 |
---|
196 | * check, if a list contains any ring dependend data |
---|
197 | */ |
---|
198 | BOOLEAN lRingDependend(lists L) |
---|
199 | { |
---|
200 | if (L==NULL) return FALSE; |
---|
201 | int i=0; |
---|
202 | while (i<=L->nr) |
---|
203 | { |
---|
204 | if ((L->m[i].rtyp!=QRING_CMD) |
---|
205 | && (BEGIN_RING<L->m[i].rtyp) |
---|
206 | && (L->m[i].rtyp<END_RING)) |
---|
207 | return TRUE; |
---|
208 | if ((L->m[i].rtyp==LIST_CMD)&&lRingDependend((lists)L->m[i].data)) |
---|
209 | return TRUE; |
---|
210 | i++; |
---|
211 | } |
---|
212 | return FALSE; |
---|
213 | } |
---|
214 | |
---|
215 | lists liMakeResolv(resolvente r, int length, int reallen, |
---|
216 | int typ0, intvec ** weights, int add_row_shift) |
---|
217 | { |
---|
218 | lists L=(lists)omAlloc0Bin(slists_bin); |
---|
219 | if (length<=0) |
---|
220 | { |
---|
221 | // handle "empty" resolutions |
---|
222 | L->Init(0); |
---|
223 | } |
---|
224 | else |
---|
225 | { |
---|
226 | int oldlength=length; |
---|
227 | while (r[length-1]==NULL) length--; |
---|
228 | if (reallen<=0) reallen=currRing->N; |
---|
229 | reallen=si_max(reallen,length); |
---|
230 | L->Init(reallen); |
---|
231 | int i=0; |
---|
232 | |
---|
233 | while (i<length) |
---|
234 | { |
---|
235 | if (r[i]!=NULL) |
---|
236 | { |
---|
237 | if (i==0) |
---|
238 | { |
---|
239 | L->m[i].rtyp=typ0; |
---|
240 | int j=IDELEMS(r[0])-1; |
---|
241 | while ((j>0) && (r[0]->m[j]==NULL)) j--; |
---|
242 | j++; |
---|
243 | if (j!=IDELEMS(r[0])) |
---|
244 | { |
---|
245 | pEnlargeSet(&(r[0]->m),IDELEMS(r[0]),j-IDELEMS(r[0])); |
---|
246 | IDELEMS(r[0])=j; |
---|
247 | } |
---|
248 | } |
---|
249 | else |
---|
250 | { |
---|
251 | L->m[i].rtyp=MODUL_CMD; |
---|
252 | int rank=IDELEMS(r[i-1]); |
---|
253 | if (idIs0(r[i-1])) |
---|
254 | { |
---|
255 | idDelete(&(r[i])); |
---|
256 | r[i]=id_FreeModule(rank, currRing); |
---|
257 | } |
---|
258 | else |
---|
259 | { |
---|
260 | r[i]->rank=si_max(rank,(int)id_RankFreeModule(r[i], currRing)); |
---|
261 | } |
---|
262 | idSkipZeroes(r[i]); |
---|
263 | } |
---|
264 | L->m[i].data=(void *)r[i]; |
---|
265 | if ((weights!=NULL) && (weights[i]!=NULL)) |
---|
266 | { |
---|
267 | intvec *w=ivCopy(weights[i]); |
---|
268 | (*w) += add_row_shift; |
---|
269 | atSet((idhdl)&L->m[i],omStrDup("isHomog"),w,INTVEC_CMD); |
---|
270 | weights[i] = NULL; |
---|
271 | } |
---|
272 | } |
---|
273 | #ifdef TEST |
---|
274 | else |
---|
275 | { |
---|
276 | // should not happen: |
---|
277 | Warn("internal NULL in resolvente"); |
---|
278 | L->m[i].data=(void *)idInit(1,1); |
---|
279 | } |
---|
280 | #endif |
---|
281 | i++; |
---|
282 | } |
---|
283 | omFreeSize((ADDRESS)r,oldlength*sizeof(ideal)); |
---|
284 | if (i==0) |
---|
285 | { |
---|
286 | L->m[0].rtyp=typ0; |
---|
287 | L->m[0].data=(char *)idInit(1,1); |
---|
288 | i=1; |
---|
289 | } |
---|
290 | while (i<reallen) |
---|
291 | { |
---|
292 | L->m[i].rtyp=MODUL_CMD; |
---|
293 | ideal I=(ideal)L->m[i-1].data; |
---|
294 | ideal J; |
---|
295 | int rank=IDELEMS(I); |
---|
296 | if (idIs0(I)) |
---|
297 | { |
---|
298 | J=idFreeModule(rank); |
---|
299 | } |
---|
300 | else |
---|
301 | { |
---|
302 | J=idInit(1,rank); |
---|
303 | } |
---|
304 | L->m[i].data=(void *)J; |
---|
305 | i++; |
---|
306 | } |
---|
307 | //Print("make res of length %d (0..%d) L:%d\n",length,length-1,L->nr); |
---|
308 | } |
---|
309 | return L; |
---|
310 | } |
---|
311 | |
---|
312 | resolvente liFindRes(lists L, int * len, int *typ0,intvec *** weights) |
---|
313 | { |
---|
314 | resolvente r; |
---|
315 | intvec ** w=NULL,*tw=NULL; |
---|
316 | |
---|
317 | *len=L->nr+1; |
---|
318 | if (*len<=0) |
---|
319 | { |
---|
320 | WerrorS("empty list"); |
---|
321 | return NULL; |
---|
322 | } |
---|
323 | r=(ideal *)omAlloc0((*len)*sizeof(ideal)); |
---|
324 | w=(intvec**)omAlloc0((*len)*sizeof(intvec*)); |
---|
325 | int i=0; |
---|
326 | *typ0=MODUL_CMD; |
---|
327 | while (i<(*len)) |
---|
328 | { |
---|
329 | if (L->m[i].rtyp != MODUL_CMD) |
---|
330 | { |
---|
331 | if (L->m[i].rtyp!=IDEAL_CMD) |
---|
332 | { |
---|
333 | Werror("element %d is not of type module",i+1); |
---|
334 | omFreeSize((ADDRESS)r,(*len)*sizeof(ideal)); |
---|
335 | return NULL; |
---|
336 | } |
---|
337 | *typ0=IDEAL_CMD; |
---|
338 | } |
---|
339 | if ((i>0) && (idIs0(r[i-1]))) |
---|
340 | { |
---|
341 | //*len=i-1; |
---|
342 | break; |
---|
343 | } |
---|
344 | r[i]=(ideal)L->m[i].data; |
---|
345 | tw=(intvec*)atGet((idhdl)&L->m[i],"isHomog",INTVEC_CMD); |
---|
346 | if (tw!=NULL) |
---|
347 | { |
---|
348 | w[i]=ivCopy(tw); |
---|
349 | } |
---|
350 | tw = NULL; |
---|
351 | i++; |
---|
352 | } |
---|
353 | BOOLEAN hom_complex=TRUE; |
---|
354 | int j=0; |
---|
355 | while ((j<i) && hom_complex) |
---|
356 | { |
---|
357 | hom_complex = hom_complex && (w[i]!=NULL); |
---|
358 | j++; |
---|
359 | } |
---|
360 | if ((!hom_complex) || (weights==NULL)) |
---|
361 | { |
---|
362 | for (j=0;j<i;j++) |
---|
363 | { |
---|
364 | if (w[j]!=NULL) delete w[j]; |
---|
365 | } |
---|
366 | omFreeSize((ADDRESS)w,(*len)*sizeof(intvec*)); |
---|
367 | } |
---|
368 | else |
---|
369 | { |
---|
370 | *weights = w; |
---|
371 | } |
---|
372 | //Print("find res of length %d (0..%d) L:%d\n",*len,(*len)-1,L->nr); |
---|
373 | return r; |
---|
374 | } |
---|
375 | |
---|
376 | char* lString(lists l, BOOLEAN typed, int dim) |
---|
377 | { |
---|
378 | if (l->nr == -1) |
---|
379 | { |
---|
380 | if (typed) return omStrDup("list()"); |
---|
381 | return omStrDup(""); |
---|
382 | } |
---|
383 | |
---|
384 | char** slist = (char**) omAlloc((l->nr+1) * sizeof(char*)); |
---|
385 | int i, j, k; |
---|
386 | char *s; |
---|
387 | for (i=0, j = 0, k = 0; i<=l->nr; i++) |
---|
388 | { |
---|
389 | slist[i] = l->m[i].String(NULL, typed, dim); |
---|
390 | assume(slist[i] != NULL); |
---|
391 | omCheckAddr(slist[i]); |
---|
392 | if (*(slist[i]) != '\0') |
---|
393 | { |
---|
394 | j += strlen(slist[i]); |
---|
395 | k++; |
---|
396 | } |
---|
397 | } |
---|
398 | s = (char*) omAlloc(j+k+2+(typed ? 10 : 0) + (dim == 2 ? k : 0)); |
---|
399 | |
---|
400 | if (typed) |
---|
401 | sprintf(s, "list("); |
---|
402 | else |
---|
403 | *s = '\0'; |
---|
404 | |
---|
405 | for (i=0; i<=l->nr; i++) |
---|
406 | { |
---|
407 | if (*(slist[i]) != '\0') |
---|
408 | { |
---|
409 | strcat(s, slist[i]); |
---|
410 | strcat(s, ","); |
---|
411 | if (dim == 2) strcat(s, "\n"); |
---|
412 | } |
---|
413 | omCheckAddr(s); |
---|
414 | omFree(slist[i]); |
---|
415 | } |
---|
416 | if (k > 0) s[strlen(s) - (dim == 2 ? 2 : 1)] = '\0'; |
---|
417 | if (typed) strcat(s, ")"); |
---|
418 | omCheckAddr(s); |
---|
419 | omFreeSize(slist, (l->nr+1) * sizeof(char*)); |
---|
420 | return s; |
---|
421 | } |
---|