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

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