source: git/Singular/clapconv.cc @ 356dc3d

fieker-DuValspielwiese
Last change on this file since 356dc3d was c8bd75, checked in by Hans Schönemann <hannes@…>, 26 years ago
*hannes: minor fixes git-svn-id: file:///usr/local/Singular/svn/trunk@1431 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.16 1998-04-23 09:52:07 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 = pInit();
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  On(SW_RATIONAL);
304  while ( p!=NULL)
305  {
306    CanonicalForm term=convSingAClapA(((lnumber)pGetCoeff(p))->z,a);
307    for ( int i = 1; i <= n; i++ )
308    {
309      if ( (e = pGetExp( p, i )) != 0 )
310        term *= power( Variable( i ), e );
311    }
312    result += term;
313    p = pNext( p );
314  }
315  return result;
316}
317
318poly convClapAPSingAP ( const CanonicalForm & f )
319{
320  int n = pVariables+1;
321  /* ASSERT( level( f ) <= pVariables, "illegal number of variables" ); */
322  int * exp = new int[n];
323  // for ( int i = 0; i < n; i++ ) exp[i] = 0;
324  memset(exp,0,n*sizeof(int));
325  poly result = NULL;
326  convRecAP( f, exp, result );
327  delete [] exp;
328  return result;
329}
330
331static void
332convRecAP ( const CanonicalForm & f, int * exp, poly & result )
333{
334  if ( ! f.inCoeffDomain() )
335  {
336    int l = f.level();
337    for ( CFIterator i = f; i.hasTerms(); i++ )
338    {
339      exp[l] = i.exp();
340      convRecAP( i.coeff(), exp, result );
341    }
342    exp[l] = 0;
343  }
344  else
345  {
346    alg z=(alg)convClapASingA( f );
347    if (z!=NULL)
348    {
349      poly term = pInit();
350      pNext( term ) = NULL;
351      for ( int i = 1; i <= pVariables; i++ )
352        pSetExp( term, i , exp[i]);
353      pSetComp(term, 0);
354      pGetCoeff(term)=(number)Alloc0(sizeof(rnumber));
355      ((lnumber)pGetCoeff(term))->z=z;
356      pSetm( term );
357      result = pAdd( result, term );
358    }
359  }
360}
361
362CanonicalForm convSingAClapA ( alg p , const Variable & a )
363{
364  CanonicalForm result = 0;
365  int e;
366
367  while ( p!=NULL )
368  {
369    CanonicalForm term;
370    /* does only work for finite fields */
371    if ( getCharacteristic() != 0 )
372    {
373      Off(SW_USE_EZGCD);
374      term = npInt( napGetCoeff( p ) );
375    }
376    else
377    {
378      On(SW_USE_EZGCD);
379      //if ( (!(int)(napGetCoeff( p )) & 1 )
380      //&&  ( napGetCoeff( p )->s == 0))
381      //  naNormalize( naGetCoeff( p ) );
382      if ( (int)(napGetCoeff( p )) & 1 )
383        term = nlInt( napGetCoeff( p ) );
384      else
385      {
386        if ( napGetCoeff( p )->s == 3 )
387        {
388          MP_INT dummy;
389          mpz_init_set( &dummy, &(napGetCoeff( p )->z) );
390          term = make_cf( dummy );
391        }
392        else  if ( napGetCoeff( p )->s == 1 )
393        {
394          MP_INT num, den;
395          On(SW_RATIONAL);
396          mpz_init_set( &num, &(napGetCoeff( p )->z) );
397          mpz_init_set( &den, &(napGetCoeff( p )->n) );
398          term = make_cf( num, den, false );
399        }
400        else
401        { // assume s == 0
402          MP_INT num, den;
403          On(SW_RATIONAL);
404          mpz_init_set( &num, &(napGetCoeff( p )->z) );
405          mpz_init_set( &den, &(napGetCoeff( p )->n) );
406          term = make_cf( num, den, true );
407        }
408      }
409    }
410    if ( (e = napGetExp( p, 1 )) != 0 )
411      term *= power( a , e );
412    result += term;
413    p = napNext( p );
414  }
415  return result;
416}
417
418static number convClapNSingAN( const CanonicalForm &f)
419{
420  if ((getCharacteristic() != 0) || ( f.isImm() ))
421    return nacInit( f.intval() );
422  else
423  {
424    number z=(number)Alloc(sizeof(rnumber));
425    #ifdef LDEBUG
426    z->debug=123456;
427    #endif
428    z->z = gmp_numerator( f );
429    if ( f.den() == 1 )
430    {
431      z->s = 3;
432    }
433    else
434    {
435      z->n = gmp_denominator( f );
436      z->s = 0;
437    }
438    #ifdef LDEBUG
439    nlDBTest(z,__FILE__,__LINE__);
440    #endif
441    return z;
442  }
443}
444
445alg convClapASingA ( const CanonicalForm & f )
446{
447  alg a=NULL;
448  alg t;
449  for( CFIterator i=f; i.hasTerms(); i++)
450  {
451    t=napNew();
452    // napNext( t ) = NULL; //already done by napNew
453    napGetCoeff(t)=convClapNSingAN( i.coeff() );
454    if (nacIsZero(napGetCoeff(t)))
455    {
456      napDelete(&t);
457    }
458    else
459    {
460      napGetExp(t,1)=i.exp();
461      a=napAdd(a,t);
462    }
463  }
464  return a;
465}
466
467CanonicalForm convSingTrPClapP ( poly p )
468{
469  CanonicalForm result = 0;
470  int e, n = pVariables;
471  int offs = rPar(currRing);
472
473  while ( p!=NULL )
474  {
475    nNormalize(pGetCoeff(p));
476    CanonicalForm term=convSingTrClapP(((lnumber)pGetCoeff(p))->z);
477    for ( int i = 1; i <= n; i++ )
478    {
479      if ( (e = pGetExp( p, i )) != 0 )
480        term = term * power( Variable( i + offs ), e );
481    }
482    result += term;
483    p = pNext( p );
484  }
485  return result;
486}
487
488poly convClapPSingTrP ( const CanonicalForm & f )
489{
490  int n = pVariables+1;
491  /* ASSERT( level( f ) <= pVariables, "illegal number of variables" ); */
492  int * exp = new int[n];
493  // for ( int i = 0; i < n; i++ ) exp[i] = 0;
494  memset(exp,0,n*sizeof(int));
495  poly result = NULL;
496  convRecTrP( f, exp, result , rPar(currRing) );
497  delete [] exp;
498  return result;
499}
500
501static void
502convRecTrP ( const CanonicalForm & f, int * exp, poly & result , int offs)
503{
504  if ( f.level() > offs )
505  {
506    int l = f.level();
507    for ( CFIterator i = f; i.hasTerms(); i++ )
508    {
509      exp[l-offs] = i.exp();
510      convRecTrP( i.coeff(), exp, result, offs );
511    }
512    exp[l-offs] = 0;
513  }
514  else
515  {
516    poly term = pInit();
517    pNext( term ) = NULL;
518    for ( int i = 1; i <= pVariables; i++ )
519      pSetExp( term, i ,exp[i]);
520    pSetComp(term, 0);
521    pGetCoeff(term)=(number)Alloc0(sizeof(rnumber));
522    ((lnumber)pGetCoeff(term))->z=convClapPSingTr( f );
523    pSetm( term );
524    result = pAdd( result, term );
525  }
526}
527
528#if 0
529CanonicalForm
530convSingGFClapGF( poly p )
531{
532  CanonicalForm result = 0;
533  int e, n = pVariables;
534
535  while ( p != NULL )
536  {
537    CanonicalForm term;
538    term = npInt( ???????pGetCoeff( p ) );
539    for ( int i = 1; i <= n; i++ )
540    {
541      if ( (e = pGetExp( p, i )) != 0 )
542         term *= power( Variable( i ), e );
543    }
544    result += term;
545    p = pNext( p );
546  }
547  return result;
548}
549
550poly
551convClapGFSingGF ( const CanonicalForm & f )
552{
553//    cerr << " f = " << f << endl;
554  int n = pVariables+1;
555  /* ASSERT( level( f ) <= pVariables, "illegal number of variables" ); */
556  int * exp = new int[n];
557  //for ( int i = 0; i < n; i++ ) exp[i] = 0;
558  memset(exp,0,n*sizeof(int));
559  poly result = NULL;
560  convRecGFGF( f, exp, result );
561  delete [] exp;
562  return result;
563}
564
565static void
566convRecGFGF ( const CanonicalForm & f, int * exp, poly & result )
567{
568  if ( ! f.inCoeffDomain() )
569  {
570    int l = f.level();
571    for ( CFIterator i = f; i.hasTerms(); i++ )
572    {
573      exp[l] = i.exp();
574      convRecGFGF( i.coeff(), exp, result );
575    }
576    exp[l] = 0;
577  }
578  else
579  {
580    poly term = pInit();
581    pNext( term ) = NULL;
582    for ( int i = 1; i <= pVariables; i++ )
583      pSetExp( term, i, exp[i]);
584    pSetComp(term, 0);
585    pGetCoeff( term ) = nInit( ?????f.intval() );
586    pSetm( term );
587    result = pAdd( result, term );
588  }
589}
590#endif
591
592int convClapISingI( const CanonicalForm & f)
593{
594  if (!f.isImm()) WerrorS("int overflow in det");
595  return f.intval();
596}
597#endif
Note: See TracBrowser for help on using the repository browser.