source: git/Singular/ipconv.cc @ a97ac0

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