source: git/Singular/lists.cc @ 16f511

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