source: git/libpolys/polys/clapconv.cc @ bd795d

spielwiese
Last change on this file since bd795d was fea2af, checked in by Oleksandr Motsak <motsak@…>, 12 years ago
SW cannot be built without factory FIX: fixed libpolys and Singular/eigenval_ip.cc (quick and dirty) TODO: make check in libpolys/tests -> all dynamic tests are missing SearchPATH :( TODO: undefined reference to strat_fac_debug & strat_nr? TODO: kernel? Singular?
  • Property mode set to 100644
File size: 10.4 KB
Line 
1// emacs edit mode for this file is -*- C++ -*-
2/****************************************
3*  Computer Algebra System SINGULAR     *
4****************************************/
5// $Id$
6/*
7* ABSTRACT: convert data between Singular and factory
8*/
9
10
11#include "config.h"
12#include <misc/auxiliary.h>
13
14#ifdef HAVE_FACTORY
15#define SI_DONT_HAVE_GLOBAL_VARS
16#include <factory/factory.h>
17
18#include <omalloc/omalloc.h>
19#include <coeffs/coeffs.h>
20#include <coeffs/longrat.h>
21#include <coeffs/modulop.h>
22#include <polys/monomials/p_polys.h>
23#include <polys/sbuckets.h>
24#include <polys/clapconv.h>
25
26#include "simpleideals.h"
27
28#define TRANSEXT_PRIVATES
29#include <polys/ext_fields/transext.h>
30
31void out_cf(char *s1,const CanonicalForm &f,char *s2);
32
33static void conv_RecPP ( const CanonicalForm & f, int * exp, sBucket_pt result, ring r );
34
35static void convRecTrP ( const CanonicalForm & f, int * exp, poly & result, int offs, const ring r );
36
37static void convRecGFGF ( const CanonicalForm & f, int * exp, poly & result );
38
39static number convFactoryNSingAN( const CanonicalForm &f, const ring r);
40
41poly convFactoryPSingP ( const CanonicalForm & f, const ring r )
42{
43  int n = rVar(r)+1;
44  /* ASSERT( level( f ) <= pVariables, "illegal number of variables" ); */
45  int * exp = (int*)omAlloc0(n*sizeof(int));
46  sBucket_pt result_bucket=sBucketCreate(r);
47  conv_RecPP( f, exp, result_bucket, r );
48  poly result; int dummy;
49  sBucketDestroyMerge(result_bucket,&result,&dummy);
50  omFreeSize((ADDRESS)exp,n*sizeof(int));
51  return result;
52}
53
54static void conv_RecPP ( const CanonicalForm & f, int * exp, sBucket_pt result, ring r )
55{
56  if (f.isZero())
57    return;
58  if ( ! f.inCoeffDomain() )
59  {
60    int l = f.level();
61    for ( CFIterator i = f; i.hasTerms(); i++ )
62    {
63      exp[l] = i.exp();
64      conv_RecPP( i.coeff(), exp, result, r );
65    }
66    exp[l] = 0;
67  }
68  else
69  {
70    poly term = p_Init(r);
71    pNext( term ) = NULL;
72    for ( int i = 1; i <= r->N; i++ )
73      p_SetExp( term, i, exp[i], r);
74    pGetCoeff( term )=r->cf->convFactoryNSingN(f, r->cf);
75    p_Setm( term, r );
76    if ( n_IsZero(pGetCoeff(term), r->cf) )
77    {
78      p_Delete(&term,r);
79    }
80    else
81    {
82      sBucket_Merge_p(result,term,1);
83    }
84  }
85}
86
87
88CanonicalForm convSingPFactoryP( poly p, const ring r )
89{
90  CanonicalForm result = 0;
91  int e, n = rVar(r);
92  BOOLEAN setChar=TRUE;
93
94  while ( p!=NULL )
95  {
96    CanonicalForm term;
97    term=r->cf->convSingNFactoryN(pGetCoeff( p ),setChar, r->cf);
98    if (errorreported) break;
99    setChar=FALSE;
100    for ( int i = n; i >0; i-- )
101    {
102      if ( (e = p_GetExp( p, i, r)) != 0 )
103        term *= power( Variable( i ), e );
104    }
105    result += term;
106    pIter( p );
107  }
108  return result;
109}
110
111int convFactoryISingI( const CanonicalForm & f)
112{
113  if (!f.isImm()) WerrorS("int overflow in det");
114  return f.intval();
115}
116
117CanonicalForm convSingAPFactoryAP ( poly p , const Variable & a, const ring r)
118{
119  CanonicalForm result = 0;
120  int e, n = r-> N;
121  int off=rPar(r);
122
123  On(SW_RATIONAL);
124  while ( p!=NULL)
125  {
126    CanonicalForm term=convSingAFactoryA(((poly)p_GetCoeff(p, r->cf->extRing)),a, r);
127    for ( int i = 1; i <= n; i++ )
128    {
129      if ( (e = p_GetExp( p, i, r )) != 0 )
130        term *= power( Variable( i + off), e );
131    }
132    result += term;
133    pIter( p );
134  }
135  return result;
136}
137
138static void
139convRecAP_R ( const CanonicalForm & f, int * exp, poly & result, int par_start, int var_start, const ring r) ;
140
141poly convFactoryAPSingAP_R ( const CanonicalForm & f, int par_start, int var_start, const ring r )
142{
143  int n = rVar(r)+rPar(r)+1;
144  int * exp = (int *)omAlloc0(n*sizeof(int));
145  poly result = NULL;
146  convRecAP_R( f, exp, result,par_start, var_start, r );
147  omFreeSize((ADDRESS)exp,n*sizeof(int));
148  return result;
149}
150
151poly convFactoryAPSingAP ( const CanonicalForm & f, const ring r )
152{
153  return convFactoryAPSingAP_R(f,0,rPar(r),r);
154}
155
156static void convRecAP_R ( const CanonicalForm & f, int * exp, poly & result, int par_start, int var_start, const ring r )
157{
158  if (f.isZero())
159    return;
160  if ( ! f.inCoeffDomain() )
161  {
162    int l = f.level();
163    for ( CFIterator i = f; i.hasTerms(); i++ )
164    {
165      exp[l] = i.exp();
166      convRecAP_R( i.coeff(), exp, result, par_start, var_start, r);
167    }
168    exp[l] = 0;
169  }
170  else
171  {
172    poly z=(poly)convFactoryASingA( f,r );
173    if (z!=NULL)
174    {
175      poly term = p_Init(r);
176      pNext( term ) = NULL;
177      int i;
178      for ( i = rVar(r); i>0 ; i-- )
179        p_SetExp( term, i , exp[i+var_start],r);
180      //if (rRing_has_Comp(currRing->extRing)) p_SetComp(term, 0, currRing->extRing); // done by pInit
181      if (par_start==0)
182      {
183        for ( i = 1; i <= var_start; i++ )
184        //z->e[i-1]+=exp[i];
185          p_AddExp(z,i,exp[i],r->cf->extRing);
186      }
187      else
188      {
189        for ( i = par_start+1; i <= var_start+rPar(r); i++ )
190        //z->e[i-1]+=exp[i];
191          p_AddExp(z,i,exp[i-par_start],r->cf->extRing);
192      }
193      pGetCoeff(term)=(number)ALLOC0_RNUMBER();
194      p_GetCoeff(term, r->cf->extRing)=(number) z;
195      p_Setm( term,r );
196      result = p_Add_q( result, term, r );
197    }
198  }
199}
200
201CanonicalForm convSingAFactoryA ( poly p , const Variable & a, const ring r )
202{
203  CanonicalForm result = 0;
204  int e;
205
206  while ( p!=NULL )
207  {
208    CanonicalForm term;
209    if ( rField_is_Zp_a(r) )
210    {
211      term = n_Int( p_GetCoeff( p, r->cf->extRing ), r->cf->extRing->cf );
212    }
213    else
214    {
215      if ( SR_HDL(p_GetCoeff( p, r->cf->extRing )) & SR_INT )
216        term = SR_TO_INT(p_GetCoeff( p, r->cf->extRing )) ;
217      else
218      {
219        if ( p_GetCoeff( p, r->cf->extRing )->s == 3 )
220        {
221          mpz_t dummy;
222          mpz_init_set( dummy, (p_GetCoeff( p,r->cf->extRing )->z) );
223          term = make_cf( dummy );
224        }
225        else
226        {
227          // assume s==0 or s==1
228          mpz_t num, den;
229          On(SW_RATIONAL);
230          mpz_init_set( num, (p_GetCoeff( p, r->cf->extRing )->z) );
231          mpz_init_set( den, (p_GetCoeff( p, r->cf->extRing )->n) );
232          term = make_cf( num, den, ( p_GetCoeff( p, r->cf->extRing )->s != 1 ));
233        }
234      }
235    }
236    if ( (e = p_GetExp( p, 1, r->cf->extRing )) != 0 )
237      term *= power( a , e );
238    result += term;
239    p = pNext( p );
240  }
241  return result;
242}
243
244static number convFactoryNSingAN( const CanonicalForm &f, const ring r)
245{
246  if ( f.isImm() )
247    return n_Init( f.intval(), r->cf->extRing->cf);
248  else
249  {
250    number z=ALLOC_RNUMBER();
251#if defined(LDEBUG)
252    z->debug=123456;
253#endif
254    gmp_numerator( f, z->z );
255    if ( f.den().isOne() )
256    {
257      z->s = 3;
258    }
259    else
260    {
261      gmp_denominator( f, z->n );
262      z->s = 0;
263      nlNormalize(z,r->cf->extRing->cf);
264    }
265    /*#ifdef LDEBUG
266    nlTest(z,r->cf->extRing->cf);
267    #endif*/
268    return z;
269  }
270}
271
272poly convFactoryASingA ( const CanonicalForm & f, const ring r )
273{
274  poly a=NULL;
275  poly t;
276  for( CFIterator i=f; i.hasTerms(); i++)
277  {
278    t= p_Init (r->cf->extRing);
279    p_GetCoeff(t, r->cf->extRing)= convFactoryNSingAN( i.coeff(), r );
280    if (n_IsZero(p_GetCoeff(t,r->cf->extRing),r->cf->extRing->cf))
281    {
282      p_Delete(&t,r->cf->extRing);
283    }
284    else
285    {
286      p_SetExp(t,1,i.exp(),r->cf->extRing);
287      a=p_Add_q(a,t,r->cf->extRing);
288    }
289  }
290  if (a!=NULL)
291  {
292    if (r->cf->extRing->minideal->m[0]!=NULL)
293    {
294      poly l=r->cf->extRing->minideal->m[0];
295      if (p_GetExp(a,1,r->cf->extRing) >= p_GetExp(l,1,r->cf->extRing))
296        a = p_PolyDiv (a, l, FALSE, r->cf->extRing);
297    }
298  }
299  return a;
300}
301
302CanonicalForm convSingTrPFactoryP ( poly p, const ring r )
303{
304  CanonicalForm result = 0;
305  int e, n = rVar(r);
306  int offs = rPar(r);
307
308  while ( p!=NULL )
309  {
310    n_Normalize(p_GetCoeff(p, r), r->cf);
311    CanonicalForm term=convSingPFactoryP(NUM(p_GetCoeff(p, r)),r->cf->extRing);
312
313    if ((DEN(p_GetCoeff(p,r))!=NULL)
314    && (!errorreported))
315    {
316      WerrorS("conversion error: denominator!= 1");
317    }
318
319    for ( int i = n; i > 0; i-- )
320    {
321      if ( (e = p_GetExp( p, i,r )) != 0 )
322        term = term * power( Variable( i + offs ), e );
323    }
324    result += term;
325    p = pNext( p );
326  }
327  return result;
328}
329
330poly convFactoryPSingTrP ( const CanonicalForm & f, const ring r )
331{
332  int n = rVar(r)+1;
333  int * exp = (int*)omAlloc0(n*sizeof(int));
334  poly result = NULL;
335  convRecTrP( f, exp, result , rPar(r), r );
336  omFreeSize((ADDRESS)exp,n*sizeof(int));
337  return result;
338}
339
340static void
341convRecTrP ( const CanonicalForm & f, int * exp, poly & result , int offs, const ring r)
342{
343  if (f.isZero())
344    return;
345  if ( f.level() > offs )
346  {
347    int l = f.level();
348    for ( CFIterator i = f; i.hasTerms(); i++ )
349    {
350      exp[l-offs] = i.exp();
351      convRecTrP( i.coeff(), exp, result, offs, r );
352    }
353    exp[l-offs] = 0;
354  }
355  else
356  {
357    poly term = p_Init(r);
358    pNext( term ) = NULL;
359    for ( int i = rVar(r); i>0; i-- )
360      p_SetExp( term, i ,exp[i], r);
361    //if (rRing_has_Comp(currRing)) p_SetComp(term, 0, currRing); // done by pInit
362    pGetCoeff(term) = ALLOC0_RNUMBER(); // Q!?
363    NUM(pGetCoeff(term))=convFactoryPSingP( f, r->cf->extRing );
364    p_Setm( term,r );
365    result = p_Add_q( result, term,r );
366  }
367}
368
369#if 0
370CanonicalForm
371convSingGFFactoryGF( poly p )
372{
373  CanonicalForm result=CanonicalForm(0);
374  int e, n = pVariables;
375
376  while ( p != NULL )
377  {
378    CanonicalForm term;
379    term = make_cf_from_gf( (int)(long)pGetCoeff( p ) );
380    //int * A=(int *)&term;
381    //Print("term=%x, == 0 ?: %d\n",*A, term.isZero());
382    for ( int i = 1; i <= n; i++ )
383    {
384      if ( (e = pGetExp( p, i )) != 0 )
385         term *= power( Variable( i ), e );
386    }
387    result += term;
388    p = pNext( p );
389  }
390  return result;
391}
392
393poly
394convFactoryGFSingGF ( const CanonicalForm & f )
395{
396//    cerr << " f = " << f << endl;
397  int n = pVariables+1;
398  /* ASSERT( level( f ) <= pVariables, "illegal number of variables" ); */
399  int * exp = (int*)omAlloc0(n*sizeof(int));
400  poly result = NULL;
401  convRecGFGF( f, exp, result );
402  omFreeSize((ADDRESS)exp,n*sizeof(int));
403  return result;
404}
405
406static void
407convRecGFGF ( const CanonicalForm & f, int * exp, poly & result )
408{
409  if (f.isZero())
410    return;
411  if ( ! f.inCoeffDomain() )
412  {
413    int l = f.level();
414    for ( CFIterator i = f; i.hasTerms(); i++ )
415    {
416      exp[l] = i.exp();
417      convRecGFGF( i.coeff(), exp, result );
418    }
419    exp[l] = 0;
420  }
421  else
422  {
423    poly term = pInit();
424    pNext( term ) = NULL;
425    for ( int i = 1; i <= pVariables; i++ )
426      pSetExp( term, i, exp[i]);
427    //if (rRing_has_Comp(currRing)) p_SetComp(term, 0, currRing); // done by pInit
428    pGetCoeff( term ) = (number) gf_value (f);
429    pSetm( term );
430    result = pAdd( result, term );
431  }
432}
433
434#endif
435#endif /* HAVE_FACTORY */
Note: See TracBrowser for help on using the repository browser.