source: git/libpolys/polys/clapconv.cc @ 6bc4cd

spielwiese
Last change on this file since 6bc4cd was 0a1c16, checked in by Martin Lee <martinlee84@…>, 12 years ago
fix: do not switch on rationals if there none
  • 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
14#ifdef HAVE_FACTORY
15#define SI_DONT_HAVE_GLOBAL_VARS
16#include <factory/factory.h>
17
18#include <omalloc/omalloc.h>
19#include <coeffs/coeffs.h>
20#include <coeffs/longrat.h>
21#include <coeffs/modulop.h>
22#include <polys/monomials/p_polys.h>
23#include <polys/sbuckets.h>
24#include <polys/clapconv.h>
25
26#include "simpleideals.h"
27
28#define TRANSEXT_PRIVATES
29#include <polys/ext_fields/transext.h>
30
31void out_cf(char *s1,const CanonicalForm &f,char *s2);
32
33static void conv_RecPP ( const CanonicalForm & f, int * exp, sBucket_pt result, ring r );
34
35static void convRecTrP ( const CanonicalForm & f, int * exp, poly & result, int offs, const ring r );
36
37static void convRecGFGF ( const CanonicalForm & f, int * exp, poly & result );
38
39static number convFactoryNSingAN( const CanonicalForm &f, const ring r);
40
41poly convFactoryPSingP ( const CanonicalForm & f, const ring r )
42{
43  int n = rVar(r)+1;
44  /* ASSERT( level( f ) <= pVariables, "illegal number of variables" ); */
45  int * exp = (int*)omAlloc0(n*sizeof(int));
46  sBucket_pt result_bucket=sBucketCreate(r);
47  conv_RecPP( f, exp, result_bucket, r );
48  poly result; int dummy;
49  sBucketDestroyMerge(result_bucket,&result,&dummy);
50  omFreeSize((ADDRESS)exp,n*sizeof(int));
51  return result;
52}
53
54static void conv_RecPP ( const CanonicalForm & f, int * exp, sBucket_pt result, ring r )
55{
56  if (f.isZero())
57    return;
58  if ( ! f.inCoeffDomain() )
59  {
60    int l = f.level();
61    for ( CFIterator i = f; i.hasTerms(); i++ )
62    {
63      exp[l] = i.exp();
64      conv_RecPP( i.coeff(), exp, result, r );
65    }
66    exp[l] = 0;
67  }
68  else
69  {
70    poly term = p_Init(r);
71    pNext( term ) = NULL;
72    for ( int i = 1; i <= r->N; i++ )
73      p_SetExp( term, i, exp[i], r);
74    pGetCoeff( term )=r->cf->convFactoryNSingN(f, r->cf);
75    p_Setm( term, r );
76    if ( n_IsZero(pGetCoeff(term), r->cf) )
77    {
78      p_Delete(&term,r);
79    }
80    else
81    {
82      sBucket_Merge_p(result,term,1);
83    }
84  }
85}
86
87
88CanonicalForm convSingPFactoryP( poly p, const ring r )
89{
90  CanonicalForm result = 0;
91  int e, n = rVar(r);
92  BOOLEAN setChar=TRUE;
93
94  while ( p!=NULL )
95  {
96    CanonicalForm term;
97    term=r->cf->convSingNFactoryN(pGetCoeff( p ),setChar, r->cf);
98    if (errorreported) break;
99    setChar=FALSE;
100    for ( int i = n; i >0; i-- )
101    {
102      if ( (e = p_GetExp( p, i, r)) != 0 )
103        term *= power( Variable( i ), e );
104    }
105    result += term;
106    pIter( p );
107  }
108  return result;
109}
110
111int convFactoryISingI( const CanonicalForm & f)
112{
113  if (!f.isImm()) WerrorS("int overflow in det");
114  return f.intval();
115}
116
117CanonicalForm convSingAPFactoryAP ( poly p , const Variable & a, const ring r)
118{
119  CanonicalForm result = 0;
120  int e, n = r-> N;
121  int off=rPar(r);
122
123  if (!rField_is_Zp_a(r))
124    On(SW_RATIONAL);
125  while ( p!=NULL)
126  {
127    CanonicalForm term=convSingAFactoryA(((poly)p_GetCoeff(p, r->cf->extRing)),a, r);
128    for ( int i = 1; i <= n; i++ )
129    {
130      if ( (e = p_GetExp( p, i, r )) != 0 )
131        term *= power( Variable( i + off), e );
132    }
133    result += term;
134    pIter( p );
135  }
136  return result;
137}
138
139static void
140convRecAP_R ( const CanonicalForm & f, int * exp, poly & result, int par_start, int var_start, const ring r) ;
141
142poly convFactoryAPSingAP_R ( const CanonicalForm & f, int par_start, int var_start, const ring r )
143{
144  int n = rVar(r)+rPar(r)+1;
145  int * exp = (int *)omAlloc0(n*sizeof(int));
146  poly result = NULL;
147  convRecAP_R( f, exp, result,par_start, var_start, r );
148  omFreeSize((ADDRESS)exp,n*sizeof(int));
149  return result;
150}
151
152poly convFactoryAPSingAP ( const CanonicalForm & f, const ring r )
153{
154  return convFactoryAPSingAP_R(f,0,rPar(r),r);
155}
156
157static void convRecAP_R ( const CanonicalForm & f, int * exp, poly & result, int par_start, int var_start, const ring r )
158{
159  if (f.isZero())
160    return;
161  if ( ! f.inCoeffDomain() )
162  {
163    int l = f.level();
164    for ( CFIterator i = f; i.hasTerms(); i++ )
165    {
166      exp[l] = i.exp();
167      convRecAP_R( i.coeff(), exp, result, par_start, var_start, r);
168    }
169    exp[l] = 0;
170  }
171  else
172  {
173    poly z=(poly)convFactoryASingA( f,r );
174    if (z!=NULL)
175    {
176      poly term = p_Init(r);
177      pNext( term ) = NULL;
178      int i;
179      for ( i = rVar(r); i>0 ; i-- )
180        p_SetExp( term, i , exp[i+var_start],r);
181      //if (rRing_has_Comp(currRing->extRing)) p_SetComp(term, 0, currRing->extRing); // done by pInit
182      if (par_start==0)
183      {
184        for ( i = 1; i <= var_start; i++ )
185        //z->e[i-1]+=exp[i];
186          p_AddExp(z,i,exp[i],r->cf->extRing);
187      }
188      else
189      {
190        for ( i = par_start+1; i <= var_start+rPar(r); i++ )
191        //z->e[i-1]+=exp[i];
192          p_AddExp(z,i,exp[i-par_start],r->cf->extRing);
193      }
194      pGetCoeff(term)=(number)ALLOC0_RNUMBER();
195      p_GetCoeff(term, r->cf->extRing)=(number) z;
196      p_Setm( term,r );
197      result = p_Add_q( result, term, r );
198    }
199  }
200}
201
202CanonicalForm convSingAFactoryA ( poly p , const Variable & a, const ring r )
203{
204  CanonicalForm result = 0;
205  int e;
206
207  while ( p!=NULL )
208  {
209    CanonicalForm term;
210    if ( rField_is_Zp_a(r) )
211    {
212      term = n_Int( p_GetCoeff( p, r->cf->extRing ), r->cf->extRing->cf );
213    }
214    else
215    {
216      if ( SR_HDL(p_GetCoeff( p, r->cf->extRing )) & SR_INT )
217        term = SR_TO_INT(p_GetCoeff( p, r->cf->extRing )) ;
218      else
219      {
220        if ( p_GetCoeff( p, r->cf->extRing )->s == 3 )
221        {
222          mpz_t dummy;
223          mpz_init_set( dummy, (p_GetCoeff( p,r->cf->extRing )->z) );
224          term = make_cf( dummy );
225        }
226        else
227        {
228          // assume s==0 or s==1
229          mpz_t num, den;
230          On(SW_RATIONAL);
231          mpz_init_set( num, (p_GetCoeff( p, r->cf->extRing )->z) );
232          mpz_init_set( den, (p_GetCoeff( p, r->cf->extRing )->n) );
233          term = make_cf( num, den, ( p_GetCoeff( p, r->cf->extRing )->s != 1 ));
234        }
235      }
236    }
237    if ( (e = p_GetExp( p, 1, r->cf->extRing )) != 0 )
238      term *= power( a , e );
239    result += term;
240    p = pNext( p );
241  }
242  return result;
243}
244
245static number convFactoryNSingAN( const CanonicalForm &f, const ring r)
246{
247  if ( f.isImm() )
248    return n_Init( f.intval(), r->cf->extRing->cf);
249  else
250  {
251    number z=ALLOC_RNUMBER();
252#if defined(LDEBUG)
253    z->debug=123456;
254#endif
255    gmp_numerator( f, z->z );
256    if ( f.den().isOne() )
257    {
258      z->s = 3;
259    }
260    else
261    {
262      gmp_denominator( f, z->n );
263      z->s = 0;
264      nlNormalize(z,r->cf->extRing->cf);
265    }
266    /*#ifdef LDEBUG
267    nlTest(z,r->cf->extRing->cf);
268    #endif*/
269    return z;
270  }
271}
272
273poly convFactoryASingA ( const CanonicalForm & f, const ring r )
274{
275  poly a=NULL;
276  poly t;
277  for( CFIterator i=f; i.hasTerms(); i++)
278  {
279    t= p_Init (r->cf->extRing);
280    p_GetCoeff(t, r->cf->extRing)= convFactoryNSingAN( i.coeff(), r );
281    if (n_IsZero(p_GetCoeff(t,r->cf->extRing),r->cf->extRing->cf))
282    {
283      p_Delete(&t,r->cf->extRing);
284    }
285    else
286    {
287      p_SetExp(t,1,i.exp(),r->cf->extRing);
288      a=p_Add_q(a,t,r->cf->extRing);
289    }
290  }
291  if (a!=NULL)
292  {
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.