source: git/libpolys/polys/clapconv.cc @ 20c540

spielwiese
Last change on this file since 20c540 was 20c540, checked in by Oleksandr Motsak <motsak@…>, 12 years ago
Preparing the removal of Frank's p_Monic/p_Gcd/p_ExtGcd from common use chg: moved most of the functions (with helpers) into algext.cc and made them static NOTE: p_PolyDiv stayed in p_polys.cc as it is used in convFactoryASingA NOTE: p_ExtGcd also is made public via algext.h (needed elsewhere?)
  • 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#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(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  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    return n_Init( f.intval(), r->cf->extRing->cf);
248  else
249  {
250    number z=ALLOC_RNUMBER();
251#if defined(LDEBUG)
252    z->debug=123456;
253#endif
254    gmp_numerator( f, z->z );
255    if ( f.den().isOne() )
256    {
257      z->s = 3;
258    }
259    else
260    {
261      gmp_denominator( f, z->n );
262      z->s = 0;
263      nlNormalize(z,r->cf->extRing->cf);
264    }
265    /*#ifdef LDEBUG
266    nlTest(z,r->cf->extRing->cf);
267    #endif*/
268    return z;
269  }
270}
271
272poly convFactoryASingA ( const CanonicalForm & f, const ring r )
273{
274  poly a=NULL;
275  poly t;
276  for( CFIterator i=f; i.hasTerms(); i++)
277  {
278    t= p_Init (r->cf->extRing);
279    p_GetCoeff(t, r->cf->extRing)= convFactoryNSingAN( i.coeff(), r );
280    if (n_IsZero(p_GetCoeff(t,r->cf->extRing),r->cf->extRing->cf))
281    {
282      p_Delete(&t,r->cf->extRing);
283    }
284    else
285    {
286      p_SetExp(t,1,i.exp(),r->cf->extRing);
287      a=p_Add_q(a,t,r->cf->extRing);
288    }
289  }
290  if (a!=NULL)
291  {
292    if( r->cf->extRing != NULL )
293      if (r->cf->extRing->qideal->m[0]!=NULL)
294      {
295        poly l=r->cf->extRing->qideal->m[0];
296        if (p_GetExp(a,1,r->cf->extRing) >= p_GetExp(l,1,r->cf->extRing))
297          a = p_PolyDiv (a, l, FALSE, r->cf->extRing); // ???
298      }
299  }
300  return a;
301}
302
303CanonicalForm convSingTrPFactoryP ( poly p, const ring r )
304{
305  CanonicalForm result = 0;
306  int e, n = rVar(r);
307  int offs = rPar(r);
308
309  while ( p!=NULL )
310  {
311    n_Normalize(p_GetCoeff(p, r), r->cf);
312    CanonicalForm term=convSingPFactoryP(NUM(p_GetCoeff(p, r)),r->cf->extRing);
313
314    if ((DEN(p_GetCoeff(p,r))!=NULL)
315    && (!errorreported))
316    {
317      WerrorS("conversion error: denominator!= 1");
318    }
319
320    for ( int i = n; i > 0; i-- )
321    {
322      if ( (e = p_GetExp( p, i,r )) != 0 )
323        term = term * power( Variable( i + offs ), e );
324    }
325    result += term;
326    p = pNext( p );
327  }
328  return result;
329}
330
331poly convFactoryPSingTrP ( const CanonicalForm & f, const ring r )
332{
333  int n = rVar(r)+1;
334  int * exp = (int*)omAlloc0(n*sizeof(int));
335  poly result = NULL;
336  convRecTrP( f, exp, result , rPar(r), r );
337  omFreeSize((ADDRESS)exp,n*sizeof(int));
338  return result;
339}
340
341static void
342convRecTrP ( const CanonicalForm & f, int * exp, poly & result , int offs, const ring r)
343{
344  if (f.isZero())
345    return;
346  if ( f.level() > offs )
347  {
348    int l = f.level();
349    for ( CFIterator i = f; i.hasTerms(); i++ )
350    {
351      exp[l-offs] = i.exp();
352      convRecTrP( i.coeff(), exp, result, offs, r );
353    }
354    exp[l-offs] = 0;
355  }
356  else
357  {
358    poly term = p_Init(r);
359    pNext( term ) = NULL;
360    for ( int i = rVar(r); i>0; i-- )
361      p_SetExp( term, i ,exp[i], r);
362    //if (rRing_has_Comp(currRing)) p_SetComp(term, 0, currRing); // done by pInit
363    pGetCoeff(term) = ALLOC0_RNUMBER(); // Q!?
364    NUM(pGetCoeff(term))=convFactoryPSingP( f, r->cf->extRing );
365    p_Setm( term,r );
366    result = p_Add_q( result, term,r );
367  }
368}
369
370#if 0
371CanonicalForm
372convSingGFFactoryGF( poly p )
373{
374  CanonicalForm result=CanonicalForm(0);
375  int e, n = pVariables;
376
377  while ( p != NULL )
378  {
379    CanonicalForm term;
380    term = make_cf_from_gf( (int)(long)pGetCoeff( p ) );
381    //int * A=(int *)&term;
382    //Print("term=%x, == 0 ?: %d\n",*A, term.isZero());
383    for ( int i = 1; i <= n; i++ )
384    {
385      if ( (e = pGetExp( p, i )) != 0 )
386         term *= power( Variable( i ), e );
387    }
388    result += term;
389    p = pNext( p );
390  }
391  return result;
392}
393
394poly
395convFactoryGFSingGF ( const CanonicalForm & f )
396{
397//    cerr << " f = " << f << endl;
398  int n = pVariables+1;
399  /* ASSERT( level( f ) <= pVariables, "illegal number of variables" ); */
400  int * exp = (int*)omAlloc0(n*sizeof(int));
401  poly result = NULL;
402  convRecGFGF( f, exp, result );
403  omFreeSize((ADDRESS)exp,n*sizeof(int));
404  return result;
405}
406
407static void
408convRecGFGF ( const CanonicalForm & f, int * exp, poly & result )
409{
410  if (f.isZero())
411    return;
412  if ( ! f.inCoeffDomain() )
413  {
414    int l = f.level();
415    for ( CFIterator i = f; i.hasTerms(); i++ )
416    {
417      exp[l] = i.exp();
418      convRecGFGF( i.coeff(), exp, result );
419    }
420    exp[l] = 0;
421  }
422  else
423  {
424    poly term = pInit();
425    pNext( term ) = NULL;
426    for ( int i = 1; i <= pVariables; i++ )
427      pSetExp( term, i, exp[i]);
428    //if (rRing_has_Comp(currRing)) p_SetComp(term, 0, currRing); // done by pInit
429    pGetCoeff( term ) = (number) gf_value (f);
430    pSetm( term );
431    result = pAdd( result, term );
432  }
433}
434
435#endif
436#endif /* HAVE_FACTORY */
Note: See TracBrowser for help on using the repository browser.