source: git/libpolys/polys/clapconv.cc @ 05fd55

spielwiese
Last change on this file since 05fd55 was 05fd55, checked in by Adrian <adi_popescum@…>, 12 years ago
64bits for Spielweise
  • Property mode set to 100644
File size: 10.6 KB
Line 
1// emacs edit mode for this file is -*- C++ -*-
2/****************************************
3*  Computer Algebra System SINGULAR     *
4****************************************/
5/*
6* ABSTRACT: convert data between Singular and factory
7*/
8
9
10#include "config.h"
11#include <misc/auxiliary.h>
12
13#ifdef HAVE_FACTORY
14#define SI_DONT_HAVE_GLOBAL_VARS
15#include <factory/factory.h>
16
17#include <omalloc/omalloc.h>
18#include <coeffs/coeffs.h>
19#include <coeffs/longrat.h>
20#include <coeffs/modulop.h>
21#include <polys/monomials/p_polys.h>
22#include <polys/sbuckets.h>
23#include <polys/clapconv.h>
24
25#include "simpleideals.h"
26
27#define TRANSEXT_PRIVATES
28#include <polys/ext_fields/transext.h>
29
30void out_cf(const char *s1,const CanonicalForm &f,const char *s2);
31
32static void conv_RecPP ( const CanonicalForm & f, int * exp, sBucket_pt result, ring r );
33
34static void convRecTrP ( const CanonicalForm & f, int * exp, poly & result, int offs, const ring r );
35
36static void convRecGFGF ( const CanonicalForm & f, int * exp, poly & result );
37
38static number convFactoryNSingAN( const CanonicalForm &f, const ring r);
39
40poly convFactoryPSingP ( const CanonicalForm & f, const ring r )
41{
42  int n = rVar(r)+1;
43  /* ASSERT( level( f ) <= pVariables, "illegal number of variables" ); */
44  int * exp = (int*)omAlloc0(n*sizeof(int));
45  sBucket_pt result_bucket=sBucketCreate(r);
46  conv_RecPP( f, exp, result_bucket, r );
47  poly result; int dummy;
48  sBucketDestroyMerge(result_bucket,&result,&dummy);
49  omFreeSize((ADDRESS)exp,n*sizeof(int));
50  return result;
51}
52
53static void conv_RecPP ( const CanonicalForm & f, int * exp, sBucket_pt result, ring r )
54{
55  if (f.isZero())
56    return;
57  if ( ! f.inCoeffDomain() )
58  {
59    int l = f.level();
60    for ( CFIterator i = f; i.hasTerms(); i++ )
61    {
62      exp[l] = i.exp();
63      conv_RecPP( i.coeff(), exp, result, r );
64    }
65    exp[l] = 0;
66  }
67  else
68  {
69    poly term = p_Init(r);
70    pNext( term ) = NULL;
71    for ( int i = 1; i <= r->N; i++ )
72      p_SetExp( term, i, exp[i], r);
73    pGetCoeff( term )=r->cf->convFactoryNSingN(f, r->cf);
74    p_Setm( term, r );
75    if ( n_IsZero(pGetCoeff(term), r->cf) )
76    {
77      p_Delete(&term,r);
78    }
79    else
80    {
81      sBucket_Merge_p(result,term,1);
82    }
83  }
84}
85
86
87CanonicalForm convSingPFactoryP( poly p, const ring r )
88{
89  CanonicalForm result = 0;
90  int e, n = rVar(r);
91  BOOLEAN setChar=TRUE;
92
93  while ( p!=NULL )
94  {
95    CanonicalForm term;
96    term=r->cf->convSingNFactoryN(pGetCoeff( p ),setChar, r->cf);
97    if (errorreported) break;
98    setChar=FALSE;
99    for ( int i = n; i >0; i-- )
100    {
101      if ( (e = p_GetExp( p, i, r)) != 0 )
102        term *= power( Variable( i ), e );
103    }
104    result += term;
105    pIter( p );
106  }
107 return result;
108}
109
110int convFactoryISingI( const CanonicalForm & f)
111{
112  if (!f.isImm()) WerrorS("int overflow in det");
113  return f.intval();
114}
115
116CanonicalForm convSingAPFactoryAP ( poly p , const Variable & a, const ring r)
117{
118  CanonicalForm result = 0;
119  int e, n = r-> N;
120  int off=rPar(r);
121
122  if (!rField_is_Zp_a(r))
123    On(SW_RATIONAL);
124  while ( p!=NULL)
125  {
126    CanonicalForm term=convSingAFactoryA(((poly)p_GetCoeff(p, r->cf->extRing)),a, r);
127    for ( int i = 1; i <= n; i++ )
128    {
129      if ( (e = p_GetExp( p, i, r )) != 0 )
130        term *= power( Variable( i + off), e );
131    }
132    result += term;
133    pIter( p );
134  }
135  return result;
136}
137
138static void
139convRecAP_R ( const CanonicalForm & f, int * exp, poly & result, int par_start, int var_start, const ring r) ;
140
141poly convFactoryAPSingAP_R ( const CanonicalForm & f, int par_start, int var_start, const ring r )
142{
143  int n = rVar(r)+rPar(r)+1;
144  int * exp = (int *)omAlloc0(n*sizeof(int));
145  poly result = NULL;
146  convRecAP_R( f, exp, result,par_start, var_start, r );
147  omFreeSize((ADDRESS)exp,n*sizeof(int));
148  return result;
149}
150
151poly convFactoryAPSingAP ( const CanonicalForm & f, const ring r )
152{
153  return convFactoryAPSingAP_R(f,0,rPar(r),r);
154}
155
156static void convRecAP_R ( const CanonicalForm & f, int * exp, poly & result, int par_start, int var_start, const ring r )
157{
158  if (f.isZero())
159    return;
160  if ( ! f.inCoeffDomain() )
161  {
162    int l = f.level();
163    for ( CFIterator i = f; i.hasTerms(); i++ )
164    {
165      exp[l] = i.exp();
166      convRecAP_R( i.coeff(), exp, result, par_start, var_start, r);
167    }
168    exp[l] = 0;
169  }
170  else
171  {
172    poly z=(poly)convFactoryASingA( f,r );
173    if (z!=NULL)
174    {
175      poly term = p_Init(r);
176      pNext( term ) = NULL;
177      int i;
178      for ( i = rVar(r); i>0 ; i-- )
179        p_SetExp( term, i , exp[i+var_start],r);
180      //if (rRing_has_Comp(currRing->extRing)) p_SetComp(term, 0, currRing->extRing); // done by pInit
181      if (par_start==0)
182      {
183        for ( i = 1; i <= var_start; i++ )
184        //z->e[i-1]+=exp[i];
185          p_AddExp(z,i,exp[i],r->cf->extRing);
186      }
187      else
188      {
189        for ( i = par_start+1; i <= var_start+rPar(r); i++ )
190        //z->e[i-1]+=exp[i];
191          p_AddExp(z,i,exp[i-par_start],r->cf->extRing);
192      }
193      pGetCoeff(term)=(number)ALLOC0_RNUMBER();
194      p_GetCoeff(term, r->cf->extRing)=(number) z;
195      p_Setm( term,r );
196      result = p_Add_q( result, term, r );
197    }
198  }
199}
200
201CanonicalForm convSingAFactoryA ( poly p , const Variable & a, const ring r )
202{
203  CanonicalForm result = 0;
204  int e;
205
206  while ( p!=NULL )
207  {
208    CanonicalForm term;
209    if ( rField_is_Zp_a(r) )
210    {
211      term = n_Int( p_GetCoeff( p, r->cf->extRing ), r->cf->extRing->cf );
212    }
213    else
214    {
215      if ( SR_HDL(p_GetCoeff( p, r->cf->extRing )) & SR_INT )
216        term = SR_TO_INT(p_GetCoeff( p, r->cf->extRing )) ;
217      else
218      {
219        if ( p_GetCoeff( p, r->cf->extRing )->s == 3 )
220        {
221          mpz_t dummy;
222          mpz_init_set( dummy, (p_GetCoeff( p,r->cf->extRing )->z) );
223          term = make_cf( dummy );
224        }
225        else
226        {
227          // assume s==0 or s==1
228          mpz_t num, den;
229          On(SW_RATIONAL);
230          mpz_init_set( num, (p_GetCoeff( p, r->cf->extRing )->z) );
231          mpz_init_set( den, (p_GetCoeff( p, r->cf->extRing )->n) );
232          term = make_cf( num, den, ( p_GetCoeff( p, r->cf->extRing )->s != 1 ));
233        }
234      }
235    }
236    if ( (e = p_GetExp( p, 1, r->cf->extRing )) != 0 )
237      term *= power( a , e );
238    result += term;
239    p = pNext( p );
240  }
241  return result;
242}
243
244static number convFactoryNSingAN( const CanonicalForm &f, const ring r)
245{
246  if ( f.isImm() )
247  {
248    long longf=f.intval();
249    int intf=(int) longf;
250    if((long)intf==longf)
251    {
252      assume (r->cf->extRing != NULL);
253      return n_Init(f.intval(),r->cf->extRing->cf);
254    }
255    else return nlRInit( longf );
256  }
257  else
258  {
259    number z=ALLOC_RNUMBER();
260#if defined(LDEBUG)
261    z->debug=123456;
262#endif
263    gmp_numerator( f, z->z );
264    if ( f.den().isOne() )
265    {
266      z->s = 3;
267    }
268    else
269    {
270      gmp_denominator( f, z->n );
271      z->s = 0;
272      nlNormalize(z,r->cf->extRing->cf);
273    }
274    /*#ifdef LDEBUG
275    nlTest(z,r->cf->extRing->cf);
276    #endif*/
277    return z;
278  }
279}
280
281poly convFactoryASingA ( const CanonicalForm & f, const ring r )
282{
283  poly a=NULL;
284  poly t;
285  for( CFIterator i=f; i.hasTerms(); i++)
286  {
287    t= p_Init (r->cf->extRing);
288    p_GetCoeff(t, r->cf->extRing)= convFactoryNSingAN( i.coeff(), r );
289    if (n_IsZero(p_GetCoeff(t,r->cf->extRing),r->cf->extRing->cf))
290    {
291      p_Delete(&t,r->cf->extRing);
292    }
293    else
294    {
295      p_SetExp(t,1,i.exp(),r->cf->extRing);
296      a=p_Add_q(a,t,r->cf->extRing);
297    }
298  }
299  if (a!=NULL)
300  {
301    if( r->cf->extRing != NULL )
302      if (r->cf->extRing->qideal->m[0]!=NULL)
303      {
304        poly l=r->cf->extRing->qideal->m[0];
305        if (p_GetExp(a,1,r->cf->extRing) >= p_GetExp(l,1,r->cf->extRing))
306          a = p_PolyDiv (a, l, FALSE, r->cf->extRing); // ???
307      }
308  }
309  return a;
310}
311
312CanonicalForm convSingTrPFactoryP ( poly p, const ring r )
313{
314  CanonicalForm result = 0;
315  int e, n = rVar(r);
316  int offs = rPar(r);
317
318  while ( p!=NULL )
319  {
320    n_Normalize(p_GetCoeff(p, r), r->cf);
321    CanonicalForm term=convSingPFactoryP(NUM(p_GetCoeff(p, r)),r->cf->extRing);
322
323    if ((DEN(p_GetCoeff(p,r))!=NULL)
324    && (!errorreported))
325    {
326      WerrorS("conversion error: denominator!= 1");
327    }
328
329    for ( int i = n; i > 0; i-- )
330    {
331      if ( (e = p_GetExp( p, i,r )) != 0 )
332        term = term * power( Variable( i + offs ), e );
333    }
334    result += term;
335    p = pNext( p );
336  }
337  return result;
338}
339
340poly convFactoryPSingTrP ( const CanonicalForm & f, const ring r )
341{
342  int n = rVar(r)+1;
343  int * exp = (int*)omAlloc0(n*sizeof(int));
344  poly result = NULL;
345  convRecTrP( f, exp, result , rPar(r), r );
346  omFreeSize((ADDRESS)exp,n*sizeof(int));
347  return result;
348}
349
350static void
351convRecTrP ( const CanonicalForm & f, int * exp, poly & result , int offs, const ring r)
352{
353  if (f.isZero())
354    return;
355  if ( f.level() > offs )
356  {
357    int l = f.level();
358    for ( CFIterator i = f; i.hasTerms(); i++ )
359    {
360      exp[l-offs] = i.exp();
361      convRecTrP( i.coeff(), exp, result, offs, r );
362    }
363    exp[l-offs] = 0;
364  }
365  else
366  {
367    poly term = p_Init(r);
368    pNext( term ) = NULL;
369    for ( int i = rVar(r); i>0; i-- )
370      p_SetExp( term, i ,exp[i], r);
371    //if (rRing_has_Comp(currRing)) p_SetComp(term, 0, currRing); // done by pInit
372    pGetCoeff(term) = ALLOC0_RNUMBER(); // Q!?
373    NUM(pGetCoeff(term))=convFactoryPSingP( f, r->cf->extRing );
374    p_Setm( term,r );
375    result = p_Add_q( result, term,r );
376  }
377}
378
379#if 0
380CanonicalForm
381convSingGFFactoryGF( poly p )
382{
383  CanonicalForm result=CanonicalForm(0);
384  int e, n = pVariables;
385
386  while ( p != NULL )
387  {
388    CanonicalForm term;
389    term = make_cf_from_gf( (int)(long)pGetCoeff( p ) );
390    //int * A=(int *)&term;
391    //Print("term=%x, == 0 ?: %d\n",*A, term.isZero());
392    for ( int i = 1; i <= n; i++ )
393    {
394      if ( (e = pGetExp( p, i )) != 0 )
395         term *= power( Variable( i ), e );
396    }
397    result += term;
398    p = pNext( p );
399  }
400  return result;
401}
402
403poly
404convFactoryGFSingGF ( const CanonicalForm & f )
405{
406//    cerr << " f = " << f << endl;
407  int n = pVariables+1;
408  /* ASSERT( level( f ) <= pVariables, "illegal number of variables" ); */
409  int * exp = (int*)omAlloc0(n*sizeof(int));
410  poly result = NULL;
411  convRecGFGF( f, exp, result );
412  omFreeSize((ADDRESS)exp,n*sizeof(int));
413  return result;
414}
415
416static void
417convRecGFGF ( const CanonicalForm & f, int * exp, poly & result )
418{
419  if (f.isZero())
420    return;
421  if ( ! f.inCoeffDomain() )
422  {
423    int l = f.level();
424    for ( CFIterator i = f; i.hasTerms(); i++ )
425    {
426      exp[l] = i.exp();
427      convRecGFGF( i.coeff(), exp, result );
428    }
429    exp[l] = 0;
430  }
431  else
432  {
433    poly term = pInit();
434    pNext( term ) = NULL;
435    for ( int i = 1; i <= pVariables; i++ )
436      pSetExp( term, i, exp[i]);
437    //if (rRing_has_Comp(currRing)) p_SetComp(term, 0, currRing); // done by pInit
438    pGetCoeff( term ) = (number) gf_value (f);
439    pSetm( term );
440    result = pAdd( result, term );
441  }
442}
443
444#endif
445#endif /* HAVE_FACTORY */
Note: See TracBrowser for help on using the repository browser.