source: git/Singular/clapconv.cc @ 6e56de

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