source: git/Singular/clapconv.cc @ 51c3da4

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