source: git/libpolys/polys/clapconv.cc @ 8b46459

spielwiese
Last change on this file since 8b46459 was 8b46459, checked in by Oleksandr Motsak <motsak@…>, 12 years ago
MP_INT -> mpz_t adaptation of libpolys/
  • Property mode set to 100644
File size: 10.4 KB
Line 
1// emacs edit mode for this file is -*- C++ -*-
2/****************************************
3*  Computer Algebra System SINGULAR     *
4****************************************/
5// $Id$
6/*
7* ABSTRACT: convert data between Singular and factory
8*/
9
10
11#include "config.h"
12#include <misc/auxiliary.h>
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(char *s1,const CanonicalForm &f,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  On(SW_RATIONAL);
123  while ( p!=NULL)
124  {
125    CanonicalForm term=convSingAFactoryA(((poly)p_GetCoeff(p, r->cf->extRing)),a, r);
126    for ( int i = 1; i <= n; i++ )
127    {
128      if ( (e = p_GetExp( p, i, r )) != 0 )
129        term *= power( Variable( i + off), e );
130    }
131    result += term;
132    pIter( p );
133  }
134  return result;
135}
136
137static void
138convRecAP_R ( const CanonicalForm & f, int * exp, poly & result, int par_start, int var_start, const ring r) ;
139
140poly convFactoryAPSingAP_R ( const CanonicalForm & f, int par_start, int var_start, const ring r )
141{
142  int n = rVar(r)+rPar(r)+1;
143  int * exp = (int *)omAlloc0(n*sizeof(int));
144  poly result = NULL;
145  convRecAP_R( f, exp, result,par_start, var_start, r );
146  omFreeSize((ADDRESS)exp,n*sizeof(int));
147  return result;
148}
149
150poly convFactoryAPSingAP ( const CanonicalForm & f, const ring r )
151{
152  return convFactoryAPSingAP_R(f,0,rPar(r),r);
153}
154
155static void convRecAP_R ( const CanonicalForm & f, int * exp, poly & result, int par_start, int var_start, const ring r )
156{
157  if (f.isZero())
158    return;
159  if ( ! f.inCoeffDomain() )
160  {
161    int l = f.level();
162    for ( CFIterator i = f; i.hasTerms(); i++ )
163    {
164      exp[l] = i.exp();
165      convRecAP_R( i.coeff(), exp, result, par_start, var_start, r);
166    }
167    exp[l] = 0;
168  }
169  else
170  {
171    poly z=(poly)convFactoryASingA( f,r );
172    if (z!=NULL)
173    {
174      poly term = p_Init(r);
175      pNext( term ) = NULL;
176      int i;
177      for ( i = rVar(r); i>0 ; i-- )
178        p_SetExp( term, i , exp[i+var_start],r);
179      //if (rRing_has_Comp(currRing->extRing)) p_SetComp(term, 0, currRing->extRing); // done by pInit
180      if (par_start==0)
181      {
182        for ( i = 1; i <= var_start; i++ )
183        //z->e[i-1]+=exp[i];
184          p_AddExp(z,i,exp[i],r->cf->extRing);
185      }
186      else
187      {
188        for ( i = par_start+1; i <= var_start+rPar(r); i++ )
189        //z->e[i-1]+=exp[i];
190          p_AddExp(z,i,exp[i-par_start],r->cf->extRing);
191      }
192      pGetCoeff(term)=(number)ALLOC0_RNUMBER();
193      p_GetCoeff(term, r->cf->extRing)=(number) z;
194      p_Setm( term,r );
195      result = p_Add_q( result, term, r );
196    }
197  }
198}
199
200CanonicalForm convSingAFactoryA ( poly p , const Variable & a, const ring r )
201{
202  CanonicalForm result = 0;
203  int e;
204
205  while ( p!=NULL )
206  {
207    CanonicalForm term;
208    if ( rField_is_Zp_a(r) )
209    {
210      term = n_Int( p_GetCoeff( p, r->cf->extRing ), r->cf->extRing->cf );
211    }
212    else
213    {
214      if ( SR_HDL(p_GetCoeff( p, r->cf->extRing )) & SR_INT )
215        term = SR_TO_INT(p_GetCoeff( p, r->cf->extRing )) ;
216      else
217      {
218        if ( p_GetCoeff( p, r->cf->extRing )->s == 3 )
219        {
220          mpz_t dummy;
221          mpz_init_set( dummy, (p_GetCoeff( p,r->cf->extRing )->z) );
222          term = make_cf( dummy );
223        }
224        else
225        {
226          // assume s==0 or s==1
227          mpz_t num, den;
228          On(SW_RATIONAL);
229          mpz_init_set( num, (p_GetCoeff( p, r->cf->extRing )->z) );
230          mpz_init_set( den, (p_GetCoeff( p, r->cf->extRing )->n) );
231          term = make_cf( num, den, ( p_GetCoeff( p, r->cf->extRing )->s != 1 ));
232        }
233      }
234    }
235    if ( (e = p_GetExp( p, 1, r->cf->extRing )) != 0 )
236      term *= power( a , e );
237    result += term;
238    p = pNext( p );
239  }
240  return result;
241}
242
243static number convFactoryNSingAN( const CanonicalForm &f, const ring r)
244{
245  if ( f.isImm() )
246    return n_Init( f.intval(), r->cf->extRing->cf);
247  else
248  {
249    number z=ALLOC_RNUMBER();
250#if defined(LDEBUG)
251    z->debug=123456;
252#endif
253    gmp_numerator( f, z->z );
254    if ( f.den().isOne() )
255    {
256      z->s = 3;
257    }
258    else
259    {
260      gmp_denominator( f, z->n );
261      z->s = 0;
262      nlNormalize(z,r->cf->extRing->cf);
263    }
264    /*#ifdef LDEBUG
265    nlTest(z,r->cf->extRing->cf);
266    #endif*/
267    return z;
268  }
269}
270
271poly convFactoryASingA ( const CanonicalForm & f, const ring r )
272{
273  poly a=NULL;
274  poly t;
275  for( CFIterator i=f; i.hasTerms(); i++)
276  {
277    t= p_Init (r->cf->extRing);
278    p_GetCoeff(t, r->cf->extRing)= convFactoryNSingAN( i.coeff(), r );
279    if (n_IsZero(p_GetCoeff(t,r->cf->extRing),r->cf->extRing->cf))
280    {
281      p_Delete(&t,r->cf->extRing);
282    }
283    else
284    {
285      p_SetExp(t,1,i.exp(),r->cf->extRing);
286      a=p_Add_q(a,t,r->cf->extRing);
287    }
288  }
289  if (a!=NULL)
290  {
291    if (r->cf->extRing->minideal->m[0]!=NULL)
292    {
293      poly l=r->cf->extRing->minideal->m[0];
294      if (p_GetExp(a,1,r->cf->extRing) >= p_GetExp(l,1,r->cf->extRing))
295        a = p_PolyDiv (a, l, FALSE, r->cf->extRing);
296    }
297  }
298  return a;
299}
300
301CanonicalForm convSingTrPFactoryP ( poly p, const ring r )
302{
303  CanonicalForm result = 0;
304  int e, n = rVar(r);
305  int offs = rPar(r);
306
307  while ( p!=NULL )
308  {
309    n_Normalize(p_GetCoeff(p, r), r->cf);
310    CanonicalForm term=convSingPFactoryP(NUM(p_GetCoeff(p, r)),r->cf->extRing);
311
312    if ((DEN(p_GetCoeff(p,r))!=NULL)
313    && (!errorreported))
314    {
315      WerrorS("conversion error: denominator!= 1");
316    }
317
318    for ( int i = n; i > 0; i-- )
319    {
320      if ( (e = p_GetExp( p, i,r )) != 0 )
321        term = term * power( Variable( i + offs ), e );
322    }
323    result += term;
324    p = pNext( p );
325  }
326  return result;
327}
328
329poly convFactoryPSingTrP ( const CanonicalForm & f, const ring r )
330{
331  int n = rVar(r)+1;
332  int * exp = (int*)omAlloc0(n*sizeof(int));
333  poly result = NULL;
334  convRecTrP( f, exp, result , rPar(r), r );
335  omFreeSize((ADDRESS)exp,n*sizeof(int));
336  return result;
337}
338
339static void
340convRecTrP ( const CanonicalForm & f, int * exp, poly & result , int offs, const ring r)
341{
342  if (f.isZero())
343    return;
344  if ( f.level() > offs )
345  {
346    int l = f.level();
347    for ( CFIterator i = f; i.hasTerms(); i++ )
348    {
349      exp[l-offs] = i.exp();
350      convRecTrP( i.coeff(), exp, result, offs, r );
351    }
352    exp[l-offs] = 0;
353  }
354  else
355  {
356    poly term = p_Init(r);
357    pNext( term ) = NULL;
358    for ( int i = rVar(r); i>0; i-- )
359      p_SetExp( term, i ,exp[i], r);
360    //if (rRing_has_Comp(currRing)) p_SetComp(term, 0, currRing); // done by pInit
361    pGetCoeff(term) = ALLOC0_RNUMBER(); // Q!?
362    NUM(pGetCoeff(term))=convFactoryPSingP( f, r->cf->extRing );
363    p_Setm( term,r );
364    result = p_Add_q( result, term,r );
365  }
366}
367
368#if 0
369CanonicalForm
370convSingGFFactoryGF( poly p )
371{
372  CanonicalForm result=CanonicalForm(0);
373  int e, n = pVariables;
374
375  while ( p != NULL )
376  {
377    CanonicalForm term;
378    term = make_cf_from_gf( (int)(long)pGetCoeff( p ) );
379    //int * A=(int *)&term;
380    //Print("term=%x, == 0 ?: %d\n",*A, term.isZero());
381    for ( int i = 1; i <= n; i++ )
382    {
383      if ( (e = pGetExp( p, i )) != 0 )
384         term *= power( Variable( i ), e );
385    }
386    result += term;
387    p = pNext( p );
388  }
389  return result;
390}
391
392poly
393convFactoryGFSingGF ( const CanonicalForm & f )
394{
395//    cerr << " f = " << f << endl;
396  int n = pVariables+1;
397  /* ASSERT( level( f ) <= pVariables, "illegal number of variables" ); */
398  int * exp = (int*)omAlloc0(n*sizeof(int));
399  poly result = NULL;
400  convRecGFGF( f, exp, result );
401  omFreeSize((ADDRESS)exp,n*sizeof(int));
402  return result;
403}
404
405static void
406convRecGFGF ( const CanonicalForm & f, int * exp, poly & result )
407{
408  if (f.isZero())
409    return;
410  if ( ! f.inCoeffDomain() )
411  {
412    int l = f.level();
413    for ( CFIterator i = f; i.hasTerms(); i++ )
414    {
415      exp[l] = i.exp();
416      convRecGFGF( i.coeff(), exp, result );
417    }
418    exp[l] = 0;
419  }
420  else
421  {
422    poly term = pInit();
423    pNext( term ) = NULL;
424    for ( int i = 1; i <= pVariables; i++ )
425      pSetExp( term, i, exp[i]);
426    //if (rRing_has_Comp(currRing)) p_SetComp(term, 0, currRing); // done by pInit
427    pGetCoeff( term ) = (number) gf_value (f);
428    pSetm( term );
429    result = pAdd( result, term );
430  }
431}
432
433#endif
434#endif
Note: See TracBrowser for help on using the repository browser.