source: git/kernel/clapconv.cc @ 7447d8

spielwiese
Last change on this file since 7447d8 was 7447d8, checked in by Hans Schönemann <hannes@…>, 19 years ago
*hannes: gcc 4 and 64bit git-svn-id: file:///usr/local/Singular/svn/trunk@8463 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 17.1 KB
Line 
1// emacs edit mode for this file is -*- C++ -*-
2/****************************************
3*  Computer Algebra System SINGULAR     *
4****************************************/
5// $Id: clapconv.cc,v 1.6 2005-07-27 15:48:28 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
24static void convRec( const CanonicalForm & f, int * exp, poly & result );
25
26static void convRecAlg( const CanonicalForm & f, int * exp, napoly & result );
27
28static void convRecPP ( const CanonicalForm & f, int * exp, poly & result );
29static void conv_RecPP ( const CanonicalForm & f, int * exp, poly & 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 convClapNSingAN( const CanonicalForm &f);
38
39CanonicalForm
40convSingNClapN( number n )
41{
42  CanonicalForm term;
43  /* does only work for Zp, Q */
44  if ( getCharacteristic() != 0 )
45  {
46    term = npInt( n );
47  }
48  else
49  {
50    if ( ((long)n) & 1L )
51    {
52      term = ((long)n) >>2;
53    }
54    else
55    {
56      if ( n->s == 3 )
57      {
58        MP_INT dummy;
59        mpz_init_set( &dummy, &(n->z) );
60        term = make_cf( dummy );
61      }
62      else  if ( n->s == 1 )
63      {
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, false );
69      }
70      else
71      { // assume s == 0
72        MP_INT num, den;
73        mpz_init_set( &num, &(n->z) );
74        mpz_init_set( &den, &(n->n) );
75        term = make_cf( num, den, true );
76      }
77    }
78  }
79  return term;
80}
81
82number
83convClapNSingN( const CanonicalForm & n)
84{
85  if (n.isImm())
86    return nInit(n.intval());
87  else
88  {
89    number z=(number)omAllocBin(rnumber_bin);
90#if defined(LDEBUG)
91    z->debug=123456;
92#endif
93    z->z = gmp_numerator( n );
94    if ( n.den() == 1 )
95      z->s = 3;
96    else
97    {
98      z->n = gmp_denominator( n );
99      z->s = 0;
100    }
101    return z;
102  }
103}
104
105CanonicalForm conv_SingPClapP( poly p, ring r )
106{
107  CanonicalForm result = 0;
108  int e, n = r->N;
109  assume( rPar(r)==0);
110
111  while ( p != NULL )
112  {
113    CanonicalForm term;
114    /* does only work for finite fields */
115    if ( getCharacteristic() != 0 )
116    {
117      term = npInt( pGetCoeff( p ) );
118    }
119    else
120    {
121      if ( (long)(pGetCoeff( p )) & 1 )
122      {
123        term = ((long)( pGetCoeff( p ) )>>2);
124      }
125      else
126      {
127        if ( pGetCoeff( p )->s == 3 )
128        {
129          MP_INT dummy;
130          mpz_init_set( &dummy, &(pGetCoeff( p )->z) );
131          term = make_cf( dummy );
132        }
133        else  if ( pGetCoeff( p )->s == 1 )
134        {
135          MP_INT num, den;
136          On(SW_RATIONAL);
137          mpz_init_set( &num, &(pGetCoeff( p )->z) );
138          mpz_init_set( &den, &(pGetCoeff( p )->n) );
139          term = make_cf( num, den, false );
140        }
141        else
142        { // assume s == 0
143          MP_INT num, den;
144          mpz_init_set( &num, &(pGetCoeff( p )->z) );
145          mpz_init_set( &den, &(pGetCoeff( p )->n) );
146          term = make_cf( num, den, true );
147        }
148      }
149    }
150    for ( int i = 1; i <= n; i++ )
151    {
152      if ( (e = p_GetExp( p, i, r )) != 0 )
153         term *= power( Variable( i ), e );
154    }
155    result += term;
156    p = pNext( p );
157  }
158  return result;
159}
160
161poly convClapPSingP ( const CanonicalForm & f )
162{
163//    cerr << " f = " << f << endl;
164  int n = pVariables+1;
165  /* ASSERT( level( f ) <= pVariables, "illegal number of variables" ); */
166  int * exp = new int[n];
167  //for ( int i = 0; i < n; i++ ) exp[i] = 0;
168  memset(exp,0,n*sizeof(int));
169  poly result = NULL;
170  convRecPP( f, exp, result );
171  delete [] exp;
172  return result;
173}
174
175static void convRecPP ( const CanonicalForm & f, int * exp, poly & result )
176{
177  if (f == 0)
178    return;
179  if ( ! f.inCoeffDomain() )
180  {
181    int l = f.level();
182    for ( CFIterator i = f; i.hasTerms(); i++ )
183    {
184      exp[l] = i.exp();
185      convRecPP( i.coeff(), exp, result );
186    }
187    exp[l] = 0;
188  }
189  else
190  {
191    poly term = pInit();
192    pNext( term ) = NULL;
193    for ( int i = 1; i <= pVariables; i++ )
194      pSetExp( term, i, exp[i]);
195    pSetComp(term, 0);
196    if ( getCharacteristic() != 0 )
197    {
198      pGetCoeff( term ) = nInit( f.intval() );
199    }
200    else
201    {
202      if ( f.isImm() )
203        pGetCoeff( term ) = nInit( f.intval() );
204      else
205      {
206        number z=(number)omAllocBin(rnumber_bin);
207#if defined(LDEBUG)
208        z->debug=123456;
209#endif
210        z->z = gmp_numerator( f );
211        if ( f.den() == 1 )
212          z->s = 3;
213        else
214        {
215          z->n = gmp_denominator( f );
216          z->s = 0;
217        }
218        pGetCoeff( term ) = z;
219      }
220    }
221    pSetm( term );
222    if ( nIsZero(pGetCoeff(term)) )
223    {
224      pDelete(&term);
225    }
226    else
227    {
228      result = pAdd( result, term );
229    }
230  }
231}
232
233poly conv_ClapPSingP ( const CanonicalForm & f, ring r )
234{
235//    cerr << " f = " << f << endl;
236  int n = r->N+1;
237  /* ASSERT( level( f ) <= pVariables, "illegal number of variables" ); */
238  int * exp = new int[n];
239  //for ( int i = 0; i < n; i++ ) exp[i] = 0;
240  memset(exp,0,n*sizeof(int));
241  poly result = NULL;
242  conv_RecPP( f, exp, result, r );
243  delete [] exp;
244  return result;
245}
246
247static void
248conv_RecPP ( const CanonicalForm & f, int * exp, poly & result, ring r )
249{
250  if (f == 0)
251    return;
252  if ( ! f.inCoeffDomain() )
253  {
254    int l = f.level();
255    for ( CFIterator i = f; i.hasTerms(); i++ )
256    {
257      exp[l] = i.exp();
258      conv_RecPP( i.coeff(), exp, result, r );
259    }
260    exp[l] = 0;
261  }
262  else
263  {
264    poly term = p_Init(r);
265    pNext( term ) = NULL;
266    for ( int i = 1; i <= r->N; i++ )
267      p_SetExp( term, i, exp[i], r);
268    if (rRing_has_Comp(r)) p_SetComp(term, 0, r);
269    if ( getCharacteristic() != 0 )
270    {
271      pGetCoeff( term ) = n_Init( f.intval(), r );
272    }
273    else
274    {
275      if ( f.isImm() )
276        pGetCoeff( term ) = n_Init( f.intval(), r );
277      else
278      {
279        number z=(number)omAllocBin(rnumber_bin);
280#if defined(LDEBUG)
281        z->debug=123456;
282#endif
283        z->z = gmp_numerator( f );
284        if ( f.den() == 1 )
285          z->s = 3;
286        else
287        {
288          z->n = gmp_denominator( f );
289          z->s = 0;
290        }
291        pGetCoeff( term ) = z;
292      }
293    }
294    p_Setm( term, r );
295    if ( n_IsZero(pGetCoeff(term), r) )
296    {
297      p_Delete(&term,r);
298    }
299    else
300    {
301      result = p_Add_q( result, term, r );
302    }
303  }
304}
305
306
307CanonicalForm
308convSingTrClapP( napoly p )
309{
310  CanonicalForm result = 0;
311  int e, n = napVariables;
312
313  while ( p!=NULL)
314  {
315    CanonicalForm term;
316    /* does only work for finite fields */
317    if ( getCharacteristic() != 0 )
318    {
319      term = npInt( napGetCoeff( p ) );
320    }
321    else
322    {
323      //if ( (!(int)(napGetCoeff( p )) & 1 )
324      //&&  ( napGetCoeff( p )->s == 0))
325      //  naNormalize( naGetCoeff( p ) );
326      if ( (long)(napGetCoeff( p )) & 1L )
327        term = nlInt( napGetCoeff( p ) );
328      else
329      {
330        if ( napGetCoeff( p )->s == 3 )
331        {
332          MP_INT dummy;
333          mpz_init_set( &dummy, &(napGetCoeff( p )->z) );
334          term = make_cf( dummy );
335        }
336        else  if ( napGetCoeff( p )->s == 1 )
337        {
338          MP_INT num, den;
339          On(SW_RATIONAL);
340          mpz_init_set( &num, &(napGetCoeff( p )->z) );
341          mpz_init_set( &den, &(napGetCoeff( p )->n) );
342          term = make_cf( num, den, false );
343        }
344        else
345        { // assume s == 0
346          MP_INT num, den;
347          On(SW_RATIONAL);
348          mpz_init_set( &num, &(napGetCoeff( p )->z) );
349          mpz_init_set( &den, &(napGetCoeff( p )->n) );
350          term = make_cf( num, den, true );
351        }
352      }
353    }
354    for ( int i = 1; i <= n; i++ )
355    {
356      if ( (e = napGetExp( p, i )) != 0 )
357        term *= power( Variable( i ), e );
358    }
359    result += term;
360    p = napNext( p );
361  }
362  return result;
363}
364
365napoly
366convClapPSingTr ( const CanonicalForm & f )
367{
368//    cerr << " f = " << f << endl;
369  int n = napVariables+1;
370  /* ASSERT( level( f ) <= napVariables, "illegal number of variables" ); */
371  int * exp = new int[n];
372  // for ( int i = 0; i < n; i++ ) exp[i] = 0;
373  memset(exp,0,n*sizeof(int));
374  napoly result = NULL;
375  convRecPTr( f, exp, result );
376  delete [] exp;
377  return result;
378}
379
380static void
381convRecPTr ( const CanonicalForm & f, int * exp, napoly & result )
382{
383  if (f == 0)
384    return;
385  if ( ! f.inCoeffDomain() )
386  {
387    int l = f.level();
388    for ( CFIterator i = f; i.hasTerms(); i++ )
389    {
390        exp[l] = i.exp();
391        convRecPTr( i.coeff(), exp, result );
392    }
393    exp[l] = 0;
394  }
395  else
396  {
397    napoly term = napNew();
398    // napNext( term ) = NULL; //already done by napNew
399    for ( int i = 1; i <= napVariables; i++ )
400      napSetExp( term, i , exp[i]);
401    if ( getCharacteristic() != 0 )
402    {
403      napGetCoeff( term ) = npInit( f.intval() );
404    }
405    else
406    {
407      if ( f.isImm() )
408        napGetCoeff( term ) = nlInit( f.intval() );
409      else
410      {
411        number z=(number)omAllocBin(rnumber_bin);
412#if defined(LDEBUG)
413        z->debug=123456;
414#endif
415        z->z = gmp_numerator( f );
416        if ( f.den() == 1 )
417        {
418          z->s = 3;
419        }
420        else
421        {
422          z->n = gmp_denominator( f );
423          if (mpz_cmp_si(&z->n,(long)1)==0)
424          {
425            mpz_clear(&z->n);
426            z->s=3;
427          }
428          else
429          {
430            z->s = 0;
431          }
432          nacNormalize(z);
433        }
434        napGetCoeff( term ) = z;
435      }
436    }
437    result = napAdd( result, term );
438  }
439}
440
441CanonicalForm convSingAPClapAP ( poly p , const Variable & a)
442{
443  CanonicalForm result = 0;
444  int e, n = pVariables;
445  int off=rPar(currRing);
446
447  On(SW_RATIONAL);
448  while ( p!=NULL)
449  {
450    CanonicalForm term=convSingAClapA(((lnumber)pGetCoeff(p))->z,a);
451    for ( int i = 1; i <= n; i++ )
452    {
453      if ( (e = pGetExp( p, i )) != 0 )
454        term *= power( Variable( i + off), e );
455    }
456    result += term;
457    p = pNext( p );
458  }
459  return result;
460}
461
462static void
463convRecAP_R ( const CanonicalForm & f, int * exp, poly & result, int par_start, int var_start) ;
464
465poly convClapAPSingAP_R ( const CanonicalForm & f, int par_start, int var_start )
466{
467  int n = pVariables+1+rPar(currRing);
468  int * exp = new int[n];
469  // for ( int i = 0; i < n; i++ ) exp[i] = 0;
470  memset(exp,0,n*sizeof(int));
471  poly result = NULL;
472  convRecAP_R( f, exp, result,par_start, var_start );
473  delete [] exp;
474  return result;
475}
476poly convClapAPSingAP ( const CanonicalForm & f )
477{
478  return convClapAPSingAP_R(f,0,rPar(currRing));
479}
480
481static void
482convRecAP_R ( const CanonicalForm & f, int * exp, poly & result, int par_start, int var_start )
483{
484  if (f == 0)
485    return;
486  if ( ! f.inCoeffDomain() )
487  {
488    int l = f.level();
489    for ( CFIterator i = f; i.hasTerms(); i++ )
490    {
491      exp[l] = i.exp();
492      convRecAP_R( i.coeff(), exp, result, par_start, var_start );
493    }
494    exp[l] = 0;
495  }
496  else
497  {
498    napoly z=(napoly)convClapASingA( f );
499    if (z!=NULL)
500    {
501      poly term = pInit();
502      pNext( term ) = NULL;
503      int i;
504      for ( i = 1; i <= pVariables; i++ )
505        pSetExp( term, i , exp[i+var_start]);
506      pSetComp(term, 0);
507      if (par_start==0)
508      {
509        for ( i = 1; i <= var_start; i++ )
510        //z->e[i-1]+=exp[i];
511          napAddExp(z,i,exp[i]);
512      }
513      else
514      {
515        for ( i = par_start+1; i <= var_start+rPar(currRing); i++ )
516        //z->e[i-1]+=exp[i];
517          napAddExp(z,i,exp[i-par_start]);
518      }
519      pGetCoeff(term)=(number)omAlloc0Bin(rnumber_bin);
520      ((lnumber)pGetCoeff(term))->z=z;
521      pSetm( term );
522      result = pAdd( result, term );
523    }
524  }
525}
526
527CanonicalForm convSingAClapA ( napoly p , const Variable & a )
528{
529  CanonicalForm result = 0;
530  int e;
531
532  while ( p!=NULL )
533  {
534    CanonicalForm term;
535    /* does only work for finite fields:Z/p */
536    if ( rField_is_Zp_a() )
537    {
538      term = npInt( napGetCoeff( p ) );
539    }
540    else
541    {
542      //if ( (!(int)(napGetCoeff( p )) & 1 )
543      //&&  ( napGetCoeff( p )->s == 0))
544      //  naNormalize( naGetCoeff( p ) );
545      if ( (long)(napGetCoeff( p )) & SR_INT )
546        term = nlInt( napGetCoeff( p ) );
547        //term = SR_TO_INT(napGetCoeff( p )) ;
548      else
549      {
550        if ( napGetCoeff( p )->s == 3 )
551        {
552          MP_INT dummy;
553          mpz_init_set( &dummy, &(napGetCoeff( p )->z) );
554          term = make_cf( dummy );
555        }
556        else  if ( napGetCoeff( p )->s == 1 )
557        {
558          MP_INT num, den;
559          On(SW_RATIONAL);
560          mpz_init_set( &num, &(napGetCoeff( p )->z) );
561          mpz_init_set( &den, &(napGetCoeff( p )->n) );
562          term = make_cf( num, den, false );
563        }
564        else
565        { // assume s == 0
566          MP_INT num, den;
567          On(SW_RATIONAL);
568          mpz_init_set( &num, &(napGetCoeff( p )->z) );
569          mpz_init_set( &den, &(napGetCoeff( p )->n) );
570          term = make_cf( num, den, true );
571        }
572      }
573    }
574    if ( (e = napGetExp( p, 1 )) != 0 )
575      term *= power( a , e );
576    result += term;
577    p = napNext( p );
578  }
579  return result;
580}
581
582static number convClapNSingAN( const CanonicalForm &f)
583{
584  if ((getCharacteristic() != 0) || ( f.isImm() ))
585    return nacInit( f.intval() );
586  else
587  {
588    number z=(number)omAllocBin(rnumber_bin);
589#if defined(LDEBUG)
590    z->debug=123456;
591#endif
592    z->z = gmp_numerator( f );
593    if ( f.den() == 1 )
594    {
595      z->s = 3;
596    }
597    else
598    {
599      z->n = gmp_denominator( f );
600      z->s = 0;
601    }
602    #ifdef LDEBUG
603    nlDBTest(z,__FILE__,__LINE__);
604    #endif
605    return z;
606  }
607}
608
609napoly convClapASingA ( const CanonicalForm & f )
610{
611  napoly a=NULL;
612  napoly t;
613  for( CFIterator i=f; i.hasTerms(); i++)
614  {
615    t=napNew();
616    // napNext( t ) = NULL; //already done by napNew
617    napGetCoeff(t)=convClapNSingAN( i.coeff() );
618    if (nacIsZero(napGetCoeff(t)))
619    {
620      napDelete(&t);
621    }
622    else
623    {
624      napSetExp(t,1,i.exp());
625      a=napAdd(a,t);
626    }
627  }
628  if (a!=NULL)
629  {
630    if (naMinimalPoly!=NULL)
631    {
632      if (napGetExp(a,1) >= napGetExp(naMinimalPoly,1))
633        a = napRemainder( a, naMinimalPoly);
634    }
635  }
636  return a;
637}
638
639CanonicalForm convSingTrPClapP ( poly p )
640{
641  CanonicalForm result = 0;
642  int e, n = pVariables;
643  int offs = rPar(currRing);
644
645  while ( p!=NULL )
646  {
647    nNormalize(pGetCoeff(p));
648    CanonicalForm term=convSingTrClapP(((lnumber)pGetCoeff(p))->z);
649
650    if ((((lnumber)pGetCoeff(p))->n!=NULL)
651    && (!errorreported))
652    {
653      WerrorS("conversion error: denominator!= 1");
654    }
655
656    for ( int i = 1; i <= n; i++ )
657    {
658      if ( (e = pGetExp( p, i )) != 0 )
659        term = term * power( Variable( i + offs ), e );
660    }
661    result += term;
662    p = pNext( p );
663  }
664  return result;
665}
666
667poly convClapPSingTrP ( const CanonicalForm & f )
668{
669  int n = pVariables+1;
670  int * exp = new int[n];
671  // for ( int i = 0; i < n; i++ ) exp[i] = 0;
672  memset(exp,0,n*sizeof(int));
673  poly result = NULL;
674  convRecTrP( f, exp, result , rPar(currRing) );
675  delete [] exp;
676  return result;
677}
678
679static void
680convRecTrP ( const CanonicalForm & f, int * exp, poly & result , int offs)
681{
682  if (f == 0)
683    return;
684  if ( f.level() > offs )
685  {
686    int l = f.level();
687    for ( CFIterator i = f; i.hasTerms(); i++ )
688    {
689      exp[l-offs] = i.exp();
690      convRecTrP( i.coeff(), exp, result, offs );
691    }
692    exp[l-offs] = 0;
693  }
694  else
695  {
696    poly term = pInit();
697    pNext( term ) = NULL;
698    for ( int i = 1; i <= pVariables; i++ )
699      pSetExp( term, i ,exp[i]);
700    pSetComp(term, 0);
701    pGetCoeff(term)=(number)omAlloc0Bin(rnumber_bin);
702    ((lnumber)pGetCoeff(term))->z=convClapPSingTr( f );
703    pSetm( term );
704    result = pAdd( result, term );
705  }
706}
707
708#if 0
709CanonicalForm
710convSingGFClapGF( poly p )
711{
712  CanonicalForm result = 0;
713  int e, n = pVariables;
714
715  while ( p != NULL )
716  {
717    CanonicalForm term;
718    term = make_cf_from_gf( pGetCoeff( p ) );
719    for ( int i = 1; i <= n; i++ )
720    {
721      if ( (e = pGetExp( p, i )) != 0 )
722         term *= power( Variable( i ), e );
723    }
724    result += term;
725    p = pNext( p );
726  }
727  return result;
728}
729
730poly
731convClapGFSingGF ( const CanonicalForm & f )
732{
733//    cerr << " f = " << f << endl;
734  int n = pVariables+1;
735  /* ASSERT( level( f ) <= pVariables, "illegal number of variables" ); */
736  int * exp = new int[n];
737  //for ( int i = 0; i < n; i++ ) exp[i] = 0;
738  memset(exp,0,n*sizeof(int));
739  poly result = NULL;
740  convRecGFGF( f, exp, result );
741  delete [] exp;
742  return result;
743}
744
745static void
746convRecGFGF ( const CanonicalForm & f, int * exp, poly & result )
747{
748  if (f == 0)
749    return;
750  if ( ! f.inCoeffDomain() )
751  {
752    int l = f.level();
753    for ( CFIterator i = f; i.hasTerms(); i++ )
754    {
755      exp[l] = i.exp();
756      convRecGFGF( i.coeff(), exp, result );
757    }
758    exp[l] = 0;
759  }
760  else
761  {
762    poly term = pInit();
763    pNext( term ) = NULL;
764    for ( int i = 1; i <= pVariables; i++ )
765      pSetExp( term, i, exp[i]);
766    pSetComp(term, 0);
767    pGetCoeff( term ) = (number) gf_value (f);
768    pSetm( term );
769    result = pAdd( result, term );
770  }
771}
772#endif
773
774int convClapISingI( const CanonicalForm & f)
775{
776  if (!f.isImm()) WerrorS("int overflow in det");
777  return f.intval();
778}
779#endif
Note: See TracBrowser for help on using the repository browser.