source: git/Singular/ipconv.cc @ fc866f6

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