source: git/libpolys/polys/clapconv.cc @ 9a416a9

fieker-DuValspielwiese
Last change on this file since 9a416a9 was 9a416a9, checked in by Hans Schoenemann <hannes@…>, 10 years ago
fix: typo
  • Property mode set to 100644
File size: 10.9 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
11
12
13#include <misc/auxiliary.h>
14
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
36//static 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  p=pReverse(p);
94  poly op=p;
95  while ( p!=NULL )
96  {
97    CanonicalForm term;
98    term=r->cf->convSingNFactoryN(pGetCoeff( p ),setChar, r->cf);
99    if (errorreported) break;
100    setChar=FALSE;
101    for ( int i = n; i >0; i-- )
102    {
103      if ( (e = p_GetExp( p, i, r)) != 0 )
104        term *= power( Variable( i ), e );
105    }
106    result += term;
107    pIter( p );
108 }
109 op=pReverse(op);
110 return result;
111}
112
113int convFactoryISingI( const CanonicalForm & f)
114{
115  if (!f.isImm()) WerrorS("int overflow in det");
116  return f.intval();
117}
118
119CanonicalForm convSingAPFactoryAP ( poly p , const Variable & a, const ring r)
120{
121  CanonicalForm result = 0;
122  int e, n = r-> N;
123  int off=rPar(r);
124
125  if (!rField_is_Zp_a(r))
126    On(SW_RATIONAL);
127  while ( p!=NULL)
128  {
129    CanonicalForm term=convSingAFactoryA(((poly)p_GetCoeff(p, r->cf->extRing)),a, r);
130    for ( int i = 1; i <= n; i++ )
131    {
132      if ( (e = p_GetExp( p, i, r )) != 0 )
133        term *= power( Variable( i + off), e );
134    }
135    result += term;
136    pIter( p );
137  }
138  return result;
139}
140
141static void
142convRecAP_R ( const CanonicalForm & f, int * exp, poly & result, int par_start, int var_start, const ring r) ;
143
144poly convFactoryAPSingAP_R ( const CanonicalForm & f, int par_start, int var_start, const ring r )
145{
146  int n = rVar(r)+rPar(r)+1;
147  int * exp = (int *)omAlloc0(n*sizeof(int));
148  poly result = NULL;
149  convRecAP_R( f, exp, result,par_start, var_start, r );
150  omFreeSize((ADDRESS)exp,n*sizeof(int));
151  return result;
152}
153
154poly convFactoryAPSingAP ( const CanonicalForm & f, const ring r )
155{
156  return convFactoryAPSingAP_R(f,0,rPar(r),r);
157}
158
159static void convRecAP_R ( const CanonicalForm & f, int * exp, poly & result, int par_start, int var_start, const ring r )
160{
161  if (f.isZero())
162    return;
163  if ( ! f.inCoeffDomain() )
164  {
165    int l = f.level();
166    for ( CFIterator i = f; i.hasTerms(); i++ )
167    {
168      exp[l] = i.exp();
169      convRecAP_R( i.coeff(), exp, result, par_start, var_start, r);
170    }
171    exp[l] = 0;
172  }
173  else
174  {
175    poly z=(poly)convFactoryASingA( f,r );
176    if (z!=NULL)
177    {
178      poly term = p_Init(r);
179      pNext( term ) = NULL;
180      int i;
181      for ( i = rVar(r); i>0 ; i-- )
182        p_SetExp( term, i , exp[i+var_start],r);
183      //if (rRing_has_Comp(currRing->extRing)) p_SetComp(term, 0, currRing->extRing); // done by pInit
184      if (par_start==0)
185      {
186        for ( i = 1; i <= var_start; i++ )
187        //z->e[i-1]+=exp[i];
188          p_AddExp(z,i,exp[i],r->cf->extRing);
189      }
190      else
191      {
192        for ( i = par_start+1; i <= var_start+rPar(r); i++ )
193        //z->e[i-1]+=exp[i];
194          p_AddExp(z,i,exp[i-par_start],r->cf->extRing);
195      }
196      pGetCoeff(term)=(number)ALLOC0_RNUMBER();
197      p_GetCoeff(term, r->cf->extRing)=(number) z;
198      p_Setm( term,r );
199      result = p_Add_q( result, term, r );
200    }
201  }
202}
203
204CanonicalForm convSingAFactoryA ( poly p , const Variable & a, const ring r )
205{
206  CanonicalForm result = 0;
207  int e;
208
209  while ( p!=NULL )
210  {
211    CanonicalForm term;
212    if ( rField_is_Zp_a(r) )
213    {
214      term = n_Int( p_GetCoeff( p, r->cf->extRing ), r->cf->extRing->cf );
215    }
216    else
217    {
218      if ( SR_HDL(p_GetCoeff( p, r->cf->extRing )) & SR_INT )
219        term = SR_TO_INT(p_GetCoeff( p, r->cf->extRing )) ;
220      else
221      {
222        if ( p_GetCoeff( p, r->cf->extRing )->s == 3 )
223        {
224          mpz_t dummy;
225          mpz_init_set( dummy, (p_GetCoeff( p,r->cf->extRing )->z) );
226          term = make_cf( dummy );
227        }
228        else
229        {
230          // assume s==0 or s==1
231          mpz_t num, den;
232          On(SW_RATIONAL);
233          mpz_init_set( num, (p_GetCoeff( p, r->cf->extRing )->z) );
234          mpz_init_set( den, (p_GetCoeff( p, r->cf->extRing )->n) );
235          term = make_cf( num, den, ( p_GetCoeff( p, r->cf->extRing )->s != 1 ));
236        }
237      }
238    }
239    if ( (e = p_GetExp( p, 1, r->cf->extRing )) != 0 )
240      term *= power( a , e );
241    result += term;
242    p = pNext( p );
243  }
244  return result;
245}
246
247static number convFactoryNSingAN( const CanonicalForm &f, const ring r)
248{
249  if ( f.isImm() )
250  {
251    long longf=f.intval();
252    int intf=(int) longf;
253    if((long)intf==longf)
254    {
255      assume (r->cf->extRing != NULL);
256      return n_Init(f.intval(),r->cf->extRing->cf);
257    }
258    else return nlRInit( longf );
259  }
260  else
261  {
262    number z=ALLOC_RNUMBER();
263#if defined(LDEBUG)
264    z->debug=123456;
265#endif
266    gmp_numerator( f, z->z );
267    if ( f.den().isOne() )
268    {
269      z->s = 3;
270    }
271    else
272    {
273      gmp_denominator( f, z->n );
274      z->s = 0;
275      nlNormalize(z,r->cf->extRing->cf);
276    }
277    /*#ifdef LDEBUG
278    nlTest(z,r->cf->extRing->cf);
279    #endif*/
280    return z;
281  }
282}
283
284poly convFactoryASingA ( const CanonicalForm & f, const ring r )
285{
286  poly a=NULL;
287  poly t;
288  for( CFIterator i=f; i.hasTerms(); i++)
289  {
290    t= p_Init (r->cf->extRing);
291    p_GetCoeff(t, r->cf->extRing)= convFactoryNSingAN( i.coeff(), r );
292    if (n_IsZero(p_GetCoeff(t,r->cf->extRing),r->cf->extRing->cf))
293    {
294      p_Delete(&t,r->cf->extRing);
295    }
296    else
297    {
298      p_SetExp(t,1,i.exp(),r->cf->extRing);
299      a=p_Add_q(a,t,r->cf->extRing);
300    }
301  }
302  if (a!=NULL)
303  {
304    if( r->cf->extRing != NULL )
305      if (r->cf->extRing->qideal->m[0]!=NULL)
306      {
307        poly l=r->cf->extRing->qideal->m[0];
308        if (p_GetExp(a,1,r->cf->extRing) >= p_GetExp(l,1,r->cf->extRing))
309          a = p_PolyDiv (a, l, FALSE, r->cf->extRing); // ???
310      }
311  }
312  return a;
313}
314
315CanonicalForm convSingTrPFactoryP ( poly p, const ring r )
316{
317  CanonicalForm result = 0;
318  int e, n = rVar(r);
319  int offs = rPar(r);
320
321  while ( p!=NULL )
322  {
323    n_Normalize(p_GetCoeff(p, r), r->cf);
324
325    // test if denominator is constant
326    if (!p_IsConstantPoly(DEN (p_GetCoeff (p,r)),r->cf->extRing) && !errorreported)
327      WerrorS("conversion error: denominator!= 1");
328
329    CanonicalForm term=convSingPFactoryP(NUM (p_GetCoeff(p, r)),r->cf->extRing);
330
331    // if denominator is not NULL it should be a constant at this point
332    if (DEN (p_GetCoeff(p,r)) != NULL)
333    {
334      CanonicalForm den= convSingPFactoryP(DEN (p_GetCoeff(p, r)),r->cf->extRing);
335      if (rChar (r) == 0)
336        On (SW_RATIONAL);
337      term /= den;
338    }
339
340    for ( int i = n; i > 0; i-- )
341    {
342      if ( (e = p_GetExp( p, i,r )) != 0 )
343        term = term * power( Variable( i + offs ), e );
344    }
345    result += term;
346    p = pNext( p );
347  }
348  return result;
349}
350
351poly convFactoryPSingTrP ( const CanonicalForm & f, const ring r )
352{
353  int n = rVar(r)+1;
354  int * exp = (int*)omAlloc0(n*sizeof(int));
355  poly result = NULL;
356  convRecTrP( f, exp, result , rPar(r), r );
357  omFreeSize((ADDRESS)exp,n*sizeof(int));
358  return result;
359}
360
361static void
362convRecTrP ( const CanonicalForm & f, int * exp, poly & result , int offs, const ring r)
363{
364  if (f.isZero())
365    return;
366  if ( f.level() > offs )
367  {
368    int l = f.level();
369    for ( CFIterator i = f; i.hasTerms(); i++ )
370    {
371      exp[l-offs] = i.exp();
372      convRecTrP( i.coeff(), exp, result, offs, r );
373    }
374    exp[l-offs] = 0;
375  }
376  else
377  {
378    poly term = p_Init(r);
379    pNext( term ) = NULL;
380    for ( int i = rVar(r); i>0; i-- )
381      p_SetExp( term, i ,exp[i], r);
382    //if (rRing_has_Comp(currRing)) p_SetComp(term, 0, currRing); // done by pInit
383    pGetCoeff(term)=ntInit(convFactoryPSingP( f, r->cf->extRing ), r->cf);
384    p_Setm( term,r );
385    result = p_Add_q( result, term,r );
386  }
387}
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
Note: See TracBrowser for help on using the repository browser.