source: git/kernel/clapconv.cc @ 9b87a3

spielwiese
Last change on this file since 9b87a3 was 46f6af6, checked in by Hans Schönemann <hannes@…>, 16 years ago
*hannes: buckte for conversions git-svn-id: file:///usr/local/Singular/svn/trunk@10491 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 15.4 KB
Line 
1// emacs edit mode for this file is -*- C++ -*-
2/****************************************
3*  Computer Algebra System SINGULAR     *
4****************************************/
5// $Id: clapconv.cc,v 1.10 2008-01-07 13:36:16 Singular Exp $
6/*
7* ABSTRACT: convert data between Singular and factory
8*/
9
10
11#include "mod2.h"
12#ifdef HAVE_FACTORY
13#include "omalloc.h"
14#include "structs.h"
15#define SI_DONT_HAVE_GLOBAL_VARS
16#include "clapconv.h"
17#include "numbers.h"
18#include "modulop.h"
19#include "longalg.h"
20#include "polys.h"
21#include "febase.h"
22#include "ring.h"
23#include "sbuckets.h"
24
25static void convRec( const CanonicalForm & f, int * exp, poly & result );
26
27static void convRecAlg( const CanonicalForm & f, int * exp, napoly & result );
28
29static void conv_RecPP ( const CanonicalForm & f, int * exp, sBucket_pt result, ring r );
30
31static void convRecPTr ( const CanonicalForm & f, int * exp, napoly & result );
32
33static void convRecTrP ( const CanonicalForm & f, int * exp, poly & result, int offs );
34
35static void convRecGFGF ( const CanonicalForm & f, int * exp, poly & result );
36
37static number convFactoryNSingAN( const CanonicalForm &f);
38
39CanonicalForm convSingNFactoryN( number n )
40{
41  CanonicalForm term;
42  /* does only work for Zp, Q */
43  if ( getCharacteristic() != 0 )
44  {
45    term = npInt( n );
46  }
47  else
48  {
49    if ( SR_HDL(n) & SR_INT )
50    {
51      term = SR_TO_INT(n);
52    }
53    else
54    {
55      if ( n->s == 3 )
56      {
57        MP_INT dummy;
58        mpz_init_set( &dummy, &(n->z) );
59        term = make_cf( dummy );
60      }
61      else
62      {
63        // assume s==0 or s==1
64        MP_INT num, den;
65        On(SW_RATIONAL);
66        mpz_init_set( &num, &(n->z) );
67        mpz_init_set( &den, &(n->n) );
68        term = make_cf( num, den, ( n->s != 1 ));
69      }
70    }
71  }
72  return term;
73}
74
75number convFactoryNSingN( const CanonicalForm & n)
76{
77  if (n.isImm())
78    return nInit(n.intval());
79  else
80  {
81    number z=(number)omAllocBin(rnumber_bin);
82#if defined(LDEBUG)
83    z->debug=123456;
84#endif
85    z->z = gmp_numerator( n );
86    if ( n.den().isOne() )
87      z->s = 3;
88    else
89    {
90      z->n = gmp_denominator( n );
91      z->s = 0;
92    }
93    return z;
94  }
95}
96
97CanonicalForm conv_SingPFactoryP( poly p, ring r )
98{
99  CanonicalForm result = 0;
100  int e, n = r->N;
101  assume( rPar(r)==0);
102
103  if ( getCharacteristic() != 0 )
104  {
105    /* does only work for finite fields Z/p*/
106    while ( p != NULL )
107    {
108      CanonicalForm term = npInt( pGetCoeff( p ) );
109      for ( int i = n; i >0; i-- )
110      {
111        if ( (e = p_GetExp( p, i, r )) != 0 )
112           term *= power( Variable( i ), e );
113      }
114      result += term;
115      pIter( p );
116    }
117  }
118  else
119  {
120    while ( p != NULL )
121    {
122      CanonicalForm term;
123      if ( SR_HDL(pGetCoeff( p )) & SR_INT )
124      {
125        term = SR_TO_INT( pGetCoeff( p ) );
126      }
127      else
128      {
129        if ( pGetCoeff( p )->s == 3 )
130        {
131          MP_INT dummy;
132          mpz_init_set( &dummy, &(pGetCoeff( p )->z) );
133          term = make_cf( dummy );
134        }
135        else
136        {
137          // assume s==1 or s==0
138          MP_INT num, den;
139          On(SW_RATIONAL);
140          mpz_init_set( &num, &(pGetCoeff( p )->z) );
141          mpz_init_set( &den, &(pGetCoeff( p )->n) );
142          term = make_cf( num, den, ( pGetCoeff( p )->s != 1 ));
143        }
144      }
145      for ( int i = n; i >0; i-- )
146      {
147        if ( (e = p_GetExp( p, i, r )) != 0 )
148           term *= power( Variable( i ), e );
149      }
150      result += term;
151      pIter( p );
152    }
153  }
154  return result;
155}
156
157poly conv_FactoryPSingP ( const CanonicalForm & f, ring r )
158{
159//    cerr << " f = " << f << endl;
160  int n = r->N+1;
161  /* ASSERT( level( f ) <= pVariables, "illegal number of variables" ); */
162  int * exp = (int*)omAlloc0(n*sizeof(int));
163  sBucket_pt result_bucket=sBucketCreate(r);
164  conv_RecPP( f, exp, result_bucket, r );
165  poly result; int dummy;
166  sBucketDestroyMerge(result_bucket,&result,&dummy);
167  omFreeSize((ADDRESS)exp,n*sizeof(int));
168  return result;
169}
170
171static void conv_RecPP ( const CanonicalForm & f, int * exp, sBucket_pt result, ring r )
172{
173  if (f.isZero())
174    return;
175  if ( ! f.inCoeffDomain() )
176  {
177    int l = f.level();
178    for ( CFIterator i = f; i.hasTerms(); i++ )
179    {
180      exp[l] = i.exp();
181      conv_RecPP( i.coeff(), exp, result, r );
182    }
183    exp[l] = 0;
184  }
185  else
186  {
187    poly term = p_Init(r);
188    pNext( term ) = NULL;
189    for ( int i = 1; i <= r->N; i++ )
190      p_SetExp( term, i, exp[i], r);
191    if (rRing_has_Comp(r)) p_SetComp(term, 0, r);
192    if ( f.isImm() )
193      pGetCoeff( term ) = n_Init( f.intval(), r );
194    else
195    {
196      number z=(number)omAllocBin(rnumber_bin);
197#if defined(LDEBUG)
198      z->debug=123456;
199#endif
200      z->z = gmp_numerator( f );
201      if ( f.den().isOne() )
202        z->s = 3;
203      else
204      {
205        z->n = gmp_denominator( f );
206        z->s = 0;
207        nlNormalize(z);
208      }
209      pGetCoeff( term ) = z;
210    }
211    p_Setm( term, r );
212    if ( n_IsZero(pGetCoeff(term), r) )
213    {
214      p_Delete(&term,r);
215    }
216    else
217    {
218      sBucket_Merge_p(result,term,1);
219    }
220  }
221}
222
223
224CanonicalForm convSingTrFactoryP( napoly p )
225{
226  CanonicalForm result = 0;
227  int e, n = napVariables;
228
229  while ( p!=NULL)
230  {
231    CanonicalForm term;
232    /* does only work for finite fields */
233    if ( getCharacteristic() != 0 )
234    {
235      term = npInt( napGetCoeff( p ) );
236    }
237    else
238    {
239      if ( SR_HDL(napGetCoeff( p )) & SR_INT )
240        term = SR_TO_INT( napGetCoeff( p ) );
241      else
242      {
243        if ( napGetCoeff( p )->s == 3 )
244        {
245          MP_INT dummy;
246          mpz_init_set( &dummy, &(napGetCoeff( p )->z) );
247          term = make_cf( dummy );
248        }
249        else
250        {
251          // assume s==0 or s==1
252          MP_INT num, den;
253          On(SW_RATIONAL);
254          mpz_init_set( &num, &(napGetCoeff( p )->z) );
255          mpz_init_set( &den, &(napGetCoeff( p )->n) );
256          term = make_cf( num, den, ( napGetCoeff( p )->s != 1 ));
257        }
258      }
259    }
260    for ( int i = n; i >0; i-- )
261    {
262      if ( (e = napGetExp( p, i )) != 0 )
263        term *= power( Variable( i ), e );
264    }
265    result += term;
266    p = napNext( p );
267  }
268  return result;
269}
270
271napoly convFactoryPSingTr ( const CanonicalForm & f )
272{
273//    cerr << " f = " << f << endl;
274  int n = napVariables+1;
275  /* ASSERT( level( f ) <= napVariables, "illegal number of variables" ); */
276  int * exp = (int *)omAlloc0(n*sizeof(int));
277  napoly result = NULL;
278  convRecPTr( f, exp, result );
279  omFreeSize((ADDRESS)exp,n*sizeof(int));
280  return result;
281}
282
283static void convRecPTr ( const CanonicalForm & f, int * exp, napoly & result )
284{
285  if (f.isZero())
286    return;
287  if ( ! f.inCoeffDomain() )
288  {
289    int l = f.level();
290    for ( CFIterator i = f; i.hasTerms(); i++ )
291    {
292        exp[l] = i.exp();
293        convRecPTr( i.coeff(), exp, result );
294    }
295    exp[l] = 0;
296  }
297  else
298  {
299    napoly term = napNew();
300    // napNext( term ) = NULL; //already done by napNew
301    for ( int i = 1; i <= napVariables; i++ )
302      napSetExp( term, i , exp[i]);
303    if ( f.isImm() )
304      napGetCoeff( term ) = nacInit( f.intval() );
305    else
306    {
307      number z=(number)omAllocBin(rnumber_bin);
308#if defined(LDEBUG)
309      z->debug=123456;
310#endif
311      z->z = gmp_numerator( f );
312      if ( f.den().isOne() )
313      {
314        z->s = 3;
315      }
316      else
317      {
318        z->n = gmp_denominator( f );
319        if (mpz_cmp_si(&z->n,(long)1)==0)
320        {
321          mpz_clear(&z->n);
322          z->s=3;
323        }
324        else
325        {
326          z->s = 0;
327          nacNormalize(z);
328        }
329      }
330      napGetCoeff( term ) = z;
331    }
332    if (nacIsZero(pGetCoeff(term)))
333    {
334      napDelete(&term);
335    }
336    else
337    {
338      result = napAdd( result, term );
339    }
340  }
341}
342
343CanonicalForm convSingAPFactoryAP ( poly p , const Variable & a)
344{
345  CanonicalForm result = 0;
346  int e, n = pVariables;
347  int off=rPar(currRing);
348
349  On(SW_RATIONAL);
350  while ( p!=NULL)
351  {
352    CanonicalForm term=convSingAFactoryA(((lnumber)pGetCoeff(p))->z,a);
353    for ( int i = 1; i <= n; i++ )
354    {
355      if ( (e = pGetExp( p, i )) != 0 )
356        term *= power( Variable( i + off), e );
357    }
358    result += term;
359    pIter( p );
360  }
361  return result;
362}
363
364static void
365convRecAP_R ( const CanonicalForm & f, int * exp, poly & result, int par_start, int var_start) ;
366
367poly convFactoryAPSingAP_R ( const CanonicalForm & f, int par_start, int var_start )
368{
369  int n = pVariables+1+rPar(currRing);
370  int * exp = (int *)omAlloc0(n*sizeof(int));
371  poly result = NULL;
372  convRecAP_R( f, exp, result,par_start, var_start );
373  omFreeSize((ADDRESS)exp,n*sizeof(int));
374  return result;
375}
376
377poly convFactoryAPSingAP ( const CanonicalForm & f )
378{
379  return convFactoryAPSingAP_R(f,0,rPar(currRing));
380}
381
382static void convRecAP_R ( const CanonicalForm & f, int * exp, poly & result, int par_start, int var_start )
383{
384  if (f.isZero())
385    return;
386  if ( ! f.inCoeffDomain() )
387  {
388    int l = f.level();
389    for ( CFIterator i = f; i.hasTerms(); i++ )
390    {
391      exp[l] = i.exp();
392      convRecAP_R( i.coeff(), exp, result, par_start, var_start );
393    }
394    exp[l] = 0;
395  }
396  else
397  {
398    napoly z=(napoly)convFactoryASingA( f );
399    if (z!=NULL)
400    {
401      poly term = pInit();
402      pNext( term ) = NULL;
403      int i;
404      for ( i = 1; i <= pVariables; i++ )
405        pSetExp( term, i , exp[i+var_start]);
406      if (rRing_has_Comp(currRing->algring))
407        p_SetComp(term, 0, currRing->algring);
408      if (par_start==0)
409      {
410        for ( i = 1; i <= var_start; i++ )
411        //z->e[i-1]+=exp[i];
412          napAddExp(z,i,exp[i]);
413      }
414      else
415      {
416        for ( i = par_start+1; i <= var_start+rPar(currRing); i++ )
417        //z->e[i-1]+=exp[i];
418          napAddExp(z,i,exp[i-par_start]);
419      }
420      pGetCoeff(term)=(number)omAlloc0Bin(rnumber_bin);
421      ((lnumber)pGetCoeff(term))->z=z;
422      pSetm( term );
423      result = pAdd( result, term );
424    }
425  }
426}
427
428CanonicalForm convSingAFactoryA ( napoly p , const Variable & a )
429{
430  CanonicalForm result = 0;
431  int e;
432
433  while ( p!=NULL )
434  {
435    CanonicalForm term;
436    /* does only work for finite fields:Z/p */
437    if ( rField_is_Zp_a() )
438    {
439      term = npInt( napGetCoeff( p ) );
440    }
441    else
442    {
443      if ( SR_HDL(napGetCoeff( p )) & SR_INT )
444        term = SR_TO_INT(napGetCoeff( p )) ;
445      else
446      {
447        if ( napGetCoeff( p )->s == 3 )
448        {
449          MP_INT dummy;
450          mpz_init_set( &dummy, &(napGetCoeff( p )->z) );
451          term = make_cf( dummy );
452        }
453        else
454        {
455          // assume s==0 or s==1
456          MP_INT num, den;
457          On(SW_RATIONAL);
458          mpz_init_set( &num, &(napGetCoeff( p )->z) );
459          mpz_init_set( &den, &(napGetCoeff( p )->n) );
460          term = make_cf( num, den, ( napGetCoeff( p )->s != 1 ));
461        }
462      }
463    }
464    if ( (e = napGetExp( p, 1 )) != 0 )
465      term *= power( a , e );
466    result += term;
467    p = napNext( p );
468  }
469  return result;
470}
471
472static number convFactoryNSingAN( const CanonicalForm &f)
473{
474  if ( f.isImm() )
475    return nacInit( f.intval() );
476  else
477  {
478    number z=(number)omAllocBin(rnumber_bin);
479#if defined(LDEBUG)
480    z->debug=123456;
481#endif
482    z->z = gmp_numerator( f );
483    if ( f.den().isOne() )
484    {
485      z->s = 3;
486    }
487    else
488    {
489      z->n = gmp_denominator( f );
490      z->s = 0;
491      nlNormalize(z);
492    }
493    #ifdef LDEBUG
494    nlDBTest(z,__FILE__,__LINE__);
495    #endif
496    return z;
497  }
498}
499
500napoly convFactoryASingA ( const CanonicalForm & f )
501{
502  napoly a=NULL;
503  napoly t;
504  for( CFIterator i=f; i.hasTerms(); i++)
505  {
506    t=napNew();
507    // napNext( t ) = NULL; //already done by napNew
508    napGetCoeff(t)=convFactoryNSingAN( i.coeff() );
509    if (nacIsZero(napGetCoeff(t)))
510    {
511      napDelete(&t);
512    }
513    else
514    {
515      napSetExp(t,1,i.exp());
516      a=napAdd(a,t);
517    }
518  }
519  if (a!=NULL)
520  {
521    if (naMinimalPoly!=NULL)
522    {
523      if (napGetExp(a,1) >= napGetExp(naMinimalPoly,1))
524        a = napRemainder( a, naMinimalPoly);
525    }
526  }
527  return a;
528}
529
530CanonicalForm convSingTrPFactoryP ( poly p )
531{
532  CanonicalForm result = 0;
533  int e, n = pVariables;
534  int offs = rPar(currRing);
535
536  while ( p!=NULL )
537  {
538    nNormalize(pGetCoeff(p));
539    CanonicalForm term=convSingTrFactoryP(((lnumber)pGetCoeff(p))->z);
540
541    if ((((lnumber)pGetCoeff(p))->n!=NULL)
542    && (!errorreported))
543    {
544      WerrorS("conversion error: denominator!= 1");
545    }
546
547    for ( int i = n; i > 0; i-- )
548    {
549      if ( (e = pGetExp( p, i )) != 0 )
550        term = term * power( Variable( i + offs ), e );
551    }
552    result += term;
553    p = pNext( p );
554  }
555  return result;
556}
557
558poly convFactoryPSingTrP ( const CanonicalForm & f )
559{
560  int n = pVariables+1;
561  int * exp = (int*)omAlloc0(n*sizeof(int));
562  poly result = NULL;
563  convRecTrP( f, exp, result , rPar(currRing) );
564  omFreeSize((ADDRESS)exp,n*sizeof(int));
565  return result;
566}
567
568static void
569convRecTrP ( const CanonicalForm & f, int * exp, poly & result , int offs)
570{
571  if (f.isZero())
572    return;
573  if ( f.level() > offs )
574  {
575    int l = f.level();
576    for ( CFIterator i = f; i.hasTerms(); i++ )
577    {
578      exp[l-offs] = i.exp();
579      convRecTrP( i.coeff(), exp, result, offs );
580    }
581    exp[l-offs] = 0;
582  }
583  else
584  {
585    poly term = pInit();
586    pNext( term ) = NULL;
587    for ( int i = 1; i <= pVariables; i++ )
588      pSetExp( term, i ,exp[i]);
589    if (rRing_has_Comp(currRing))
590        p_SetComp(term, 0, currRing);
591    pGetCoeff(term)=(number)omAlloc0Bin(rnumber_bin);
592    ((lnumber)pGetCoeff(term))->z=convFactoryPSingTr( f );
593    pSetm( term );
594    result = pAdd( result, term );
595  }
596}
597
598number   nlChineseRemainder(number *x, number *q,int rl)
599// elemenst in the array are x[0..(rl-1)], q[0..(rl-1)]
600{
601#ifdef HAVE_FACTORY
602  CFArray X(rl), Q(rl);
603  int i;
604  for(i=rl-1;i>=0;i--)
605  {
606    X[i]=CanonicalForm(nlInt(x[i]));
607    Q[i]=CanonicalForm(nlInt(q[i]));
608  }
609  CanonicalForm xnew,qnew;
610  chineseRemainder(X,Q,xnew,qnew);
611  number n=convFactoryNSingN(xnew);
612  number p=convFactoryNSingN(qnew);
613  number p2=nlIntDiv(p,nlInit(2));
614  if (nlGreater(n,p2))
615  {
616     number n2=nlSub(n,p);
617     nlDelete(&n,currRing);
618     n=n2;
619  }
620  nlDelete(&p,currRing);
621  nlDelete(&p2,currRing);
622  return n;
623#else
624  WerrorS("not implemented");
625  return nlInit(0);
626#endif
627}
628
629#if 0
630CanonicalForm
631convSingGFFactoryGF( poly p )
632{
633  CanonicalForm result = 0;
634  int e, n = pVariables;
635
636  while ( p != NULL )
637  {
638    CanonicalForm term;
639    term = make_cf_from_gf( pGetCoeff( p ) );
640    for ( int i = 1; i <= n; i++ )
641    {
642      if ( (e = pGetExp( p, i )) != 0 )
643         term *= power( Variable( i ), e );
644    }
645    result += term;
646    p = pNext( p );
647  }
648  return result;
649}
650
651poly
652convFactoryGFSingGF ( const CanonicalForm & f )
653{
654//    cerr << " f = " << f << endl;
655  int n = pVariables+1;
656  /* ASSERT( level( f ) <= pVariables, "illegal number of variables" ); */
657  int * exp = (int*)omAlloc0(n*sizeof(int));
658  poly result = NULL;
659  convRecGFGF( f, exp, result );
660  omFreeSize((ADDRESS)exp,n*sizeof(int));
661  return result;
662}
663
664static void
665convRecGFGF ( const CanonicalForm & f, int * exp, poly & result )
666{
667  if (f.isZero())
668    return;
669  if ( ! f.inCoeffDomain() )
670  {
671    int l = f.level();
672    for ( CFIterator i = f; i.hasTerms(); i++ )
673    {
674      exp[l] = i.exp();
675      convRecGFGF( i.coeff(), exp, result );
676    }
677    exp[l] = 0;
678  }
679  else
680  {
681    poly term = pInit();
682    pNext( term ) = NULL;
683    for ( int i = 1; i <= pVariables; i++ )
684      pSetExp( term, i, exp[i]);
685    if (rRing_has_Comp(currRing))
686        p_SetComp(term, 0, currRing);
687    pGetCoeff( term ) = (number) gf_value (f);
688    pSetm( term );
689    result = pAdd( result, term );
690  }
691}
692#endif
693
694int convFactoryISingI( const CanonicalForm & f)
695{
696  if (!f.isImm()) WerrorS("int overflow in det");
697  return f.intval();
698}
699#endif
Note: See TracBrowser for help on using the repository browser.