source: git/libpolys/polys/clapconv.cc @ 16f511

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