source: git/Singular/ipconv.cc @ 1b2216

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