source: git/Singular/clapconv.cc @ 856b11

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