source: git/Singular/lists.cc @ 665ca8

spielwiese
Last change on this file since 665ca8 was 5c8906, checked in by Hans Schoenemann <hannes@…>, 12 years ago
fix: lSize (from master)
  • Property mode set to 100644
File size: 8.8 KB
RevLine 
[0e1846]1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/*
5* ABSTRACT: handling of the list type
6*/
[ea0601]7// to produce a non-inline version from lists.h
[0f1252]8#define LISTS_CC
9
[762407]10#include "config.h"
[b1dfaf]11#include <kernel/mod2.h>
[599326]12#include <Singular/tok.h>
13#include <kernel/febase.h>
[a9a7be]14//#include "ipid.h"
[737a68]15#include <kernel/polys.h>
[599326]16#include <kernel/ideals.h>
17#include <Singular/attrib.h>
18#include <Singular/ipshell.h>
[0fb34ba]19#include <misc/intvec.h>
[599326]20#include <Singular/lists.h>
[0e1846]21
[c232af]22omBin slists_bin = omGetSpecBin(sizeof(slists));
23
[ecf019]24int lSize(lists L)
25{
26  int n=L->nr;
[5c8906]27   while ((n>=0)&&((L->m[n].rtyp==DEF_CMD)||(L->m[n].rtyp==0))) n--;
[ecf019]28  return n;
29}
30
[0e1846]31lists lCopy(lists L)
32{
[c232af]33  lists N=(lists)omAlloc0Bin(slists_bin);
[0e1846]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*/
50BOOLEAN lAdd(leftv res, leftv u, leftv v)
51{
[c232af]52  lists l=(lists) omAllocBin(slists_bin);
[0e1846]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  }
[795c3f]70  if (ul->m != NULL)
[c232af]71    omFreeSize((ADDRESS)ul->m,(ul->nr+1)*sizeof(sleftv));
72  omFreeBin((ADDRESS)ul, slists_bin);
[795c3f]73  if (vl->m != NULL)
[c232af]74    omFreeSize((ADDRESS)vl->m,(vl->nr+1)*sizeof(sleftv));
75  omFreeBin((ADDRESS)vl, slists_bin);
[0e1846]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
[8af95a]84* insert v into list ul, destroys u
[0e1846]85*/
86lists lInsert0(lists ul, leftv v, int pos)
87{
[6ae4f5]88  if ((pos<0)||(v->rtyp==NONE))
89    return NULL;
[c232af]90  lists l=(lists) omAllocBin(slists_bin);
[f43a74]91  l->Init(si_max(ul->nr+2,pos+1));
[0e1846]92  int i,j;
93
94  for(i=j=0;i<=ul->nr;i++,j++)
95  {
96    if(j==pos) j++;
[8af95a]97    l->m[j]=ul->m[i];
[0e1846]98  }
99  for(j=ul->nr+1;j<pos;j++)
100    l->m[j].rtyp=DEF_CMD;
[0c1144]101  // memset(&(l->m[pos]),0,sizeof(sleftv)); - done by Init
[0e1846]102  l->m[pos].rtyp=v->Typ();
103  l->m[pos].data=v->CopyD();
104  l->m[pos].flag=v->flag;
[c227a2f]105  attr *a=v->Attribute();
106  if (a!=NULL)
[06ab6e5]107  {
[c227a2f]108    l->m[pos].attribute=(*a)->Copy();
[06ab6e5]109  }
[795c3f]110  if (ul->m != NULL)
[c232af]111    omFreeSize((ADDRESS)ul->m,(ul->nr+1)*sizeof(sleftv));
112  omFreeBin((ADDRESS)ul, slists_bin);
[0e1846]113  return l;
114}
115
116/*2
117* insert v into list u, at the beginning
118*/
119BOOLEAN lInsert(leftv res, leftv u, leftv v)
120{
121  lists ul=(lists)u->CopyD();
122  res->data=(char *)lInsert0(ul,v,0);
[2d0ae3]123  if (res->data==NULL)
124  {
125    Werror("cannot insert type `%s`",Tok2Cmdname(v->Typ()));
126    return TRUE;
127  }
128  return FALSE;
[0e1846]129}
130
131/*2
132* insert v into list u at pos w
133*/
134BOOLEAN lInsert3(leftv res, leftv u, leftv v, leftv w)
135{
136  lists ul=(lists)u->CopyD();
[7447d8]137  res->data=(char *)lInsert0(ul,v,(int)(long)w->Data());
[2d0ae3]138  if (res->data==NULL)
139  {
140    Werror("cannot insert type `%s` at pos. %d",
[7447d8]141      Tok2Cmdname(v->Typ()),(int)(long)w->Data());
[2d0ae3]142    return TRUE;
143  }
144  return FALSE;
[0e1846]145}
146
147/*2
148* append v to list u
149*/
150BOOLEAN lAppend(leftv res, leftv u, leftv v)
151{
152  lists ul=(lists)u->CopyD();
153  res->data=(char *)lInsert0(ul,v,ul->nr+1);
[6ae4f5]154  return (res->data==NULL);
[0e1846]155}
156
157/*2
158* delete v-th element from list u
159*/
160BOOLEAN lDelete(leftv res, leftv u, leftv v)
161{
162  lists ul=(lists)u->Data();
[7447d8]163  int VIndex=(int)(long)v->Data()-1;
[ecf019]164  int EndIndex=lSize(ul);
[0e1846]165
166  if((0<=VIndex)&&(VIndex<=ul->nr))
167  {
[795c3f]168    ul=(lists)u->CopyD();
[0e1846]169    int i,j;
[c232af]170    lists l=(lists) omAllocBin(slists_bin);
[ecf019]171    l->Init(EndIndex+(VIndex>EndIndex));
[0e1846]172
[ecf019]173    for(i=j=0;i<=EndIndex;i++,j++)
[0e1846]174    {
175      if (i!=VIndex)
176      {
[795c3f]177        l->m[j]=ul->m[i];
[c227a2f]178        memset(&ul->m[i],0,sizeof(ul->m[i]));
[0e1846]179      }
180      else
181      {
182        j--;
183        ul->m[i].CleanUp();
184      }
185    }
[795c3f]186    omFreeSize((ADDRESS)ul->m,(ul->nr+1)*sizeof(sleftv));
187    omFreeBin((ADDRESS)ul, slists_bin);
[0e1846]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*/
198BOOLEAN lRingDependend(lists L)
199{
[9bc0b8]200  if (L==NULL) return FALSE;
[0e1846]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
215lists liMakeResolv(resolvente r, int length, int reallen,
[f43a74]216  int typ0, intvec ** weights, int add_row_shift)
[0e1846]217{
[c232af]218  lists L=(lists)omAlloc0Bin(slists_bin);
[7b5d0a]219  if (length<=0)
220  {
221    // handle "empty" resolutions
222    L->Init(0);
223  }
224  else
[0e1846]225  {
[7b5d0a]226    int oldlength=length;
227    while (r[length-1]==NULL) length--;
[f5a3a23]228    if (reallen<=0) reallen=currRing->N;
[f43a74]229    reallen=si_max(reallen,length);
[7b5d0a]230    L->Init(reallen);
231    int i=0;
[25003c]232
[7b5d0a]233    while (i<length)
[0e1846]234    {
[7b5d0a]235      if (r[i]!=NULL)
[0e1846]236      {
[7b5d0a]237        if (i==0)
[0e1846]238        {
[7b5d0a]239          L->m[i].rtyp=typ0;
[5812c69]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          }
[0e1846]248        }
249        else
250        {
[7b5d0a]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]));
[3630fa8]256            r[i]=id_FreeModule(rank, currRing);
[7b5d0a]257          }
258          else
259          {
[f5a3a23]260            r[i]->rank=si_max(rank,(int)id_RankFreeModule(r[i], currRing));
[7b5d0a]261          }
[b807b0b]262          idSkipZeroes(r[i]);
[7b5d0a]263        }
264        L->m[i].data=(void *)r[i];
265        if ((weights!=NULL) && (weights[i]!=NULL))
266        {
[f43a74]267          intvec *w=ivCopy(weights[i]);
[b794bd3]268          (*w) += add_row_shift;
[f43a74]269          atSet((idhdl)&L->m[i],omStrDup("isHomog"),w,INTVEC_CMD);
[7b5d0a]270          weights[i] = NULL;
[0e1846]271        }
272      }
[326a59]273      #ifdef TEST
[7b5d0a]274      else
[0e1846]275      {
[7b5d0a]276        // should not happen:
277        Warn("internal NULL in resolvente");
278        L->m[i].data=(void *)idInit(1,1);
[0e1846]279      }
[326a59]280      #endif
[7b5d0a]281      i++;
[0e1846]282    }
[c232af]283    omFreeSize((ADDRESS)r,oldlength*sizeof(ideal));
[7b5d0a]284    if (i==0)
[0e1846]285    {
[7b5d0a]286      L->m[0].rtyp=typ0;
287      L->m[0].data=(char *)idInit(1,1);
288      i=1;
[0e1846]289    }
[7b5d0a]290    while (i<reallen)
[0e1846]291    {
[7b5d0a]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++;
[0e1846]306    }
[7b5d0a]307    //Print("make res of length %d (0..%d) L:%d\n",length,length-1,L->nr);
[0e1846]308  }
309  return L;
310}
311
[25003c]312resolvente liFindRes(lists L, int * len, int *typ0,intvec *** weights)
[0e1846]313{
314  resolvente r;
[25003c]315  intvec ** w=NULL,*tw=NULL;
[0e1846]316
317  *len=L->nr+1;
318  if (*len<=0)
319  {
320    WerrorS("empty list");
321    return NULL;
322  }
[c232af]323  r=(ideal *)omAlloc0((*len)*sizeof(ideal));
324  w=(intvec**)omAlloc0((*len)*sizeof(intvec*));
[0e1846]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);
[c232af]334        omFreeSize((ADDRESS)r,(*len)*sizeof(ideal));
[0e1846]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;
[74162eb]345    tw=(intvec*)atGet((idhdl)&L->m[i],"isHomog",INTVEC_CMD);
[25003c]346    if (tw!=NULL)
347    {
348      w[i]=ivCopy(tw);
349    }
350    tw = NULL;
[0e1846]351    i++;
352  }
[25003c]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++)
[c5f17b]363    {
[25003c]364      if (w[j]!=NULL) delete w[j];
[c5f17b]365    }
[c232af]366    omFreeSize((ADDRESS)w,(*len)*sizeof(intvec*));
[25003c]367  }
368  else
369  {
370    *weights = w;
371  }
[0e1846]372  //Print("find res of length %d (0..%d) L:%d\n",*len,(*len)-1,L->nr);
373  return r;
374}
375
[a79a128]376char* lString(lists l, BOOLEAN typed, int dim)
[4b2155]377{
[0c1144]378  if (l->nr == -1)
[7604db]379  {
[c232af]380    if (typed) return omStrDup("list()");
381    return omStrDup("");
[7604db]382  }
[0c1144]383
[c232af]384  char** slist = (char**) omAlloc((l->nr+1) * sizeof(char*));
[4b2155]385  int i, j, k;
386  char *s;
387  for (i=0, j = 0, k = 0; i<=l->nr; i++)
388  {
[7604db]389    slist[i] = l->m[i].String(NULL, typed, dim);
[4b2155]390    assume(slist[i] != NULL);
[c232af]391    omCheckAddr(slist[i]);
[4b2155]392    if (*(slist[i]) != '\0')
393    {
394      j += strlen(slist[i]);
395      k++;
396    }
397  }
[c232af]398  s = (char*) omAlloc(j+k+2+(typed ? 10 : 0) + (dim == 2 ? k : 0));
[7604db]399
400  if (typed)
401    sprintf(s, "list(");
402  else
403    *s = '\0';
404
[4b2155]405  for (i=0; i<=l->nr; i++)
406  {
407    if (*(slist[i]) != '\0')
408    {
409      strcat(s, slist[i]);
410      strcat(s, ",");
[7604db]411      if (dim == 2) strcat(s, "\n");
[4b2155]412    }
[c232af]413    omCheckAddr(s);
414    omFree(slist[i]);
[4b2155]415  }
[7604db]416  if (k > 0) s[strlen(s) - (dim == 2 ? 2 : 1)] = '\0';
417  if (typed) strcat(s, ")");
[c232af]418  omCheckAddr(s);
419  omFreeSize(slist, (l->nr+1) * sizeof(char*));
[4b2155]420  return s;
421}
Note: See TracBrowser for help on using the repository browser.