source: git/kernel/ratgring.cc @ 52e2f6

spielwiese
Last change on this file since 52e2f6 was 52e2f6, checked in by Motsak Oleksandr <motsak@…>, 16 years ago
*motsak: major redesign of NC Subsystem, maybe something more... git-svn-id: file:///usr/local/Singular/svn/trunk@10739 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 12.4 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.10 2008-06-10 10:17:32 motsak 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  /* 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// void pLcmRat(poly a, poly b, poly m, poly pshift)
41// {
42//   /* shift is the exp of rational elements */
43//   int i;
44//   for (i=pVariables; i; i--)
45//   {
46//     if (!pGetExp(pshift,i))
47//     {
48//       pSetExp(m,i, si_max( pGetExp(a,i), pGetExp(b,i)));
49//     }
50//     else
51//     {
52//       /* do we really need it? */
53//       pSetExp(m,i,0);
54//     }
55//   }
56//   pSetComp(m, si_max(pGetComp(a), pGetComp(b)));
57//   /* Don't do a pSetm here, otherwise hres/lres chockes */
58// }
59
60/* returns a subpoly of p, s.t. its monomials have the same D-part */
61
62poly p_HeadRat(poly p, int ishift, ring r)
63{
64  poly q   = pNext(p);
65  if (q == NULL) return p;
66  poly res = p_Head(p,r);
67  while ( (q!=NULL) && (p_Comp_k_n(p, q, ishift+1, r)))
68  {
69    res = p_Add_q(res,p_Head(q,r),r);
70    q   = pNext(q);
71  }
72  return res;
73}
74
75/* TO TEST!!! */
76/* returns x-coeff of p, i.e. a poly in x, s.t. corresponding xd-monomials
77have the same D-part */
78
79poly p_GetCoeffRat(poly p, int ishift, ring r)
80{
81  poly q   = pNext(p);
82  poly res; // = p_Head(p,r);
83  res = p_GetExp_k_n(p, ishift+1, r->N, r);
84  p_SetCoeff(res,n_Copy(p_GetCoeff(p,r),r),r);
85  poly s;
86  while ((q!= NULL) && (p_Comp_k_n(p, q, ishift+1, r)))
87  {
88    s   = p_GetExp_k_n(q, ishift+1, r->N, r);
89    p_SetCoeff(s,n_Copy(p_GetCoeff(q,r),r),r);
90    res = p_Add_q(res,s,r);
91    q   = pNext(q);
92  }
93  return res;
94}
95
96void p_LmDeleteAndNextRat(poly *p, int ishift, ring r)
97{
98  /* modifies p*/
99  Print("start: "); Print(" "); p_wrp(*p,r);
100  p_LmCheckPolyRing2(*p, r);
101  poly q = p_Head(*p,r);
102  // in the next line ishift is correct
103  while ( ( (*p)!=NULL ) && ( p_Comp_k_n(*p, q, ishift, r) ))
104  {
105    p_LmDelete(p,r);
106    Print("while: ");p_wrp(*p,r);Print(" ");
107  }
108  p_wrp(*p,r);Print(" ");
109  PrintS("end\n"); 
110  p_LmDelete(&q,r);
111}
112
113/* to test!!! */
114/* ExpVector(pr) = ExpVector(p1) - ExpVector(p2) */
115void p_ExpVectorDiffRat(poly pr, poly p1, poly p2, int ishift, ring r)
116{
117  p_LmCheckPolyRing1(p1, r);
118  p_LmCheckPolyRing1(p2, r);
119  p_LmCheckPolyRing1(pr, r); 
120  int i;
121  poly t=pr;
122  Exponent_t e1,e2;
123  for (i=ishift+1 ; i<=r->N; i++)
124  {
125    e1 = p_GetExp(p1, i, r);
126    e2 = p_GetExp(p2, i, r);
127    //    pAssume1(p_GetExp(p1, i, r) >= p_GetExp(p2, i, r));
128    if (e1 < e2)
129    {
130#ifdef PDEBUG
131      Print("negative ExpVectorDiff\n");
132#endif   
133      p_Delete(&t,r);
134      break;
135    }
136    else
137    {
138      p_SetExp(t,i, e1-e2,r);
139    }
140  }
141  p_Setm(t,r);
142}
143
144
145
146
147/* returns ideal (u,v) s.t. up + vq = 0 */
148
149ideal ncGCD(poly p, poly q, const ring r)
150{
151  intvec *w = NULL;
152  ideal h = idInit(2,1);
153  h->m[0] = p_Copy(p,r);
154  h->m[1] = p_Copy(q,r);
155#ifdef PDEBUG
156  Print("running syzygy comp. for nc_GCD:\n");
157#endif
158  ideal sh = idSyzygies(h, testHomog, &w);
159#ifdef PDEBUG
160  Print("done syzygy comp. for nc_GCD\n");
161#endif
162  /* in comm case, there is only 1 syzygy */
163  /*   singclap_gcd(); */
164  poly K, K1, K2;
165  K  = sh->m[0]; /* take just the first element - to be enhanced later */
166  K1 = pTakeOutComp(&K, 1); // 1st component is taken out from K
167//  pShift(&K,-2); // 2nd component to 0th comp.
168  K2 = pTakeOutComp(&K, 1);
169//  K2 = K;
170
171  Print("syz1: "); p_wrp(K1,r);
172  Print("syz2: "); p_wrp(K2,r);
173
174  /* checking signs before multiplying */   
175  number ck1 = p_GetCoeff(K1,r);
176  number ck2 = p_GetCoeff(K2,r);
177  BOOLEAN bck1, bck2;
178  bck1 = n_GreaterZero(ck1,r);
179  bck2 = n_GreaterZero(ck2,r);
180  /* K1 <0, K2 <0 (-K1,-K2)    */
181//   if ( !(bck1 && bck2) ) /* - , - */
182//   {
183//     K1 = p_Neg(K1,r);
184//     K2 = p_Neg(K2,r);
185//   }
186  id_Delete(&h,r);
187  h = idInit(2,1);
188  h->m[0] = p_Copy(K1,r);
189  h->m[1] = p_Copy(K2,r);
190  id_Delete(&sh,r);
191  return(h);
192}
193
194
195
196/* PINLINE1 void p_ExpVectorDiff
197   remains as is -> BUT we can do memory shift on smaller number of exp's */
198
199
200/*4 - follow the numbering of gring.cc
201* creates the S-polynomial of p1 and p2
202* do not destroy p1 and p2
203*/
204// poly nc_rat_CreateSpoly(poly p1, poly p2, poly spNoether, int ishift, const ring r)
205// {
206//   if ((p_GetComp(p1,r)!=p_GetComp(p2,r))
207//   && (p_GetComp(p1,r)!=0)
208//   && (p_GetComp(p2,r)!=0))
209//   {
210// #ifdef PDEBUG
211//     Print("nc_CreateSpoly : different components!");
212// #endif
213//     return(NULL);
214//   }
215//   /* prod. crit does not apply yet */
216// //   if ((r->nc->type==nc_lie) && pHasNotCF(p1,p2)) /* prod crit */
217// //   {
218// //     return(nc_p_Bracket_qq(pCopy(p2),p1));
219// //   }
220//   poly pL=pOne();
221//   poly m1=pOne();
222//   poly m2=pOne();
223//   /* define shift */
224//   int is = ishift; /* TODO */
225//   pLcmRat(p1,p2,pL,is);
226//   p_Setm(pL,r);
227//   poly pr1 = p_GetExp_k_n(p1,1,ishift-1,r); /* rat D-exp of p1 */
228//   poly pr2 = p_GetExp_k_n(p2,1,ishift-1,r); /* rat D-exp of p2 */
229// #ifdef PDEBUG
230//   p_Test(pL,r);
231// #endif
232//   p_ExpVectorDiff(m1,pL,p1,r); /* purely in D part by construction */
233//   //p_SetComp(m1,0,r);
234//   //p_Setm(m1,r);
235// #ifdef PDEBUG
236//   p_Test(m1,r);
237// #endif
238//   p_ExpVectorDiff(m2,pL,p2,r); /* purely in D part by construction */
239//   //p_SetComp(m2,0,r);
240//   //p_Setm(m2,r);
241// #ifdef PDEBUG
242//   p_Test(m2,r);
243// #endif
244//   p_Delete(&pL,r);
245//   /* zero exponents ! */
246
247//   /* EXTRACT LEADCOEF */
248
249//   poly H1  = p_HeadRat(p1,is,r);
250//   poly M1  = r->nc->p_Procs.mm_Mult_p(m1,p_Copy(H1,r),r);
251
252//   /* POLY:  number C1  = n_Copy(p_GetCoeff(M1,r),r); */
253//   /* RAT: */
254
255//   poly C1  = p_GetCoeffRat(M1,ishift,r);
256
257//   poly H2  = p_HeadRat(p2,is,r);
258//   poly M2  = r->nc->p_Procs.mm_Mult_p(m2,p_Copy(H2,r),r);
259
260//   /* POLY:  number C2  = n_Copy(p_GetCoeff(M2,r),r); */
261//   /* RAT: */
262
263//   poly C2  = p_GetCoeffRat(M2,ishift,r);
264
265// /* we do not assume that X's commute */
266// /* we just run NC syzygies */
267
268// /* NEW IDEA: change the ring to K<X>, map things there
269//    and return the result back; seems to be a good optimization */
270// /* to be done later */
271// /* problem: map to subalgebra. contexts, induced (non-unique) orderings etc. */
272
273//   intvec *w = NULL;
274//   ideal h = idInit(2,1);
275//   h->m[0] = p_Copy(C1,r);
276//   h->m[1] = p_Copy(C2,r);
277// #ifdef PDEBUG
278//   Print("running syzygy comp. for coeffs");
279// #endif
280//   ideal sh = idSyzygies(h, testHomog, &w);
281//   /* in comm case, there is only 1 syzygy */
282//   /*   singclap_gcd(); */
283//   poly K,K1,K2;
284//   K  = sh->m[0];
285//   K1 = pTakeOutComp(&K, 1); // 1st component is taken out from K
286//   pShift(&K,-2); // 2nd component to 0th comp.
287//   K2 = K;
288
289//   /* checking signs before multiplying */   
290//   number ck1 = p_GetCoeff(K1,r);
291//   number ck2 = p_GetCoeff(K2,r);
292//   BOOLEAN bck1, bck2;
293//   bck1 = n_GreaterZero(ck1,r);
294//   bck2 = n_GreaterZero(ck2,r);
295//   /* K1 >0, K2 >0 (K1,-K2)    */
296//   /* K1 >0, K2 <0 (K1,-K2)    */
297//   /* K1 <0, K2 >0 (-K1,K2)    */
298//   /* K1 <0, K2 <0 (-K1,K2)    */
299//   if ( (bck1) && (bck2) ) /* +, + */
300//   {
301//     K2 = p_Neg(K2,r);
302//   }
303//   if ( (bck1) && (!bck2) ) /* + , - */
304//   {
305//     K2 = p_Neg(K2,r);
306//   }
307//   if ( (!bck1) && (bck2) ) /* - , + */
308//   {
309//     K1 = p_Neg(K1,r);
310//   }
311//   if ( !(bck1 && bck2) ) /* - , - */
312//   {
313//     K1 = p_Neg(K1,r);
314//   }
315
316//   poly P1,P2;
317
318//   //  p_LmDeleteRat(M1,ishift,r); // get tail(D^(gamma-alpha) * lm(p1)) = h_f
319//   P1 = p_Copy(p1,r);
320//   p_LmDeleteAndNextRat(P1,ishift,r); // get tail(p1) = t_f
321//   P1 = r->nc->p_Procs.mm_Mult_p(m1,P1,r);
322//   P1 = p_Add_q(P1,M1,r);
323
324//   //  p_LmDeleteRat(M2,ishift,r);
325//   P2 = p_Copy(p2,r);
326//   p_LmDeleteAndNextRat(P2,ishift,r);// get tail(p2)=t_g
327//   P2 = r->nc->p_Procs.mm_Mult_p(m2,P2,r);
328//   P2 = p_Add_q(P2,M2,r);
329
330//   /* coeff business */
331
332//   P1 = p_Mult_q(P1,K1,r);
333//   P2 = p_Mult_q(P2,K2,r);
334//   P1 = p_Add_q(P1,P2,r);
335
336//   /* cleaning up */
337
338// #ifdef PDEBUG
339//   p_Test(p1,r);
340// #endif
341//   /* questionable: */
342//   if (P1!=NULL) pCleardenom(P1);
343//   if (P1!=NULL) pContent(P1);
344//   return(P1);
345// }
346
347/*2
348* reduction of p2 with p1
349* do not destroy p1, but p2
350* p1 divides p2 -> for use in NF algorithm
351* works in an integer fashion
352*/
353
354poly nc_rat_ReduceSpolyNew(const poly p1, poly p2, int ishift, const ring r)
355{
356  const long lCompP1 = p_GetComp(p1,r);
357  const long lCompP2 = p_GetComp(p2,r);
358
359  if ((lCompP1!=lCompP2) && (lCompP1!=0) && (lCompP2!=0))
360  {
361#ifdef PDEBUG
362    Werror("nc_rat_ReduceSpolyNew: different non-zero components!");
363#endif
364    return(NULL);
365  }
366
367  int is = ishift; /* TODO */
368
369  poly m = pOne();
370  p_ExpVectorDiffRat(m, p2, p1, ishift, r); // includes X and D parts
371  //p_Setm(m,r);
372  //  m = p_GetExp_k_n(m,1,ishift,r); /* rat D-exp of m */
373#ifdef PDEBUG
374  p_Test(m,r);
375#endif
376
377  /* pSetComp(m,r)=0? */
378  poly HH = NULL;
379  poly H  = NULL;
380  HH = p_Copy(p_HeadRat(p1,is,r),r); // lm_D(g)
381//  H  = r->nc->p_Procs.mm_Mult_p(m, p_Copy(HH, r), r); // d^aplha lm_D(g)
382  H  = nc_mm_Mult_pp(m, HH, r); // d^aplha lm_D(g)
383
384  poly K  = p_Copy( p_GetCoeffRat(H,  is, r), r);
385  Print("k: "); p_wrp(K,r); PrintS("\n");
386  poly P  = p_Copy( p_GetCoeffRat(p2, is, r), r);
387  Print("p: "); p_wrp(P,r); PrintS("\n");
388
389  //  HH = p_Neg(HH, r);
390  //  poly out = NULL;
391  //  out = p_Add_q(p_Copy(p1,r), HH, r); // out == t_g
392
393  Print("f: "); p_wrp(p2,r); PrintS("\n");
394  Print("g: "); p_wrp(p1,r); PrintS("\n");
395
396  // alt:
397  poly out = p1; //p_Copy(p1,r);
398  p_LmDeleteAndNextRat(&out, is+1, r); // out == t_g
399
400  Print("t_g: "); p_wrp(out,r);
401
402  ideal ncsyz = ncGCD(P,K,r);
403  poly KK = p_Copy(ncsyz->m[0],r); // k'
404  poly PP = p_Copy(ncsyz->m[1],r); // p'
405 
406//    HH = p_Copy(p_HeadRat(p2,is,r),r);
407//    HH = p_Neg(HH, r);
408//    p2 = p_Add_q(p2, HH, r); // t_f
409
410  // alt:
411  p_LmDeleteAndNextRat(&p2, is+1, r); // t_f
412
413  Print("t_f: "); p_wrp(p2,r);
414
415//    HH = p_Copy(p_HeadRat(H,is,r),r);
416//    HH = p_Neg(HH, r);
417//    H  = p_Add_q(H, HH, r); // r_g
418
419  // alt:
420  p_LmDeleteAndNextRat(&H, is+1, r); // r_g
421
422  Print("r_g: "); p_wrp(H,r);
423
424  p2 = p_Mult_q(KK, p2, r); // p2 = k' t_f
425  p_Test(p2,r);
426//  p_Delete(&KK,r);
427
428  Print("k' t_f: "); p_wrp(p2,r);
429
430//  out = r->nc->p_Procs.mm_Mult_p(m, out, r); // d^aplha t_g
431  out = nc_mm_Mult_p(m, out, r); // d^aplha t_g 
432 
433  p_Delete(&m,r);
434
435  Print("d^a t_g: "); p_wrp(out,r);
436  PrintS(" end reduction");
437  out = p_Add_q(H, out, r);
438  p_Test(out,r);
439  out  = p_Mult_q(PP, out, r);
440
441  out = p_Add_q(p2,out,r); // delete out, p2
442  p_Test(out,r);
443  if ( out!=NULL ) pContent(out);
444  return(out);
445}
446
447// return: FALSE, if there exists i in ishift..r->N,
448//                 such that a->exp[i] > b->exp[i]
449//         TRUE, otherwise
450
451BOOLEAN p_DivisibleByRat(poly a, poly b, int ishift, const ring r)
452{
453  int i;
454  for(i=r->N;i>ishift;i--)
455  {
456    if (p_GetExp(a,i,r) > p_GetExp(b,i,r)) return FALSE;
457  }
458  return ((p_GetComp(a,r)==p_GetComp(b,r)) || (p_GetComp(a,r)==0));
459}
460/*2
461*reduces h with elements from reducer choosing the best possible
462* element in t with respect to the given red_length
463* arrays reducer and red_length are [0..(rl-1)]
464*/
465int redRat (poly* h, poly *reducer, int *red_length, int rl, int ishift, ring r)
466{
467  if ((*h)==NULL) return 0;
468
469  int j,i,l;
470
471  loop
472  {
473    j=rl;l=MAX_INT_VAL;
474    for(i=rl-1;i>=0;i--)
475    {
476      if ((l>red_length[i]) && (p_DivisibleByRat(reducer[i],*h,ishift,r)))
477      {
478        j=i; l=red_length[i];
479      }
480    }
481    if (j >=rl)
482    {
483      return 1; // not reducible
484    }
485
486    if (TEST_OPT_DEBUG)
487    {
488      PrintS("reduce ");
489      p_wrp(*h,r);
490      PrintS(" with ");
491      p_wrp(reducer[j],r);
492    }
493    poly hh=nc_rat_ReduceSpolyNew(reducer[j], *h, ishift, r);
494    //    p_Delete(h,r);
495    *h=hh;
496    if (TEST_OPT_DEBUG)
497    {
498      PrintS(" to ");
499      p_wrp(*h,r);
500      PrintLn();
501    }
502    if ((*h)==NULL)
503    {
504      return 0;
505    }
506  }
507}
508#endif
Note: See TracBrowser for help on using the repository browser.