source: git/kernel/ringgb.cc @ d0f98e

spielwiese
Last change on this file since d0f98e was 1e579c6, checked in by Oliver Wienand <wienand@…>, 17 years ago
kutil.cc: * use nExtGcd, nIsUnit * extended spolys only for non-domains numbers.cc, numbers.h, structs.h: * new Functions: nIsUnit, nGetUnit, nExtGcd * new Field: the Integers polys.cc, polys.h: * nGetUnit hack removed * minor memory glitch polys1.cc: * no pContent for Integers ring.h: new rField methos for Integers and p^n rintegers.*: implementation of the whole numbers using gmp rmodulo*: added funcs: nIsUnit, nGetUnit, nExtGcd git-svn-id: file:///usr/local/Singular/svn/trunk@10125 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.16 2007-06-20 09:39:25 wienand 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_RING2TOM
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  long test = (long) n;
43  int i = 0;
44  while (test%2 == 0) {
45    i++;
46    test = test / 2;
47  }
48  return i;
49}
50
51/***************************************************************
52 *
53 * Lcm business
54 *
55 ***************************************************************/
56// get m1 = LCM(LM(p1), LM(p2))/LM(p1)
57//     m2 = LCM(LM(p1), LM(p2))/LM(p2)
58BOOLEAN ring2toM_GetLeadTerms(const poly p1, const poly p2, const ring p_r,
59                               poly &m1, poly &m2, const ring m_r)
60{
61
62  int i;
63  Exponent_t x;
64  m1 = p_Init(m_r);
65  m2 = p_Init(m_r);
66
67  for (i = p_r->N; i; i--)
68  {
69    x = p_GetExpDiff(p1, p2, i, p_r);
70    if (x > 0)
71    {
72      p_SetExp(m2,i,x, m_r);
73      p_SetExp(m1,i,0, m_r);
74    }
75    else
76    {
77      p_SetExp(m1,i,-x, m_r);
78      p_SetExp(m2,i,0, m_r);
79    }
80  }
81  p_Setm(m1, m_r);
82  p_Setm(m2, m_r);
83  long cp1 = (long) pGetCoeff(p1);
84  long cp2 = (long) pGetCoeff(p2);
85  if (cp1 != 0 && cp2 != 0) {
86    while (cp1%2 == 0 && cp2%2 == 0) {
87      cp1 = cp1 / 2;
88      cp2 = cp2 / 2;
89    }
90  }
91  p_SetCoeff(m1, (number) cp2, m_r);
92  p_SetCoeff(m2, (number) cp1, m_r);
93  return TRUE;
94}
95
96void printPolyMsg(const char * start, poly f, const char * end)
97{
98  PrintS(start);
99  wrp(f);
100  PrintS(end);
101}
102
103poly spolyRing2toM(poly f, poly g, ring r) {
104  poly m1 = NULL;
105  poly m2 = NULL;
106  ring2toM_GetLeadTerms(f, g, r, m1, m2, r);
107  // printPolyMsg("spoly: m1=", m1, " | ");
108  // printPolyMsg("m2=", m2, "");
109  // PrintLn();
110  poly sp = pSub(p_Mult_mm(f, m1, r), pp_Mult_mm(g, m2, r));
111  pDelete(&m1);
112  pDelete(&m2);
113  return(sp);
114}
115
116poly ringRedNF (poly f, ideal G, ring r) {
117  // If f = 0, then normal form is also 0
118  if (f == NULL) { return NULL; }
119  poly h = NULL;
120  poly g = pCopy(f);
121  int c = 0;
122  while (g != NULL) {
123    Print("%d-step RedNF - g=", c);
124    wrp(g);
125    PrintS(" | h=");
126    wrp(h);
127    PrintLn();
128    g = ringNF(g, G, r);
129    if (g != NULL) {
130      h = pAdd(h, pHead(g));
131      pLmDelete(&g);
132    }
133    c++;
134  }
135  return h;
136}
137
138#endif
139
140#ifdef HAVE_RINGS
141
142/*
143 * Find an index i from G, such that
144 * LT(rside) = x * LT(G[i]) has a solution
145 * or -1 if rside is not in the
146 * ideal of the leading coefficients
147 * of the suitable g from G.
148 */
149int findRingSolver(poly rside, ideal G, ring r) {
150  if (rside == NULL) return -1;
151  int i;
152//  int iO2rside = indexOf2(pGetCoeff(rside));
153  for (i = 0; i < IDELEMS(G); i++) {
154    if // (indexOf2(pGetCoeff(G->m[i])) <= iO2rside &&    / should not be necessary any more
155       (p_LmDivisibleBy(G->m[i], rside, r)) {
156      return i;
157    }
158  }
159  return -1;
160}
161
162poly plain_spoly(poly f, poly g) {
163  number cf = nCopy(pGetCoeff(f)), cg = nCopy(pGetCoeff(g));
164  int ct = ksCheckCoeff(&cf, &cg); // gcd and zero divisors
165  poly fm, gm;
166  k_GetLeadTerms(f, g, currRing, fm, gm, currRing);
167  pSetCoeff0(fm, cg);
168  pSetCoeff0(gm, cf);  // and now, m1 * LT(p1) == m2 * LT(p2)
169  poly sp = pSub(ppMult_mm(f, fm), ppMult_mm(g, gm));
170  pDelete(&fm);
171  pDelete(&gm);
172  return(sp);
173}
174
175/*2
176* Generates spoly(0, h) if applicable. Assumes ring in Z/2^n.
177*/
178poly plain_zero_spoly(poly h)
179{
180  poly p = NULL;
181  number gcd = nGcd((number) 0, pGetCoeff(h), currRing);
182  if ((NATNUMBER) gcd > 1)
183  {
184    p = p_Copy(h->next, currRing);
185    p = p_Mult_nn(p, nIntDiv(0, gcd), currRing);
186  }
187  return p;
188}
189
190poly ringNF(poly f, ideal G, ring r) {
191  // If f = 0, then normal form is also 0
192  if (f == NULL) { return NULL; }
193  poly tmp = NULL;
194  poly h = pCopy(f);
195  int i = findRingSolver(h, G, r);
196  int c = 1;
197  while (h != NULL && i >= 0) {
198//    Print("%d-step NF - h:", c);
199//    wrp(h);
200//    PrintS(" ");
201//    PrintS("G->m[i]:");
202//    wrp(G->m[i]);
203//    PrintLn();
204    tmp = h;
205    h = plain_spoly(h, G->m[i]);
206    pDelete(&tmp);
207//    PrintS("=> h=");
208//    wrp(h);
209//    PrintLn();
210    i = findRingSolver(h, G, r);
211    c++;
212  }
213  return h;
214}
215
216int testGB(ideal I, ideal GI) {
217  poly f, g, h, nf;
218  int i = 0;
219  int j = 0;
220  Print("I included?");
221  for (i = 0; i < IDELEMS(I); i++) {
222    if (ringNF(I->m[i], GI, currRing) != NULL) {
223      Print("Not reduced to zero from I: ");
224      wrp(I->m[i]);
225      Print(" --> ");
226      wrp(ringNF(I->m[i], GI, currRing));
227      PrintLn();
228      return(0);
229    }
230    Print("-");
231  }
232  Print(" Yes!\nspoly --> 0?");
233  for (i = 0; i < IDELEMS(GI); i++) {
234    for (j = i + 1; j < IDELEMS(GI); j++) {
235      f = pCopy(GI->m[i]);
236      g = pCopy(GI->m[j]);
237      h = plain_spoly(f, g);
238      nf = ringNF(h, GI, currRing);
239      if (nf != NULL) {
240        Print("spoly(");
241        wrp(GI->m[i]);
242        Print(", ");
243        wrp(GI->m[j]);
244        Print(") = ");
245        wrp(h);
246        Print(" --> ");
247        wrp(nf);
248        PrintLn();
249        return(0);
250      }
251      pDelete(&f);
252      pDelete(&g);
253      pDelete(&h);
254      pDelete(&nf);
255      Print("-");
256    }
257  }
258  if (!(rField_is_Domain()))
259  {
260    Print(" Yes!\nzero-spoly --> 0?");
261    for (i = 0; i < IDELEMS(GI); i++) 
262    {
263      f = plain_zero_spoly(GI->m[i]);
264      nf = ringNF(f, GI, currRing);
265      if (nf != NULL) {
266        Print("spoly(");
267        wrp(GI->m[i]);
268        Print(", ");
269        wrp(0);
270        Print(") = ");
271        wrp(h);
272        Print(" --> ");
273        wrp(nf);
274        PrintLn();
275        return(0);
276      }
277      pDelete(&f);
278      pDelete(&nf);
279      Print("-");
280    }
281  }
282  Print(" Yes!");
283  PrintLn();
284  return(1);
285}
286
287#endif
Note: See TracBrowser for help on using the repository browser.