source: git/Singular/ipconv.cc @ fde66a9

fieker-DuValspielwiese
Last change on this file since fde66a9 was 43d920, checked in by Hans Schoenemann <hannes@…>, 10 years ago
removed cfName and related
  • Property mode set to 100644
File size: 9.9 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/*
5* ABSTRACT: automatic type conversions
6*/
7
8
9
10
11#include <kernel/mod2.h>
12#include <Singular/tok.h>
13#include <Singular/ipid.h>
14#include <misc/intvec.h>
15#include <misc/options.h>
16#include <omalloc/omalloc.h>
17#include <kernel/polys.h>
18#include <kernel/ideals.h>
19#include <Singular/subexpr.h>
20#include <coeffs/numbers.h>
21#include <coeffs/coeffs.h>
22#include <coeffs/bigintmat.h>
23//#include <polys/ext_fields/longalg.h>
24#ifdef HAVE_RINGS
25#include <coeffs/rmodulon.h>
26#include <coeffs/rmodulo2m.h>
27#include <coeffs/rintegers.h>
28#endif
29#include <polys/matpol.h>
30#include <Singular/links/silink.h>
31#include <kernel/GBEngine/syz.h>
32#include <Singular/attrib.h>
33#include <polys/monomials/ring.h>
34#include <Singular/ipshell.h>
35#include <Singular/ipconv.h>
36
37typedef void *   (*iiConvertProc)(void * data);
38typedef void    (*iiConvertProcL)(leftv out,leftv in);
39struct sConvertTypes
40{
41  int i_typ;
42  int o_typ;
43  iiConvertProc p;
44  iiConvertProcL pl;
45};
46
47// all of these static conversion routines work destructive on their input
48
49static void * iiI2P(void *data)
50{
51  poly p=pISet((int)(long)data);
52  return (void *)p;
53}
54
55static void * iiBI2P(void *data)
56{
57  nMapFunc nMap=n_SetMap(coeffs_BIGINT,currRing->cf);
58  if (nMap==NULL)
59  {
60    Werror("no conversion from bigint to %s",currRing->cf->cfCoeffString(currRing->cf));
61    return NULL;
62  }
63  number n=nMap((number)data,coeffs_BIGINT,currRing->cf);
64  n_Delete((number *)&data, coeffs_BIGINT);
65  poly p=p_NSet(n, currRing);
66  return (void *)p;
67}
68
69static void * iiI2V(void *data)
70{
71  poly p=pISet((int)(long)data);
72  if (p!=NULL) pSetComp(p,1);
73  return (void *)p;
74}
75
76static void * iiBI2V(void *data)
77{
78  nMapFunc nMap=n_SetMap(coeffs_BIGINT,currRing->cf);
79  if (nMap==NULL)
80  {
81    Werror("no conversion from bigint to %s",currRing->cf->cfCoeffString(currRing->cf));
82    return NULL;
83  }
84  number n=nMap((number)data,coeffs_BIGINT,currRing->cf);
85  n_Delete((number *)&data, coeffs_BIGINT);
86  poly p=p_NSet(n, currRing);
87  if (p!=NULL) pSetComp(p,1);
88  return (void *)p;
89}
90
91static void * iiI2Id(void *data)
92{
93  ideal I=idInit(1,1);
94  I->m[0]=pISet((int)(long)data);
95  return (void *)I;
96}
97
98static void * iiBI2Id(void *data)
99{
100  ideal I=idInit(1,1);
101  nMapFunc nMap=n_SetMap(coeffs_BIGINT,currRing->cf);
102  if (nMap==NULL)
103  {
104    Werror("no conversion from bigint to %s",currRing->cf->cfCoeffString(currRing->cf));
105    return NULL;
106  }
107  number n=nMap((number)data,coeffs_BIGINT,currRing->cf);
108  n_Delete((number *)&data,coeffs_BIGINT);
109  poly p=pNSet(n);
110  I->m[0]=p;
111  return (void *)I;
112}
113static void * iiP2V(void *data)
114{
115  poly p=(poly)data;
116  if (p!=NULL) pSetCompP(p,1);
117  return (void *)p;
118}
119
120static void * iiP2Id(void *data)
121{
122  ideal I=idInit(1,1);
123
124  if (data!=NULL)
125  {
126    poly p=(poly)data;
127    I->m[0]=p;
128    if (pGetComp(p)!=0) I->rank=pMaxComp(p);
129  }
130  return (void *)I;
131}
132
133static void * iiV2Ma(void *data)
134{
135  matrix m=(matrix)idVec2Ideal((poly)data);
136  int h=MATCOLS(m);
137  MATCOLS(m)=MATROWS(m);
138  MATROWS(m)=h;
139  m->rank=h;
140  pDelete((poly *)&data);
141  return (void *)m;
142}
143
144static void * iiN2P(void *data);
145
146static void * iiDummy(void *data)
147{
148  return data;
149}
150
151static void * iiMo2Ma(void *data)
152{
153  void *res=id_Module2Matrix((ideal)data,currRing);
154  return res;
155}
156
157static void * iiMa2Mo(void *data)
158{
159  void *res=id_Matrix2Module((matrix)data,currRing);
160  return res;
161}
162
163static void * iiI2Iv(void *data)
164{
165  int s=(int)(long)data;
166  intvec *iv=new intvec(s,s);
167  return (void *)iv;
168}
169
170static void * iiI2N(void *data)
171{
172  number n=nInit((int)(long)data);
173  return (void *)n;
174}
175
176static void * iiI2BI(void *data)
177{
178  number n=n_Init((int)(long)data, coeffs_BIGINT);
179  return (void *)n;
180}
181
182static void * iiBI2N(void *data)
183{
184  if (currRing==NULL) return NULL;
185  nMapFunc nMap=n_SetMap(coeffs_BIGINT,currRing->cf);
186  if (nMap==NULL)
187  {
188    Werror("no conversion from bigint to %s",currRing->cf->cfCoeffString(currRing->cf));
189    return NULL;
190  }
191  number n=nMap((number)data,coeffs_BIGINT,currRing->cf);
192  n_Delete((number *)&data, coeffs_BIGINT);
193  return (void*)n;
194}
195
196static void * iiIm2Ma(void *data)
197{
198  int i, j;
199  intvec *iv = (intvec *)data;
200  matrix m = mpNew(iv->rows(), iv->cols());
201
202  for (i=iv->rows(); i>0; i--)
203  {
204    for (j=iv->cols(); j>0; j--)
205    {
206      MATELEM(m, i, j) = pISet(IMATELEM(*iv, i, j));
207    }
208  }
209  delete iv;
210  return (void *)m;
211}
212
213static void * iiIm2Bim(void *data)
214{
215  intvec *iv=(intvec*)data;
216  void *r=(void *)iv2bim(iv,coeffs_BIGINT);
217  delete iv;
218  return r;
219}
220
221static void * iiBim2Im(void *data)
222{
223  bigintmat *b=(bigintmat*)data;
224  void *r=(void *)bim2iv(b);
225  delete b;
226  return r;
227}
228
229static void * iiN2P(void *data)
230{
231  poly p=NULL;
232  if (!nIsZero((number)data))
233  {
234    p=pNSet((number)data);
235  }
236  //else
237  //{
238  //  nDelete((number *)&data);
239  //}
240  return (void *)p;
241}
242
243static void * iiN2Ma(void *data)
244{
245  ideal I=idInit(1,1);
246  if (!nIsZero((number)data))
247  {
248    poly p=pNSet((number)data);
249    I->m[0]=p;
250  }
251  //else
252  //{
253  //  nDelete((number *)&data);
254  //}
255  return (void *)I;
256}
257
258static void * iiS2Link(void *data)
259{
260  si_link l=(si_link)omAlloc0Bin(ip_link_bin);
261  slInit(l, (char *) data);
262  omFree((ADDRESS)data);
263  return (void *)l;
264}
265
266/*
267static void * iiR2L(void * data)
268{
269  syStrategy tmp=(syStrategy)data;
270  return  (void *)syConvRes(tmp,TRUE);
271}
272*/
273static void iiR2L_l(leftv out, leftv in)
274{
275  int add_row_shift = 0;
276  intvec *weights=(intvec*)atGet(in,"isHomog",INTVEC_CMD);
277  if (weights!=NULL)  add_row_shift=weights->min_in();
278
279  syStrategy tmp=(syStrategy)in->CopyD();
280
281  out->data=(void *)syConvRes(tmp,TRUE,add_row_shift);
282}
283
284static void * iiL2R(void * data)
285{
286  return (void *)syConvList((lists)data,TRUE);
287}
288
289//
290// automatic conversions:
291//
292#define IPCONV
293#define D(A)     A
294#define NULL_VAL NULL
295#include <Singular/table.h>
296/*2
297* try to convert 'input' of type 'inputType' to 'output' of type 'outputType'
298* return FALSE on success
299*/
300BOOLEAN iiConvert (int inputType, int outputType, int index, leftv input, leftv output)
301{
302  memset(output,0,sizeof(sleftv));
303  if ((inputType==outputType)
304  || (outputType==DEF_CMD)
305  || ((outputType==IDHDL)&&(input->rtyp==IDHDL)))
306  {
307    memcpy(output,input,sizeof(*output));
308    memset(input,0,sizeof(*input));
309    return FALSE;
310  }
311  else if (outputType==ANY_TYPE)
312  {
313    output->rtyp=ANY_TYPE;
314    output->data=(char *)(long)input->Typ();
315    /* the name of the object:*/
316    if (input->e==NULL)
317    {
318      if (input->rtyp==IDHDL)
319      /* preserve name: copy it */
320        output->name=omStrDup(IDID((idhdl)(input->data)));
321      else if (input->name!=NULL)
322      {
323        if (input->rtyp==ALIAS_CMD)
324        output->name=omStrDup(input->name);
325        else
326        {
327          output->name=input->name;
328          input->name=NULL;
329        }
330      }
331      else if ((input->rtyp==POLY_CMD) && (input->name==NULL))
332      {
333        if (input->data!=NULL)
334        {
335          int nr=pIsPurePower((poly)input->data);
336          if (nr!=0)
337          {
338            if (pGetExp((poly)input->data,nr)==1)
339            {
340              output->name=omStrDup(currRing->names[nr-1]);
341            }
342            else
343            {
344              char *tmp=(char *)omAlloc(4);
345              sprintf(tmp,"%c%d",*(currRing->names[nr-1]),
346                (int)pGetExp((poly)input->data,nr));
347              output->name=tmp;
348            }
349          }
350          else if(pIsConstant((poly)input->data))
351          {
352            StringSetS("");
353            number n=(pGetCoeff((poly)input->data));
354            n_Write(n, currRing->cf);
355            (pGetCoeff((poly)input->data))=n;
356            output->name=StringEndS();
357          }
358        }
359      }
360      else if ((input->rtyp==NUMBER_CMD) && (input->name==NULL))
361      {
362        StringSetS("");
363        number n=(number)input->data;
364        n_Write(n, currRing->cf);
365        input->data=(void*)n;
366        output->name=StringEndS();
367      }
368      else
369      {
370        /* no need to preserve name: use it */
371        output->name=input->name;
372        memset(input,0,sizeof(*input));
373      }
374    }
375    output->next=input->next;
376    input->next=NULL;
377    return FALSE;
378  }
379  if (index!=0) /* iiTestConvert does not returned 'failure' */
380  {
381    index--;
382
383    if((dConvertTypes[index].i_typ==inputType)
384    &&(dConvertTypes[index].o_typ==outputType))
385    {
386      if(traceit&TRACE_CONV)
387      {
388        Print("automatic  conversion %s -> %s\n",
389        Tok2Cmdname(inputType),Tok2Cmdname(outputType));
390      }
391      if ((currRing==NULL) && (outputType>BEGIN_RING) && (outputType<END_RING))
392        return TRUE;
393      output->rtyp=outputType;
394      if (dConvertTypes[index].p!=NULL)
395      {
396        output->data=dConvertTypes[index].p(input->CopyD());
397      }
398      else
399      {
400        dConvertTypes[index].pl(output,input);
401      }
402      if ((output->data==NULL)
403      && ((outputType!=INT_CMD)
404        &&(outputType!=POLY_CMD)
405        &&(outputType!=VECTOR_CMD)
406        &&(outputType!=NUMBER_CMD)))
407      {
408        return TRUE;
409      }
410      output->next=input->next;
411      input->next=NULL;
412  //if (outputType==MATRIX_CMD) Print("convert %d -> matrix\n",inputType);
413      return FALSE;
414    }
415  }
416  return TRUE;
417}
418
419/*2
420* try to convert 'inputType' in 'outputType'
421* return 0 on failure, an index (<>0) on success
422*/
423int iiTestConvert (int inputType, int outputType)
424{
425  if ((inputType==outputType)
426  || (outputType==DEF_CMD)
427  || (outputType==IDHDL)
428  || (outputType==ANY_TYPE))
429  {
430    return -1;
431  }
432
433  if ((currRing==NULL) && (outputType>BEGIN_RING) && (outputType<END_RING))
434    return 0;
435
436  // search the list
437  int i=0;
438  while (dConvertTypes[i].i_typ!=0)
439  {
440    if((dConvertTypes[i].i_typ==inputType)
441    &&(dConvertTypes[i].o_typ==outputType))
442    {
443      //Print("test convert %d to %d (%s -> %s):%d\n",inputType,outputType,
444      //Tok2Cmdname(inputType), Tok2Cmdname(outputType),i+1);
445      return i+1;
446    }
447    i++;
448  }
449  //Print("test convert %d to %d (%s -> %s):0\n",inputType,outputType,
450  // Tok2Cmdname(inputType), Tok2Cmdname(outputType));
451  return 0;
452}
Note: See TracBrowser for help on using the repository browser.