source: git/libpolys/polys/clapconv.cc @ 33981c

spielwiese
Last change on this file since 33981c was 33981c, checked in by Hans Schoenemann <hannes@…>, 5 years ago
opt: convSingAPFactoryAP
  • Property mode set to 100644
File size: 10.5 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 "coeffs/coeffs.h"
18
19#include "coeffs/longrat.h" // snumber is necessary
20
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    number n=r->cf->convFactoryNSingN(f, r->cf);
70    if ( n_IsZero(n, r->cf) )
71    {
72      n_Delete(&n,r->cf);
73    }
74    else
75    {
76      poly term = p_Init(r);
77      //pNext( term ) = NULL; // done by p_Init
78      pGetCoeff(term)=n;
79      p_SetExpV(term,exp,r);
80      sBucket_Merge_m(result,term);
81    }
82  }
83}
84
85CanonicalForm convSingPFactoryP( poly p, const ring r )
86{
87  CanonicalForm result = 0;
88  int e, n = rVar(r);
89  BOOLEAN setChar=TRUE;
90
91//  p=pReverse(p);
92  poly op=p;
93  while ( p!=NULL )
94  {
95    CanonicalForm term=r->cf->convSingNFactoryN(pGetCoeff( p ),setChar, r->cf);
96    if (errorreported) break;
97    setChar=FALSE;
98    for ( int i = 1; i <=n; i++ )
99    {
100      if ( (e = p_GetExp( p, i, r)) != 0 )
101        term *= CanonicalForm( Variable( i ), e );
102    }
103    result += term;
104    pIter( p );
105 }
106// op=pReverse(op);
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 *= CanonicalForm( 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; // done by p_Init
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      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  assume (r != NULL);
246  assume (r->cf != NULL);
247  assume (r->cf->extRing != NULL);
248  // r->cf->extRing->cf has to be Q or Z/p (the supported types of factory)
249  return n_convFactoryNSingN( f, r->cf->extRing->cf );
250}
251
252poly convFactoryASingA ( const CanonicalForm & f, const ring r )
253{
254  poly a=NULL;
255  for( CFIterator i=f; i.hasTerms(); i++)
256  {
257    number n= convFactoryNSingAN( i.coeff(), r );
258    if (n_IsZero(n,r->cf->extRing->cf))
259    {
260      n_Delete(&n,r->cf->extRing->cf);
261    }
262    else
263    {
264      poly t= p_Init (r->cf->extRing);
265      pGetCoeff(t)=n;
266      p_SetExp(t,1,i.exp(),r->cf->extRing);
267      //p_Setm(t,r->cf->extRing);// not needed for rings with 1 variable
268      a=p_Add_q(a,t,r->cf->extRing);
269    }
270  }
271  if (a!=NULL)
272  {
273    if( r->cf->extRing != NULL )
274      if (r->cf->extRing->qideal->m[0]!=NULL)
275      {
276        poly l=r->cf->extRing->qideal->m[0];
277        if (p_GetExp(a,1,r->cf->extRing) >= p_GetExp(l,1,r->cf->extRing))
278          a = p_PolyDiv (a, l, FALSE, r->cf->extRing); // ???
279      }
280  }
281  return a;
282}
283
284CanonicalForm convSingTrPFactoryP ( poly p, const ring r )
285{
286  CanonicalForm result = 0;
287  int e, n = rVar(r);
288  int offs = rPar(r);
289
290  while ( p!=NULL )
291  {
292    n_Normalize(p_GetCoeff(p, r), r->cf);
293
294    // test if denominator is constant
295    if (!p_IsConstantPoly(DEN ((fraction)p_GetCoeff (p,r)),r->cf->extRing) && !errorreported)
296      WerrorS("conversion error: denominator!= 1");
297
298    CanonicalForm term=convSingPFactoryP(NUM ((fraction)p_GetCoeff(p, r)),r->cf->extRing);
299
300    // if denominator is not NULL it should be a constant at this point
301    if (DEN ((fraction)p_GetCoeff(p,r)) != NULL)
302    {
303      CanonicalForm den= convSingPFactoryP(DEN ((fraction)p_GetCoeff(p, r)),r->cf->extRing);
304      if (rChar (r) == 0)
305        On (SW_RATIONAL);
306      term /= den;
307    }
308
309    for ( int i = n; i > 0; i-- )
310    {
311      if ( (e = p_GetExp( p, i,r )) != 0 )
312        term = term * power( Variable( i + offs ), e );
313    }
314    result += term;
315    p = pNext( p );
316  }
317  return result;
318}
319
320poly convFactoryPSingTrP ( const CanonicalForm & f, const ring r )
321{
322  int n = rVar(r)+1;
323  int * exp = (int*)omAlloc0(n*sizeof(int));
324  poly result = NULL;
325  convRecTrP( f, exp, result , rPar(r), r );
326  omFreeSize((ADDRESS)exp,n*sizeof(int));
327  return result;
328}
329
330static void
331convRecTrP ( const CanonicalForm & f, int * exp, poly & result , int offs, const ring r)
332{
333  if (f.isZero())
334    return;
335  if ( f.level() > offs )
336  {
337    int l = f.level();
338    for ( CFIterator i = f; i.hasTerms(); i++ )
339    {
340      exp[l-offs] = i.exp();
341      convRecTrP( i.coeff(), exp, result, offs, r );
342    }
343    exp[l-offs] = 0;
344  }
345  else
346  {
347    poly term = p_Init(r);
348    //pNext( term ) = NULL; // done by p_Init
349    for ( int i = rVar(r); i>0; i-- )
350      p_SetExp( term, i ,exp[i], r);
351    //if (rRing_has_Comp(currRing)) p_SetComp(term, 0, currRing); // done by pInit
352    pGetCoeff(term)=ntInit(convFactoryPSingP( f, r->cf->extRing ), r->cf);
353    p_Setm( term,r );
354    result = p_Add_q( result, term,r );
355  }
356}
357
358#if 0
359CanonicalForm
360convSingGFFactoryGF( poly p )
361{
362  CanonicalForm result=CanonicalForm(0);
363  int e, n = pVariables;
364
365  while ( p != NULL )
366  {
367    CanonicalForm term;
368    term = make_cf_from_gf( (int)(long)pGetCoeff( p ) );
369    //int * A=(int *)&term;
370    //Print("term=%x, == 0 ?: %d\n",*A, term.isZero());
371    for ( int i = 1; i <= n; i++ )
372    {
373      if ( (e = pGetExp( p, i )) != 0 )
374         term *= power( Variable( i ), e );
375    }
376    result += term;
377    p = pNext( p );
378  }
379  return result;
380}
381
382poly
383convFactoryGFSingGF ( const CanonicalForm & f )
384{
385//    cerr << " f = " << f << endl;
386  int n = pVariables+1;
387  /* ASSERT( level( f ) <= pVariables, "illegal number of variables" ); */
388  int * exp = (int*)omAlloc0(n*sizeof(int));
389  poly result = NULL;
390  convRecGFGF( f, exp, result );
391  omFreeSize((ADDRESS)exp,n*sizeof(int));
392  return result;
393}
394
395static void
396convRecGFGF ( const CanonicalForm & f, int * exp, poly & result )
397{
398  if (f.isZero())
399    return;
400  if ( ! f.inCoeffDomain() )
401  {
402    int l = f.level();
403    for ( CFIterator i = f; i.hasTerms(); i++ )
404    {
405      exp[l] = i.exp();
406      convRecGFGF( i.coeff(), exp, result );
407    }
408    exp[l] = 0;
409  }
410  else
411  {
412    poly term = pInit();
413    pNext( term ) = NULL;
414    for ( int i = 1; i <= pVariables; i++ )
415      pSetExp( term, i, exp[i]);
416    //if (rRing_has_Comp(currRing)) p_SetComp(term, 0, currRing); // done by pInit
417    pGetCoeff( term ) = (number) gf_value (f);
418    pSetm( term );
419    result = pAdd( result, term );
420  }
421}
422
423#endif
Note: See TracBrowser for help on using the repository browser.