source: git/kernel/weight.cc @ 111cfe

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