My Project
Loading...
Searching...
No Matches
lists.cc
Go to the documentation of this file.
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
11
12
13#include "kernel/mod2.h"
14#include "Singular/tok.h"
15//#include "ipid.h"
16#include "kernel/polys.h"
17#include "kernel/ideals.h"
18#include "Singular/attrib.h"
19#include "Singular/ipshell.h"
20#include "misc/intvec.h"
21#include "Singular/lists.h"
22
24
26{
27 int n=L->nr;
28 while ((n>=0)&&((L->m[n].rtyp==DEF_CMD)||(L->m[n].rtyp==0))) n--;
29 return n;
30}
31
33{
35 int n=L->nr;
36 if (n>=0)
37 N->Init(n+1);
38 else
39 N->Init();
40 for(;n>=0;n--)
41 {
42 N->m[n].Copy(&L->m[n]);
43 }
44 //Print("copy list with %d -> %d elems\n",L->nr,N->nr);
45 return N;
46}
47
48/*2
49* concat 2 lists
50*/
52{
54 lists ul=(lists)u->CopyD();
55 lists vl=(lists)v->CopyD();
56 l->Init(ul->nr+vl->nr+2);
57 int i;
58
59 for(i=0;i<=ul->nr;i++)
60 {
61 //Print("u[%d]->r[%d]\n",i,i);
62 l->m[i].rtyp=ul->m[i].rtyp;
63 l->m[i].data=ul->m[i].data;
64 }
65 for(i=0;i<=vl->nr;i++)
66 {
67 //Print("v[%d]->r[%d]\n",i,i+ul->nr+1);
68 l->m[i+ul->nr+1].rtyp=vl->m[i].rtyp;
69 l->m[i+ul->nr+1].data=vl->m[i].data;
70 }
71 if (ul->m != NULL)
72 omFreeSize((ADDRESS)ul->m,(ul->nr+1)*sizeof(sleftv));
74 if (vl->m != NULL)
75 omFreeSize((ADDRESS)vl->m,(vl->nr+1)*sizeof(sleftv));
77 memset(u,0,sizeof(*u));
78 memset(v,0,sizeof(*v));
79 res->data = (char *)l;
80 //res->Print();
81 return FALSE;
82}
83
84/*2
85* insert v into list ul, destroys u
86*/
87lists lInsert0(lists ul, leftv v, int pos)
88{
89 if ((pos<0)||(v->rtyp==NONE))
90 return NULL;
92 l->Init(si_max(ul->nr+2,pos+1));
93 int i,j;
94
95 for(i=j=0;i<=ul->nr;i++,j++)
96 {
97 if(j==pos) j++;
98 l->m[j]=ul->m[i];
99 }
100 for(j=ul->nr+1;j<pos;j++)
101 l->m[j].rtyp=DEF_CMD;
102 // memset(&(l->m[pos]),0,sizeof(sleftv)); - done by Init
103 l->m[pos].rtyp=v->Typ();
104 l->m[pos].data=v->CopyD();
105 l->m[pos].flag=v->flag;
106 attr *a=v->Attribute();
107 if ((a!=NULL)&&(*a!=NULL))
108 {
109 l->m[pos].attribute=(*a)->Copy();
110 }
111 if (ul->m != NULL)
112 omFreeSize((ADDRESS)ul->m,(ul->nr+1)*sizeof(sleftv));
114 return l;
115}
116
117/*2
118* insert v into list u, at the beginning
119*/
121{
122 lists ul=(lists)u->CopyD();
123 res->data=(char *)lInsert0(ul,v,0);
124 if (res->data==NULL)
125 {
126 Werror("cannot insert type `%s`",Tok2Cmdname(v->Typ()));
127 return TRUE;
128 }
129 return FALSE;
130}
131
132/*2
133* insert v into list u at pos w
134*/
136{
137 lists ul=(lists)u->CopyD();
138 res->data=(char *)lInsert0(ul,v,(int)(long)w->Data());
139 if (res->data==NULL)
140 {
141 Werror("cannot insert type `%s` at pos. %d",
142 Tok2Cmdname(v->Typ()),(int)(long)w->Data());
143 return TRUE;
144 }
145 return FALSE;
146}
147
148/*2
149* append v to list u
150*/
152{
153 lists ul=(lists)u->CopyD();
154 res->data=(char *)lInsert0(ul,v,ul->nr+1);
155 return (res->data==NULL);
156}
157
158/*2
159* delete v-th element from list u
160*/
162{
163 lists ul=(lists)u->Data();
164 int VIndex=(int)(long)v->Data()-1;
165 int EndIndex=lSize(ul);
166
167 if((0<=VIndex)&&(VIndex<=ul->nr))
168 {
169 ul=(lists)u->CopyD();
170 int i,j;
172 l->Init(EndIndex+(VIndex>EndIndex));
173
174 ul->m[VIndex].CleanUp();
175 for(i=0;i<VIndex;i++)
176 {
177 l->m[i]=ul->m[i];
178 }
179 for(i=VIndex+1;i<=ul->nr;i++)
180 {
181 l->m[i-1]=ul->m[i];
182 }
183 omFreeSize((ADDRESS)ul->m,(ul->nr+1)*sizeof(sleftv));
185 res->data = (char *)l;
186 return FALSE;
187 }
188 Werror("wrong index %d in list(%d)",VIndex+1,ul->nr+1);
189 return TRUE;
190}
191
193{
194 lists ul=(lists)u->CopyD();
195 intvec* vl=(intvec*)v->Data();
196 int i,j,cnt;
197 cnt=0;
198 for(i=vl->length()-1;i>=0;i--)
199 {
200 j=(*vl)[i];
201 if ((j>0)&&(j<=ul->nr))
202 {
203 cnt++;
204 ul->m[j-1].CleanUp();
205 memcpy(&(ul->m[j-1]),&(ul->m[j]),(ul->nr-j+1)*sizeof(sleftv));
206 ul->m[ul->nr].rtyp=DEF_CMD;
207 ul->m[ul->nr].data=NULL;
208 }
209 }
210 if ((cnt*2>=ul->nr)||(cnt*sizeof(sleftv)>=1024))
211 {
212 ul->m=(leftv)omReallocSize(ul->m,(ul->nr+1)*sizeof(sleftv),(ul->nr-cnt+1)*sizeof(sleftv));
213 ul->nr -= cnt;
214 }
215 res->data = (char *)ul;
216 return FALSE;
217}
218
219/*2
220* check, if a list contains any ring dependend data
221*/
223{
224 if (L==NULL) return FALSE;
225 int i=L->nr;
226 while (i>=0)
227 {
228 REGISTER int t=L->m[i].rtyp;
229 if ((BEGIN_RING<t /*L->m[i].rtyp*/)
230 && (/*L->m[i].rtyp*/ t<END_RING))
231 return TRUE;
232 if ((/*L->m[i].rtyp*/ t==LIST_CMD)&&lRingDependend((lists)L->m[i].data))
233 return TRUE;
234 i--;
235 }
236 return FALSE;
237}
238
240 int typ0, intvec ** weights, int add_row_shift)
241{
242 // re-uses r, weights[i]
244 if (length<=0)
245 {
246 // handle "empty" resolutions
247 L->Init(0);
248 }
249 else
250 {
251 int oldlength=length;
252 while (r[length-1]==NULL) length--;
253 if (reallen<=0) reallen=currRing->N;
254 reallen=si_max(reallen,length);
255 L->Init(reallen);
256 int i=0;
257
258 while (i<length)
259 {
260 if (r[i]!=NULL)
261 {
262 if (i==0)
263 {
264 L->m[i].rtyp=typ0;
265 int j=IDELEMS(r[0])-1;
266 while ((j>0) && (r[0]->m[j]==NULL)) j--;
267 j++;
268 if (j!=IDELEMS(r[0]))
269 {
270 pEnlargeSet(&(r[0]->m),IDELEMS(r[0]),j-IDELEMS(r[0]));
271 IDELEMS(r[0])=j;
272 }
273 }
274 else
275 {
276 L->m[i].rtyp=MODUL_CMD;
277 int rank=IDELEMS(r[i-1]);
278 if (idIs0(r[i-1]))
279 {
280 idDelete(&(r[i]));
281 r[i]=id_FreeModule(rank, currRing);
282 }
283 else
284 {
285 r[i]->rank=si_max(rank,(int)id_RankFreeModule(r[i], currRing));
286 }
287 idSkipZeroes(r[i]);
288 }
289 L->m[i].data=(void *)r[i];
290 if ((weights!=NULL) && (weights[i]!=NULL))
291 {
292 intvec *w=weights[i];
293 (*w) += add_row_shift;
294 atSet((idhdl)&L->m[i],omStrDup("isHomog"),w,INTVEC_CMD);
295 weights[i] = NULL;
296 }
297 }
298 #ifdef TEST
299 else
300 {
301 // should not happen:
302 WarnS("internal NULL in resolvente");
303 L->m[i].data=(void *)idInit(1,1);
304 }
305 #endif
306 i++;
307 }
308 omFreeSize((ADDRESS)r,oldlength*sizeof(ideal));
309 if (weights!=NULL) omFreeSize(weights,oldlength*sizeof(intvec*));
310 if (i==0)
311 {
312 L->m[0].rtyp=typ0;
313 L->m[0].data=(char *)idInit(1,1);
314 i=1;
315 }
316 while (i<reallen)
317 {
318 L->m[i].rtyp=MODUL_CMD;
319 ideal I=(ideal)L->m[i-1].data;
320 ideal J;
321 int rank=IDELEMS(I);
322 if (idIs0(I))
323 {
324 J=idFreeModule(rank);
325 }
326 else
327 {
328 J=idInit(1,rank);
329 }
330 L->m[i].data=(void *)J;
331 i++;
332 }
333 //Print("make res of length %d (0..%d) L:%d\n",length,length-1,L->nr);
334 }
335 return L;
336}
337
338resolvente liFindRes(lists L, int * len, int *typ0,intvec *** weights)
339{
340 resolvente r;
341 intvec ** w=NULL,*tw=NULL;
342
343 *len=L->nr+1;
344 if (*len<=0)
345 {
346 WerrorS("empty list");
347 return NULL;
348 }
349 r=(ideal *)omAlloc0((*len)*sizeof(ideal));
350 w=(intvec**)omAlloc0((*len)*sizeof(intvec*));
351 int i=0;
352 *typ0=MODUL_CMD;
353 while (i<(*len))
354 {
355 if (L->m[i].rtyp != MODUL_CMD)
356 {
357 if (L->m[i].rtyp!=IDEAL_CMD)
358 {
359 Werror("element %d is not of type module",i+1);
360 omFreeSize((ADDRESS)r,(*len)*sizeof(ideal));
361 return NULL;
362 }
363 *typ0=IDEAL_CMD;
364 }
365 if ((i>0) && (idIs0(r[i-1])))
366 {
367 //*len=i-1;
368 break;
369 }
370 r[i]=(ideal)L->m[i].data;
371 tw=(intvec*)atGet(&(L->m[i]),"isHomog",INTVEC_CMD);
372 if (tw!=NULL)
373 {
374 w[i]=ivCopy(tw);
375 }
376 tw = NULL;
377 i++;
378 }
379 BOOLEAN hom_complex=TRUE;
380 int j=0;
381 while ((j<i) && hom_complex)
382 {
383 hom_complex = hom_complex && (w[j]!=NULL);
384 j++;
385 }
386 if ((!hom_complex) || (weights==NULL))
387 {
388 for (j=0;j<i;j++)
389 {
390 if (w[j]!=NULL) delete w[j];
391 }
392 omFreeSize((ADDRESS)w,(*len)*sizeof(intvec*));
393 if (weights!=NULL) *weights=NULL;
394 }
395 else
396 {
397 *weights = w;
398 }
399 //Print("find res of length %d (0..%d) L:%d\n",*len,(*len)-1,L->nr);
400 return r;
401}
402
403char* lString(lists l, BOOLEAN typed, int dim)
404{
405 if (l->nr == -1)
406 {
407 if (typed) return omStrDup("list()");
408 return omStrDup("");
409 }
410
411 char** slist = (char**) omAlloc((l->nr+1) * sizeof(char*));
412 int i, j, k;
413 char *s;
414 for (i=0, j = 0, k = 0; i<=l->nr; i++)
415 {
416 slist[i] = l->m[i].String(NULL, typed, dim);
417 assume(slist[i] != NULL);
418 omCheckAddr(slist[i]);
419 if (*(slist[i]) != '\0')
420 {
421 j += strlen(slist[i]);
422 k++;
423 }
424 }
425 s = (char*) omAlloc(j+k+2+(typed ? 10 : 0) + (dim == 2 ? k : 0));
426
427 if (typed)
428 sprintf(s, "list(");
429 else
430 *s = '\0';
431
432 for (i=0; i<=l->nr; i++)
433 {
434 if (*(slist[i]) != '\0')
435 {
436 strcat(s, slist[i]);
437 strcat(s, ",");
438 if (dim == 2) strcat(s, "\n");
439 }
440 omCheckAddr(s);
441 omFree(slist[i]);
442 }
443 if (k > 0) s[strlen(s) - (dim == 2 ? 2 : 1)] = '\0';
444 if (typed) strcat(s, ")");
445 omCheckAddr(s);
446 omFreeSize(slist, (l->nr+1) * sizeof(char*));
447 return s;
448}
void atSet(idhdl root, char *name, void *data, int typ)
Definition: attrib.cc:153
void * atGet(idhdl root, const char *name, int t, void *defaultReturnValue)
Definition: attrib.cc:132
static int si_max(const int a, const int b)
Definition: auxiliary.h:124
int BOOLEAN
Definition: auxiliary.h:87
#define TRUE
Definition: auxiliary.h:100
#define FALSE
Definition: auxiliary.h:96
void * ADDRESS
Definition: auxiliary.h:119
const CanonicalForm CFMap CFMap & N
Definition: cfEzgcd.cc:56
int l
Definition: cfEzgcd.cc:100
int m
Definition: cfEzgcd.cc:128
int i
Definition: cfEzgcd.cc:132
int k
Definition: cfEzgcd.cc:99
Definition: idrec.h:35
Definition: intvec.h:23
int length() const
Definition: intvec.h:94
Definition: attrib.h:21
Class used for (list of) interpreter objects.
Definition: subexpr.h:83
void * CopyD(int t)
Definition: subexpr.cc:710
int rtyp
Definition: subexpr.h:91
void * Data()
Definition: subexpr.cc:1162
void * data
Definition: subexpr.h:88
void CleanUp(ring r=currRing)
Definition: subexpr.cc:348
Definition: lists.h:24
sleftv * m
Definition: lists.h:46
INLINE_THIS void Init(int l=0)
int nr
Definition: lists.h:44
#define WarnS
Definition: emacs.cc:78
const CanonicalForm int s
Definition: facAbsFact.cc:51
CanonicalForm res
Definition: facAbsFact.cc:60
const CanonicalForm & w
Definition: facAbsFact.cc:51
const Variable & v
< [in] a sqrfree bivariate poly
Definition: facBivar.h:39
int j
Definition: facHensel.cc:110
void WerrorS(const char *s)
Definition: feFopen.cc:24
const char * Tok2Cmdname(int tok)
Definition: gentable.cc:140
#define VAR
Definition: globaldefs.h:5
@ END_RING
Definition: grammar.cc:310
@ IDEAL_CMD
Definition: grammar.cc:284
@ MODUL_CMD
Definition: grammar.cc:287
#define idDelete(H)
delete an ideal
Definition: ideals.h:29
BOOLEAN idIs0(ideal h)
returns true if h is the zero ideal
ideal * resolvente
Definition: ideals.h:18
ideal idFreeModule(int i)
Definition: ideals.h:111
static BOOLEAN length(leftv result, leftv arg)
Definition: interval.cc:257
intvec * ivCopy(const intvec *o)
Definition: intvec.h:145
char * lString(lists l, BOOLEAN typed, int dim)
Definition: lists.cc:403
BOOLEAN lDelete(leftv res, leftv u, leftv v)
Definition: lists.cc:161
VAR omBin slists_bin
Definition: lists.cc:23
BOOLEAN lRingDependend(lists L)
Definition: lists.cc:222
BOOLEAN lAdd(leftv res, leftv u, leftv v)
Definition: lists.cc:51
lists lInsert0(lists ul, leftv v, int pos)
Definition: lists.cc:87
BOOLEAN lDeleteIV(leftv res, leftv u, leftv v)
Definition: lists.cc:192
BOOLEAN lAppend(leftv res, leftv u, leftv v)
Definition: lists.cc:151
BOOLEAN lInsert3(leftv res, leftv u, leftv v, leftv w)
Definition: lists.cc:135
resolvente liFindRes(lists L, int *len, int *typ0, intvec ***weights)
Definition: lists.cc:338
BOOLEAN lInsert(leftv res, leftv u, leftv v)
Definition: lists.cc:120
lists liMakeResolv(resolvente r, int length, int reallen, int typ0, intvec **weights, int add_row_shift)
Definition: lists.cc:239
int lSize(lists L)
Definition: lists.cc:25
lists lCopy(lists L)
Definition: lists.cc:32
#define assume(x)
Definition: mod2.h:389
slists * lists
Definition: mpr_numeric.h:146
#define omStrDup(s)
Definition: omAllocDecl.h:263
#define omFreeSize(addr, size)
Definition: omAllocDecl.h:260
#define omCheckAddr(addr)
Definition: omAllocDecl.h:328
#define omAlloc(size)
Definition: omAllocDecl.h:210
#define omReallocSize(addr, o_size, size)
Definition: omAllocDecl.h:220
#define omAllocBin(bin)
Definition: omAllocDecl.h:205
#define omAlloc0Bin(bin)
Definition: omAllocDecl.h:206
#define omFree(addr)
Definition: omAllocDecl.h:261
#define omAlloc0(size)
Definition: omAllocDecl.h:211
#define omFreeBin(addr, bin)
Definition: omAllocDecl.h:259
#define omGetSpecBin(size)
Definition: omBin.h:11
#define NULL
Definition: omList.c:12
omBin_t * omBin
Definition: omStructs.h:12
#define REGISTER
Definition: omalloc.h:27
void pEnlargeSet(poly **p, int l, int increment)
Definition: p_polys.cc:3696
VAR ring currRing
Widely used global variable which specifies the current polynomial ring for Singular interpreter and ...
Definition: polys.cc:13
Compatibility layer for legacy polynomial operations (over currRing)
void Werror(const char *fmt,...)
Definition: reporter.cc:189
ideal idInit(int idsize, int rank)
initialise an ideal / module
Definition: simpleideals.cc:35
ideal id_FreeModule(int i, const ring r)
the free module of rank i
long id_RankFreeModule(ideal s, ring lmRing, ring tailRing)
return the maximal component number found in any polynomial in s
void idSkipZeroes(ideal ide)
gives an ideal/module the minimal possible size
#define IDELEMS(i)
Definition: simpleideals.h:23
sleftv * leftv
Definition: structs.h:57
@ LIST_CMD
Definition: tok.h:118
@ INTVEC_CMD
Definition: tok.h:101
@ DEF_CMD
Definition: tok.h:58
#define NONE
Definition: tok.h:221
int dim(ideal I, ring r)