source: git/Singular/ipconv.cc @ 634dab0

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