source: git/Singular/ipconv.cc @ 810491

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