source: git/Singular/ipconv.cc @ 9ae5a3

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