source: git/libpolys/polys/weight.cc @ 117e00e

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