source: git/Singular/ipconv.cc @ 0b8bf7

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