source: git/Singular/ipconv.cc @ 85e68dd

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