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

fieker-DuValspielwiese
Last change on this file since b38d70 was d663f2, checked in by Oleksandr Motsak <motsak@…>, 13 years ago
FIX: fixing longrat-related bugs (due to the Uglyness in p_Numbers.h!)
  • Property mode set to 100644
File size: 10.8 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#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 <polys/monomials/p_polys.h>
20#include <polys/sbuckets.h>
21#include <polys/clapconv.h>
22
23//typedef __mpz_struct lint;
24
25void out_cf(char *s1,const CanonicalForm &f,char *s2);
26
27static void conv_RecPP ( const CanonicalForm & f, int * exp, sBucket_pt result, ring r );
28
29static void convRecTrP ( const CanonicalForm & f, int * exp, poly & result, int offs, const ring r );
30
31static void convRecGFGF ( const CanonicalForm & f, int * exp, poly & result );
32
33static number convFactoryNSingAN( const CanonicalForm &f, const ring r);
34
35poly convFactoryPSingP ( const CanonicalForm & f, const ring r )
36{
37  int n = rVar(r)+1;
38  /* ASSERT( level( f ) <= pVariables, "illegal number of variables" ); */
39  int * exp = (int*)omAlloc0(n*sizeof(int));
40  sBucket_pt result_bucket=sBucketCreate(r);
41  conv_RecPP( f, exp, result_bucket, r );
42  poly result; int dummy;
43  sBucketDestroyMerge(result_bucket,&result,&dummy);
44  omFreeSize((ADDRESS)exp,n*sizeof(int));
45  return result;
46}
47
48static void conv_RecPP ( const CanonicalForm & f, int * exp, sBucket_pt result, ring r )
49{
50  if (f.isZero())
51    return;
52  if ( ! f.inCoeffDomain() )
53  {
54    int l = f.level();
55    for ( CFIterator i = f; i.hasTerms(); i++ )
56    {
57      exp[l] = i.exp();
58      conv_RecPP( i.coeff(), exp, result, r );
59    }
60    exp[l] = 0;
61  }
62  else
63  {
64    poly term = p_Init(r);
65    pNext( term ) = NULL;
66    for ( int i = 1; i <= r->N; i++ )
67      p_SetExp( term, i, exp[i], r);
68    pGetCoeff( term )=r->cf->convFactoryNSingN(f, r->cf);
69    p_Setm( term, r );
70    if ( n_IsZero(pGetCoeff(term), r->cf) )
71    {
72      p_Delete(&term,r);
73    }
74    else
75    {
76      sBucket_Merge_p(result,term,1);
77    }
78  }
79}
80
81
82CanonicalForm convSingPFactoryP( poly p, const ring r )
83{
84  CanonicalForm result = 0;
85  int e, n = rVar(r);
86  BOOLEAN setChar=TRUE;
87
88  while ( p!=NULL )
89  {
90    CanonicalForm term;
91    term=r->cf->convSingNFactoryN(pGetCoeff( p ),setChar, r->cf);
92    if (errorreported) break;
93    setChar=FALSE;
94    for ( int i = n; i >0; i-- )
95    {
96      if ( (e = p_GetExp( p, i, r)) != 0 )
97        term *= power( Variable( i ), e );
98    }
99    result += term;
100    pIter( p );
101  }
102  return result;
103}
104
105int convFactoryISingI( const CanonicalForm & f)
106{
107  if (!f.isImm()) WerrorS("int overflow in det");
108  return f.intval();
109}
110#if 0
111CanonicalForm convSingAPFactoryAP ( poly p , const Variable & a, const ring r)
112{
113  CanonicalForm result = 0;
114  int e, n = pVariables;
115  int off=rPar(currRing);
116
117  On(SW_RATIONAL);
118  while ( p!=NULL)
119  {
120    CanonicalForm term=convSingAFactoryA(((lnumber)pGetCoeff(p))->z,a, r);
121    for ( int i = 1; i <= n; i++ )
122    {
123      if ( (e = pGetExp( p, i )) != 0 )
124        term *= power( Variable( i + off), e );
125    }
126    result += term;
127    pIter( p );
128  }
129  return result;
130}
131
132static void
133convRecAP_R ( const CanonicalForm & f, int * exp, poly & result, int par_start, int var_start, const ring r) ;
134
135poly convFactoryAPSingAP_R ( const CanonicalForm & f, int par_start, int var_start, const ring r )
136{
137  int n = rVar(r)+rPar(r)+1;
138  int * exp = (int *)omAlloc0(n*sizeof(int));
139  poly result = NULL;
140  convRecAP_R( f, exp, result,par_start, var_start, r );
141  omFreeSize((ADDRESS)exp,n*sizeof(int));
142  return result;
143}
144
145poly convFactoryAPSingAP ( const CanonicalForm & f, const ring r )
146{
147  return convFactoryAPSingAP_R(f,0,rPar(r),r);
148}
149
150static void convRecAP_R ( const CanonicalForm & f, int * exp, poly & result, int par_start, int var_start, const ring r )
151{
152  if (f.isZero())
153    return;
154  if ( ! f.inCoeffDomain() )
155  {
156    int l = f.level();
157    for ( CFIterator i = f; i.hasTerms(); i++ )
158    {
159      exp[l] = i.exp();
160      convRecAP_R( i.coeff(), exp, result, par_start, var_start, r);
161    }
162    exp[l] = 0;
163  }
164  else
165  {
166    poly z=(poly)convFactoryASingA( f,r );
167    if (z!=NULL)
168    {
169      poly term = p_Init(r);
170      pNext( term ) = NULL;
171      int i;
172      for ( i = rVar(r); i>0 ; i-- )
173        p_SetExp( term, i , exp[i+var_start],r);
174      //if (rRing_has_Comp(currRing->extRing)) p_SetComp(term, 0, currRing->extRing); // done by pInit
175      if (par_start==0)
176      {
177        for ( i = 1; i <= var_start; i++ )
178        //z->e[i-1]+=exp[i];
179          p_AddExp(z,i,exp[i],r->extRing);
180      }
181      else
182      {
183        for ( i = par_start+1; i <= var_start+rPar(currRing); i++ )
184        //z->e[i-1]+=exp[i];
185          p_AddExp(z,i,exp[i-par_start],r->extRing);
186      }
187      pGetCoeff(term)=(number)ALLOC0_LNUMBER();
188      ((lnumber)pGetCoeff(term))->z=z;
189      p_Setm( term,r );
190      result = p_Add_q( result, term, r );
191    }
192  }
193}
194
195CanonicalForm convSingAFactoryA ( poly p , const Variable & a, const ring r )
196{
197  CanonicalForm result = 0;
198  int e;
199
200  while ( p!=NULL )
201  {
202    CanonicalForm term;
203    /* does only work for finite fields:Z/p */
204    if ( rField_is_Zp_a() )
205    {
206      term = npInt( napGetCoeff( p ),r );
207    }
208    else
209    {
210      if ( SR_HDL(napGetCoeff( p )) & SR_INT )
211        term = SR_TO_INT(napGetCoeff( p )) ;
212      else
213      {
214        if ( napGetCoeff( p )->s == 3 )
215        {
216          lint dummy;
217          mpz_init_set( &dummy, (napGetCoeff( p )->z) );
218          term = make_cf( dummy );
219        }
220        else
221        {
222          // assume s==0 or s==1
223          lint num, den;
224          On(SW_RATIONAL);
225          mpz_init_set( &num, (napGetCoeff( p )->z) );
226          mpz_init_set( &den, (napGetCoeff( p )->n) );
227          term = make_cf( num, den, ( napGetCoeff( p )->s != 1 ));
228        }
229      }
230    }
231    if ( (e = p_GetExp( p, 1, r->extRing )) != 0 )
232      term *= power( a , e );
233    result += term;
234    p = pNext( p );
235  }
236  return result;
237}
238
239static number convFactoryNSingAN( const CanonicalForm &f, const ring r)
240{
241  if ( f.isImm() )
242    return n_Init( f.intval(), r->extRing );
243  else
244  {
245    number z=ALLOC_RNUMBER();
246#if defined(LDEBUG)
247    z->debug=123456;
248#endif
249    gmp_numerator( f, z->z );
250    if ( f.den().isOne() )
251    {
252      z->s = 3;
253    }
254    else
255    {
256      gmp_denominator( f, z->n );
257      z->s = 0;
258      nlNormalize(z);
259    }
260    #ifdef LDEBUG
261    nlDBTest(z,__FILE__,__LINE__);
262    #endif
263    return z;
264  }
265}
266
267poly convFactoryASingA ( const CanonicalForm & f, const ring r )
268{
269  poly a=NULL;
270  poly t;
271  for( CFIterator i=f; i.hasTerms(); i++)
272  {
273    t=napNew();
274    // pNext( t ) = NULL; //already done by napNew
275    pGetCoeff(t)=convFactoryNSingAN( i.coeff(), r );
276    if (n_IsZero(napGetCoeff(t),r->extRing))
277    {
278      p_Delete(&t,r->extRing);
279    }
280    else
281    {
282      p_SetExp(t,1,i.exp(),r->extRing);
283      a=p_Add_q(a,t,r->extRing);
284    }
285  }
286  if (a!=NULL)
287  {
288    if (r->minpoly!=NULL)
289    {
290      lnumber l=(lnumber)r->minpoly;
291      if (p_GetExp(a,1,r->extRing) >= p_GetExp(l->z,1,r->extRing))
292        a = napRemainder( a, l->z);
293    }
294  }
295  return a;
296}
297
298CanonicalForm convSingTrPFactoryP ( poly p, const ring r )
299{
300  CanonicalForm result = 0;
301  int e, n = rVar(r);
302  int offs = rPar(r);
303
304  while ( p!=NULL )
305  {
306    n_Normalize(pGetCoeff(p),r);
307    CanonicalForm term=convSingPFactoryP(((lnumber)pGetCoeff(p))->z,r->extRing);
308
309    if ((((lnumber)pGetCoeff(p))->n!=NULL)
310    && (!errorreported))
311    {
312      WerrorS("conversion error: denominator!= 1");
313    }
314
315    for ( int i = n; i > 0; i-- )
316    {
317      if ( (e = p_GetExp( p, i,r )) != 0 )
318        term = term * power( Variable( i + offs ), e );
319    }
320    result += term;
321    p = pNext( p );
322  }
323  return result;
324}
325
326poly convFactoryPSingTrP ( const CanonicalForm & f, const ring r )
327{
328  int n = rVar(r)+1;
329  int * exp = (int*)omAlloc0(n*sizeof(int));
330  poly result = NULL;
331  convRecTrP( f, exp, result , rPar(r), r );
332  omFreeSize((ADDRESS)exp,n*sizeof(int));
333  return result;
334}
335
336static void
337convRecTrP ( const CanonicalForm & f, int * exp, poly & result , int offs, const ring r)
338{
339  if (f.isZero())
340    return;
341  if ( f.level() > offs )
342  {
343    int l = f.level();
344    for ( CFIterator i = f; i.hasTerms(); i++ )
345    {
346      exp[l-offs] = i.exp();
347      convRecTrP( i.coeff(), exp, result, offs, r );
348    }
349    exp[l-offs] = 0;
350  }
351  else
352  {
353    poly term = p_Init(r);
354    pNext( term ) = NULL;
355    for ( int i = rVar(r); i>0; i-- )
356      p_SetExp( term, i ,exp[i], r);
357    //if (rRing_has_Comp(currRing)) p_SetComp(term, 0, currRing); // done by pInit
358    pGetCoeff(term) = ALLOC0_RNUMBER(); // Q!?
359    ((lnumber)pGetCoeff(term))->z=convFactoryPSingP( f, r->extRing );
360    p_Setm( term,r );
361    result = p_Add_q( result, term,r );
362  }
363}
364
365number   nlChineseRemainder(number *x, number *q,int rl)
366// elemenst in the array are x[0..(rl-1)], q[0..(rl-1)]
367{
368#ifdef HAVE_FACTORY
369  setCharacteristic( 0 ); // only in char 0
370  CFArray X(rl), Q(rl);
371  int i;
372  for(i=rl-1;i>=0;i--)
373  {
374    X[i]=convSingNFactoryN(x[i],NULL); // may be larger MAX_INT
375    Q[i]=convSingNFactoryN(q[i],NULL); // may be larger MAX_INT
376  }
377  CanonicalForm xnew,qnew;
378  chineseRemainder(X,Q,xnew,qnew);
379  number n=convFactoryNSingN(xnew,NULL);
380  number p=convFactoryNSingN(qnew,NULL);
381  number p2=nlIntDiv(p,nlInit(2, NULL));
382  if (nlGreater(n,p2))
383  {
384     number n2=nlSub(n,p);
385     nlDelete(&n,NULL);
386     n=n2;
387  }
388  nlDelete(&p,NULL);
389  nlDelete(&p2,NULL);
390  return n;
391#else
392  WerrorS("not implemented");
393  return nlInit(0);
394#endif
395}
396
397CanonicalForm
398convSingGFFactoryGF( poly p )
399{
400  CanonicalForm result=CanonicalForm(0);
401  int e, n = pVariables;
402
403  while ( p != NULL )
404  {
405    CanonicalForm term;
406    term = make_cf_from_gf( (int)(long)pGetCoeff( p ) );
407    //int * A=(int *)&term;
408    //Print("term=%x, == 0 ?: %d\n",*A, term.isZero());
409    for ( int i = 1; i <= n; i++ )
410    {
411      if ( (e = pGetExp( p, i )) != 0 )
412         term *= power( Variable( i ), e );
413    }
414    result += term;
415    p = pNext( p );
416  }
417  return result;
418}
419
420poly
421convFactoryGFSingGF ( const CanonicalForm & f )
422{
423//    cerr << " f = " << f << endl;
424  int n = pVariables+1;
425  /* ASSERT( level( f ) <= pVariables, "illegal number of variables" ); */
426  int * exp = (int*)omAlloc0(n*sizeof(int));
427  poly result = NULL;
428  convRecGFGF( f, exp, result );
429  omFreeSize((ADDRESS)exp,n*sizeof(int));
430  return result;
431}
432
433static void
434convRecGFGF ( const CanonicalForm & f, int * exp, poly & result )
435{
436  if (f.isZero())
437    return;
438  if ( ! f.inCoeffDomain() )
439  {
440    int l = f.level();
441    for ( CFIterator i = f; i.hasTerms(); i++ )
442    {
443      exp[l] = i.exp();
444      convRecGFGF( i.coeff(), exp, result );
445    }
446    exp[l] = 0;
447  }
448  else
449  {
450    poly term = pInit();
451    pNext( term ) = NULL;
452    for ( int i = 1; i <= pVariables; i++ )
453      pSetExp( term, i, exp[i]);
454    //if (rRing_has_Comp(currRing)) p_SetComp(term, 0, currRing); // done by pInit
455    pGetCoeff( term ) = (number) gf_value (f);
456    pSetm( term );
457    result = pAdd( result, term );
458  }
459}
460
461#endif
462#endif
Note: See TracBrowser for help on using the repository browser.