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 
25 int lSize(lists L)
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 (L->nr>=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 */
87 lists 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  for(i=j=0;i<=EndIndex;i++,j++)
175  {
176  if (i!=VIndex)
177  {
178  l->m[j]=ul->m[i];
179  memset(&ul->m[i],0,sizeof(ul->m[i]));
180  }
181  else
182  {
183  j--;
184  ul->m[i].CleanUp();
185  }
186  }
187  omFreeSize((ADDRESS)ul->m,(ul->nr+1)*sizeof(sleftv));
189  res->data = (char *)l;
190  return FALSE;
191  }
192  Werror("wrong index %d in list(%d)",VIndex+1,ul->nr+1);
193  return TRUE;
194 }
195 
196 /*2
197 * check, if a list contains any ring dependend data
198 */
200 {
201  if (L==NULL) return FALSE;
202  int i=0;
203  while (i<=L->nr)
204  {
205  if ((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 {
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(&(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  if (weights!=NULL) *weights=NULL;
368  }
369  else
370  {
371  *weights = w;
372  }
373  //Print("find res of length %d (0..%d) L:%d\n",*len,(*len)-1,L->nr);
374  return r;
375 }
376 
377 char* lString(lists l, BOOLEAN typed, int dim)
378 {
379  if (l->nr == -1)
380  {
381  if (typed) return omStrDup("list()");
382  return omStrDup("");
383  }
384 
385  char** slist = (char**) omAlloc((l->nr+1) * sizeof(char*));
386  int i, j, k;
387  char *s;
388  for (i=0, j = 0, k = 0; i<=l->nr; i++)
389  {
390  slist[i] = l->m[i].String(NULL, typed, dim);
391  assume(slist[i] != NULL);
392  omCheckAddr(slist[i]);
393  if (*(slist[i]) != '\0')
394  {
395  j += strlen(slist[i]);
396  k++;
397  }
398  }
399  s = (char*) omAlloc(j+k+2+(typed ? 10 : 0) + (dim == 2 ? k : 0));
400 
401  if (typed)
402  sprintf(s, "list(");
403  else
404  *s = '\0';
405 
406  for (i=0; i<=l->nr; i++)
407  {
408  if (*(slist[i]) != '\0')
409  {
410  strcat(s, slist[i]);
411  strcat(s, ",");
412  if (dim == 2) strcat(s, "\n");
413  }
414  omCheckAddr(s);
415  omFree(slist[i]);
416  }
417  if (k > 0) s[strlen(s) - (dim == 2 ? 2 : 1)] = '\0';
418  if (typed) strcat(s, ")");
419  omCheckAddr(s);
420  omFreeSize(slist, (l->nr+1) * sizeof(char*));
421  return s;
422 }
BOOLEAN lAdd(leftv res, leftv u, leftv v)
Definition: lists.cc:51
#define omAllocBin(bin)
Definition: omAllocDecl.h:205
const CanonicalForm int s
Definition: facAbsFact.cc:55
ideal id_FreeModule(int i, const ring r)
the free module of rank i
sleftv * m
Definition: lists.h:45
void atSet(idhdl root, const char *name, void *data, int typ)
Definition: attrib.cc:156
Class used for (list of) interpreter objects.
Definition: subexpr.h:83
const poly a
Definition: syzextra.cc:212
omBin_t * omBin
Definition: omStructs.h:12
Definition: attrib.h:15
resolvente liFindRes(lists L, int *len, int *typ0, intvec ***weights)
Definition: lists.cc:312
#define idDelete(H)
delete an ideal
Definition: ideals.h:29
BITSET flag
Definition: subexpr.h:91
Definition: lists.h:22
#define FALSE
Definition: auxiliary.h:94
Compatiblity layer for legacy polynomial operations (over currRing)
attr * Attribute()
Definition: subexpr.cc:1392
#define omFreeSize(addr, size)
Definition: omAllocDecl.h:260
intvec * ivCopy(const intvec *o)
Definition: intvec.h:126
#define TRUE
Definition: auxiliary.h:98
void * ADDRESS
Definition: auxiliary.h:115
BOOLEAN lDelete(leftv res, leftv u, leftv v)
Definition: lists.cc:161
void WerrorS(const char *s)
Definition: feFopen.cc:24
int k
Definition: cfEzgcd.cc:93
char * lString(lists l, BOOLEAN typed, int dim)
Definition: lists.cc:377
int Typ()
Definition: subexpr.cc:995
#define omAlloc(size)
Definition: omAllocDecl.h:210
Definition: idrec.h:34
char * String(void *d=NULL, BOOLEAN typed=FALSE, int dim=1)
Called for conversion to string (used by string(..), write(..),..)
Definition: subexpr.cc:751
void * data
Definition: subexpr.h:89
poly res
Definition: myNF.cc:322
ring currRing
Widely used global variable which specifies the current polynomial ring for Singular interpreter and ...
Definition: polys.cc:10
const ring r
Definition: syzextra.cc:208
Definition: intvec.h:14
long id_RankFreeModule(ideal s, ring lmRing, ring tailRing)
return the maximal component number found in any polynomial in s
const CanonicalForm CFMap CFMap & N
Definition: cfEzgcd.cc:49
int j
Definition: myNF.cc:70
Definition: tok.h:58
#define omFree(addr)
Definition: omAllocDecl.h:261
ideal idFreeModule(int i)
Definition: ideals.h:111
#define assume(x)
Definition: mod2.h:394
BOOLEAN lInsert(leftv res, leftv u, leftv v)
Definition: lists.cc:120
void Copy(leftv e)
Definition: subexpr.cc:688
lists lInsert0(lists ul, leftv v, int pos)
Definition: lists.cc:87
int m
Definition: cfEzgcd.cc:119
static int si_max(const int a, const int b)
Definition: auxiliary.h:120
int dim(ideal I, ring r)
int i
Definition: cfEzgcd.cc:123
#define IDELEMS(i)
Definition: simpleideals.h:24
int lSize(lists L)
Definition: lists.cc:25
void idSkipZeroes(ideal ide)
gives an ideal/module the minimal possible size
lists lCopy(lists L)
Definition: lists.cc:32
INLINE_THIS void Init(int l=0)
#define omAlloc0Bin(bin)
Definition: omAllocDecl.h:206
#define omGetSpecBin(size)
Definition: omBin.h:11
ideal idInit(int idsize, int rank)
initialise an ideal / module
Definition: simpleideals.cc:38
const Variable & v
< [in] a sqrfree bivariate poly
Definition: facBivar.h:37
void * atGet(idhdl root, const char *name, int t, void *defaultReturnValue)
Definition: attrib.cc:135
int nr
Definition: lists.h:43
BOOLEAN lInsert3(leftv res, leftv u, leftv v, leftv w)
Definition: lists.cc:135
#define NULL
Definition: omList.c:10
slists * lists
Definition: mpr_numeric.h:146
void pEnlargeSet(poly **p, int l, int increment)
Definition: p_polys.cc:3594
const char * Tok2Cmdname(int tok)
Definition: gentable.cc:132
lists liMakeResolv(resolvente r, int length, int reallen, int typ0, intvec **weights, int add_row_shift)
Definition: lists.cc:215
const CanonicalForm & w
Definition: facAbsFact.cc:55
BOOLEAN lAppend(leftv res, leftv u, leftv v)
Definition: lists.cc:151
int rtyp
Definition: subexpr.h:92
void CleanUp(ring r=currRing)
Definition: subexpr.cc:332
void * Data()
Definition: subexpr.cc:1137
Definition: tok.h:117
ideal * resolvente
Definition: ideals.h:18
attr Copy()
Definition: attrib.cc:41
attr attribute
Definition: subexpr.h:90
omBin slists_bin
Definition: lists.cc:23
#define omCheckAddr(addr)
Definition: omAllocDecl.h:328
#define omFreeBin(addr, bin)
Definition: omAllocDecl.h:259
int BOOLEAN
Definition: auxiliary.h:85
BOOLEAN idIs0(ideal h)
returns true if h is the zero ideal
#define NONE
Definition: tok.h:216
BOOLEAN lRingDependend(lists L)
Definition: lists.cc:199
void Werror(const char *fmt,...)
Definition: reporter.cc:189
void * CopyD(int t)
Definition: subexpr.cc:707
#define omAlloc0(size)
Definition: omAllocDecl.h:211
int l
Definition: cfEzgcd.cc:94
#define Warn
Definition: emacs.cc:80
#define omStrDup(s)
Definition: omAllocDecl.h:263