source: git/Singular/ipconv.cc @ 341696

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