source: git/libpolys/polys/clapconv.cc @ dc4782

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