source: git/Singular/ipconv.cc @ 551fd7

spielwiese
Last change on this file since 551fd7 was 416465, checked in by Olaf Bachmann <obachman@…>, 24 years ago
* bug-fixes from work with Thomas git-svn-id: file:///usr/local/Singular/svn/trunk@3826 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.21 1999-11-15 17:20:09 obachman 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  return res;
105}
106
107static void * iiMa2Mo(void *data)
108{
109  void *res=idMatrix2Module((matrix)data);
110  return res;
111}
112
113static void * iiI2Iv(void *data)
114{
115  intvec *iv=NewIntvec2((int)data,(int)data);
116  return (void *)iv;
117}
118
119static void * iiI2N(void *data)
120{
121  number n=nInit((int)data);
122  return (void *)n;
123}
124
125static void * iiIm2Ma(void *data)
126{
127  int i, j;
128  intvec *iv = (intvec *)data;
129  matrix m = mpNew(iv->rows(), iv->cols());
130
131  for (i=iv->rows(); i>0; i--)
132  {
133    for (j=iv->cols(); j>0; j--)
134    {
135      MATELEM(m, i, j) = pISet(IMATELEM(*iv, i, j));
136    }
137  }
138  delete iv;
139  return (void *)m;
140}
141
142static void * iiN2P(void *data)
143{
144  poly p=NULL;
145  if (!nIsZero((number)data))
146  {
147    p=pOne();
148    pSetCoeff(p,(number)data);
149  }
150  //else
151  //{
152  //  nDelete((number *)&data);
153  //}
154  return (void *)p;
155}
156
157static void * iiN2Ma(void *data)
158{
159  ideal I=idInit(1,1);
160  if (!nIsZero((number)data))
161  {
162    poly p=pOne();
163    pSetCoeff(p,(number)data);
164    I->m[0]=p;
165  }
166  //else
167  //{
168  //  nDelete((number *)&data);
169  //}
170  return (void *)I;
171}
172
173static void * iiS2Link(void *data)
174{
175  si_link l=(si_link)Alloc0SizeOf(ip_link);
176  slInit(l, (char *) data);
177  FreeL((ADDRESS)data);
178  return (void *)l;
179}
180
181static void * iiR2L(void * data)
182{
183  //syStrategy tmp=syMinimize((syStrategy)data);
184  //(tmp->references)--;
185  //return (void *)syConvRes(tmp,TRUE);
186  syStrategy tmp=(syStrategy)data;
187  return (void *)syConvRes(tmp);
188}
189
190#if defined(INIT_BUG) || defined(PROC_BUG)
191static void * iiL2R(void * data)
192{
193  return (void *)syConvList((lists)data,TRUE);
194}
195#else
196#define iiL2R (iiConvertProc)syConvList
197#endif
198
199//
200// automatic conversions:
201//
202struct sConvertTypes dConvertTypes[] =
203{
204//   input type       output type     convert procedure
205//  int -> number
206   { INT_CMD,         NUMBER_CMD,     iiI2N },
207//  int -> poly
208   { INT_CMD,         POLY_CMD,       iiI2P },
209//  int -> vector
210   { INT_CMD,         VECTOR_CMD,     iiI2V },
211//  int -> ideal
212   { INT_CMD,         IDEAL_CMD,      iiI2Id },
213//  int -> matrix
214   { INT_CMD,         MATRIX_CMD,     iiI2Id },
215//  int -> intvec
216   { INT_CMD,         INTVEC_CMD,     iiI2Iv },
217//  intvec -> intmat
218   { INTVEC_CMD,      INTMAT_CMD,     iiDummy},
219//  intvec -> matrix
220   { INTVEC_CMD,      MATRIX_CMD,     iiIm2Ma },
221//  intmat -> matrix
222   { INTMAT_CMD,      MATRIX_CMD,     iiIm2Ma },
223//  number -> poly
224   { NUMBER_CMD,      POLY_CMD,       iiN2P  },
225//  number -> matrix
226   { NUMBER_CMD,      MATRIX_CMD,     iiN2Ma  },
227//  number -> ideal
228//  number -> vector
229//  number -> module
230//  poly -> number
231//  poly -> ideal
232   { POLY_CMD,        IDEAL_CMD,      iiP2Id },
233//  poly -> vector
234   { POLY_CMD,        VECTOR_CMD,     iiP2V },
235//  poly -> matrix
236   { POLY_CMD,        MATRIX_CMD,     iiP2Id },
237//  vector -> module
238   { VECTOR_CMD,      MODUL_CMD,      iiP2Id },
239//  vector -> matrix
240   { VECTOR_CMD,      MATRIX_CMD,     iiV2Ma },
241//  ideal -> module
242   { IDEAL_CMD,       MODUL_CMD,      iiMa2Mo },
243//  ideal -> matrix
244   { IDEAL_CMD,       MATRIX_CMD,     iiDummy },
245//  module -> matrix
246   { MODUL_CMD,       MATRIX_CMD,     iiMo2Ma },
247//  matrix -> ideal
248//  matrix -> module
249   { MATRIX_CMD,      MODUL_CMD,      iiMa2Mo },
250//  intvec
251//  link
252   { STRING_CMD,      LINK_CMD,       iiS2Link },
253// resolution -> list
254   { RESOLUTION_CMD,  LIST_CMD,       iiR2L },
255// list -> resolution
256   { LIST_CMD,        RESOLUTION_CMD, iiL2R },
257//  end of list
258   { 0,               0,              NULL }
259};
260
261/*2
262* try to convert 'input' of type 'inputType' to 'output' of type 'outputType'
263* return FALSE on success
264*/
265BOOLEAN iiConvert (int inputType, int outputType, int index, leftv input, leftv output)
266{
267  memset(output,0,sizeof(sleftv));
268  if ((inputType==outputType)
269  || (outputType==DEF_CMD)
270  || ((outputType==IDHDL)&&(input->rtyp==IDHDL)))
271  {
272    memcpy(output,input,sizeof(*output));
273    memset(input,0,sizeof(*input));
274    return FALSE;
275  }
276  else if (outputType==ANY_TYPE)
277  {
278    output->rtyp=ANY_TYPE;
279    output->data=(char *)input->Typ();
280    /* the name of the object:*/
281    if (input->e==NULL)
282    {
283      if (input->rtyp==IDHDL)
284      /* preserve name: copy it */
285        output->name=mstrdup(IDID((idhdl)(input->data)));
286      else if (input->name!=NULL)
287      {
288        output->name=input->name;
289        input->name=NULL;
290      }
291      else if ((input->rtyp==POLY_CMD) && (input->name==NULL))
292      {
293        if (input->data!=NULL)
294        {
295          int nr=pIsPurePower((poly)input->data);
296          if (nr!=0)
297          {
298            if (pGetExp((poly)input->data,nr)==1)
299            {
300              output->name=mstrdup(currRing->names[nr-1]);
301            }
302            else
303            {
304              output->name=(char *)AllocL(4);
305              sprintf(output->name,"%c%d",*(currRing->names[nr-1]),
306              pGetExp((poly)input->data,nr));
307            }
308          }
309          else if(pIsConstant((poly)input->data))
310          {
311            output->name=nName(pGetCoeff((poly)input->data));
312          }
313#ifdef TEST
314          else
315          {
316            WerrorS("wrong name, should not happen");
317            output->name=mstrdup("?");
318          }
319#endif
320        }
321      }
322      else if ((input->rtyp==NUMBER_CMD) && (input->name==NULL))
323      {
324        output->name=nName((number)input->data);
325      }
326      else
327      {
328        /* no need to preserve name: use it */
329        output->name=input->name;
330        memset(input,0,sizeof(*input));
331      }
332    }
333#ifdef HAVE_NAMESPACES
334    output->packhdl = input->packhdl;
335    output->req_packhdl = input->req_packhdl;
336#endif /* HAVE_NAMESPACES */
337    output->next=input->next;
338    input->next=NULL;
339    return FALSE;
340  }
341  if (index!=0) /* iiTestConvert does not returned 'failure' */
342  {
343    index--;
344
345    if((dConvertTypes[index].i_typ==inputType)
346    &&(dConvertTypes[index].o_typ==outputType))
347    {
348      if ((currRing==NULL) && (outputType>BEGIN_RING) && (outputType<END_RING))
349        return TRUE;
350      output->rtyp=outputType;
351      output->data=dConvertTypes[index].p(input->CopyD());
352      if ((output->data==NULL)
353      && ((outputType==IDEAL_CMD)
354        ||(outputType==MODUL_CMD)
355        ||(outputType==MATRIX_CMD)
356        ||(outputType==INTVEC_CMD)))
357      {
358        return TRUE;
359      }
360#ifdef HAVE_NAMESPACES
361      //output->packhdl = input->packhdl;
362      //output->req_packhdl = input->req_packhdl;
363#endif /* HAVE_NAMESPACES */
364      output->next=input->next;
365      input->next=NULL;
366      return FALSE;
367    }
368  }
369  return TRUE;
370}
371
372/*2
373* try to convert 'inputType' in 'outputType'
374* return 0 on failure, an index (<>0) on success
375*/
376int iiTestConvert (int inputType, int outputType)
377{
378  if ((inputType==outputType)
379  || (outputType==DEF_CMD)
380  || (outputType==IDHDL)
381  || (outputType==ANY_TYPE))
382  {
383    return -1;
384  }
385
386  if ((currRing==NULL) && (outputType>BEGIN_RING) && (outputType<END_RING))
387    return 0;
388
389  // search the list
390  int i=0;
391  while (dConvertTypes[i].i_typ!=0)
392  {
393    if((dConvertTypes[i].i_typ==inputType)
394    &&(dConvertTypes[i].o_typ==outputType))
395    {
396      //Print("test convert %d to %d (%s -> %s):%d\n",inputType,outputType,
397      //Tok2Cmdname(inputType), Tok2Cmdname(outputType),i+1);
398      return i+1;
399    }
400    i++;
401  }
402  //Print("test convert %d to %d (%s -> %s):0\n",inputType,outputType,
403  // Tok2Cmdname(inputType), Tok2Cmdname(outputType));
404  return 0;
405}
Note: See TracBrowser for help on using the repository browser.