source: git/kernel/ringgb.cc @ 37318d

spielwiese
Last change on this file since 37318d was c90b43, checked in by Hans Schönemann <hannes@…>, 15 years ago
*hannes: HAVE_RINGS only git-svn-id: file:///usr/local/Singular/svn/trunk@11781 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 6.0 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: ringgb.cc,v 1.17 2009-05-06 12:53:49 Singular Exp $ */
5/*
6* ABSTRACT: ringgb interface
7*/
8//#define HAVE_TAIL_RING
9#define NO_BUCKETS
10
11#include "mod2.h"
12#include "kutil.h"
13#include "structs.h"
14#include "omalloc.h"
15#include "polys.h"
16#include "p_polys.h"
17#include "ideals.h"
18#include "febase.h"
19#include "kstd1.h"
20#include "khstd.h"
21#include "kbuckets.h"
22#include "weight.h"
23#include "intvec.h"
24#include "pInline1.h"
25#ifdef HAVE_PLURAL
26#include "gring.h"
27#endif
28
29#include "ringgb.h"
30
31#ifdef HAVE_RINGS
32poly reduce_poly_fct(poly p, ring r)
33{
34   return kFindZeroPoly(p, r, r);
35}
36
37/*
38 * Returns maximal k, such that
39 * 2^k | n
40 */
41int indexOf2(number n)
42{
43  long test = (long) n;
44  int i = 0;
45  while (test%2 == 0)
46  {
47    i++;
48    test = test / 2;
49  }
50  return i;
51}
52
53/***************************************************************
54 *
55 * Lcm business
56 *
57 ***************************************************************/
58// get m1 = LCM(LM(p1), LM(p2))/LM(p1)
59//     m2 = LCM(LM(p1), LM(p2))/LM(p2)
60BOOLEAN ring2toM_GetLeadTerms(const poly p1, const poly p2, const ring p_r,
61                               poly &m1, poly &m2, const ring m_r)
62{
63  int i;
64  Exponent_t x;
65  m1 = p_Init(m_r);
66  m2 = p_Init(m_r);
67
68  for (i = p_r->N; i; i--)
69  {
70    x = p_GetExpDiff(p1, p2, i, p_r);
71    if (x > 0)
72    {
73      p_SetExp(m2,i,x, m_r);
74      p_SetExp(m1,i,0, m_r);
75    }
76    else
77    {
78      p_SetExp(m1,i,-x, m_r);
79      p_SetExp(m2,i,0, m_r);
80    }
81  }
82  p_Setm(m1, m_r);
83  p_Setm(m2, m_r);
84  long cp1 = (long) pGetCoeff(p1);
85  long cp2 = (long) pGetCoeff(p2);
86  if (cp1 != 0 && cp2 != 0)
87  {
88    while (cp1%2 == 0 && cp2%2 == 0)
89    {
90      cp1 = cp1 / 2;
91      cp2 = cp2 / 2;
92    }
93  }
94  p_SetCoeff(m1, (number) cp2, m_r);
95  p_SetCoeff(m2, (number) cp1, m_r);
96  return TRUE;
97}
98
99void printPolyMsg(const char * start, poly f, const char * end)
100{
101  PrintS(start);
102  wrp(f);
103  PrintS(end);
104}
105
106poly spolyRing2toM(poly f, poly g, ring r)
107{
108  poly m1 = NULL;
109  poly m2 = NULL;
110  ring2toM_GetLeadTerms(f, g, r, m1, m2, r);
111  // printPolyMsg("spoly: m1=", m1, " | ");
112  // printPolyMsg("m2=", m2, "");
113  // PrintLn();
114  poly sp = pSub(p_Mult_mm(f, m1, r), pp_Mult_mm(g, m2, r));
115  pDelete(&m1);
116  pDelete(&m2);
117  return(sp);
118}
119
120poly ringRedNF (poly f, ideal G, ring r)
121{
122  // If f = 0, then normal form is also 0
123  if (f == NULL) { return NULL; }
124  poly h = NULL;
125  poly g = pCopy(f);
126  int c = 0;
127  while (g != NULL) {
128    Print("%d-step RedNF - g=", c);
129    wrp(g);
130    PrintS(" | h=");
131    wrp(h);
132    PrintLn();
133    g = ringNF(g, G, r);
134    if (g != NULL) {
135      h = pAdd(h, pHead(g));
136      pLmDelete(&g);
137    }
138    c++;
139  }
140  return h;
141}
142
143#endif
144
145#ifdef HAVE_RINGS
146
147/*
148 * Find an index i from G, such that
149 * LT(rside) = x * LT(G[i]) has a solution
150 * or -1 if rside is not in the
151 * ideal of the leading coefficients
152 * of the suitable g from G.
153 */
154int findRingSolver(poly rside, ideal G, ring r)
155{
156  if (rside == NULL) return -1;
157  int i;
158//  int iO2rside = indexOf2(pGetCoeff(rside));
159  for (i = 0; i < IDELEMS(G); i++)
160  {
161    if // (indexOf2(pGetCoeff(G->m[i])) <= iO2rside &&    / should not be necessary any more
162       (p_LmDivisibleBy(G->m[i], rside, r))
163    {
164      return i;
165    }
166  }
167  return -1;
168}
169
170poly plain_spoly(poly f, poly g)
171{
172  number cf = nCopy(pGetCoeff(f)), cg = nCopy(pGetCoeff(g));
173  int ct = ksCheckCoeff(&cf, &cg); // gcd and zero divisors
174  poly fm, gm;
175  k_GetLeadTerms(f, g, currRing, fm, gm, currRing);
176  pSetCoeff0(fm, cg);
177  pSetCoeff0(gm, cf);  // and now, m1 * LT(p1) == m2 * LT(p2)
178  poly sp = pSub(ppMult_mm(f, fm), ppMult_mm(g, gm));
179  pDelete(&fm);
180  pDelete(&gm);
181  return(sp);
182}
183
184/*2
185* Generates spoly(0, h) if applicable. Assumes ring in Z/2^n.
186*/
187poly plain_zero_spoly(poly h)
188{
189  poly p = NULL;
190  number gcd = nGcd((number) 0, pGetCoeff(h), currRing);
191  if ((NATNUMBER) gcd > 1)
192  {
193    p = p_Copy(h->next, currRing);
194    p = p_Mult_nn(p, nIntDiv(0, gcd), currRing);
195  }
196  return p;
197}
198
199poly ringNF(poly f, ideal G, ring r)
200{
201  // If f = 0, then normal form is also 0
202  if (f == NULL) { return NULL; }
203  poly tmp = NULL;
204  poly h = pCopy(f);
205  int i = findRingSolver(h, G, r);
206  int c = 1;
207  while (h != NULL && i >= 0) {
208//    Print("%d-step NF - h:", c);
209//    wrp(h);
210//    PrintS(" ");
211//    PrintS("G->m[i]:");
212//    wrp(G->m[i]);
213//    PrintLn();
214    tmp = h;
215    h = plain_spoly(h, G->m[i]);
216    pDelete(&tmp);
217//    PrintS("=> h=");
218//    wrp(h);
219//    PrintLn();
220    i = findRingSolver(h, G, r);
221    c++;
222  }
223  return h;
224}
225
226int testGB(ideal I, ideal GI) {
227  poly f, g, h, nf;
228  int i = 0;
229  int j = 0;
230  Print("I included?");
231  for (i = 0; i < IDELEMS(I); i++) {
232    if (ringNF(I->m[i], GI, currRing) != NULL) {
233      Print("Not reduced to zero from I: ");
234      wrp(I->m[i]);
235      Print(" --> ");
236      wrp(ringNF(I->m[i], GI, currRing));
237      PrintLn();
238      return(0);
239    }
240    Print("-");
241  }
242  Print(" Yes!\nspoly --> 0?");
243  for (i = 0; i < IDELEMS(GI); i++) {
244    for (j = i + 1; j < IDELEMS(GI); j++) {
245      f = pCopy(GI->m[i]);
246      g = pCopy(GI->m[j]);
247      h = plain_spoly(f, g);
248      nf = ringNF(h, GI, currRing);
249      if (nf != NULL) {
250        Print("spoly(");
251        wrp(GI->m[i]);
252        Print(", ");
253        wrp(GI->m[j]);
254        Print(") = ");
255        wrp(h);
256        Print(" --> ");
257        wrp(nf);
258        PrintLn();
259        return(0);
260      }
261      pDelete(&f);
262      pDelete(&g);
263      pDelete(&h);
264      pDelete(&nf);
265      Print("-");
266    }
267  }
268  if (!(rField_is_Domain()))
269  {
270    Print(" Yes!\nzero-spoly --> 0?");
271    for (i = 0; i < IDELEMS(GI); i++)
272    {
273      f = plain_zero_spoly(GI->m[i]);
274      nf = ringNF(f, GI, currRing);
275      if (nf != NULL) {
276        Print("spoly(");
277        wrp(GI->m[i]);
278        Print(", ");
279        wrp(0);
280        Print(") = ");
281        wrp(h);
282        Print(" --> ");
283        wrp(nf);
284        PrintLn();
285        return(0);
286      }
287      pDelete(&f);
288      pDelete(&nf);
289      Print("-");
290    }
291  }
292  Print(" Yes!");
293  PrintLn();
294  return(1);
295}
296
297#endif
Note: See TracBrowser for help on using the repository browser.