source: git/Singular/ipconv.cc @ 237b3e4

fieker-DuValspielwiese
Last change on this file since 237b3e4 was 599326, checked in by Kai Krüger <krueger@…>, 14 years ago
Anne, Kai, Frank: - changes to #include "..." statements to allow cleaner build structure - affected directories: omalloc, kernel, Singular - not yet done: IntergerProgramming git-svn-id: file:///usr/local/Singular/svn/trunk@13032 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 10.6 KB
RevLine 
[0e1846]1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
[341696]4/* $Id$ */
[0e1846]5/*
6* ABSTRACT: automatic type conversions
7*/
8
[599326]9#include <Singular/mod2.h>
10#include <Singular/tok.h>
11#include <Singular/ipid.h>
12#include <kernel/intvec.h>
13#include <omalloc.h>
14#include <kernel/febase.h>
15#include <kernel/polys.h>
16#include <kernel/ideals.h>
17#include <Singular/subexpr.h>
18#include <kernel/numbers.h>
19#include <kernel/modulop.h>
20#include <kernel/longrat.h>
21#include <kernel/longalg.h>
[c81a40]22#ifdef HAVE_RINGS
[599326]23#include <kernel/rmodulon.h>
24#include <kernel/rmodulo2m.h>
25#include <kernel/rintegers.h>
[c81a40]26#endif
[599326]27#include <kernel/matpol.h>
28#include <Singular/silink.h>
29#include <kernel/syz.h>
30#include <Singular/attrib.h>
31#include <kernel/ring.h>
32#include <Singular/ipconv.h>
[0e1846]33
34typedef void *   (*iiConvertProc)(void * data);
[f43a74]35typedef void    (*iiConvertProcL)(leftv out,leftv in);
[0e1846]36struct sConvertTypes
37{
38  int i_typ;
39  int o_typ;
40  iiConvertProc p;
[f43a74]41  iiConvertProcL pl;
[0e1846]42};
43
[befd6b5]44// all of these static conversion routines work destructive on their input
45
[0e1846]46static void * iiI2P(void *data)
47{
[7447d8]48  poly p=pISet((int)(long)data);
[0e1846]49  return (void *)p;
50}
51
[db5523]52static void * iiBI2P(void *data)
53{
[8bd247]54  number n=nInit_bigint((number)data);
[db5523]55  nlDelete((number *)&data,NULL);
56  poly p=pNSet(n);
57  return (void *)p;
58}
59
[0e1846]60static void * iiI2V(void *data)
61{
[7447d8]62  poly p=pISet((int)(long)data);
[0e1846]63  if (p!=NULL) pSetComp(p,1);
64  return (void *)p;
65}
66
[db5523]67static void * iiBI2V(void *data)
68{
[8bd247]69  number n=nInit_bigint((number)data);
[db5523]70  nlDelete((number *)&data,NULL);
71  poly p=pNSet(n);
72  if (p!=NULL) pSetComp(p,1);
73  return (void *)p;
74}
75
[0e1846]76static void * iiI2Id(void *data)
77{
78  ideal I=idInit(1,1);
[7447d8]79  I->m[0]=pISet((int)(long)data);
[0e1846]80  return (void *)I;
81}
82
[db5523]83static void * iiBI2Id(void *data)
84{
85  ideal I=idInit(1,1);
[8bd247]86  number n=nInit_bigint((number)data);
[db5523]87  nlDelete((number *)&data,NULL);
88  poly p=pNSet(n);
89  I->m[0]=p;
90  return (void *)I;
91}
[0e1846]92static void * iiP2V(void *data)
93{
94  poly p=(poly)data;
95  if (p!=NULL) pSetCompP(p,1);
96  return (void *)p;
97}
98
99static void * iiP2Id(void *data)
100{
101  ideal I=idInit(1,1);
102
103  I->m[0]=(poly)data;
104  if (data!=NULL)
105  {
106    poly p=(poly)data;
107    if (pGetComp(p)!=0) I->rank=pMaxComp(p);
108  }
109  return (void *)I;
110}
111
112static void * iiV2Ma(void *data)
113{
114  matrix m=(matrix)idVec2Ideal((poly)data);
115  int h=MATCOLS(m);
116  MATCOLS(m)=MATROWS(m);
117  MATROWS(m)=h;
118  m->rank=h;
[befd6b5]119  pDelete((poly *)&data);
[0e1846]120  return (void *)m;
121}
[6fc941]122
123static void * iiN2P(void *data);
[0e1846]124
125static void * iiDummy(void *data)
126{
127  return data;
128}
129
130static void * iiMo2Ma(void *data)
131{
132  void *res=idModule2Matrix((ideal)data);
133  return res;
134}
135
136static void * iiMa2Mo(void *data)
137{
138  void *res=idMatrix2Module((matrix)data);
139  return res;
140}
141
142static void * iiI2Iv(void *data)
143{
[7447d8]144  int s=(int)(long)data;
145  intvec *iv=new intvec(s,s);
[0e1846]146  return (void *)iv;
147}
148
149static void * iiI2N(void *data)
150{
[7447d8]151  number n=nInit((int)(long)data);
[0e1846]152  return (void *)n;
153}
154
[6fc941]155static void * iiI2BI(void *data)
156{
[8391d8]157  number n=nlInit((int)(long)data, NULL /*dummy for nlInit*/);
[6fc941]158  return (void *)n;
159}
160
[cfce1fc]161static void * iiBI2N(void *data)
162{
163  if (currRing==NULL) return NULL;
164  // a bigint is really a number from char 0, with diffrent operations...
[41ea815]165  return (void*)nInit_bigint((number)data);
[cfce1fc]166}
167
[0e1846]168static void * iiIm2Ma(void *data)
169{
170  int i, j;
171  intvec *iv = (intvec *)data;
172  matrix m = mpNew(iv->rows(), iv->cols());
173
174  for (i=iv->rows(); i>0; i--)
175  {
176    for (j=iv->cols(); j>0; j--)
177    {
178      MATELEM(m, i, j) = pISet(IMATELEM(*iv, i, j));
179    }
180  }
[befd6b5]181  delete iv;
[0e1846]182  return (void *)m;
183}
184
185static void * iiN2P(void *data)
186{
187  poly p=NULL;
188  if (!nIsZero((number)data))
189  {
[3eabdb]190    p=pNSet((number)data);
[0e1846]191  }
[805b06c]192  //else
193  //{
194  //  nDelete((number *)&data);
[0d84792]195  //}
[0e1846]196  return (void *)p;
197}
198
199static void * iiN2Ma(void *data)
200{
201  ideal I=idInit(1,1);
202  if (!nIsZero((number)data))
203  {
[3eabdb]204    poly p=pNSet((number)data);
[0e1846]205    I->m[0]=p;
206  }
[805b06c]207  //else
208  //{
209  //  nDelete((number *)&data);
[0d84792]210  //}
[0e1846]211  return (void *)I;
212}
213
214static void * iiS2Link(void *data)
215{
[c232af]216  si_link l=(si_link)omAlloc0Bin(ip_link_bin);
[0e1846]217  slInit(l, (char *) data);
[c232af]218  omFree((ADDRESS)data);
[0e1846]219  return (void *)l;
220}
221
[f43a74]222/*
[dfc6b54]223static void * iiR2L(void * data)
224{
[3b2d29]225  syStrategy tmp=(syStrategy)data;
[c89df6]226  return  (void *)syConvRes(tmp,TRUE);
[dfc6b54]227}
[f43a74]228*/
229static void iiR2L_l(leftv out, leftv in)
230{
231  int add_row_shift = 0;
232  intvec *weights=(intvec*)atGet(in,"isHomog",INTVEC_CMD);
233  if (weights!=NULL)  add_row_shift=weights->min_in();
234
235  syStrategy tmp=(syStrategy)in->CopyD();
236  out->data=(void *)syConvRes(tmp,TRUE,add_row_shift);
237}
[dfc6b54]238
[3ebd198]239static void * iiL2R(void * data)
240{
[b982ef]241  return (void *)syConvList((lists)data,TRUE);
[3ebd198]242}
243
[0e1846]244//
[78c048b]245// automatic conversions:
[0e1846]246//
247struct sConvertTypes dConvertTypes[] =
248{
249//   input type       output type     convert procedure
[6fc941]250//  int -> bigint
251   { INT_CMD,         BIGINT_CMD,     iiI2BI , NULL },
[0e1846]252//  int -> number
[f43a74]253   { INT_CMD,         NUMBER_CMD,     iiI2N , NULL },
[db5523]254   { BIGINT_CMD,      NUMBER_CMD,     iiBI2N , NULL },
[0e1846]255//  int -> poly
[f43a74]256   { INT_CMD,         POLY_CMD,       iiI2P , NULL },
[db5523]257   { BIGINT_CMD,      POLY_CMD,       iiBI2P , NULL },
[0e1846]258//  int -> vector
[f43a74]259   { INT_CMD,         VECTOR_CMD,     iiI2V , NULL },
[db5523]260   { BIGINT_CMD,      VECTOR_CMD,     iiBI2V , NULL },
[0e1846]261//  int -> ideal
[f43a74]262   { INT_CMD,         IDEAL_CMD,      iiI2Id , NULL },
[db5523]263   { BIGINT_CMD,      IDEAL_CMD,      iiBI2Id , NULL },
[0e1846]264//  int -> matrix
[f43a74]265   { INT_CMD,         MATRIX_CMD,     iiI2Id , NULL },
[db5523]266   { BIGINT_CMD,      MATRIX_CMD,     iiBI2Id , NULL },
[0e1846]267//  int -> intvec
[f43a74]268   { INT_CMD,         INTVEC_CMD,     iiI2Iv , NULL },
[0e1846]269//  intvec -> intmat
[f43a74]270   { INTVEC_CMD,      INTMAT_CMD,     iiDummy, NULL },
[0e1846]271//  intvec -> matrix
[f43a74]272   { INTVEC_CMD,      MATRIX_CMD,     iiIm2Ma , NULL },
[0e1846]273//  intmat -> matrix
[f43a74]274   { INTMAT_CMD,      MATRIX_CMD,     iiIm2Ma , NULL },
[0e1846]275//  number -> poly
[f43a74]276   { NUMBER_CMD,      POLY_CMD,       iiN2P  , NULL },
[0e1846]277//  number -> matrix
[f43a74]278   { NUMBER_CMD,      MATRIX_CMD,     iiN2Ma  , NULL },
[0e1846]279//  number -> ideal
280//  number -> vector
281//  number -> module
282//  poly -> number
283//  poly -> ideal
[f43a74]284   { POLY_CMD,        IDEAL_CMD,      iiP2Id , NULL },
[0e1846]285//  poly -> vector
[f43a74]286   { POLY_CMD,        VECTOR_CMD,     iiP2V , NULL },
[0e1846]287//  poly -> matrix
[f43a74]288   { POLY_CMD,        MATRIX_CMD,     iiP2Id , NULL },
[0e1846]289//  vector -> module
[f43a74]290   { VECTOR_CMD,      MODUL_CMD,      iiP2Id , NULL },
[0e1846]291//  vector -> matrix
[f43a74]292   { VECTOR_CMD,      MATRIX_CMD,     iiV2Ma , NULL },
[0e1846]293//  ideal -> module
[f43a74]294   { IDEAL_CMD,       MODUL_CMD,      iiMa2Mo , NULL },
[0e1846]295//  ideal -> matrix
[f43a74]296   { IDEAL_CMD,       MATRIX_CMD,     iiDummy , NULL },
[0e1846]297//  module -> matrix
[f43a74]298   { MODUL_CMD,       MATRIX_CMD,     iiMo2Ma , NULL },
[0e1846]299//  matrix -> ideal
300//  matrix -> module
[f43a74]301   { MATRIX_CMD,      MODUL_CMD,      iiMa2Mo , NULL },
[0e1846]302//  intvec
303//  link
[f43a74]304   { STRING_CMD,      LINK_CMD,       iiS2Link , NULL },
[5812c69]305// resolution -> list
[f43a74]306   { RESOLUTION_CMD,  LIST_CMD,       NULL /*iiR2L*/ , iiR2L_l },
[3ebd198]307// list -> resolution
[f43a74]308   { LIST_CMD,        RESOLUTION_CMD, iiL2R , NULL },
[0e1846]309//  end of list
[f43a74]310   { 0,               0,              NULL , NULL }
[0e1846]311};
312
313/*2
314* try to convert 'input' of type 'inputType' to 'output' of type 'outputType'
315* return FALSE on success
316*/
317BOOLEAN iiConvert (int inputType, int outputType, int index, leftv input, leftv output)
318{
319  memset(output,0,sizeof(sleftv));
320  if ((inputType==outputType)
321  || (outputType==DEF_CMD)
322  || ((outputType==IDHDL)&&(input->rtyp==IDHDL)))
323  {
324    memcpy(output,input,sizeof(*output));
325    memset(input,0,sizeof(*input));
326    return FALSE;
327  }
328  else if (outputType==ANY_TYPE)
329  {
330    output->rtyp=ANY_TYPE;
331    output->data=(char *)input->Typ();
332    /* the name of the object:*/
333    if (input->e==NULL)
334    {
335      if (input->rtyp==IDHDL)
336      /* preserve name: copy it */
[c232af]337        output->name=omStrDup(IDID((idhdl)(input->data)));
[0e1846]338      else if (input->name!=NULL)
339      {
340        output->name=input->name;
341        input->name=NULL;
342      }
343      else if ((input->rtyp==POLY_CMD) && (input->name==NULL))
344      {
[0d84792]345        if (input->data!=NULL)
[0e1846]346        {
[0d84792]347          int nr=pIsPurePower((poly)input->data);
348          if (nr!=0)
[0e1846]349          {
[0d84792]350            if (pGetExp((poly)input->data,nr)==1)
351            {
[c232af]352              output->name=omStrDup(currRing->names[nr-1]);
[0d84792]353            }
354            else
355            {
[85e68dd]356              char *tmp=(char *)omAlloc(4);
357              sprintf(tmp,"%c%d",*(currRing->names[nr-1]),
358                (int)pGetExp((poly)input->data,nr));
359              output->name=tmp;
[0d84792]360            }
[0e1846]361          }
[0d84792]362          else if(pIsConstant((poly)input->data))
[0e1846]363          {
[0d84792]364            output->name=nName(pGetCoeff((poly)input->data));
[0e1846]365          }
366#ifdef TEST
[0d84792]367          else
368          {
369            WerrorS("wrong name, should not happen");
[c232af]370            output->name=omStrDup("?");
[0d84792]371          }
[0e1846]372#endif
[0d84792]373        }
[0e1846]374      }
375      else if ((input->rtyp==NUMBER_CMD) && (input->name==NULL))
376      {
377        output->name=nName((number)input->data);
378      }
379      else
380      {
381        /* no need to preserve name: use it */
382        output->name=input->name;
383        memset(input,0,sizeof(*input));
384      }
385    }
386    output->next=input->next;
387    input->next=NULL;
388    return FALSE;
389  }
390  if (index!=0) /* iiTestConvert does not returned 'failure' */
391  {
392    index--;
393
394    if((dConvertTypes[index].i_typ==inputType)
395    &&(dConvertTypes[index].o_typ==outputType))
396    {
397      if ((currRing==NULL) && (outputType>BEGIN_RING) && (outputType<END_RING))
398        return TRUE;
399      output->rtyp=outputType;
[f43a74]400      if (dConvertTypes[index].p!=NULL)
401      {
402        output->data=dConvertTypes[index].p(input->CopyD());
403      }
404      else
405      {
406        dConvertTypes[index].pl(output,input);
407      }
[0e1846]408      if ((output->data==NULL)
409      && ((outputType==IDEAL_CMD)
410        ||(outputType==MODUL_CMD)
411        ||(outputType==MATRIX_CMD)
[f43a74]412        ||(outputType==INTMAT_CMD)
[0e1846]413        ||(outputType==INTVEC_CMD)))
414      {
415        return TRUE;
416      }
417      output->next=input->next;
418      input->next=NULL;
[bc669c]419  //if (outputType==MATRIX_CMD) Print("convert %d -> matrix\n",inputType);
[0e1846]420      return FALSE;
421    }
422  }
423  return TRUE;
424}
425
426/*2
427* try to convert 'inputType' in 'outputType'
428* return 0 on failure, an index (<>0) on success
429*/
430int iiTestConvert (int inputType, int outputType)
431{
432  if ((inputType==outputType)
433  || (outputType==DEF_CMD)
434  || (outputType==IDHDL)
435  || (outputType==ANY_TYPE))
436  {
437    return -1;
438  }
439
440  if ((currRing==NULL) && (outputType>BEGIN_RING) && (outputType<END_RING))
441    return 0;
442
443  // search the list
444  int i=0;
445  while (dConvertTypes[i].i_typ!=0)
446  {
447    if((dConvertTypes[i].i_typ==inputType)
448    &&(dConvertTypes[i].o_typ==outputType))
449    {
450      //Print("test convert %d to %d (%s -> %s):%d\n",inputType,outputType,
451      //Tok2Cmdname(inputType), Tok2Cmdname(outputType),i+1);
452      return i+1;
453    }
454    i++;
455  }
456  //Print("test convert %d to %d (%s -> %s):0\n",inputType,outputType,
457  // Tok2Cmdname(inputType), Tok2Cmdname(outputType));
458  return 0;
459}
Note: See TracBrowser for help on using the repository browser.