source: git/kernel/clapconv.cc @ 8c5988

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