source: git/Singular/ipconv.cc @ 3d124a7

spielwiese
Last change on this file since 3d124a7 was 32df82, checked in by Hans Schönemann <hannes@…>, 27 years ago
* hannes: removed rcsid and Log: entries, added assignment module=poly corected type conversion int->module git-svn-id: file:///usr/local/Singular/svn/trunk@128 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 7.9 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: ipconv.cc,v 1.4 1997-04-02 15:07:10 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 "ipshell.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//
171// conversions:
172//
173struct sConvertTypes dConvertTypes[] =
174{
175//   input type       output type     convert procedure
176//  int -> number
177   { INT_CMD,         NUMBER_CMD,     iiI2N },
178//  int -> poly
179   { INT_CMD,         POLY_CMD,       iiI2P },
180//  int -> vector
181   { INT_CMD,         VECTOR_CMD,     iiI2V },
182//  int -> ideal
183   { INT_CMD,         IDEAL_CMD,      iiI2Id },
184//  int -> matrix
185   { INT_CMD,         MATRIX_CMD,     iiI2Id },
186//  int -> intvec
187   { INT_CMD,         INTVEC_CMD,     iiI2Iv },
188//  intvec -> intmat
189   { INTVEC_CMD,      INTMAT_CMD,     iiDummy},
190//  intvec -> matrix
191   { INTVEC_CMD,      MATRIX_CMD,     iiIm2Ma },
192//  intmat -> matrix
193   { INTMAT_CMD,      MATRIX_CMD,     iiIm2Ma },
194//  number -> poly
195   { NUMBER_CMD,      POLY_CMD,       iiN2P  },
196//  number -> matrix
197   { NUMBER_CMD,      MATRIX_CMD,     iiN2Ma  },
198//  number -> ideal
199//  number -> vector
200//  number -> module
201//  poly -> number
202//  poly -> ideal
203   { POLY_CMD,        IDEAL_CMD,      iiP2Id },
204//  poly -> vector
205   { POLY_CMD,        VECTOR_CMD,     iiP2V },
206//  poly -> matrix
207   { POLY_CMD,        MATRIX_CMD,     iiP2Id },
208//  vector -> module
209   { VECTOR_CMD,      MODUL_CMD,      iiP2Id },
210//  vector -> matrix
211   { VECTOR_CMD,      MATRIX_CMD,     iiV2Ma },
212//  ideal -> module
213   { IDEAL_CMD,       MODUL_CMD,      iiMa2Mo },
214//  ideal -> matrix
215   { IDEAL_CMD,       MATRIX_CMD,     iiDummy },
216//  module -> matrix
217   { MODUL_CMD,       MATRIX_CMD,     iiMo2Ma },
218//  matrix -> ideal
219//  matrix -> module
220   { MATRIX_CMD,      MODUL_CMD,      iiMa2Mo },
221//  intvec
222//  link
223   { STRING_CMD,      LINK_CMD,       iiS2Link },
224//  end of list
225   { 0,               0,              NULL }
226};
227
228/*2
229* try to convert 'input' of type 'inputType' to 'output' of type 'outputType'
230* return FALSE on success
231*/
232BOOLEAN iiConvert (int inputType, int outputType, int index, leftv input, leftv output)
233{
234  memset(output,0,sizeof(sleftv));
235  if ((inputType==outputType)
236  || (outputType==DEF_CMD)
237  || ((outputType==IDHDL)&&(input->rtyp==IDHDL)))
238  {
239    memcpy(output,input,sizeof(*output));
240    memset(input,0,sizeof(*input));
241    return FALSE;
242  }
243  else if (outputType==ANY_TYPE)
244  {
245    output->rtyp=ANY_TYPE;
246    output->data=(char *)input->Typ();
247    /* the name of the object:*/
248    if (input->e==NULL)
249    {
250      if (input->rtyp==IDHDL)
251      /* preserve name: copy it */
252        output->name=mstrdup(IDID((idhdl)(input->data)));
253      else if (input->name!=NULL)
254      {
255        output->name=input->name;
256        input->name=NULL;
257      }
258      else if ((input->rtyp==POLY_CMD) && (input->name==NULL))
259      {
260        int nr=pIsPurePower((poly)input->data);
261        if (nr!=0)
262        {
263          if (pGetExp((poly)input->data,nr)==1)
264          {
265            output->name=mstrdup(currRing->names[nr-1]);
266          }
267          else
268          {
269            output->name=(char *)AllocL(4);
270            sprintf(output->name,"%c%d",*(currRing->names[nr-1]),
271              pGetExp((poly)input->data,nr));
272          }
273        }
274        else if(pIsConstant((poly)input->data))
275        {
276          output->name=nName(pGetCoeff((poly)input->data));
277        }
278#ifdef TEST
279        else
280        {
281          Werror("wrong name, should not happen");
282          output->name=mstrdup("?");
283        }
284#endif
285      }
286      else if ((input->rtyp==NUMBER_CMD) && (input->name==NULL))
287      {
288        output->name=nName((number)input->data);
289      }
290      else
291      {
292        /* no need to preserve name: use it */
293        output->name=input->name;
294        memset(input,0,sizeof(*input));
295      }
296    }
297    output->next=input->next;
298    input->next=NULL;
299    return FALSE;
300  }
301  if (index!=0) /* iiTestConvert does not returned 'failure' */
302  {
303    index--;
304
305    if((dConvertTypes[index].i_typ==inputType)
306    &&(dConvertTypes[index].o_typ==outputType))
307    {
308      if ((currRing==NULL) && (outputType>BEGIN_RING) && (outputType<END_RING))
309        return TRUE;
310      output->rtyp=outputType;
311      output->data=dConvertTypes[index].p(input->CopyD());
312      if ((output->data==NULL)
313      && ((outputType==IDEAL_CMD)
314        ||(outputType==MODUL_CMD)
315        ||(outputType==MATRIX_CMD)
316        ||(outputType==INTVEC_CMD)))
317      {
318        return TRUE;
319      }
320      output->next=input->next;
321      input->next=NULL;
322      return FALSE;
323    }
324  }
325  return TRUE;
326}
327
328/*2
329* try to convert 'inputType' in 'outputType'
330* return 0 on failure, an index (<>0) on success
331*/
332int iiTestConvert (int inputType, int outputType)
333{
334  if ((inputType==outputType)
335#ifdef SIC
336  || (inputType==0)
337#endif
338  || (outputType==DEF_CMD)
339  || (outputType==IDHDL)
340  || (outputType==ANY_TYPE))
341  {
342    return -1;
343  }
344
345  if ((currRing==NULL) && (outputType>BEGIN_RING) && (outputType<END_RING))
346    return 0;
347
348  // search the list
349  int i=0;
350  while (dConvertTypes[i].i_typ!=0)
351  {
352    if((dConvertTypes[i].i_typ==inputType)
353    &&(dConvertTypes[i].o_typ==outputType))
354    {
355      //Print("test convert %d to %d (%s -> %s):%d\n",inputType,outputType,
356      //Tok2Cmdname(inputType), Tok2Cmdname(outputType),i+1);
357      return i+1;
358    }
359    i++;
360  }
361  //Print("test convert %d to %d (%s -> %s):0\n",inputType,outputType,
362  // Tok2Cmdname(inputType), Tok2Cmdname(outputType));
363  return 0;
364}
Note: See TracBrowser for help on using the repository browser.