source: git/Singular/clapconv.cc @ 97a7b44

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