source: git/kernel/ratgring.cc @ 0ffc823

spielwiese
Last change on this file since 0ffc823 was 0ffc823, checked in by Viktor Levandovskyy <levandov@…>, 15 years ago
*levandov: early unit detection in rational GB git-svn-id: file:///usr/local/Singular/svn/trunk@11495 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 20.5 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.23 2009-02-27 19:30:47 levandov 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  if ( (p_LmIsConstantRat(p1,r)) || (p_LmIsConstantRat(p2,r)) )
419  {
420    p_Delete(&p1,r);
421    p_Delete(&p2,r);
422    return( NULL );
423  }
424
425
426/* note: prod. crit does not apply! */
427  poly pL=pOne();
428  poly m1=pOne();
429  poly m2=pOne();
430  int is = ishift; /* TODO */
431  pLcmRat(p1,p2,pL,is);
432  p_Setm(pL,r);
433#ifdef PDEBUG
434  p_Test(pL,r);
435#endif
436  poly pr1 = p_GetExp_k_n(p1,1,ishift,r); /* rat D-exp of p1 */
437  poly pr2 = p_GetExp_k_n(p2,1,ishift,r); /* rat D-exp of p2 */
438  p_ExpVectorDiff(m1,pL,pr1,r); /* purely in D part by construction */
439  p_ExpVectorDiff(m2,pL,pr2,r); /* purely in D part by construction */
440  p_Delete(&pr1,r);
441  p_Delete(&pr2,r);
442  p_Delete(&pL,r);
443#ifdef PDEBUG
444  p_Test(m1,r);
445  PrintS("d^{gamma-alpha} = "); p_wrp(m1,r); PrintLn();
446  p_Test(m2,r);
447  PrintS("d^{gamma-beta} = "); p_wrp(m2,r); PrintLn();
448#endif
449
450  poly HF = NULL;
451  HF = p_HeadRat(p1,is,r); // lm_D(f)
452  HF  = nc_mm_Mult_p(m1, HF, r); // // d^{gamma-alpha} lm_D(f)
453  poly C  = p_GetCoeffRat(HF,  is, r); // c = lc_D(h_f) in the paper
454
455  poly HG = NULL;
456  HG = p_HeadRat(p2,is,r); // lm_D(g)
457  HG  = nc_mm_Mult_p(m2, HG, r); // // d^{gamma-beta} lm_D(g)
458  poly K  = p_GetCoeffRat(HG,  is, r); // k = lc_D(h_g) in the paper
459
460#ifdef PDEBUG
461  PrintS("f: "); p_wrp(p1,r); PrintS("\n");
462  PrintS("c: "); p_wrp(C,r); PrintS("\n");
463  PrintS("g: "); p_wrp(p2,r); PrintS("\n");
464  PrintS("k: "); p_wrp(K,r); PrintS("\n");
465#endif
466 
467  ideal ncsyz = ncGCD(C,K,r);
468  poly KK = ncsyz->m[0]; ncsyz->m[0]=NULL; //p_Copy(ncsyz->m[0],r); // k'
469  poly CC = ncsyz->m[1]; ncsyz->m[1]= NULL; //p_Copy(ncsyz->m[1],r); // c'
470  id_Delete(&ncsyz,r);
471
472  p_LmDeleteAndNextRat(&p1, is, r); // t_f
473  p_LmDeleteAndNextRat(&HF, is, r); // r_f = h_f - lt_D(h_f)
474
475  p_LmDeleteAndNextRat(&p2, is, r); // t_g
476  p_LmDeleteAndNextRat(&HG, is, r); // r_g = h_g - lt_D(h_g)
477
478
479#ifdef PDEBUG
480  PrintS(" t_f: "); p_wrp(p1,r); PrintS("\n"); 
481  PrintS(" t_g: "); p_wrp(p2,r); PrintS("\n"); 
482  PrintS(" r_f: "); p_wrp(HF,r); PrintS("\n"); 
483  PrintS(" r_g: "); p_wrp(HG,r); PrintS("\n"); 
484  PrintS(" c': "); p_wrp(CC,r); PrintS("\n"); 
485  PrintS(" k': "); p_wrp(KK,r); PrintS("\n"); 
486
487#endif
488
489  // k'(r_f + d^{gamma-alpha} t_f)
490
491  p1 = p_Mult_q(m1, p1, r); // p1 = d^{gamma-alpha} t_f
492  p1 = p_Add_q(p1,HF,r); // p1 = r_f + d^{gamma-alpha} t_f
493  p1 = p_Mult_q(KK,p1,r); // p1 = k'(r_f + d^{gamma-alpha} t_f)
494
495  // c'(r_f + d^{gamma-beta} t_g)
496
497  p2 = p_Mult_q(m2, p2, r); // p2 = d^{gamma-beta} t_g
498  p2 = p_Add_q(p2,HG,r); // p2 = r_g + d^{gamma-beta} t_g
499  p2 = p_Mult_q(CC,p2,r); // p2 = c'(r_g + d^{gamma-beta} t_g)
500
501#ifdef PDEBUG
502  p_Test(p1,r);
503  p_Test(p2,r);
504  PrintS(" k'(r_f + d^{gamma-alpha} t_f): "); p_wrp(p1,r);
505  PrintS(" c'(r_g + d^{gamma-beta} t_g): "); p_wrp(p2,r);
506#endif
507
508  poly out = p_Add_q(p1,p2,r); // delete p1, p2; // the sum
509
510#ifdef PDEBUG
511  p_Test(out,r);
512#endif
513
514  //  if ( out!=NULL ) pContent(out); // postponed to enterS
515  return(out);
516}
517
518
519/*2
520* reduction of p2 with p1
521* do not destroy p1, but p2
522* p1 divides p2 -> for use in NF algorithm
523* works in an integer fashion
524*/
525
526poly nc_rat_ReduceSpolyNew(const poly p1, poly p2, int ishift, const ring r)
527{
528  const long lCompP1 = p_GetComp(p1,r);
529  const long lCompP2 = p_GetComp(p2,r);
530
531  if ((lCompP1!=lCompP2) && (lCompP1!=0) && (lCompP2!=0))
532  {
533#ifdef PDEBUG
534    Werror("nc_rat_ReduceSpolyNew: different non-zero components!");
535#endif
536    return(NULL);
537  }
538
539  if (p_LmIsConstantRat(p1,r))
540  {
541    return( NULL );
542  }
543
544
545  int is = ishift; /* TODO */
546
547  poly m = pOne();
548  p_ExpVectorDiffRat(m, p2, p1, ishift, r); // includes X and D parts
549  //p_Setm(m,r);
550  //  m = p_GetExp_k_n(m,1,ishift,r); /* rat D-exp of m */
551#ifdef PDEBUG
552  p_Test(m,r);
553  PrintS("d^alpha = "); p_wrp(m,r); PrintLn();
554#endif
555
556  /* pSetComp(m,r)=0? */
557  poly HH = NULL;
558  poly H  = NULL;
559  HH = p_HeadRat(p1,is,r); //p_Copy(p_HeadRat(p1,is,r),r); // lm_D(g)
560//  H  = r->nc->p_Procs.mm_Mult_p(m, p_Copy(HH, r), r); // d^aplha lm_D(g)
561  H  = nc_mm_Mult_p(m, HH, r); // d^aplha lm_D(g) == h_g in the paper
562
563  poly K  = p_GetCoeffRat(H,  is, r); //p_Copy( p_GetCoeffRat(H,  is, r), r); // k in the paper
564  poly P  = p_GetCoeffRat(p2, is, r); //p_Copy( p_GetCoeffRat(p2, is, r), r); // lc_D(p_2) == lc_D(f)
565
566#ifdef PDEBUG
567  PrintS("k: "); p_wrp(K,r); PrintS("\n");
568  PrintS("p: "); p_wrp(P,r); PrintS("\n");
569  PrintS("f: "); p_wrp(p2,r); PrintS("\n");
570  PrintS("g: "); p_wrp(p1,r); PrintS("\n");
571#endif
572  // alt:
573  poly out = p_Copy(p1,r);
574  p_LmDeleteAndNextRat(&out, is, r); // out == t_g
575
576  ideal ncsyz = ncGCD(P,K,r);
577  poly KK = ncsyz->m[0]; ncsyz->m[0]=NULL; //p_Copy(ncsyz->m[0],r); // k'
578  poly PP = ncsyz->m[1]; ncsyz->m[1]= NULL; //p_Copy(ncsyz->m[1],r); // p'
579
580#ifdef PDEBUG
581  PrintS("t_g: "); p_wrp(out,r);
582  PrintS("k': "); p_wrp(KK,r); PrintS("\n"); 
583  PrintS("p': "); p_wrp(PP,r); PrintS("\n"); 
584#endif
585  id_Delete(&ncsyz,r);
586  p_LmDeleteAndNextRat(&p2, is, r); // t_f
587  p_LmDeleteAndNextRat(&H, is, r); // r_g = h_g - lt_D(h_g)
588
589#ifdef PDEBUG
590  PrintS(" t_f: "); p_wrp(p2,r);
591  PrintS(" r_g: "); p_wrp(H,r);
592#endif
593
594  p2 = p_Mult_q(KK, p2, r); // p2 = k' t_f
595
596#ifdef PDEBUG
597  p_Test(p2,r);
598  PrintS(" k' t_f: "); p_wrp(p2,r);
599#endif
600
601//  out = r->nc->p_Procs.mm_Mult_p(m, out, r); // d^aplha t_g
602  out = nc_mm_Mult_p(m, out, r); // d^aplha t_g 
603  p_Delete(&m,r);
604
605#ifdef PDEBUG
606  PrintS(" d^a t_g: "); p_wrp(out,r);
607  PrintS(" end reduction\n");
608#endif
609
610  out = p_Add_q(H, out, r); // r_g + d^a t_g
611
612#ifdef PDEBUG
613  p_Test(out,r);
614#endif
615  out = p_Mult_q(PP, out, r); // p' (r_g + d^a t_g)
616  out = p_Add_q(p2,out,r); // delete out, p2; // the sum
617
618#ifdef PDEBUG
619  p_Test(out,r);
620#endif
621
622  //  if ( out!=NULL ) pContent(out); // postponed to enterS
623  return(out);
624}
625
626// return: FALSE, if there exists i in ishift..r->N,
627//                 such that a->exp[i] > b->exp[i]
628//         TRUE, otherwise
629
630BOOLEAN p_DivisibleByRat(poly a, poly b, int ishift, const ring r)
631{
632#ifdef PDEBUG
633  PrintS("invoke p_DivByRat with a = ");
634  p_wrp(p_Head(a,r),r);
635  PrintS(" and b= ");
636  p_wrp(p_Head(b,r),r); 
637  PrintLn();
638#endif
639  int i;
640  for(i=r->N; i>ishift; i--)
641  {
642#ifdef PDEBUG
643    Print("i=%d,",i);
644#endif
645    if (p_GetExp(a,i,r) > p_GetExp(b,i,r)) return FALSE;
646  }
647  return ((p_GetComp(a,r)==p_GetComp(b,r)) || (p_GetComp(a,r)==0));
648}
649/*2
650*reduces h with elements from reducer choosing the best possible
651* element in t with respect to the given red_length
652* arrays reducer and red_length are [0..(rl-1)]
653*/
654int redRat (poly* h, poly *reducer, int *red_length, int rl, int ishift, ring r)
655{
656  if ((*h)==NULL) return 0;
657
658  int j,i,l;
659
660  loop
661  {
662    j=rl;l=MAX_INT_VAL;
663    for(i=rl-1;i>=0;i--)
664    {
665      //      Print("test %d, l=%d (curr=%d, l=%d\n",i,red_length[i],j,l);
666      if ((l>red_length[i]) && (p_DivisibleByRat(reducer[i],*h,ishift,r)))
667      {
668        j=i; l=red_length[i];
669        //        PrintS(" yes\n");
670      }
671      //      else PrintS(" no\n");
672    }
673    if (j >=rl)
674    {
675      return 1; // not reducible
676    }
677
678    if (TEST_OPT_DEBUG)
679    {
680      PrintS("reduce ");
681      p_wrp(*h,r);
682      PrintS(" with ");
683      p_wrp(reducer[j],r);
684    }
685    poly hh=nc_rat_ReduceSpolyNew(reducer[j], *h, ishift, r);
686    //    p_Delete(h,r);
687    *h=hh;
688    if (TEST_OPT_DEBUG)
689    {
690      PrintS(" to ");
691      p_wrp(*h,r);
692      PrintLn();
693    }
694    if ((*h)==NULL)
695    {
696      return 0;
697    }
698  }
699}
700
701void pContentRat(poly &ph)
702// changes ph
703// for rat coefficients in K(x1,..xN)
704{
705
706  // init array of RatLeadCoeffs
707  //  poly p_GetCoeffRat(poly p, int ishift, ring r);
708
709  int len=pLength(ph);
710  poly *C = (poly *)omAlloc0((len+1)*sizeof(poly));  //rat coeffs
711  poly *LM = (poly *)omAlloc0((len+1)*sizeof(poly));  // rat lead terms
712  int *D = (int *)omAlloc0((len+1)*sizeof(int));  //degrees of coeffs
713  int *L = (int *)omAlloc0((len+1)*sizeof(int));  //lengths of coeffs
714  int k = 0;
715  poly p = pCopy(ph); // ph will be needed below
716  int mintdeg = pTotaldegree(p);
717  int minlen = len;
718  int dd = 0; int i;
719  int HasConstantCoef = 0;
720  int is = currRing->real_var_start - 1;
721  while (p!=NULL)
722  {
723    LM[k] = p_GetExp_k_n(p,1,is,currRing); // need LmRat istead of  p_HeadRat(p, is, currRing); !
724    C[k] = p_GetCoeffRat(p, is, currRing);
725    D[k] =  pTotaldegree(C[k]);
726    mintdeg = si_min(mintdeg,D[k]);
727    L[k] = pLength(C[k]);
728    minlen = si_min(minlen,L[k]);
729    if (pIsConstant(C[k]))
730    {
731      // C[k] = const, so the content will be numerical
732      HasConstantCoef = 1;
733      // smth like goto cleanup and return(pContent(p));
734    }
735    p_LmDeleteAndNextRat(&p, is, currRing);
736    k++;
737  }
738
739  // look for 1 element of minimal degree and of minimal length
740  k--;
741  poly d;
742  int mindeglen = len;
743  if (k<=0) // this poly is not a ratgring poly -> pContent
744  {
745    pDelete(&C[0]);
746    pDelete(&LM[0]);
747    pContent(ph);
748    goto cleanup;
749  }
750
751  int pmindeglen;
752  for(i=0; i<=k; i++)
753  {
754    if (D[i] == mintdeg)
755    {
756      if (L[i] < mindeglen)
757      {
758        mindeglen=L[i];
759        pmindeglen = i;
760      }
761    }
762  }
763  d = pCopy(C[pmindeglen]);
764  // there are dd>=1 mindeg elements
765  // and pmideglen is the coordinate of one of the smallest among them
766
767  //  poly g = singclap_gcd(p_Copy(p,r),p_Copy(q,r));
768  //  return naGcd(d,d2,currRing);
769
770  // adjoin pContentRat here?
771  for(i=0; i<=k; i++)
772  {
773    d=singclap_gcd(d,pCopy(C[i]));
774    if (pTotaldegree(d)==0) 
775    {
776      // cleanup, pContent, return
777      pDelete(&d);
778      for(;k>=0;k--)
779      {
780        pDelete(&C[k]);
781        pDelete(&LM[k]);
782      }
783      pContent(ph);
784      goto cleanup;
785    }
786  }
787  for(i=0; i<=k; i++)
788  {
789   poly h=singclap_pdivide(C[i],d);
790   pDelete(&C[i]);
791   C[i]=h;
792  }
793 
794  // zusammensetzen,
795  p=NULL; // just to be sure
796  for(i=0; i<=k; i++)
797  {
798   p = pAdd(p, pMult(C[i],LM[i]) );
799   C[i]=NULL; LM[i]=NULL;
800  }
801  pDelete(&ph); // do not need it anymore
802  ph = p;
803  // aufraeumen, return
804cleanup:
805  omFree(C);
806  omFree(LM);
807  omFree(D);
808  omFree(L);
809}
810
811// test if monomial is a constant, i.e. if all exponents and the component
812// is zero
813BOOLEAN p_LmIsConstantRat(const poly p, const ring r)
814{
815  if (p_LmIsConstantCompRat(p, r))
816    return (p_GetComp(p, r) == 0);
817  return FALSE;
818}
819
820// test if the monomial is a constant as a vector component
821// i.e., test if all exponents are zero
822BOOLEAN p_LmIsConstantCompRat(const poly p, const ring r)
823{
824  int i = r->real_var_end;
825
826  while ( (p_GetExp(p,i,r)==0) && (i>=r->real_var_start))
827  {
828    i--;
829  }
830  return ( i+1 == r->real_var_start );
831}
832
833#endif
Note: See TracBrowser for help on using the repository browser.