source: git/Singular/ipconv.cc @ 61c85ae

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