source: git/Singular/clapconv.cc @ 3a3392c

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