source: git/Singular/weight.cc @ 551fd7

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