source: git/Singular/weight.cc @ 84b0a1b

fieker-DuValspielwiese
Last change on this file since 84b0a1b was 111cfe, checked in by Hans Schönemann <hannes@…>, 19 years ago
*hannes: removed MWERKS git-svn-id: file:///usr/local/Singular/svn/trunk@8454 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 6.5 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: weight.cc,v 1.24 2005-07-26 17:06:59 Singular Exp $ */
5
6/*
7* ABSTRACT:
8*/
9
10#include <math.h>
11#include "mod2.h"
12#include "tok.h"
13#include "omalloc.h"
14#include "polys.h"
15#include "intvec.h"
16#include "febase.h"
17#include "ipid.h"
18#include "ipshell.h"
19#include "subexpr.h"
20#include "ideals.h"
21#include "weight.h"
22
23/*0 implementation*/
24extern "C" double (*wFunctional)(int *degw, int *lpol, int npol,
25       double *rel, double wx);
26extern "C" double wFunctionalMora(int *degw, int *lpol, int npol,
27       double *rel, double wx);
28extern "C" double wFunctionalBuch(int *degw, int *lpol, int npol,
29       double *rel, double wx);
30extern "C" void wAdd(int *A, int mons, int kn, int xx);
31extern "C" void wNorm(int *degw, int *lpol, int npol, double *rel);
32extern "C" void wFirstSearch(int *A, int *x, int mons,
33        int *lpol, int npol, double *rel, double *fopt);
34extern "C" void wSecondSearch(int *A, int *x, int *lpol,
35        int npol, int mons, double *rel, double *fk);
36extern "C" void wGcd(int *x, int n);
37extern double wNsqr;
38
39static void wDimensions(polyset s, int sl, int *lpol, int *npol, int *mons)
40{
41  int  i, i1, j, k;
42  poly p, q;
43
44  i1 = j = 0;
45  for (i = 0; i <= sl; i++)
46  {
47    p = s[i];
48    if (p!=NULL)
49    {
50      k = 1;
51      q = pNext(p);
52      while (q!=NULL)
53      {
54        k++;
55        q = pNext(q);
56      }
57      if (k > 1)
58      {
59        lpol[i1++] = k;
60        j += k;
61      }
62    }
63  }
64  *npol = i1;
65  *mons = j;
66}
67
68
69static void wInit(polyset s, int sl, int mons, int *A)
70{
71  int  n, a, i, j, *B, *C;
72  poly p, q;
73  int *pl;
74
75  B = A;
76  n = pVariables;
77  a = (n + 1) * sizeof(int);
78  pl = (int *)omAlloc(a);
79  for (i = 0; i <= sl; i++)
80  {
81    p = s[i];
82    if (p!=NULL)
83    {
84      q = pNext(p);
85      if (q!=NULL)
86      {
87        C = B;
88        B++;
89        pGetExpV(p, pl);
90        for (j = 0; j < n; j++)
91        {
92          *C = pl[j+1];
93          C += mons;
94        }
95      }
96      while (q!=NULL)
97      {
98        C = B;
99        B++;
100        pGetExpV(q, pl);
101        for (j = 0; j < n; j++)
102        {
103          *C = pl[j+1];
104          C += mons;
105        }
106        pIter(q);
107      }
108    }
109  }
110  omFreeSize((ADDRESS)pl, a);
111}
112
113static void wCall(polyset s, int sl, int *x)
114{
115  int  n, q, npol, mons, i;
116  int  *A, *xopt, *lpol, *degw;
117  double  f1, fx, eps, *rel;
118  void *adr;
119
120  n = pVariables;
121  lpol = (int * )omAlloc((sl + 1) * sizeof(int));
122  wDimensions(s, sl, lpol, &npol, &mons);
123  xopt = x + (n + 1);
124  for (i = n; i!=0; i--)
125    xopt[i] = 1;
126  if (mons==0)
127  {
128    omFreeSize((ADDRESS)lpol, (sl + 1) * sizeof(int));
129    return;
130  }
131  adr = (void * )omAllocAligned(npol * sizeof(double));
132  rel = (double*)adr;
133  q = (n + 1) * mons * sizeof(int);
134  A = (int * )omAlloc(q);
135  wInit(s, sl, mons, A);
136  degw = A + (n * mons);
137  memset(degw, 0, mons * sizeof(int));
138  for (i = n; i!=0; i--)
139    wAdd(A, mons, i, 1);
140  wNorm(degw, lpol, npol, rel);
141  f1 = (*wFunctional)(degw, lpol, npol, rel, (double)1.0);
142  if (TEST_OPT_PROT) Print("// %e\n",f1);
143  eps = f1;
144  fx = (double)2.0 * eps;
145  memset(x, 0, (n + 1) * sizeof(int));
146  wFirstSearch(A, x, mons, lpol, npol, rel, &fx);
147  if (TEST_OPT_PROT) Print("// %e\n",fx);
148  memcpy(x + 1, xopt + 1, n * sizeof(int));
149  memset(degw, 0, mons * sizeof(int));
150  for (i = n; i!=0; i--)
151  {
152    x[i] *= 16;
153    wAdd(A, mons, i, x[i]);
154  }
155  wSecondSearch(A, x, lpol, npol, mons, rel, &fx);
156  if (TEST_OPT_PROT) Print("// %e\n",fx);
157  if (fx >= eps)
158  {
159    for (i = n; i!=0; i--)
160      xopt[i] = 1;
161  }
162  else
163  {
164    wGcd(xopt, n);
165//    if (BTEST1(22))
166//    {
167//      f1 = fx + (double)0.1 * (f1 - fx);
168//      wSimple(x, n);
169//      memset(degw, 0, mons * sizeof(int));
170//      for (i = n; i!=0; i--)
171//        wAdd(A, mons, i, x[i]);
172//      eps = wPrWeight(x, n);
173//      fx = (*wFunctional)(degw, lpol, npol, rel, eps);
174//      if (fx < f1)
175//      {
176//        if (TEST_OPT_PROT) Print("// %e\n",fx);
177//        memcpy(xopt + 1, x + 1, n * sizeof(int));
178//      }
179//    }
180  }
181  omFreeSize((ADDRESS)A, q);
182  omFreeSize((ADDRESS)lpol, (sl + 1) * sizeof(int));
183  omFreeSize((ADDRESS)adr, npol * sizeof(double));
184}
185
186
187void kEcartWeights(polyset s, int sl, short *eweight)
188{
189  int  n, i;
190  int  *x;
191
192  *eweight = 0;
193  n = pVariables;
194  wNsqr = (double)2.0 / (double)n;
195  if (pOrdSgn == -1)
196    wFunctional = wFunctionalMora;
197  else
198    wFunctional = wFunctionalBuch;
199  x = (int * )omAlloc(2 * (n + 1) * sizeof(int));
200  wCall(s, sl, x);
201  for (i = n; i!=0; i--)
202    eweight[i] = x[i + n + 1];
203  omFreeSize((ADDRESS)x, 2 * (n + 1) * sizeof(int));
204}
205
206
207BOOLEAN kWeight(leftv res,leftv id)
208{
209  ideal F=(ideal)id->Data();
210  intvec * iv = new intvec(pVariables);
211  polyset s;
212  int  sl, n, i;
213  int  *x;
214
215  res->data=(char *)iv;
216  s = F->m;
217  sl = IDELEMS(F) - 1;
218  n = pVariables;
219  wNsqr = (double)2.0 / (double)n;
220  wFunctional = wFunctionalBuch;
221  x = (int * )omAlloc(2 * (n + 1) * sizeof(int));
222  wCall(s, sl, x);
223  for (i = n; i!=0; i--)
224    (*iv)[i-1] = x[i + n + 1];
225  omFreeSize((ADDRESS)x, 2 * (n + 1) * sizeof(int));
226  return FALSE;
227}
228
229BOOLEAN kQHWeight(leftv res,leftv v)
230{
231  res->data=(char *)idQHomWeight((ideal)v->Data());
232  if (res->data==NULL)
233    res->data=(char *)new intvec(pVariables);
234  return FALSE;
235}
236
237short * iv2array(intvec * iv)
238{
239  short *s=(short *)omAlloc0((pVariables+1)*sizeof(short));
240  int len=0;
241  if(iv!=NULL)
242    len=iv->length();
243  int i;
244  //for(i=pVariables;i>len;i--) s[i]=1;
245  for(i=len;i>0;i--)               s[i]=(*iv)[i-1];
246  return s;
247}
248
249/*2
250*computes the degree of the leading term of the polynomial
251*with respect to given ecartWeights
252*used for Graebes method if BTEST1(31) is set
253*/
254long totaldegreeWecart(poly p, ring r)
255{
256  int i;
257  long j =0;
258
259  for (i=r->N; i; i--)
260    j += (int)(p_GetExp(p,i,r) * ecartWeights[i]);
261  return  j;
262}
263
264/*2
265*computes the degree of the leading term of the polynomial
266*with respect to given weights
267*/
268long totaldegreeWecart_IV(poly p, ring r, const short *w)
269{
270  int i;
271  long j =0;
272
273  for (i=r->N; i; i--)
274    j += (int)(p_GetExp(p,i,r) * w[i]);
275  return  j;
276}
277
278/*2
279*computes the maximal degree of all terms of the polynomial
280*with respect to given ecartWeights and
281*computes the length of the polynomial
282*used for Graebes method if BTEST1(31) is set
283*/
284long maxdegreeWecart(poly p,int *l, ring r)
285{
286  short k=p_GetComp(p, r);
287  int ll=1;
288  long t,max;
289
290  max=totaldegreeWecart(p, r);
291  pIter(p);
292  while ((p!=NULL) && (p_GetComp(p, r)==k))
293  {
294    t=totaldegreeWecart(p, r);
295    if (t>max) max=t;
296    ll++;
297    pIter(p);
298  }
299  *l=ll;
300  return max;
301}
Note: See TracBrowser for help on using the repository browser.