source: git/Singular/ipconv.cc @ 82716e

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