source: git/kernel/ratgring.cc @ c0e2c36

spielwiese
Last change on this file since c0e2c36 was 4751d7, checked in by Hans Schönemann <hannes@…>, 15 years ago
*hannes: pContentRat may change p git-svn-id: file:///usr/local/Singular/svn/trunk@11480 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 19.8 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/***************************************************************
5 *  File:    ratgring.cc
6 *  Purpose: Ore-noncommutative kernel procedures
7 *  Author:  levandov (Viktor Levandovsky)
8 *  Created: 8/00 - 11/00
9 *  Version: $Id: ratgring.cc,v 1.22 2009-02-26 14:24:23 Singular Exp $
10 *******************************************************************/
11#include "mod2.h"
12#include "ratgring.h"
13#ifdef HAVE_RATGRING
14#include "gring.h"
15#include "febase.h"
16#include "ring.h"
17#include "polys.h"
18#include "numbers.h"
19#include "ideals.h"
20#include "matpol.h"
21#include "kbuckets.h"
22#include "kstd1.h"
23#include "sbuckets.h"
24#include "prCopy.h"
25#include "p_Mult_q.h"
26#include "clapsing.h"
27
28void pLcmRat(poly a, poly b, poly m, int rat_shift)
29{
30  /* rat_shift is the last exp one should count with */
31  int i;
32  for (i=pVariables; i>=rat_shift; i--)
33  {
34    pSetExp(m,i, si_max( pGetExp(a,i), pGetExp(b,i)));
35  }
36  pSetComp(m, si_max(pGetComp(a), pGetComp(b)));
37  /* Don't do a pSetm here, otherwise hres/lres chockes */ 
38}
39
40/*2
41* returns the rational LCM of the head terms of a and b
42* without coefficient!!!
43*/
44poly p_LcmRat(const poly a, const poly b, const long lCompM, const ring r)
45{
46  poly m = // p_One( r);
47          p_Init(r);
48
49  const int pVariables = r->N;
50
51  //  for (int i = pVariables; i>=r->real_var_start; i--)
52  for (int i = r->real_var_end; i>=r->real_var_start; i--)
53  {
54    const int lExpA = p_GetExp (a, i, r);
55    const int lExpB = p_GetExp (b, i, r);
56
57    p_SetExp (m, i, si_max(lExpA, lExpB), r);
58  }
59
60  p_SetComp (m, lCompM, r);
61  p_Setm(m,r);
62  n_New(&(p_GetCoeff(m, r)), r);
63
64  return(m);
65};
66
67// void pLcmRat(poly a, poly b, poly m, poly pshift)
68// {
69//   /* shift is the exp of rational elements */
70//   int i;
71//   for (i=pVariables; i; i--)
72//   {
73//     if (!pGetExp(pshift,i))
74//     {
75//       pSetExp(m,i, si_max( pGetExp(a,i), pGetExp(b,i)));
76//     }
77//     else
78//     {
79//       /* do we really need it? */
80//       pSetExp(m,i,0);
81//     }
82//   }
83//   pSetComp(m, si_max(pGetComp(a), pGetComp(b)));
84//   /* Don't do a pSetm here, otherwise hres/lres chockes */
85// }
86
87/* returns a subpoly of p, s.t. its monomials have the same D-part */
88
89poly p_HeadRat(poly p, int ishift, ring r)
90{
91  poly q   = pNext(p);
92  if (q == NULL) return p;
93  poly res = p_Head(p,r);
94  while ( (q!=NULL) && (p_Comp_k_n(p, q, ishift+1, r)))
95  {
96    res = p_Add_q(res,p_Head(q,r),r);
97    q   = pNext(q);
98  }
99  return res;
100}
101
102/* returns x-coeff of p, i.e. a poly in x, s.t. corresponding xd-monomials
103have the same D-part
104does not destroy p
105*/
106
107poly p_GetCoeffRat(poly p, int ishift, ring r)
108{
109  poly q   = pNext(p);
110  poly res; // = p_Head(p,r);
111  res = p_GetExp_k_n(p, ishift+1, r->N, r); // does pSetm internally
112  p_SetCoeff(res,n_Copy(p_GetCoeff(p,r),r),r);
113  poly s;
114  while ((q!= NULL) && (p_Comp_k_n(p, q, ishift+1, r)))
115  {
116    s   = p_GetExp_k_n(q, ishift+1, r->N, r);
117    p_SetCoeff(s,n_Copy(p_GetCoeff(q,r),r),r);
118    res = p_Add_q(res,s,r);
119    q   = pNext(q);
120  }
121  return res;
122}
123
124void p_LmDeleteAndNextRat(poly *p, int ishift, ring r)
125{
126  /* modifies p*/
127  //  Print("start: "); Print(" "); p_wrp(*p,r);
128  p_LmCheckPolyRing2(*p, r);
129  poly q = p_Head(*p,r);
130  while ( ( (*p)!=NULL ) && ( p_Comp_k_n(*p, q, ishift+1, r) ))
131  {
132    p_LmDelete(p,r);
133    //    Print("while: ");p_wrp(*p,r);Print(" ");
134  }
135  //  p_wrp(*p,r);Print(" ");
136  //  PrintS("end\n");
137  p_LmDelete(&q,r);
138}
139
140/* to test!!! */
141/* ExpVector(pr) = ExpVector(p1) - ExpVector(p2) */
142void p_ExpVectorDiffRat(poly pr, poly p1, poly p2, int ishift, ring r)
143{
144  p_LmCheckPolyRing1(p1, r);
145  p_LmCheckPolyRing1(p2, r);
146  p_LmCheckPolyRing1(pr, r); 
147  int i;
148  poly t=pr;
149  Exponent_t e1,e2;
150  for (i=ishift+1; i<=r->N; i++)
151  {
152    e1 = p_GetExp(p1, i, r);
153    e2 = p_GetExp(p2, i, r);
154    //    pAssume1(p_GetExp(p1, i, r) >= p_GetExp(p2, i, r));
155    if (e1 < e2)
156    {
157#ifdef PDEBUG
158      Print("negative ExpVectorDiff\n");
159#endif   
160      p_Delete(&t,r);
161      break;
162    }
163    else
164    {
165      p_SetExp(t,i, e1-e2,r);
166    }
167  }
168  p_Setm(t,r);
169}
170
171/* returns ideal (u,v) s.t. up + vq = 0 */
172
173ideal ncGCD2(poly p, poly q, const ring r)
174{
175  // todo: must destroy p,q
176  intvec *w = NULL;
177  ideal h = idInit(2,1);
178  h->m[0] = p_Copy(p,r);
179  h->m[1] = p_Copy(q,r);
180#ifdef PDEBUG
181  Print("running syzygy comp. for nc_GCD:\n");
182#endif
183  ideal sh = idSyzygies(h, testHomog, &w);
184#ifdef PDEBUG
185  Print("done syzygy comp. for nc_GCD\n");
186#endif
187  /* in comm case, there is only 1 syzygy */
188  /*   singclap_gcd(); */
189  poly K, K1, K2;
190  K  = sh->m[0]; /* take just the first element - to be enhanced later */
191  K1 = pTakeOutComp(&K, 1); // 1st component is taken out from K
192//  pShift(&K,-2); // 2nd component to 0th comp.
193  K2 = pTakeOutComp(&K, 1);
194//  K2 = K;
195
196  Print("syz1: "); p_wrp(K1,r);
197  Print("syz2: "); p_wrp(K2,r);
198
199  /* checking signs before multiplying */   
200  number ck1 = p_GetCoeff(K1,r);
201  number ck2 = p_GetCoeff(K2,r);
202  BOOLEAN bck1, bck2;
203  bck1 = n_GreaterZero(ck1,r);
204  bck2 = n_GreaterZero(ck2,r);
205  /* K1 <0, K2 <0 (-K1,-K2)    */
206//   if ( !(bck1 && bck2) ) /* - , - */
207//   {
208//     K1 = p_Neg(K1,r);
209//     K2 = p_Neg(K2,r);
210//   }
211  id_Delete(&h,r);
212  h = idInit(2,1);
213  h->m[0] = p_Copy(K1,r);
214  h->m[1] = p_Copy(K2,r);
215  id_Delete(&sh,r);
216  return(h);
217}
218
219/* returns ideal (u,v) s.t. up + vq = 0 */
220
221ideal ncGCD(poly p, poly q, const ring r)
222{
223  // destroys p and q
224  // assume: p,q are in the comm. ring
225  // to be used in the coeff business
226#ifdef PDEBUG
227  PrintS(" GCD_start:");
228#endif
229  poly g = singclap_gcd(p_Copy(p,r),p_Copy(q,r));
230#ifdef PDEBUG
231  p_wrp(g,r);
232  PrintS(" GCD_end;\n");
233#endif
234  poly u = singclap_pdivide(q,g); //q/g
235  poly v = singclap_pdivide(p,g); //p/g
236  v = p_Neg(v,r);
237  p_Delete(&p,r);
238  p_Delete(&q,r);
239  ideal h = idInit(2,1);
240  h->m[0] = u; // p_Copy(u,r);
241  h->m[1] = v; // p_Copy(v,r);
242  return(h);
243}
244
245/* PINLINE1 void p_ExpVectorDiff
246   remains as is -> BUT we can do memory shift on smaller number of exp's */
247
248
249/*4 - follow the numbering of gring.cc
250* creates the S-polynomial of p1 and p2
251* do not destroy p1 and p2
252*/
253// poly nc_rat_CreateSpoly(poly p1, poly p2, poly spNoether, int ishift, const ring r)
254// {
255//   if ((p_GetComp(p1,r)!=p_GetComp(p2,r))
256//   && (p_GetComp(p1,r)!=0)
257//   && (p_GetComp(p2,r)!=0))
258//   {
259// #ifdef PDEBUG
260//     Print("nc_CreateSpoly : different components!");
261// #endif
262//     return(NULL);
263//   }
264//   /* prod. crit does not apply yet */
265// //   if ((r->nc->type==nc_lie) && pHasNotCF(p1,p2)) /* prod crit */
266// //   {
267// //     return(nc_p_Bracket_qq(pCopy(p2),p1));
268// //   }
269//   poly pL=pOne();
270//   poly m1=pOne();
271//   poly m2=pOne();
272//   /* define shift */
273//   int is = ishift; /* TODO */
274//   pLcmRat(p1,p2,pL,is);
275//   p_Setm(pL,r);
276//   poly pr1 = p_GetExp_k_n(p1,1,ishift-1,r); /* rat D-exp of p1 */
277//   poly pr2 = p_GetExp_k_n(p2,1,ishift-1,r); /* rat D-exp of p2 */
278// #ifdef PDEBUG
279//   p_Test(pL,r);
280// #endif
281//   p_ExpVectorDiff(m1,pL,p1,r); /* purely in D part by construction */
282//   //p_SetComp(m1,0,r);
283//   //p_Setm(m1,r);
284// #ifdef PDEBUG
285//   p_Test(m1,r);
286// #endif
287//   p_ExpVectorDiff(m2,pL,p2,r); /* purely in D part by construction */
288//   //p_SetComp(m2,0,r);
289//   //p_Setm(m2,r);
290// #ifdef PDEBUG
291//   p_Test(m2,r);
292// #endif
293//   p_Delete(&pL,r);
294//   /* zero exponents ! */
295
296//   /* EXTRACT LEADCOEF */
297
298//   poly H1  = p_HeadRat(p1,is,r);
299//   poly M1  = r->nc->p_Procs.mm_Mult_p(m1,p_Copy(H1,r),r);
300
301//   /* POLY:  number C1  = n_Copy(p_GetCoeff(M1,r),r); */
302//   /* RAT: */
303
304//   poly C1  = p_GetCoeffRat(M1,ishift,r);
305
306//   poly H2  = p_HeadRat(p2,is,r);
307//   poly M2  = r->nc->p_Procs.mm_Mult_p(m2,p_Copy(H2,r),r);
308
309//   /* POLY:  number C2  = n_Copy(p_GetCoeff(M2,r),r); */
310//   /* RAT: */
311
312//   poly C2  = p_GetCoeffRat(M2,ishift,r);
313
314// /* we do not assume that X's commute */
315// /* we just run NC syzygies */
316
317// /* NEW IDEA: change the ring to K<X>, map things there
318//    and return the result back; seems to be a good optimization */
319// /* to be done later */
320// /* problem: map to subalgebra. contexts, induced (non-unique) orderings etc. */
321
322//   intvec *w = NULL;
323//   ideal h = idInit(2,1);
324//   h->m[0] = p_Copy(C1,r);
325//   h->m[1] = p_Copy(C2,r);
326// #ifdef PDEBUG
327//   Print("running syzygy comp. for coeffs");
328// #endif
329//   ideal sh = idSyzygies(h, testHomog, &w);
330//   /* in comm case, there is only 1 syzygy */
331//   /*   singclap_gcd(); */
332//   poly K,K1,K2;
333//   K  = sh->m[0];
334//   K1 = pTakeOutComp(&K, 1); // 1st component is taken out from K
335//   pShift(&K,-2); // 2nd component to 0th comp.
336//   K2 = K;
337
338//   /* checking signs before multiplying */   
339//   number ck1 = p_GetCoeff(K1,r);
340//   number ck2 = p_GetCoeff(K2,r);
341//   BOOLEAN bck1, bck2;
342//   bck1 = n_GreaterZero(ck1,r);
343//   bck2 = n_GreaterZero(ck2,r);
344//   /* K1 >0, K2 >0 (K1,-K2)    */
345//   /* K1 >0, K2 <0 (K1,-K2)    */
346//   /* K1 <0, K2 >0 (-K1,K2)    */
347//   /* K1 <0, K2 <0 (-K1,K2)    */
348//   if ( (bck1) && (bck2) ) /* +, + */
349//   {
350//     K2 = p_Neg(K2,r);
351//   }
352//   if ( (bck1) && (!bck2) ) /* + , - */
353//   {
354//     K2 = p_Neg(K2,r);
355//   }
356//   if ( (!bck1) && (bck2) ) /* - , + */
357//   {
358//     K1 = p_Neg(K1,r);
359//   }
360//   if ( !(bck1 && bck2) ) /* - , - */
361//   {
362//     K1 = p_Neg(K1,r);
363//   }
364
365//   poly P1,P2;
366
367//   //  p_LmDeleteRat(M1,ishift,r); // get tail(D^(gamma-alpha) * lm(p1)) = h_f
368//   P1 = p_Copy(p1,r);
369//   p_LmDeleteAndNextRat(P1,ishift,r); // get tail(p1) = t_f
370//   P1 = r->nc->p_Procs.mm_Mult_p(m1,P1,r);
371//   P1 = p_Add_q(P1,M1,r);
372
373//   //  p_LmDeleteRat(M2,ishift,r);
374//   P2 = p_Copy(p2,r);
375//   p_LmDeleteAndNextRat(P2,ishift,r);// get tail(p2)=t_g
376//   P2 = r->nc->p_Procs.mm_Mult_p(m2,P2,r);
377//   P2 = p_Add_q(P2,M2,r);
378
379//   /* coeff business */
380
381//   P1 = p_Mult_q(P1,K1,r);
382//   P2 = p_Mult_q(P2,K2,r);
383//   P1 = p_Add_q(P1,P2,r);
384
385//   /* cleaning up */
386
387// #ifdef PDEBUG
388//   p_Test(p1,r);
389// #endif
390//   /* questionable: */
391//   if (P1!=NULL) pCleardenom(P1);
392//   if (P1!=NULL) pContent(P1);
393//   return(P1);
394// }
395
396
397/*4 - follow the numbering of gring.cc
398* creates the S-polynomial of p1 and p2
399* do not destroy p1 and p2
400*/
401poly nc_rat_CreateSpoly(poly pp1, poly pp2, int ishift, const ring r)
402{
403
404  poly p1 = p_Copy(pp1,r);
405  poly p2 = p_Copy(pp2,r);
406
407  const long lCompP1 = p_GetComp(p1,r);
408  const long lCompP2 = p_GetComp(p2,r);
409
410  if ((lCompP1!=lCompP2) && (lCompP1!=0) && (lCompP2!=0))
411  {
412#ifdef PDEBUG
413    Werror("nc_rat_CreateSpoly: different non-zero components!");
414#endif
415    return(NULL);
416  }
417
418/* note: prod. crit does not apply! */
419  poly pL=pOne();
420  poly m1=pOne();
421  poly m2=pOne();
422  int is = ishift; /* TODO */
423  pLcmRat(p1,p2,pL,is);
424  p_Setm(pL,r);
425#ifdef PDEBUG
426  p_Test(pL,r);
427#endif
428  poly pr1 = p_GetExp_k_n(p1,1,ishift,r); /* rat D-exp of p1 */
429  poly pr2 = p_GetExp_k_n(p2,1,ishift,r); /* rat D-exp of p2 */
430  p_ExpVectorDiff(m1,pL,pr1,r); /* purely in D part by construction */
431  p_ExpVectorDiff(m2,pL,pr2,r); /* purely in D part by construction */
432  p_Delete(&pr1,r);
433  p_Delete(&pr2,r);
434  p_Delete(&pL,r);
435#ifdef PDEBUG
436  p_Test(m1,r);
437  PrintS("d^{gamma-alpha} = "); p_wrp(m1,r); PrintLn();
438  p_Test(m2,r);
439  PrintS("d^{gamma-beta} = "); p_wrp(m2,r); PrintLn();
440#endif
441
442  poly HF = NULL;
443  HF = p_HeadRat(p1,is,r); // lm_D(f)
444  HF  = nc_mm_Mult_p(m1, HF, r); // // d^{gamma-alpha} lm_D(f)
445  poly C  = p_GetCoeffRat(HF,  is, r); // c = lc_D(h_f) in the paper
446
447  poly HG = NULL;
448  HG = p_HeadRat(p2,is,r); // lm_D(g)
449  HG  = nc_mm_Mult_p(m2, HG, r); // // d^{gamma-beta} lm_D(g)
450  poly K  = p_GetCoeffRat(HG,  is, r); // k = lc_D(h_g) in the paper
451
452#ifdef PDEBUG
453  PrintS("f: "); p_wrp(p1,r); PrintS("\n");
454  PrintS("c: "); p_wrp(C,r); PrintS("\n");
455  PrintS("g: "); p_wrp(p2,r); PrintS("\n");
456  PrintS("k: "); p_wrp(K,r); PrintS("\n");
457#endif
458 
459  ideal ncsyz = ncGCD(C,K,r);
460  poly KK = ncsyz->m[0]; ncsyz->m[0]=NULL; //p_Copy(ncsyz->m[0],r); // k'
461  poly CC = ncsyz->m[1]; ncsyz->m[1]= NULL; //p_Copy(ncsyz->m[1],r); // c'
462  id_Delete(&ncsyz,r);
463
464  p_LmDeleteAndNextRat(&p1, is, r); // t_f
465  p_LmDeleteAndNextRat(&HF, is, r); // r_f = h_f - lt_D(h_f)
466
467  p_LmDeleteAndNextRat(&p2, is, r); // t_g
468  p_LmDeleteAndNextRat(&HG, is, r); // r_g = h_g - lt_D(h_g)
469
470
471#ifdef PDEBUG
472  PrintS(" t_f: "); p_wrp(p1,r); PrintS("\n"); 
473  PrintS(" t_g: "); p_wrp(p2,r); PrintS("\n"); 
474  PrintS(" r_f: "); p_wrp(HF,r); PrintS("\n"); 
475  PrintS(" r_g: "); p_wrp(HG,r); PrintS("\n"); 
476  PrintS(" c': "); p_wrp(CC,r); PrintS("\n"); 
477  PrintS(" k': "); p_wrp(KK,r); PrintS("\n"); 
478
479#endif
480
481  // k'(r_f + d^{gamma-alpha} t_f)
482
483  p1 = p_Mult_q(m1, p1, r); // p1 = d^{gamma-alpha} t_f
484  p1 = p_Add_q(p1,HF,r); // p1 = r_f + d^{gamma-alpha} t_f
485  p1 = p_Mult_q(KK,p1,r); // p1 = k'(r_f + d^{gamma-alpha} t_f)
486
487  // c'(r_f + d^{gamma-beta} t_g)
488
489  p2 = p_Mult_q(m2, p2, r); // p2 = d^{gamma-beta} t_g
490  p2 = p_Add_q(p2,HG,r); // p2 = r_g + d^{gamma-beta} t_g
491  p2 = p_Mult_q(CC,p2,r); // p2 = c'(r_g + d^{gamma-beta} t_g)
492
493#ifdef PDEBUG
494  p_Test(p1,r);
495  p_Test(p2,r);
496  PrintS(" k'(r_f + d^{gamma-alpha} t_f): "); p_wrp(p1,r);
497  PrintS(" c'(r_g + d^{gamma-beta} t_g): "); p_wrp(p2,r);
498#endif
499
500  poly out = p_Add_q(p1,p2,r); // delete p1, p2; // the sum
501
502#ifdef PDEBUG
503  p_Test(out,r);
504#endif
505
506  //  if ( out!=NULL ) pContent(out); // postponed to enterS
507  return(out);
508}
509
510
511/*2
512* reduction of p2 with p1
513* do not destroy p1, but p2
514* p1 divides p2 -> for use in NF algorithm
515* works in an integer fashion
516*/
517
518poly nc_rat_ReduceSpolyNew(const poly p1, poly p2, int ishift, const ring r)
519{
520  const long lCompP1 = p_GetComp(p1,r);
521  const long lCompP2 = p_GetComp(p2,r);
522
523  if ((lCompP1!=lCompP2) && (lCompP1!=0) && (lCompP2!=0))
524  {
525#ifdef PDEBUG
526    Werror("nc_rat_ReduceSpolyNew: different non-zero components!");
527#endif
528    return(NULL);
529  }
530
531  int is = ishift; /* TODO */
532
533  poly m = pOne();
534  p_ExpVectorDiffRat(m, p2, p1, ishift, r); // includes X and D parts
535  //p_Setm(m,r);
536  //  m = p_GetExp_k_n(m,1,ishift,r); /* rat D-exp of m */
537#ifdef PDEBUG
538  p_Test(m,r);
539  PrintS("d^alpha = "); p_wrp(m,r); PrintLn();
540#endif
541
542  /* pSetComp(m,r)=0? */
543  poly HH = NULL;
544  poly H  = NULL;
545  HH = p_HeadRat(p1,is,r); //p_Copy(p_HeadRat(p1,is,r),r); // lm_D(g)
546//  H  = r->nc->p_Procs.mm_Mult_p(m, p_Copy(HH, r), r); // d^aplha lm_D(g)
547  H  = nc_mm_Mult_p(m, HH, r); // d^aplha lm_D(g) == h_g in the paper
548
549  poly K  = p_GetCoeffRat(H,  is, r); //p_Copy( p_GetCoeffRat(H,  is, r), r); // k in the paper
550  poly P  = p_GetCoeffRat(p2, is, r); //p_Copy( p_GetCoeffRat(p2, is, r), r); // lc_D(p_2) == lc_D(f)
551
552#ifdef PDEBUG
553  PrintS("k: "); p_wrp(K,r); PrintS("\n");
554  PrintS("p: "); p_wrp(P,r); PrintS("\n");
555  PrintS("f: "); p_wrp(p2,r); PrintS("\n");
556  PrintS("g: "); p_wrp(p1,r); PrintS("\n");
557#endif
558  // alt:
559  poly out = p_Copy(p1,r);
560  p_LmDeleteAndNextRat(&out, is, r); // out == t_g
561
562  ideal ncsyz = ncGCD(P,K,r);
563  poly KK = ncsyz->m[0]; ncsyz->m[0]=NULL; //p_Copy(ncsyz->m[0],r); // k'
564  poly PP = ncsyz->m[1]; ncsyz->m[1]= NULL; //p_Copy(ncsyz->m[1],r); // p'
565
566#ifdef PDEBUG
567  PrintS("t_g: "); p_wrp(out,r);
568  PrintS("k': "); p_wrp(KK,r); PrintS("\n"); 
569  PrintS("p': "); p_wrp(PP,r); PrintS("\n"); 
570#endif
571  id_Delete(&ncsyz,r);
572  p_LmDeleteAndNextRat(&p2, is, r); // t_f
573  p_LmDeleteAndNextRat(&H, is, r); // r_g = h_g - lt_D(h_g)
574
575#ifdef PDEBUG
576  PrintS(" t_f: "); p_wrp(p2,r);
577  PrintS(" r_g: "); p_wrp(H,r);
578#endif
579
580  p2 = p_Mult_q(KK, p2, r); // p2 = k' t_f
581
582#ifdef PDEBUG
583  p_Test(p2,r);
584  PrintS(" k' t_f: "); p_wrp(p2,r);
585#endif
586
587//  out = r->nc->p_Procs.mm_Mult_p(m, out, r); // d^aplha t_g
588  out = nc_mm_Mult_p(m, out, r); // d^aplha t_g 
589  p_Delete(&m,r);
590
591#ifdef PDEBUG
592  PrintS(" d^a t_g: "); p_wrp(out,r);
593  PrintS(" end reduction\n");
594#endif
595
596  out = p_Add_q(H, out, r); // r_g + d^a t_g
597
598#ifdef PDEBUG
599  p_Test(out,r);
600#endif
601  out = p_Mult_q(PP, out, r); // p' (r_g + d^a t_g)
602  out = p_Add_q(p2,out,r); // delete out, p2; // the sum
603
604#ifdef PDEBUG
605  p_Test(out,r);
606#endif
607
608  //  if ( out!=NULL ) pContent(out); // postponed to enterS
609  return(out);
610}
611
612// return: FALSE, if there exists i in ishift..r->N,
613//                 such that a->exp[i] > b->exp[i]
614//         TRUE, otherwise
615
616BOOLEAN p_DivisibleByRat(poly a, poly b, int ishift, const ring r)
617{
618#ifdef PDEBUG
619  PrintS("invoke p_DivByRat with a = ");
620  p_wrp(p_Head(a,r),r);
621  PrintS(" and b= ");
622  p_wrp(p_Head(b,r),r); 
623  PrintLn();
624#endif
625  int i;
626  for(i=r->N; i>ishift; i--)
627  {
628#ifdef PDEBUG
629    Print("i=%d,",i);
630#endif
631    if (p_GetExp(a,i,r) > p_GetExp(b,i,r)) return FALSE;
632  }
633  return ((p_GetComp(a,r)==p_GetComp(b,r)) || (p_GetComp(a,r)==0));
634}
635/*2
636*reduces h with elements from reducer choosing the best possible
637* element in t with respect to the given red_length
638* arrays reducer and red_length are [0..(rl-1)]
639*/
640int redRat (poly* h, poly *reducer, int *red_length, int rl, int ishift, ring r)
641{
642  if ((*h)==NULL) return 0;
643
644  int j,i,l;
645
646  loop
647  {
648    j=rl;l=MAX_INT_VAL;
649    for(i=rl-1;i>=0;i--)
650    {
651      //      Print("test %d, l=%d (curr=%d, l=%d\n",i,red_length[i],j,l);
652      if ((l>red_length[i]) && (p_DivisibleByRat(reducer[i],*h,ishift,r)))
653      {
654        j=i; l=red_length[i];
655        //        PrintS(" yes\n");
656      }
657      //      else PrintS(" no\n");
658    }
659    if (j >=rl)
660    {
661      return 1; // not reducible
662    }
663
664    if (TEST_OPT_DEBUG)
665    {
666      PrintS("reduce ");
667      p_wrp(*h,r);
668      PrintS(" with ");
669      p_wrp(reducer[j],r);
670    }
671    poly hh=nc_rat_ReduceSpolyNew(reducer[j], *h, ishift, r);
672    //    p_Delete(h,r);
673    *h=hh;
674    if (TEST_OPT_DEBUG)
675    {
676      PrintS(" to ");
677      p_wrp(*h,r);
678      PrintLn();
679    }
680    if ((*h)==NULL)
681    {
682      return 0;
683    }
684  }
685}
686
687void pContentRat(poly &ph)
688// changes ph
689// for rat coefficients in K(x1,..xN)
690{
691
692  // init array of RatLeadCoeffs
693  //  poly p_GetCoeffRat(poly p, int ishift, ring r);
694
695  int len=pLength(ph);
696  poly *C = (poly *)omAlloc0((len+1)*sizeof(poly));  //rat coeffs
697  poly *LM = (poly *)omAlloc0((len+1)*sizeof(poly));  // rat lead terms
698  int *D = (int *)omAlloc0((len+1)*sizeof(int));  //degrees of coeffs
699  int *L = (int *)omAlloc0((len+1)*sizeof(int));  //lengths of coeffs
700  int k = 0;
701  poly p = pCopy(ph); // ph will be needed below
702  int mintdeg = pTotaldegree(p);
703  int minlen = len;
704  int dd = 0; int i;
705  int HasConstantCoef = 0;
706  int is = currRing->real_var_start - 1;
707  while (p!=NULL)
708  {
709    LM[k] = p_GetExp_k_n(p,1,is,currRing); // need LmRat istead of  p_HeadRat(p, is, currRing); !
710    C[k] = p_GetCoeffRat(p, is, currRing);
711    D[k] =  pTotaldegree(C[k]);
712    mintdeg = si_min(mintdeg,D[k]);
713    L[k] = pLength(C[k]);
714    minlen = si_min(minlen,L[k]);
715    if (pIsConstant(C[k]))
716    {
717      // C[k] = const, so the content will be numerical
718      HasConstantCoef = 1;
719      // smth like goto cleanup and return(pContent(p));
720    }
721    p_LmDeleteAndNextRat(&p, is, currRing);
722    k++;
723  }
724
725  // look for 1 element of minimal degree and of minimal length
726  k--;
727  poly d;
728  int mindeglen = len;
729  if (k<=0) // this poly is not a ratgring poly -> pContent
730  {
731    pDelete(&C[0]);
732    pDelete(&LM[0]);
733    pContent(ph);
734    goto cleanup;
735  }
736
737  int pmindeglen;
738  for(i=0; i<=k; i++)
739  {
740    if (D[i] == mintdeg)
741    {
742      if (L[i] < mindeglen)
743      {
744        mindeglen=L[i];
745        pmindeglen = i;
746      }
747    }
748  }
749  d = pCopy(C[pmindeglen]);
750  // there are dd>=1 mindeg elements
751  // and pmideglen is the coordinate of one of the smallest among them
752
753  //  poly g = singclap_gcd(p_Copy(p,r),p_Copy(q,r));
754  //  return naGcd(d,d2,currRing);
755
756  // adjoin pContentRat here?
757  for(i=0; i<=k; i++)
758  {
759    d=singclap_gcd(d,pCopy(C[i]));
760    if (pTotaldegree(d)==0) 
761    {
762      // cleanup, pContent, return
763      pDelete(&d);
764      for(;k>=0;k--)
765      {
766        pDelete(&C[k]);
767        pDelete(&LM[k]);
768      }
769      pContent(ph);
770      goto cleanup;
771    }
772  }
773  for(i=0; i<=k; i++)
774  {
775   poly h=singclap_pdivide(C[i],d);
776   pDelete(&C[i]);
777   C[i]=h;
778  }
779 
780  // zusammensetzen,
781  p=NULL; // just to be sure
782  for(i=0; i<=k; i++)
783  {
784   p = pAdd(p, pMult(C[i],LM[i]) );
785   C[i]=NULL; LM[i]=NULL;
786  }
787  pDelete(&ph); // do not need it anymore
788  ph = p;
789  // aufraeumen, return
790cleanup:
791  omFree(C);
792  omFree(LM);
793  omFree(D);
794  omFree(L);
795}
796#endif
Note: See TracBrowser for help on using the repository browser.