source: git/Singular/dyn_modules/gfanlib/containsMonomial.cc @ e744d9

spielwiese
Last change on this file since e744d9 was e744d9, checked in by Yue Ren <ren@…>, 9 years ago
chg: status update 17.07.
  • Property mode set to 100644
File size: 5.7 KB
Line 
1#include <bbcone.h>
2#include <kernel/polys.h>
3#include <kernel/kstd1.h>
4#include <libpolys/polys/prCopy.h>
5
6#if 0
7// /***
8//  * Creates an int* representing the transposition of the last two variables
9//  **/
10// static inline int* createPermutationVectorForSaturation(static const ring &r)
11// {
12//   int* w = (int*) omAlloc0((rVar(r)+1)*sizeof(int));
13//   for (int i=1; i<=rVar(r)-2; i++)
14//     w[i] = i;
15//   w[rVar(r)-1] = rVar(r);
16//   w[rVar(r)] = rVar(r)-1;
17// }
18
19
20/***
21 * Creates an int* representing the permutation
22 * 1 -> 1, ..., i-1 -> i-1, i -> n, i+1 -> n-1, ... , n -> i
23 **/
24static inline int* createPermutationVectorForSaturation(const ring &r, const int i)
25{
26  int* sigma = (int*) omAlloc0((rVar(r)+1)*sizeof(int));
27  int j;
28  for (j=1; j<i; j++)
29    sigma[j] = j;
30  for (; j<=rVar(r); j++)
31    sigma[j] = rVar(r)-j+i;
32  return(sigma);
33}
34
35
36/***
37 * Changes the int* representing the permutation
38 * 1 -> 1, ..., i -> i, i+1 -> n, i+2 -> n-1, ... , n -> i+1
39 * to an int* representing the permutation
40 * 1 -> 1, ..., i-1 -> i-1, i -> n, i+1 -> n-1, ... , n -> i
41 **/
42static void changePermutationVectorForSaturation(int* sigma, const ring &r, const int i)
43{
44  for (int j=i; j<rVar(r); j++)
45    sigma[j] = rVar(r)-j+i;
46  sigma[rVar(r)] = i;
47}
48
49
50/***
51 * returns a ring in which the weights of the ring variables are permuted
52 * if handed over a poly in which the variables are permuted, this is basically
53 * as good as permuting the variables of the ring itself.
54 **/
55static ring permuteWeighstOfRingVariables(const ring &r, const int* const sigma)
56{
57  ring s = rCopy0(r);
58  for (int j=0; j<rVar(r); j++)
59  {
60    s->wvhdl[0][j] = r->wvhdl[0][sigma[j+1]];
61    s->wvhdl[1][j] = r->wvhdl[1][sigma[j+1]];
62  }
63  rComplete(s,1);
64  return s;
65}
66
67
68/***
69 * creates a ring s that is a copy of r except with ordering wp(w)
70 **/
71static inline ring createInitialRingForSaturation(const ring &r, const gfan::ZVector &w, bool &ok)
72{
73  assume(rVar(r) == (int) w.size());
74
75  ring s = rCopy0(r); int i;
76  for (i=0; s->order[i]; i++)
77    omFreeSize(s->wvhdl[i],rVar(r)*sizeof(int));
78  i++;
79  omFreeSize(s->order,i*sizeof(int));
80  s->order  = (int*) omAlloc0(3*sizeof(int));
81  omFreeSize(s->block0,i*sizeof(int));
82  s->block0 = (int*) omAlloc0(3*sizeof(int));
83  omFreeSize(s->block1,i*sizeof(int));
84  s->block1 = (int*) omAlloc0(3*sizeof(int));
85  omFreeSize(s->wvhdl,i*sizeof(int*));
86  s->wvhdl  = (int**) omAlloc0(3*sizeof(int*));
87
88  s->order[0]  = ringorder_wp;
89  s->block0[0] = 1;
90  s->block1[0] = rVar(r);
91  s->wvhdl[0]  = ZVectorToIntStar(w,ok);
92  s->order[1]=ringorder_C;
93
94  rComplete(s,1);
95  return s;
96}
97
98
99/***
100 * Given an weighted homogeneous ideal I with respect to weight w
101 * that in standard basis form with respect to the ordering ws(-w),
102 * derives the standard basis of I:<x_n>^\infty
103 * and returns a long k such that I:<x_n>^\infty=I:<x_n>^k
104 **/
105static long deriveStandardBasisOfSaturation(ideal &I, ring &r)
106{
107  long k=0, l; poly current;
108  for (int i=0; i<IDELEMS(I); i++)
109  {
110    current = I->m[i];
111    l = p_GetExp(current,rVar(r),r);
112    if (k<l) k=l;
113    while (current)
114    {
115      p_SubExp(current,rVar(r),l,r); p_Setm(current,r);
116      pIter(current);
117    }
118  }
119  return k;
120}
121
122
123/***
124 * Given a weighted homogeneous ideal I with respect to weight w
125 * with constant first element,
126 * returns NULL if I does not contain a monomial
127 * otherwise returns the monomial contained in I
128 **/
129poly checkForMonomialsViaStepwiseSaturation(const ideal &I, const gfan::ZVector &w)
130{
131  // assume(rField_is_Ring_Z(currRing));
132
133  // first we switch to the ground field currRing->cf / I->m[0]
134  ring r = rCopy0(currRing);
135  nKillChar(r->cf);
136  r->cf = nInitChar(n_Zp,(void*)(long)n_Int(p_GetCoeff(I->m[0],currRing),currRing->cf));
137  rComplete(r);
138
139  ideal J = id_Copy(I, currRing); poly cache; number temp;
140  for (int i=0; i<IDELEMS(I); i++)
141  {
142    cache = J->m[i];
143    while (cache)
144    {
145      // TODO: temp = npMapGMP(p_GetCoeff(cache,currRing),currRing->cf,r->cf);
146      p_SetCoeff(cache,temp,r); pIter(cache);
147    }
148  }
149
150  J = kStd(J,NULL,isHomog,NULL);
151
152  bool b = false;
153  ring s = createInitialRingForSaturation(currRing, w, b);
154  if (b)
155  {
156    WerrorS("containsMonomial: overflow in weight vector");
157    return NULL;
158  }
159
160  return NULL;
161}
162#endif
163
164
165poly checkForMonomialViaSuddenSaturation(const ideal I, const ring r)
166{
167  ring origin = currRing;
168  ideal M = idInit(1);
169  M->m[0] = p_Init(r);
170  for (int i=1; i<=rVar(r); i++)
171    p_SetExp(M->m[0],i,1,r);
172  p_SetCoeff(M->m[0],n_Init(1,r->cf),r);
173  p_Setm(M->m[0],r); p_Test(M->m[0],r);
174
175  ideal J = id_Copy(I,r); bool b; int k = 0;
176  if (currRing != r) rChangeCurrRing(r);
177  intvec* nullVector = NULL;
178  do
179  {
180    ideal Jstd = kStd(J,currQuotient,testHomog,&nullVector);
181    ideal JquotM = idQuot(Jstd,M,true,true); k++;
182    ideal JquotMredJ = kNF(JquotM,currQuotient,Jstd);
183    b = idIs0(JquotMredJ);
184    id_Delete(&Jstd,r); id_Delete(&J,r); J = JquotM;
185    id_Delete(&JquotMredJ,r);
186  } while (!b);
187
188  if (currRing != origin) rChangeCurrRing(origin);
189  poly monom = NULL;
190  if (id_IsConstant(J,r))
191  {
192    monom = p_Init(r);
193    for (int i=1; i<=rVar(r); i++)
194      p_SetExp(monom,i,k,r);
195    p_SetCoeff(monom,n_Init(1,r->cf),r);
196    p_Setm(monom,r);
197  }
198  id_Delete(&M,r);
199  id_Delete(&J,r);
200  return monom;
201}
202
203
204BOOLEAN checkForMonomial(leftv res, leftv args)
205{
206  leftv u = args;
207  if ((u != NULL) && (u->Typ() == IDEAL_CMD))
208  {
209    ideal I; poly monom;
210    omUpdateInfo();
211    Print("usedBytesBefore=%ld\n",om_Info.UsedBytes);
212    I = (ideal) u->CopyD();
213    monom = checkForMonomialViaSuddenSaturation(I,currRing);
214    id_Delete(&I,currRing);
215    p_Delete(&monom,currRing);
216    omUpdateInfo();
217    Print("usedBytesAfter=%ld\n",om_Info.UsedBytes);
218    I = (ideal) u->Data();
219    res->rtyp = POLY_CMD;
220    res->data = (char*) checkForMonomialViaSuddenSaturation(I,currRing);
221    return FALSE;
222  }
223  return TRUE;
224}
Note: See TracBrowser for help on using the repository browser.