source: git/Singular/ipconv.cc @ c7ae4d

spielwiese
Last change on this file since c7ae4d was c7ae4d, checked in by Hans Schoenemann <hannes@…>, 8 years ago
Singular_4_1: parent(..)
  • Property mode set to 100644
File size: 12.4 KB
RevLine 
[0e1846]1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/*
5* ABSTRACT: automatic type conversions
6*/
7
[9f7665]8
9
10
[b1dfaf]11#include <kernel/mod2.h>
[599326]12#include <Singular/tok.h>
13#include <Singular/ipid.h>
[0fb34ba]14#include <misc/intvec.h>
15#include <misc/options.h>
[b1dfaf]16#include <omalloc/omalloc.h>
[737a68]17#include <kernel/polys.h>
[599326]18#include <kernel/ideals.h>
19#include <Singular/subexpr.h>
[0fb34ba]20#include <coeffs/numbers.h>
[a0432f]21#include <coeffs/coeffs.h>
[75f10d]22#include <coeffs/bigintmat.h>
[b787fb6]23//#include <polys/ext_fields/longalg.h>
[0fb34ba]24#include <polys/matpol.h>
[44ca2f]25#include <Singular/links/silink.h>
[57fa2c4]26#include <kernel/GBEngine/syz.h>
[599326]27#include <Singular/attrib.h>
[0fb34ba]28#include <polys/monomials/ring.h>
[d693da2]29#include <Singular/ipshell.h>
[afbc156]30#include <Singular/number2.h>
[599326]31#include <Singular/ipconv.h>
[0e1846]32
33typedef void *   (*iiConvertProc)(void * data);
[f43a74]34typedef void    (*iiConvertProcL)(leftv out,leftv in);
[0e1846]35struct sConvertTypes
36{
37  int i_typ;
38  int o_typ;
39  iiConvertProc p;
[f43a74]40  iiConvertProcL pl;
[0e1846]41};
42
[befd6b5]43// all of these static conversion routines work destructive on their input
44
[0e1846]45static void * iiI2P(void *data)
46{
[7447d8]47  poly p=pISet((int)(long)data);
[0e1846]48  return (void *)p;
49}
50
[db5523]51static void * iiBI2P(void *data)
52{
[6a1aa7]53  nMapFunc nMap=n_SetMap(coeffs_BIGINT,currRing->cf);
54  if (nMap==NULL)
55  {
[ca90c60]56    Werror("no conversion from bigint to %s", nCoeffString(currRing->cf));
[6a1aa7]57    return NULL;
58  }
59  number n=nMap((number)data,coeffs_BIGINT,currRing->cf);
[61b2e16]60  n_Delete((number *)&data, coeffs_BIGINT);
61  poly p=p_NSet(n, currRing);
[db5523]62  return (void *)p;
63}
64
[0e1846]65static void * iiI2V(void *data)
66{
[7447d8]67  poly p=pISet((int)(long)data);
[0e1846]68  if (p!=NULL) pSetComp(p,1);
69  return (void *)p;
70}
71
[db5523]72static void * iiBI2V(void *data)
73{
[6a1aa7]74  nMapFunc nMap=n_SetMap(coeffs_BIGINT,currRing->cf);
75  if (nMap==NULL)
76  {
[ca90c60]77    Werror("no conversion from bigint to %s", nCoeffString(currRing->cf));
[6a1aa7]78    return NULL;
79  }
80  number n=nMap((number)data,coeffs_BIGINT,currRing->cf);
[61b2e16]81  n_Delete((number *)&data, coeffs_BIGINT);
82  poly p=p_NSet(n, currRing);
[db5523]83  if (p!=NULL) pSetComp(p,1);
84  return (void *)p;
85}
86
[0e1846]87static void * iiI2Id(void *data)
88{
89  ideal I=idInit(1,1);
[7447d8]90  I->m[0]=pISet((int)(long)data);
[0e1846]91  return (void *)I;
92}
93
[db5523]94static void * iiBI2Id(void *data)
95{
96  ideal I=idInit(1,1);
[6a1aa7]97  nMapFunc nMap=n_SetMap(coeffs_BIGINT,currRing->cf);
98  if (nMap==NULL)
99  {
[ca90c60]100    Werror("no conversion from bigint to %s", nCoeffString(currRing->cf));
[6a1aa7]101    return NULL;
102  }
103  number n=nMap((number)data,coeffs_BIGINT,currRing->cf);
[98b2d31]104  n_Delete((number *)&data,coeffs_BIGINT);
[db5523]105  poly p=pNSet(n);
106  I->m[0]=p;
107  return (void *)I;
108}
[0e1846]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;
[e5324a]123    I->m[0]=p;
[0e1846]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;
[befd6b5]136  pDelete((poly *)&data);
[0e1846]137  return (void *)m;
138}
[6fc941]139
140static void * iiN2P(void *data);
[0e1846]141
142static void * iiDummy(void *data)
143{
144  return data;
145}
146
147static void * iiMo2Ma(void *data)
148{
[39457c]149  void *res=id_Module2Matrix((ideal)data,currRing);
[0e1846]150  return res;
151}
152
153static void * iiMa2Mo(void *data)
154{
[39457c]155  void *res=id_Matrix2Module((matrix)data,currRing);
[0e1846]156  return res;
157}
158
159static void * iiI2Iv(void *data)
160{
[7447d8]161  int s=(int)(long)data;
162  intvec *iv=new intvec(s,s);
[0e1846]163  return (void *)iv;
164}
165
166static void * iiI2N(void *data)
167{
[7447d8]168  number n=nInit((int)(long)data);
[0e1846]169  return (void *)n;
170}
171
[6fc941]172static void * iiI2BI(void *data)
173{
[98b2d31]174  number n=n_Init((int)(long)data, coeffs_BIGINT);
[6fc941]175  return (void *)n;
176}
177
[afbc156]178#ifdef SINGULAR_4_1
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);
[c7ae4d]187  number2 nn=(number2)omAlloc(sizeof(*nn));
[afbc156]188  nn->cf=currRing->cf; nn->cf->ref++;
189  nn->n=n;
190  return (void *)nn;
191}
[c7ae4d]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}
[afbc156]205#endif
206
[cfce1fc]207static void * iiBI2N(void *data)
208{
209  if (currRing==NULL) return NULL;
[6a1aa7]210  nMapFunc nMap=n_SetMap(coeffs_BIGINT,currRing->cf);
211  if (nMap==NULL)
212  {
[ca90c60]213    Werror("no conversion from bigint to %s", nCoeffString(currRing->cf));
[6a1aa7]214    return NULL;
215  }
216  number n=nMap((number)data,coeffs_BIGINT,currRing->cf);
[98adcd]217  n_Delete((number *)&data, coeffs_BIGINT);
[61b2e16]218  return (void*)n;
[cfce1fc]219}
220
[afbc156]221#ifdef SINGULAR_4_1
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->cfCoeffString(currRing->cf));
233    return NULL;
234  }
235  number n=nMap((number)data,coeffs_BIGINT,currRing->cf);
236  n_Delete((number *)&data, coeffs_BIGINT);
[c7ae4d]237  number2 nn=(number2)omAlloc(sizeof(*nn));
[afbc156]238  nn->cf=currRing->cf; nn->cf->ref++;
[c7ae4d]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->cfCoeffString(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;
[afbc156]267  return (void*)nn;
268}
269#endif
270
271#ifdef SINGULAR_4_1
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_1
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
[0e1846]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  }
[befd6b5]316  delete iv;
[0e1846]317  return (void *)m;
318}
319
[75f10d]320static void * iiIm2Bim(void *data)
321{
[e5324a]322  intvec *iv=(intvec*)data;
323  void *r=(void *)iv2bim(iv,coeffs_BIGINT);
324  delete iv;
325  return r;
[75f10d]326}
327
[0e1846]328static void * iiN2P(void *data)
329{
330  poly p=NULL;
331  if (!nIsZero((number)data))
332  {
[3eabdb]333    p=pNSet((number)data);
[0e1846]334  }
[805b06c]335  //else
336  //{
337  //  nDelete((number *)&data);
[0d84792]338  //}
[0e1846]339  return (void *)p;
340}
341
342static void * iiN2Ma(void *data)
343{
344  ideal I=idInit(1,1);
345  if (!nIsZero((number)data))
346  {
[3eabdb]347    poly p=pNSet((number)data);
[0e1846]348    I->m[0]=p;
349  }
[805b06c]350  //else
351  //{
352  //  nDelete((number *)&data);
[0d84792]353  //}
[0e1846]354  return (void *)I;
355}
356
357static void * iiS2Link(void *data)
358{
[c232af]359  si_link l=(si_link)omAlloc0Bin(ip_link_bin);
[0e1846]360  slInit(l, (char *) data);
[c232af]361  omFree((ADDRESS)data);
[0e1846]362  return (void *)l;
363}
364
[f43a74]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();
[22579cf]372
[f43a74]373  out->data=(void *)syConvRes(tmp,TRUE,add_row_shift);
374}
[dfc6b54]375
[fc866f6]376static void iiL2R(leftv out, leftv in)
[3ebd198]377{
[fc866f6]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  }
[3ebd198]388}
389
[0e1846]390//
[78c048b]391// automatic conversions:
[0e1846]392//
[56c789]393#define IPCONV
[1882ae]394#define D(A)     A
395#define NULL_VAL NULL
[56c789]396#include <Singular/table.h>
[0e1846]397/*2
398* try to convert 'input' of type 'inputType' to 'output' of type 'outputType'
399* return FALSE on success
400*/
[cd73d1]401BOOLEAN iiConvert (int inputType, int outputType, int index, leftv input, leftv output,const struct sConvertTypes *dConvertTypes)
[0e1846]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;
[1c5a1a]415    output->data=(char *)(long)input->Typ();
[0e1846]416    /* the name of the object:*/
417    if (input->e==NULL)
418    {
419      if (input->rtyp==IDHDL)
420      /* preserve name: copy it */
[c232af]421        output->name=omStrDup(IDID((idhdl)(input->data)));
[0e1846]422      else if (input->name!=NULL)
423      {
[98adcd]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        }
[0e1846]431      }
432      else if ((input->rtyp==POLY_CMD) && (input->name==NULL))
433      {
[0d84792]434        if (input->data!=NULL)
[0e1846]435        {
[0d84792]436          int nr=pIsPurePower((poly)input->data);
437          if (nr!=0)
[0e1846]438          {
[0d84792]439            if (pGetExp((poly)input->data,nr)==1)
440            {
[c232af]441              output->name=omStrDup(currRing->names[nr-1]);
[0d84792]442            }
443            else
444            {
[85e68dd]445              char *tmp=(char *)omAlloc(4);
446              sprintf(tmp,"%c%d",*(currRing->names[nr-1]),
447                (int)pGetExp((poly)input->data,nr));
[3f4454b]448              output->name=tmp;
[0d84792]449            }
[0e1846]450          }
[0d84792]451          else if(pIsConstant((poly)input->data))
[0e1846]452          {
[43d920]453            StringSetS("");
454            number n=(pGetCoeff((poly)input->data));
455            n_Write(n, currRing->cf);
456            (pGetCoeff((poly)input->data))=n;
457            output->name=StringEndS();
[0e1846]458          }
[0d84792]459        }
[0e1846]460      }
461      else if ((input->rtyp==NUMBER_CMD) && (input->name==NULL))
462      {
[43d920]463        StringSetS("");
464        number n=(number)input->data;
465        n_Write(n, currRing->cf);
466        input->data=(void*)n;
467        output->name=StringEndS();
[0e1846]468      }
469      else
470      {
471        /* no need to preserve name: use it */
472        output->name=input->name;
473        memset(input,0,sizeof(*input));
474      }
475    }
476    output->next=input->next;
477    input->next=NULL;
[afbc156]478    return errorreported;
[0e1846]479  }
480  if (index!=0) /* iiTestConvert does not returned 'failure' */
481  {
482    index--;
483
484    if((dConvertTypes[index].i_typ==inputType)
485    &&(dConvertTypes[index].o_typ==outputType))
486    {
[a97ac0]487      if(traceit&TRACE_CONV)
[d693da2]488      {
489        Print("automatic  conversion %s -> %s\n",
[3f4454b]490        Tok2Cmdname(inputType),Tok2Cmdname(outputType));
[d693da2]491      }
[0e1846]492      if ((currRing==NULL) && (outputType>BEGIN_RING) && (outputType<END_RING))
493        return TRUE;
494      output->rtyp=outputType;
[f43a74]495      if (dConvertTypes[index].p!=NULL)
496      {
497        output->data=dConvertTypes[index].p(input->CopyD());
498      }
499      else
500      {
501        dConvertTypes[index].pl(output,input);
502      }
[0e1846]503      if ((output->data==NULL)
[3f4454b]504      && ((outputType!=INT_CMD)
505        &&(outputType!=POLY_CMD)
506        &&(outputType!=VECTOR_CMD)
507        &&(outputType!=NUMBER_CMD)))
[0e1846]508      {
509        return TRUE;
510      }
[afbc156]511      if (errorreported) return TRUE;
[0e1846]512      output->next=input->next;
513      input->next=NULL;
[bc669c]514  //if (outputType==MATRIX_CMD) Print("convert %d -> matrix\n",inputType);
[0e1846]515      return FALSE;
516    }
517  }
518  return TRUE;
519}
520
521/*2
522* try to convert 'inputType' in 'outputType'
523* return 0 on failure, an index (<>0) on success
524*/
[cd73d1]525int iiTestConvert (int inputType, int outputType,const struct sConvertTypes *dConvertTypes)
[0e1846]526{
527  if ((inputType==outputType)
528  || (outputType==DEF_CMD)
529  || (outputType==IDHDL)
530  || (outputType==ANY_TYPE))
531  {
532    return -1;
533  }
[52d6d8]534  if (inputType==UNKNOWN) return 0;
[0e1846]535
536  if ((currRing==NULL) && (outputType>BEGIN_RING) && (outputType<END_RING))
537    return 0;
[afbc156]538  //if ((currRing==NULL) && (outputType==CNUMBER_CMD))
539  //  return 0;
[0e1846]540
541  // search the list
542  int i=0;
543  while (dConvertTypes[i].i_typ!=0)
544  {
545    if((dConvertTypes[i].i_typ==inputType)
546    &&(dConvertTypes[i].o_typ==outputType))
547    {
548      //Print("test convert %d to %d (%s -> %s):%d\n",inputType,outputType,
549      //Tok2Cmdname(inputType), Tok2Cmdname(outputType),i+1);
550      return i+1;
551    }
552    i++;
553  }
[afbc156]554  //Print("test convert %d to %d (%s -> %s):0, tested:%d\n",inputType,outputType,
555  // Tok2Cmdname(inputType), Tok2Cmdname(outputType),i);
[0e1846]556  return 0;
557}
Note: See TracBrowser for help on using the repository browser.