source: git/Singular/ipconv.cc @ 73f3dcc

spielwiese
Last change on this file since 73f3dcc was 73f3dcc, checked in by Hans Schoenemann <hannes@…>, 5 years ago
fix: memory leak in conversion polyBucket->poly
  • Property mode set to 100644
File size: 13.4 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/*
5* ABSTRACT: automatic type conversions
6*/
7
8
9
10
11#include "kernel/mod2.h"
12#include "Singular/tok.h"
13#include "Singular/ipid.h"
14#include "misc/intvec.h"
15#include "misc/options.h"
16#include "omalloc/omalloc.h"
17#include "kernel/polys.h"
18#include "kernel/ideals.h"
19#include "Singular/subexpr.h"
20#include "coeffs/numbers.h"
21#include "coeffs/coeffs.h"
22#include "coeffs/bigintmat.h"
23//#include "polys/ext_fields/longalg.h"
24#include "polys/matpol.h"
25#include "Singular/links/silink.h"
26#include "kernel/GBEngine/syz.h"
27#include "Singular/attrib.h"
28#include "polys/monomials/ring.h"
29#include "Singular/ipshell.h"
30#include "Singular/number2.h"
31#include "Singular/ipconv.h"
32
33typedef void *   (*iiConvertProc)(void * data);
34typedef void    (*iiConvertProcL)(leftv out,leftv in);
35struct sConvertTypes
36{
37  int i_typ;
38  int o_typ;
39  iiConvertProc p;
40  iiConvertProcL pl;
41};
42
43// all of these static conversion routines work destructive on their input
44
45static void * iiI2P(void *data)
46{
47  poly p=pISet((int)(long)data);
48  return (void *)p;
49}
50
51static void * iiBI2P(void *data)
52{
53  nMapFunc nMap=n_SetMap(coeffs_BIGINT,currRing->cf);
54  if (nMap==NULL)
55  {
56    Werror("no conversion from bigint to %s", nCoeffName(currRing->cf));
57    return NULL;
58  }
59  number n=nMap((number)data,coeffs_BIGINT,currRing->cf);
60  n_Delete((number *)&data, coeffs_BIGINT);
61  poly p=p_NSet(n, currRing);
62  return (void *)p;
63}
64
65static void iiBu2P(leftv out, leftv in)
66{
67  sBucket_pt b=(sBucket_pt)in->Data();
68  poly p; int l;
69  sBucketDestroyAdd(b,&p,&l);
70  out->data=(void*)p;
71}
72
73static void * iiI2V(void *data)
74{
75  poly p=pISet((int)(long)data);
76  if (p!=NULL) pSetComp(p,1);
77  return (void *)p;
78}
79
80static void * iiBI2V(void *data)
81{
82  nMapFunc nMap=n_SetMap(coeffs_BIGINT,currRing->cf);
83  if (nMap==NULL)
84  {
85    Werror("no conversion from bigint to %s", nCoeffName(currRing->cf));
86    return NULL;
87  }
88  number n=nMap((number)data,coeffs_BIGINT,currRing->cf);
89  n_Delete((number *)&data, coeffs_BIGINT);
90  poly p=p_NSet(n, currRing);
91  if (p!=NULL) pSetComp(p,1);
92  return (void *)p;
93}
94
95static void * iiI2Id(void *data)
96{
97  ideal I=idInit(1,1);
98  I->m[0]=pISet((int)(long)data);
99  return (void *)I;
100}
101
102static void * iiBI2Id(void *data)
103{
104  ideal I=idInit(1,1);
105  nMapFunc nMap=n_SetMap(coeffs_BIGINT,currRing->cf);
106  if (nMap==NULL)
107  {
108    Werror("no conversion from bigint to %s", nCoeffName(currRing->cf));
109    return NULL;
110  }
111  number n=nMap((number)data,coeffs_BIGINT,currRing->cf);
112  n_Delete((number *)&data,coeffs_BIGINT);
113  poly p=pNSet(n);
114  I->m[0]=p;
115  return (void *)I;
116}
117static void * iiBu2V(void *data)
118{
119  poly p=NULL;
120  if (data!=NULL)
121  {
122    sBucket_pt b=(sBucket_pt)data;
123    int l;
124    sBucketDestroyAdd(b,&p,&l);
125    if (p!=NULL) pSetCompP(p,1);
126  }
127  return (void *)p;
128}
129
130static void * iiP2V(void *data)
131{
132  poly p=(poly)data;
133  if (p!=NULL) pSetCompP(p,1);
134  return (void *)p;
135}
136
137static void * iiBu2Id(void *data)
138{
139  ideal I=idInit(1,1);
140
141  if (data!=NULL)
142  {
143    sBucket_pt b=(sBucket_pt)data;
144    poly p; int l;
145    sBucketDestroyAdd(b,&p,&l);
146    I->m[0]=p;
147  }
148  return (void *)I;
149}
150
151static void * iiP2Id(void *data)
152{
153  ideal I=idInit(1,1);
154
155  if (data!=NULL)
156  {
157    poly p=(poly)data;
158    I->m[0]=p;
159    if (pGetComp(p)!=0) I->rank=pMaxComp(p);
160  }
161  return (void *)I;
162}
163
164static void * iiV2Ma(void *data)
165{
166  matrix m=(matrix)idVec2Ideal((poly)data);
167  int h=MATCOLS(m);
168  MATCOLS(m)=MATROWS(m);
169  MATROWS(m)=h;
170  m->rank=h;
171  pDelete((poly *)&data);
172  return (void *)m;
173}
174
175static void * iiN2P(void *data);
176
177static void * iiDummy(void *data)
178{
179  return data;
180}
181
182static void * iiMo2Ma(void *data)
183{
184  void *res=id_Module2Matrix((ideal)data,currRing);
185  return res;
186}
187
188static void * iiMa2Mo(void *data)
189{
190  void *res=id_Matrix2Module((matrix)data,currRing);
191  return res;
192}
193
194static void * iiI2Iv(void *data)
195{
196  int s=(int)(long)data;
197  intvec *iv=new intvec(s,s);
198  return (void *)iv;
199}
200
201static void * iiI2N(void *data)
202{
203  number n=nInit((int)(long)data);
204  return (void *)n;
205}
206
207static void * iiI2BI(void *data)
208{
209  number n=n_Init((int)(long)data, coeffs_BIGINT);
210  return (void *)n;
211}
212
213#ifdef SINGULAR_4_2
214static void * iiI2NN(void *data)
215{
216  if (currRing==NULL)
217  {
218    WerrorS("missing basering while converting int to Number");
219    return NULL;
220  }
221  number n=nInit((int)(long)data);
222  number2 nn=(number2)omAlloc(sizeof(*nn));
223  nn->cf=currRing->cf; nn->cf->ref++;
224  nn->n=n;
225  return (void *)nn;
226}
227static void * iiI2CP(void *data)
228{
229  if (currRing==NULL)
230  {
231    WerrorS("missing basering while converting int to Poly");
232    return NULL;
233  }
234  poly n=pISet((int)(long)data);
235  poly2 nn=(poly2)omAlloc(sizeof(*nn));
236  nn->cf=currRing; nn->cf->ref++;
237  nn->n=n;
238  return (void *)nn;
239}
240#endif
241
242static void * iiBI2N(void *data)
243{
244  if (currRing==NULL) return NULL;
245  nMapFunc nMap=n_SetMap(coeffs_BIGINT,currRing->cf);
246  if (nMap==NULL)
247  {
248    Werror("no conversion from bigint to %s", nCoeffName(currRing->cf));
249    return NULL;
250  }
251  number n=nMap((number)data,coeffs_BIGINT,currRing->cf);
252  n_Delete((number *)&data, coeffs_BIGINT);
253  return (void*)n;
254}
255
256#ifdef SINGULAR_4_2
257static void * iiBI2NN(void *data)
258{
259  if (currRing==NULL)
260  {
261    WerrorS("missing basering while converting bigint to Number");
262    return NULL;
263  }
264  nMapFunc nMap=n_SetMap(coeffs_BIGINT,currRing->cf);
265  if (nMap==NULL)
266  {
267    Werror("no conversion from bigint to %s",currRing->cf->cfCoeffName(currRing->cf));
268    return NULL;
269  }
270  number n=nMap((number)data,coeffs_BIGINT,currRing->cf);
271  n_Delete((number *)&data, coeffs_BIGINT);
272  number2 nn=(number2)omAlloc(sizeof(*nn));
273  nn->cf=currRing->cf; nn->cf->ref++;
274  nn->n=n;
275  return (void*)nn;
276}
277static void * iiBI2CP(void *data)
278{
279  if (currRing==NULL)
280  {
281    WerrorS("missing basering while converting bigint to Poly");
282    return NULL;
283  }
284  nMapFunc nMap=n_SetMap(coeffs_BIGINT,currRing->cf);
285  if (nMap==NULL)
286  {
287    Werror("no conversion from bigint to %s",currRing->cf->cfCoeffName(currRing->cf));
288    return NULL;
289  }
290  number n=nMap((number)data,coeffs_BIGINT,currRing->cf);
291  n_Delete((number *)&data, coeffs_BIGINT);
292  poly2 nn=(poly2)omAlloc(sizeof(*nn));
293  nn->cf=currRing; nn->cf->ref++;
294  nn->n=pNSet(n);
295  return (void*)nn;
296}
297static void * iiP2CP(void *data)
298{
299  poly2 nn=(poly2)omAlloc(sizeof(*nn));
300  nn->cf=currRing; nn->cf->ref++;
301  nn->n=(poly)data;
302  return (void*)nn;
303}
304#endif
305
306#ifdef SINGULAR_4_2
307static void * iiNN2N(void *data)
308{
309  number2 d=(number2)data;
310  if ((currRing==NULL)
311  || (currRing->cf!=d->cf))
312  {
313    WerrorS("cannot convert: incompatible");
314    return NULL;
315  }
316  number n = n_Copy(d->n, d->cf);
317  n2Delete(d);
318  return (void*)n;
319}
320#endif
321
322#ifdef SINGULAR_4_2
323static void * iiNN2P(void *data)
324{
325  number2 d=(number2)data;
326  if ((currRing==NULL)
327  || (currRing->cf!=d->cf))
328  {
329    WerrorS("cannot convert: incompatible");
330    return NULL;
331  }
332  number n = n_Copy(d->n, d->cf);
333  n2Delete(d);
334  return (void*)p_NSet(n,currRing);
335}
336#endif
337
338static void * iiIm2Ma(void *data)
339{
340  int i, j;
341  intvec *iv = (intvec *)data;
342  matrix m = mpNew(iv->rows(), iv->cols());
343
344  for (i=iv->rows(); i>0; i--)
345  {
346    for (j=iv->cols(); j>0; j--)
347    {
348      MATELEM(m, i, j) = pISet(IMATELEM(*iv, i, j));
349    }
350  }
351  delete iv;
352  return (void *)m;
353}
354
355static void * iiIm2Bim(void *data)
356{
357  intvec *iv=(intvec*)data;
358  void *r=(void *)iv2bim(iv,coeffs_BIGINT);
359  delete iv;
360  return r;
361}
362
363static void * iiN2P(void *data)
364{
365  poly p=NULL;
366  if (!nIsZero((number)data))
367  {
368    p=pNSet((number)data);
369  }
370  //else
371  //{
372  //  nDelete((number *)&data);
373  //}
374  return (void *)p;
375}
376
377static void * iiN2Ma(void *data)
378{
379  ideal I=idInit(1,1);
380  if (!nIsZero((number)data))
381  {
382    poly p=pNSet((number)data);
383    I->m[0]=p;
384  }
385  //else
386  //{
387  //  nDelete((number *)&data);
388  //}
389  return (void *)I;
390}
391
392static void * iiS2Link(void *data)
393{
394  si_link l=(si_link)omAlloc0Bin(ip_link_bin);
395  slInit(l, (char *) data);
396  omFree((ADDRESS)data);
397  return (void *)l;
398}
399
400static void iiR2L_l(leftv out, leftv in)
401{
402  int add_row_shift = 0;
403  intvec *weights=(intvec*)atGet(in,"isHomog",INTVEC_CMD);
404  if (weights!=NULL)  add_row_shift=weights->min_in();
405
406  syStrategy tmp=(syStrategy)in->CopyD();
407
408  out->data=(void *)syConvRes(tmp,TRUE,add_row_shift);
409}
410
411static void iiL2R(leftv out, leftv in)
412{
413  //int add_row_shift = 0;
414  lists l=(lists)in->Data();
415  intvec *ww=NULL;
416  if (l->nr>=0) ww=(intvec *)atGet(&(l->m[0]),"isHomog",INTVEC_CMD);
417  out->data=(void *)syConvList(l);
418  if (ww!=NULL)
419  {
420    intvec *weights=ivCopy(ww);
421    atSet(out,omStrDup("isHomog"),weights,INTVEC_CMD);
422  }
423}
424
425//
426// automatic conversions:
427//
428#define IPCONV
429#define D(A)     A
430#define NULL_VAL NULL
431#include "Singular/table.h"
432/*2
433* try to convert 'input' of type 'inputType' to 'output' of type 'outputType'
434* return FALSE on success
435*/
436BOOLEAN iiConvert (int inputType, int outputType, int index, leftv input, leftv output,const struct sConvertTypes *dConvertTypes)
437{
438  memset(output,0,sizeof(sleftv));
439  if ((inputType==outputType)
440  || (outputType==DEF_CMD)
441  || ((outputType==IDHDL)&&(input->rtyp==IDHDL)))
442  {
443    memcpy(output,input,sizeof(*output));
444    memset(input,0,sizeof(*input));
445    return FALSE;
446  }
447  else if (outputType==ANY_TYPE)
448  {
449    output->rtyp=ANY_TYPE;
450    output->data=(char *)(long)input->Typ();
451    /* the name of the object:*/
452    if (input->e==NULL)
453    {
454      if (input->rtyp==IDHDL)
455      /* preserve name: copy it */
456        output->name=omStrDup(IDID((idhdl)(input->data)));
457      else if (input->name!=NULL)
458      {
459        if (input->rtyp==ALIAS_CMD)
460        output->name=omStrDup(input->name);
461        else
462        {
463          output->name=input->name;
464          input->name=NULL;
465        }
466      }
467      else if ((input->rtyp==POLY_CMD) && (input->name==NULL))
468      {
469        if (input->data!=NULL)
470        {
471          int nr=pIsPurePower((poly)input->data);
472          if (nr!=0)
473          {
474            if (pGetExp((poly)input->data,nr)==1)
475            {
476              output->name=omStrDup(currRing->names[nr-1]);
477            }
478            else
479            {
480              char *tmp=(char *)omAlloc(4);
481              sprintf(tmp,"%c%d",*(currRing->names[nr-1]),
482                (int)pGetExp((poly)input->data,nr));
483              output->name=tmp;
484            }
485          }
486          else if(pIsConstant((poly)input->data))
487          {
488            StringSetS("");
489            number n=(pGetCoeff((poly)input->data));
490            n_Write(n, currRing->cf);
491            (pGetCoeff((poly)input->data))=n; // n_Write may have changed n
492            output->name=StringEndS();
493          }
494        }
495      }
496      else if ((input->rtyp==NUMBER_CMD) && (input->name==NULL))
497      {
498        StringSetS("");
499        number n=(number)input->data;
500        n_Write(n, currRing->cf);
501        input->data=(void*)n; // n_Write may have changed n
502        output->name=StringEndS();
503      }
504      else
505      {
506        /* no need to preserve name: use it */
507        output->name=input->name;
508        input->name=NULL;
509      }
510    }
511    output->next=input->next;
512    input->next=NULL;
513    if (!errorreported) input->CleanUp();
514    return errorreported;
515  }
516  if (index!=0) /* iiTestConvert does not returned 'failure' */
517  {
518    index--;
519
520    if((dConvertTypes[index].i_typ==inputType)
521    &&(dConvertTypes[index].o_typ==outputType))
522    {
523      if(traceit&TRACE_CONV)
524      {
525        Print("automatic  conversion %s -> %s\n",
526        Tok2Cmdname(inputType),Tok2Cmdname(outputType));
527      }
528      if ((currRing==NULL) && (outputType>BEGIN_RING) && (outputType<END_RING))
529        return TRUE;
530      output->rtyp=outputType;
531      if (dConvertTypes[index].p!=NULL)
532      {
533        output->data=dConvertTypes[index].p(input->CopyD());
534      }
535      else
536      {
537        dConvertTypes[index].pl(output,input);
538      }
539      if ((output->data==NULL)
540      && ((outputType!=INT_CMD)
541        &&(outputType!=POLY_CMD)
542        &&(outputType!=VECTOR_CMD)
543        &&(outputType!=NUMBER_CMD)))
544      {
545        return TRUE;
546      }
547      if (errorreported) return TRUE;
548      output->next=input->next;
549      input->next=NULL;
550      if ((input->rtyp!=IDHDL) && (input->attribute!=NULL))
551      {
552        input->attribute->killAll(currRing);
553        input->attribute=NULL;
554      }
555      if (input->e!=NULL)
556      {
557        Subexpr h;
558        while (input->e!=NULL)
559        {
560          h=input->e->next;
561          omFreeBin((ADDRESS)input->e, sSubexpr_bin);
562          input->e=h;
563        }
564      }
565      //input->Init(); // seems that input (rtyp?) is still needed
566      return FALSE;
567    }
568  }
569  return TRUE;
570}
571
572/*2
573* try to convert 'inputType' in 'outputType'
574* return 0 on failure, an index (<>0) on success
575*/
576int iiTestConvert (int inputType, int outputType,const struct sConvertTypes *dConvertTypes)
577{
578  if ((inputType==outputType)
579  || (outputType==DEF_CMD)
580  || (outputType==IDHDL)
581  || (outputType==ANY_TYPE))
582  {
583    return -1;
584  }
585  if (inputType==UNKNOWN) return 0;
586
587  if ((currRing==NULL) && (outputType>BEGIN_RING) && (outputType<END_RING))
588    return 0;
589  //if ((currRing==NULL) && (outputType==CNUMBER_CMD))
590  //  return 0;
591
592  // search the list
593  int i=0;
594  while (dConvertTypes[i].i_typ!=0)
595  {
596    if((dConvertTypes[i].i_typ==inputType)
597    &&(dConvertTypes[i].o_typ==outputType))
598    {
599      //Print("test convert %d to %d (%s -> %s):%d\n",inputType,outputType,
600      //Tok2Cmdname(inputType), Tok2Cmdname(outputType),i+1);
601      return i+1;
602    }
603    i++;
604  }
605  //Print("test convert %d to %d (%s -> %s):0, tested:%d\n",inputType,outputType,
606  // Tok2Cmdname(inputType), Tok2Cmdname(outputType),i);
607  return 0;
608}
Note: See TracBrowser for help on using the repository browser.