source: git/Singular/clapconv.cc @ e2f1c7

spielwiese
Last change on this file since e2f1c7 was e2f1c7, checked in by Hans Schönemann <hannes@…>, 27 years ago
* hannes: changed Log: to Id:, added Singular header git-svn-id: file:///usr/local/Singular/svn/trunk@144 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 11.9 KB
Line 
1// emacs edit mode for this file is -*- C++ -*-
2/****************************************
3*  Computer Algebra System SINGULAR     *
4****************************************/
5// $Id: clapconv.cc,v 1.3 1997-04-08 16:49:16 Singular Exp $
6/*
7* ABSTRACT: convert data between Singular and factory
8*/
9
10
11#include "mod2.h"
12#ifdef HAVE_LIBFACTORY
13#include "tok.h"
14#define SI_DONT_HAVE_GLOBAL_VARS
15#include "clapconv.h"
16#include "numbers.h"
17#include "polys.h"
18#include "modulop.h"
19#include "mmemory.h"
20#include "febase.h"
21#include "ipid.h"
22#include "ring.h"
23
24static void convRec( const CanonicalForm & f, int * exp, poly & result );
25
26static void convRecAlg( const CanonicalForm & f, int * exp, alg & result );
27
28static void convRecPP ( const CanonicalForm & f, int * exp, poly & result );
29
30static void convRecPTr ( const CanonicalForm & f, int * exp, alg & result );
31
32static void convRecAP ( const CanonicalForm & f, int * exp, poly & result );
33
34static void convRecTrP ( const CanonicalForm & f, int * exp, poly & result, int offs );
35
36static number convClapNSingAN( const CanonicalForm &f);
37
38CanonicalForm
39convSingPClapP( poly p )
40{
41  CanonicalForm result = 0;
42  int e, n = pVariables;
43
44  while ( p != NULL )
45  {
46    CanonicalForm term;
47    /* does only work for finite fields */
48    if ( getCharacteristic() != 0 )
49    {
50      Off(SW_USE_EZGCD);
51      term = npInt( pGetCoeff( p ) );
52    }
53    else
54    {
55      if ( (int)(pGetCoeff( p )) & 1 )
56      {
57        term = ((int)( pGetCoeff( p ) )>>2);
58      }
59      else
60      {
61        if ( pGetCoeff( p )->s == 3 )
62        {
63          MP_INT dummy;
64          mpz_init_set( &dummy, &(pGetCoeff( p )->z) );
65          term = make_cf( dummy );
66        }
67        else  if ( pGetCoeff( p )->s == 1 )
68        {
69          MP_INT num, den;
70          On(SW_RATIONAL);
71          mpz_init_set( &num, &(pGetCoeff( p )->z) );
72          mpz_init_set( &den, &(pGetCoeff( p )->n) );
73          term = make_cf( num, den, false );
74        }
75        else
76        { // assume s == 0
77          MP_INT num, den;
78          mpz_init_set( &num, &(pGetCoeff( p )->z) );
79          mpz_init_set( &den, &(pGetCoeff( p )->n) );
80          term = make_cf( num, den, true );
81        }
82      }
83    }
84    for ( int i = 1; i <= n; i++ )
85    {
86      if ( (e = pGetExp( p, i )) != 0 )
87         term = term * power( Variable( i ), e );
88    }
89    result = result + term;
90    p = pNext( p );
91  }
92  return result;
93}
94
95poly
96convClapPSingP ( const CanonicalForm & f )
97{
98//    cerr << " f = " << f << endl;
99  int n = pVariables+1;
100  /* ASSERT( level( f ) <= pVariables, "illegal number of variables" ); */
101  int * exp = new int[n];
102  for ( int i = 0; i < n; i++ )
103    exp[i] = 0;
104  poly result = NULL;
105  convRecPP( f, exp, result );
106  delete [] exp;
107  return result;
108}
109
110static void
111convRecPP ( const CanonicalForm & f, int * exp, poly & result )
112{
113  if ( ! f.inCoeffDomain() )
114  {
115    int l = f.level();
116    for ( CFIterator i = f; i.hasTerms(); i++ )
117    {
118      exp[l] = i.exp();
119      convRecPP( i.coeff(), exp, result );
120    }
121    exp[l] = 0;
122  }
123  else
124  {
125    poly term = pNew();
126    pNext( term ) = 0;
127    for ( int i = 0; i <= pVariables; i++ )
128      pGetExp( term, i ) = exp[i];
129    if ( getCharacteristic() != 0 )
130    {
131      pGetCoeff( term ) = nInit( f.intval() );
132    }
133    else
134    {
135      if ( f.isImm() )
136        pGetCoeff( term ) = nInit( f.intval() );
137      else
138      {
139        number z=(number)Alloc(sizeof(rnumber));
140        z->z = gmp_numerator( f );
141        if ( f.den() == 1 )
142          z->s = 3;
143        else
144        {
145          z->n = gmp_denominator( f );
146          z->s = 0;
147        }
148        pGetCoeff( term ) = z;
149      }
150    }
151    pSetm( term );
152    result = pAdd( result, term );
153  }
154}
155
156
157CanonicalForm
158convSingTrClapP( alg p )
159{
160  CanonicalForm result = 0;
161  int e, n = napVariables;
162
163  while ( p!=NULL)
164  {
165    CanonicalForm term;
166    /* does only work for finite fields */
167    if ( getCharacteristic() != 0 )
168    {
169      Off(SW_USE_EZGCD);
170      term = npInt( napGetCoeff( p ) );
171    }
172    else
173    {
174      On(SW_USE_EZGCD);
175      //if ( (!(int)(napGetCoeff( p )) & 1 )
176      //&&  ( napGetCoeff( p )->s == 0))
177      //  napNormalize( napGetCoeff( p ) );
178      if ( (int)(napGetCoeff( p )) & 1 )
179        term = nlInt( napGetCoeff( p ) );
180      else
181      {
182        if ( napGetCoeff( p )->s == 3 )
183        {
184          MP_INT dummy;
185          mpz_init_set( &dummy, &(napGetCoeff( p )->z) );
186          term = make_cf( dummy );
187        }
188        else  if ( napGetCoeff( p )->s == 1 )
189        {
190          MP_INT num, den;
191          On(SW_RATIONAL);
192          Print("switch to rational\n");
193          mpz_init_set( &num, &(napGetCoeff( p )->z) );
194          mpz_init_set( &den, &(napGetCoeff( p )->n) );
195          term = make_cf( num, den, false );
196        }
197        else
198        { // assume s == 0
199          MP_INT num, den;
200          On(SW_RATIONAL);
201          Print("switch to rational ?1\n");
202          mpz_init_set( &num, &(napGetCoeff( p )->z) );
203          mpz_init_set( &den, &(napGetCoeff( p )->n) );
204          term = make_cf( num, den, true );
205        }
206      }
207    }
208    for ( int i = 1; i <= n; i++ )
209    {
210      if ( (e = napGetExp( p, i )) != 0 )
211        term = term * power( Variable( i ), e );
212    }
213    result = result + term;
214    p = napNext( p );
215  }
216  return result;
217}
218
219alg
220convClapPSingTr ( const CanonicalForm & f )
221{
222//    cerr << " f = " << f << endl;
223  int n = napVariables+1;
224  /* ASSERT( level( f ) <= napVariables, "illegal number of variables" ); */
225  int * exp = new int[n];
226  for ( int i = 0; i < n; i++ )
227    exp[i] = 0;
228  alg result = NULL;
229  convRecPTr( f, exp, result );
230  delete [] exp;
231  return result;
232}
233
234static void
235convRecPTr ( const CanonicalForm & f, int * exp, alg & result )
236{
237  if ( ! f.inCoeffDomain() )
238  {
239    int l = f.level();
240    for ( CFIterator i = f; i.hasTerms(); i++ )
241    {
242        exp[l] = i.exp();
243        convRecPTr( i.coeff(), exp, result );
244    }
245    exp[l] = 0;
246  }
247  else
248  {
249    alg term = napNew();
250    napNext( term ) = 0;
251    for ( int i = 1; i <= napVariables; i++ )
252      napGetExp( term, i ) = exp[i];
253    if ( getCharacteristic() != 0 )
254    {
255      napGetCoeff( term ) = npInit( f.intval() );
256    }
257    else
258    {
259      if ( f.isImm() )
260        napGetCoeff( term ) = nlInit( f.intval() );
261      else
262      {
263        number z=(number)Alloc(sizeof(rnumber));
264        z->z = gmp_numerator( f );
265        if ( f.den() == 1 )
266          z->s = 3;
267        else
268        {
269          z->n = gmp_denominator( f );
270          if (mpz_cmp_si(&z->n,(long)1)==0)
271          {
272            mpz_clear(&z->n);
273            z->s=3;
274            if (mpz_size1(&z->z)<=MP_SMALL)
275            {
276              int ui=(int)mpz_get_si(&z->z);
277              if ((((ui<<3)>>3)==ui)
278              && (mpz_cmp_si(&z->z,(long)ui)==0))
279              {
280                mpz_clear(&z->z);
281                Free((ADDRESS)z,sizeof(rnumber));
282                z=INT_TO_SR(ui);
283              }
284            }
285          }
286          else
287          {
288            z->s = 0;
289          }
290          nacNormalize(z);
291        }
292        napGetCoeff( term ) = z;
293      }
294    }
295    result = napAdd( result, term );
296  }
297}
298
299CanonicalForm convSingAPClapAP ( poly p , const Variable & a)
300{
301  CanonicalForm result = 0;
302  int e, n = pVariables;
303
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 = term * power( Variable( i ), e );
311    }
312    result = 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++ )
324    exp[i] = 0;
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 = pNew();
350      pNext( term ) = 0;
351      for ( int i = 0; i <= pVariables; i++ )
352        pGetExp( term, i ) = exp[i];
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      //  napNormalize( napGetCoeff( 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          Print("switch to rational\n");
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          Print("switch to rational ?2\n");
405          mpz_init_set( &num, &(napGetCoeff( p )->z) );
406          mpz_init_set( &den, &(napGetCoeff( p )->n) );
407          term = make_cf( num, den, true );
408        }
409      }
410    }
411    if ( (e = napGetExp( p, 1 )) != 0 )
412      term = term * power( a , e );
413    result = result + term;
414    p = napNext( p );
415  }
416  return result;
417}
418
419static number convClapNSingAN( const CanonicalForm &f)
420{
421  if ((getCharacteristic() != 0) || ( f.isImm() ))
422    return nacInit( f.intval() );
423  else
424  {
425    number z=(number)Alloc(sizeof(rnumber));
426    z->z = gmp_numerator( f );
427    if ( f.den() == 1 )
428      z->s = 3;
429    else
430    {
431      z->n = gmp_denominator( f );
432      z->s = 0;
433    }
434    return z;
435  }
436}
437
438alg convClapASingA ( const CanonicalForm & f )
439{
440  alg a=NULL;
441  alg t;
442  for( CFIterator i=f; i.hasTerms(); i++)
443  {
444    t=napNew();
445    napNext( t ) = NULL;
446    napGetCoeff(t)=convClapNSingAN( i.coeff() );
447    if (nacIsZero(napGetCoeff(t)))
448    {
449      napDelete(&t);
450    }
451    else
452    {
453      napGetExp(t,1)=i.exp();
454      a=napAdd(a,t);
455    }
456  }
457  return a;
458}
459
460CanonicalForm convSingTrPClapP ( poly p )
461{
462  CanonicalForm result = 0;
463  int e, n = pVariables;
464  int offs = rPar(currRing);
465
466  while ( p!=NULL )
467  {
468    CanonicalForm term=convSingTrClapP(((lnumber)pGetCoeff(p))->z);
469    for ( int i = 1; i <= n; i++ )
470    {
471      if ( (e = pGetExp( p, i )) != 0 )
472        term = term * power( Variable( i + offs ), e );
473    }
474    result = result + term;
475    p = pNext( p );
476  }
477  return result;
478}
479
480poly convClapPSingTrP ( const CanonicalForm & f )
481{
482  int n = pVariables+1;
483  /* ASSERT( level( f ) <= pVariables, "illegal number of variables" ); */
484  int * exp = new int[n];
485  for ( int i = 0; i < n; i++ )
486    exp[i] = 0;
487  poly result = NULL;
488  convRecTrP( f, exp, result , rPar(currRing) );
489  delete [] exp;
490  return result;
491}
492
493static void
494convRecTrP ( const CanonicalForm & f, int * exp, poly & result , int offs)
495{
496  if ( f.level() > offs )
497  {
498    int l = f.level();
499    for ( CFIterator i = f; i.hasTerms(); i++ )
500    {
501      exp[l-offs] = i.exp();
502      convRecTrP( i.coeff(), exp, result, offs );
503    }
504    exp[l-offs] = 0;
505  }
506  else
507  {
508    poly term = pNew();
509    pNext( term ) = NULL;
510    for ( int i = 0; i <= pVariables; i++ )
511      pGetExp( term, i ) = exp[i];
512    pGetCoeff(term)=(number)Alloc0(sizeof(rnumber));
513    ((lnumber)pGetCoeff(term))->z=convClapPSingTr( f );
514    pSetm( term );
515    result = pAdd( result, term );
516  }
517}
518
519#endif
Note: See TracBrowser for help on using the repository browser.