source: git/Singular/clapconv.cc @ a3bc95e

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