source: git/kernel/mpr_numeric.cc @ 68349d

spielwiese
Last change on this file since 68349d was 35aab3, checked in by Hans Schönemann <hannes@…>, 21 years ago
This commit was generated by cvs2svn to compensate for changes in r6879, which included commits to RCS files with non-trunk default branches. git-svn-id: file:///usr/local/Singular/svn/trunk@6880 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 29.9 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4
5/* $Id: mpr_numeric.cc,v 1.1.1.1 2003-10-06 12:15:56 Singular Exp $ */
6
7/*
8* ABSTRACT - multipolynomial resultants - numeric stuff
9*            ( root finder, vandermonde system solver, simplex )
10*/
11
12#include "mod2.h"
13//#ifdef HAVE_MPR
14
15//#define mprDEBUG_ALL
16
17//-> includes
18#include "structs.h"
19#include "febase.h"
20#include "omalloc.h"
21#include "numbers.h"
22#include "polys.h"
23#include "ideals.h"
24#include "intvec.h"
25#include "matpol.h"
26#include "ring.h"
27//#include "longrat.h"
28
29#include <math.h>
30#include "mpr_numeric.h"
31
32extern size_t gmp_output_digits;
33//<-
34
35extern void nPrint(number n);  // for debugging output
36
37//-----------------------------------------------------------------------------
38//-------------- vandermonde system solver ------------------------------------
39//-----------------------------------------------------------------------------
40
41//-> vandermonde::*
42vandermonde::vandermonde( const long _cn, const long _n, const long _maxdeg,
43                          number *_p, const bool _homog )
44  : n(_n), cn(_cn), maxdeg(_maxdeg), p(_p), homog(_homog)
45{
46  long j;
47  l= (long)pow((double)maxdeg+1,(int)n);
48  x= (number *)omAlloc( cn * sizeof(number) );
49  for ( j= 0; j < cn; j++ ) x[j]= nInit(1);
50  init();
51}
52
53vandermonde::~vandermonde()
54{
55  int j;
56  for ( j= 0; j < cn; j++ ) nDelete( x+j );
57  omFreeSize( (ADDRESS)x, cn * sizeof( number ) );
58}
59
60void vandermonde::init()
61{
62  int j;
63  long i,c,sum;
64  number tmp,tmp1;
65
66  c=0;
67  sum=0;
68
69  intvec exp( n );
70  for ( j= 0; j < n; j++ ) exp[j]=0;
71
72  for ( i= 0; i < l; i++ )
73  {
74    if ( !homog || (sum == maxdeg) )
75    {
76      for ( j= 0; j < n; j++ )
77      {
78        nPower( p[j], exp[j], &tmp );
79        tmp1 = nMult( tmp, x[c] );
80        x[c]= tmp1;
81        nDelete( &tmp );
82      }
83      c++;
84    }
85    exp[0]++;
86    sum=0;
87    for ( j= 0; j < n - 1; j++ )
88    {
89      if ( exp[j] > maxdeg )
90      {
91        exp[j]= 0;
92        exp[j + 1]++;
93        }
94      sum+= exp[j];
95    }
96    sum+= exp[n - 1];
97  }
98}
99
100poly vandermonde::numvec2poly( const number * q )
101{
102  int j;
103  long i,c,sum;
104  number tmp;
105
106  poly pnew,pit=NULL;
107
108  c=0;
109  sum=0;
110
111  int *exp= (int *) omAlloc( (n+1) * sizeof(int) );
112
113  for ( j= 0; j < n+1; j++ ) exp[j]=0;
114
115  for ( i= 0; i < l; i++ )
116  {
117    if ( (!homog || (sum == maxdeg)) && q[i] && !nIsZero(q[i]) )
118    {
119      pnew= pOne();
120      pSetCoeff(pnew,q[i]);
121      pSetExpV(pnew,exp);
122      if ( pit )
123      {
124        pNext(pnew)= pit;
125        pit= pnew;
126      }
127      else
128      {
129        pit= pnew;
130        pNext(pnew)= NULL;
131      }
132      pSetm(pit);
133    }
134    exp[1]++;
135    sum=0;
136    for ( j= 1; j < n; j++ )
137    {
138      if ( exp[j] > maxdeg )
139      {
140        exp[j]= 0;
141        exp[j + 1]++;
142        }
143      sum+= exp[j];
144    }
145    sum+= exp[n];
146  }
147
148  omFreeSize( (ADDRESS) exp, (n+1) * sizeof(int) );
149
150  pSortAdd(pit);
151  return pit;
152}
153
154number * vandermonde::interpolateDense( const number * q )
155{
156  int i,j,k;
157  number newnum,tmp1;
158  number b,t,xx,s;
159  number *c;
160  number *w;
161
162  b=t=xx=s=tmp1=NULL;
163
164  w= (number *)omAlloc( cn * sizeof(number) );
165  c= (number *)omAlloc( cn * sizeof(number) );
166  for ( j= 0; j < cn; j++ )
167  {
168    w[j]= nInit(0);
169    c[j]= nInit(0);
170  }
171
172  if ( cn == 1 )
173  {
174    nDelete( &w[0] );
175    w[0]= nCopy(q[0]);
176  }
177  else
178  {
179    nDelete( &c[cn-1] );
180    c[cn-1]= nCopy(x[0]);
181    c[cn-1]= nNeg(c[cn-1]);              // c[cn]= -x[1]
182
183    for ( i= 1; i < cn; i++ ) {              // i=2; i <= cn
184      nDelete( &xx );
185      xx= nCopy(x[i]);
186      xx= nNeg(xx);               // xx= -x[i]
187
188      for ( j= (cn-i-1); j <= (cn-2); j++) { // j=(cn+1-i); j <= (cn-1)
189        nDelete( &tmp1 );
190        tmp1= nMult( xx, c[j+1] );           // c[j]= c[j] + (xx * c[j+1])
191        newnum= nAdd( c[j], tmp1 );
192        nDelete( &c[j] );
193        c[j]= newnum;
194      }
195
196      newnum= nAdd( xx, c[cn-1] );           // c[cn-1]= c[cn-1] + xx
197      nDelete( &c[cn-1] );
198      c[cn-1]= newnum;
199    }
200
201    for ( i= 0; i < cn; i++ ) {              // i=1; i <= cn
202      nDelete( &xx );
203      xx= nCopy(x[i]);                     // xx= x[i]
204
205      nDelete( &t );
206      t= nInit( 1 );                         // t= b= 1
207      nDelete( &b );
208      b= nInit( 1 );
209      nDelete( &s );                         // s= q[cn-1]
210      s= nCopy( q[cn-1] );
211
212      for ( k= cn-1; k >= 1; k-- ) {         // k=cn; k >= 2
213        nDelete( &tmp1 );
214        tmp1= nMult( xx, b );                // b= c[k] + (xx * b)
215        nDelete( &b );
216        b= nAdd( c[k], tmp1 );
217
218        nDelete( &tmp1 );
219        tmp1= nMult( q[k-1], b );            // s= s + (q[k-1] * b)
220        newnum= nAdd( s, tmp1 );
221        nDelete( &s );
222        s= newnum;
223
224        nDelete( &tmp1 );
225        tmp1= nMult( xx, t );                // t= (t * xx) + b
226        newnum= nAdd( tmp1, b );
227        nDelete( &t );
228        t= newnum;
229      }
230
231      if (!nIsZero(t))
232      {
233        nDelete( &w[i] );                      // w[i]= s/t
234        w[i]= nDiv( s, t );
235        nNormalize( w[i] );
236      }
237
238      mprSTICKYPROT(ST_VANDER_STEP);
239    }
240  }
241  mprSTICKYPROT("\n");
242
243  // free mem
244  for ( j= 0; j < cn; j++ ) nDelete( c+j );
245  omFreeSize( (ADDRESS)c, cn * sizeof( number ) );
246
247  nDelete( &tmp1 );
248  nDelete( &s );
249  nDelete( &t );
250  nDelete( &b );
251  nDelete( &xx );
252
253  // makes quotiens smaller
254  for ( j= 0; j < cn; j++ ) nNormalize( w[j] );
255
256  return w;
257}
258//<-
259
260//-----------------------------------------------------------------------------
261//-------------- rootContainer ------------------------------------------------
262//-----------------------------------------------------------------------------
263
264//-> definitions
265#define MR       8        // never change this value
266#define MT      5
267#define MMOD    (MT*MR)
268#define MAXIT   (5*MMOD)   // max number of iterations in laguer root finder
269
270
271//-> rootContainer::rootContainer()
272rootContainer::rootContainer()
273{
274  rt=none;
275
276  coeffs= NULL;
277  ievpoint= NULL;
278  theroots= NULL;
279
280  found_roots= false;
281}
282//<-
283
284//-> rootContainer::~rootContainer()
285rootContainer::~rootContainer()
286{
287  int i;
288  int n= pVariables;
289
290  // free coeffs, ievpoint
291  if ( ievpoint != NULL )
292  {
293    for ( i=0; i < anz+2; i++ ) nDelete( ievpoint + i );
294    omFreeSize( (ADDRESS)ievpoint, (anz+2) * sizeof( number ) );
295  }
296
297  for ( i=0; i <= tdg; i++ ) nDelete( coeffs + i );
298  omFreeSize( (ADDRESS)coeffs, (tdg+1) * sizeof( number ) );
299
300  // theroots löschen
301  for ( i=0; i < tdg; i++ ) delete theroots[i];
302  omFreeSize( (ADDRESS) theroots, (tdg)*sizeof(gmp_complex*) );
303
304  mprPROTnl("~rootContainer()");
305}
306//<-
307
308//-> void rootContainer::fillContainer( ... )
309void rootContainer::fillContainer( number *_coeffs, number *_ievpoint,
310                                   const int _var, const int _tdg,
311                                   const rootType  _rt, const int _anz )
312{
313  int i;
314  number nn= nInit(0);
315  var=_var;
316  tdg=_tdg;
317  coeffs=_coeffs;
318  rt=_rt;
319  anz=_anz;
320
321  for ( i=0; i <= tdg; i++ )
322  {
323    if ( nEqual(coeffs[i],nn) )
324    {
325      nDelete( &coeffs[i] );
326      coeffs[i]=NULL;
327    }
328  }
329  nDelete( &nn );
330
331  if ( rt == cspecialmu && _ievpoint ) { // copy ievpoint
332    ievpoint= (number *)omAlloc( (anz+2) * sizeof( number ) );
333    for (i=0; i < anz+2; i++) ievpoint[i]= nCopy( _ievpoint[i] );
334  }
335
336  theroots= NULL;
337  found_roots= false;
338}
339//<-
340
341//-> poly rootContainer::getPoly()
342poly rootContainer::getPoly()
343{
344  int i;
345
346  poly result= NULL;
347  poly ppos;
348
349  if ( (rt == cspecial) || ( rt == cspecialmu ) )
350  {
351    for ( i= tdg; i >= 0; i-- )
352    {
353      if ( coeffs[i] )
354      {
355        poly p= pOne();
356        //pSetExp( p, var+1, i);
357        pSetExp( p, 1, i);
358        pSetCoeff( p, nCopy( coeffs[i] ) );
359        pSetm( p );
360        if (result)
361        {
362          ppos->next=p;
363          ppos=ppos->next;
364        }
365        else
366        {
367          result=p;
368          ppos=p;
369        }
370
371      }
372    }
373    pSetm( result );
374  }
375
376  return result;
377}
378//<-
379
380//-> const gmp_complex & rootContainer::opterator[] ( const int i )
381// this is now inline!
382//  gmp_complex & rootContainer::operator[] ( const int i )
383//  {
384//    if ( found_roots && ( i >= 0) && ( i < tdg ) )
385//    {
386//      return *theroots[i];
387//    }
388//    // warning
389//    Warn("rootContainer::getRoot: Wrong index %d, found_roots %s",i,found_roots?"true":"false");
390//    gmp_complex *tmp= new gmp_complex();
391//    return *tmp;
392//  }
393//<-
394
395//-> gmp_complex & rootContainer::evPointCoord( int i )
396gmp_complex & rootContainer::evPointCoord( const int i )
397{
398  if (! ((i >= 0) && (i < anz+2) ) )
399    WarnS("rootContainer::evPointCoord: index out of range");
400  if (ievpoint == NULL)
401    WarnS("rootContainer::evPointCoord: ievpoint == NULL");
402
403  if ( (rt == cspecialmu) && found_roots ) {  // FIX ME
404    if ( ievpoint[i] != NULL )
405    {
406      gmp_complex *tmp= new gmp_complex();
407      *tmp= numberToComplex(ievpoint[i]);
408      return *tmp;
409    }
410    else
411    {
412      Warn("rootContainer::evPointCoord: NULL index %d",i);
413    }
414  }
415
416  // warning
417  Warn("rootContainer::evPointCoord: Wrong index %d, found_roots %s",i,found_roots?"true":"false");
418  gmp_complex *tmp= new gmp_complex();
419  return *tmp;
420}
421//<-
422
423//-> bool rootContainer::changeRoots( int from, int to )
424bool rootContainer::swapRoots( const int from, const int to )
425{
426  if ( found_roots && ( from >= 0) && ( from < tdg ) && ( to >= 0) && ( to < tdg ) )
427  {
428    if ( to != from )
429    {
430      gmp_complex tmp( *theroots[from] );
431      *theroots[from]= *theroots[to];
432      *theroots[to]= tmp;
433    }
434    return true;
435  }
436
437  // warning
438  Warn(" rootContainer::changeRoots: Wrong index %d, %d",from,to);
439  return false;
440}
441//<-
442
443//-> void rootContainer::solver()
444bool rootContainer::solver( const int polishmode )
445{
446  int i;
447
448  // there are maximal tdg roots, so *roots ranges form 0 to tdg-1.
449  theroots= (gmp_complex**)omAlloc( tdg*sizeof(gmp_complex*) );
450  for ( i=0; i < tdg; i++ ) theroots[i]= new gmp_complex();
451
452  // copy the coefficients of type number to type gmp_complex
453  gmp_complex **ad= (gmp_complex**)omAlloc( (tdg+1)*sizeof(gmp_complex*) );
454  for ( i=0; i <= tdg; i++ )
455  {
456    ad[i]= new gmp_complex();
457    if ( coeffs[i] ) *ad[i] = numberToComplex( coeffs[i] );
458  }
459
460  // now solve
461  found_roots= laguer_driver( ad, theroots, polishmode != 0 );
462  if (!found_roots)
463    WarnS("rootContainer::solver: No roots found!");
464 
465  // free memory
466  for ( i=0; i <= tdg; i++ ) delete ad[i];
467  omFreeSize( (ADDRESS) ad, (tdg+1)*sizeof(gmp_complex*) );
468
469  return found_roots;
470}
471//<-
472
473//-> gmp_complex* rootContainer::laguer_driver( bool polish )
474bool rootContainer::laguer_driver(gmp_complex ** a, gmp_complex ** roots, bool polish )
475{
476  int i,j,k,its;
477  gmp_float zero(0.0);
478  gmp_complex x(0.0),o(1.0);
479  bool ret= true, isf=isfloat(a), type=true;
480
481  gmp_complex ** ad= (gmp_complex**)omAlloc( (tdg+1)*sizeof(gmp_complex*) );
482  for ( i=0; i <= tdg; i++ ) ad[i]= new gmp_complex( *a[i] );
483
484  k = 0;
485  i = tdg;
486  j = i-1;
487  while (i>2)
488  {
489    // run laguer alg
490    x = zero;
491    laguer(ad, i, &x, &its, type);
492    if ( its > MAXIT )
493    {
494      type = !type;
495      x = zero;
496      laguer(ad, i, &x, &its, type);
497    }
498
499    mprSTICKYPROT(ST_ROOTS_LGSTEP);
500    if ( its > MAXIT )
501    {  // error
502      WarnS("rootContainer::laguer_driver: To many iterations!");
503      ret= false;
504      goto theend;
505    }
506    if ( polish )
507    {
508      laguer( a, tdg, &x, &its , type);
509      if ( its > MAXIT )
510      {  // error
511        WarnS("rootContainer::laguer_driver: To many iterations in polish!");
512        ret= false;
513        goto theend;
514      }
515    }
516    if (!type) x = o/x;
517    if (x.imag() == zero)
518    {
519      *roots[k] = x;
520      k++;
521      divlin(ad,x,i);
522      i--;
523    }
524    else
525    {
526      if(isf)
527      {
528        *roots[j] = x;
529        *roots[j-1]= gmp_complex(x.real(),-x.imag());
530        j -= 2;
531        divquad(ad,x,i);
532        i -= 2;
533      }
534      else
535      {
536        *roots[j] = x;
537        j--;
538        divlin(ad,x,i);
539        i--;
540      }
541    }
542    type = !type;
543  }
544  solvequad(ad,roots,k,j);
545  sortroots(roots,k,j,isf);
546
547theend:
548  mprSTICKYPROT("\n");
549  for ( i=0; i <= tdg; i++ ) delete ad[i];
550  omFreeSize( (ADDRESS) ad, (tdg+1)*sizeof( gmp_complex* ));
551
552  return ret;
553}
554//<-
555
556//-> void rootContainer::laguer(...)
557void rootContainer::laguer(gmp_complex ** a, int m, gmp_complex *x, int *its, bool type)
558{
559  int iter,j;
560  gmp_float zero(0.0),one(1.0),deg(m);
561  gmp_float abx_g, err_g, fabs;
562  gmp_complex dx, x1, b, d, f, g, h, sq, gp, gm, g2;
563  gmp_float frac_g[MR+1] = { 0.0, 0.5, 0.25, 0.75, 0.125, 0.375, 0.625, 0.875, 1.0 };
564
565  gmp_float epss(0.1);
566  mpf_pow_ui(*epss._mpfp(),*epss.mpfp(),getGMPFloatDigits());
567
568  for ( iter= 1; iter <= MAXIT; iter++ )
569  {
570    mprSTICKYPROT(ST_ROOTS_LG);
571    *its=iter;
572    if (type)
573      computefx(a,*x,m,b,d,f,abx_g,err_g);
574    else
575      computegx(a,*x,m,b,d,f,abx_g,err_g);
576    err_g *= epss; // EPSS;
577
578    fabs = abs(b);
579    if (fabs <= err_g)
580    {
581      if ((fabs==zero) || (abs(d)==zero)) return;
582      *x -= (b/d); // a last newton-step
583      goto ende;
584    }
585
586    g= d / b;
587    g2 = g * g;
588    h= g2 - (((f+f) / b ));
589    sq= sqrt(( ( h * deg ) - g2 ) * (deg - one));
590    gp= g + sq;
591    gm= g - sq;
592    if (abs(gp)<abs(gm))
593    {
594      dx = deg/gm;
595    }
596    else
597    {
598      if((gp.real()==zero)&&(gp.imag()==zero))
599      {
600        dx.real(cos((mprfloat)iter));
601        dx.imag(sin((mprfloat)iter));
602        dx = dx*(one+abx_g);
603      }
604      else
605      {
606        dx = deg/gp;
607      }
608    }
609    x1= *x - dx;
610
611    if (*x == x1) goto ende;
612
613    j = iter%MMOD;
614    if (j==0) j=MT;
615    if ( j % MT ) *x= x1;
616    else *x -= ( dx * frac_g[ j / MT ] );
617  }
618
619  *its= MAXIT+1;
620ende:
621  checkimag(x,epss);
622}
623
624void rootContainer::checkimag(gmp_complex *x, gmp_float &e)
625{
626  if(abs(x->imag())<abs(x->real())*e)
627  {
628    x->imag(0.0);
629  }
630}
631
632bool rootContainer::isfloat(gmp_complex **a)
633{
634  gmp_float z(0.0);
635  gmp_complex *b;
636  for (int i=tdg; i >= 0; i-- )
637  {
638    b = &(*a[i]);
639    if (!(b->imag()==z))
640      return false;
641  }
642  return true;
643}
644
645void rootContainer::divlin(gmp_complex **a, gmp_complex x, int j)
646{
647  int i;
648  gmp_float o(1.0);
649
650  if (abs(x)<o)
651  {
652    for (i= j-1; i > 0; i-- )
653      *a[i] += (*a[i+1]*x);
654    for (i= 0; i < j; i++ )
655      *a[i] = *a[i+1];
656  }
657  else
658  {
659    gmp_complex y(o/x);
660    for (i= 1; i < j; i++)
661      *a[i] += (*a[i-1]*y);
662  }
663}
664
665void rootContainer::divquad(gmp_complex **a, gmp_complex x, int j)
666{
667  int i;
668  gmp_float o(1.0),p(x.real()+x.real()),
669            q((x.real()*x.real())+(x.imag()*x.imag()));
670
671  if (abs(x)<o)
672  {
673    *a[j-1] += (*a[j]*p);
674    for (i= j-2; i > 1; i-- )
675      *a[i] += ((*a[i+1]*p)-(*a[i+2]*q));
676    for (i= 0; i < j-1; i++ )
677      *a[i] = *a[i+2];
678  }
679  else
680  {
681    p = p/q;
682    q = o/q;
683    *a[1] += (*a[0]*p);
684    for (i= 2; i < j-1; i++)
685      *a[i] += ((*a[i-1]*p)-(*a[i-2]*q));
686  }
687}
688
689void rootContainer::solvequad(gmp_complex **a, gmp_complex **r, int &k, int &j)
690{
691  gmp_float zero(0.0);
692
693  if (j>k)
694  {
695    gmp_complex sq(zero);
696    gmp_complex h1(*a[1]/(*a[2] + *a[2])), h2(*a[0] / *a[2]);
697    gmp_complex disk((h1 * h1) - h2);
698    if (disk.imag()==zero)
699    {
700      if (disk.real()<zero)
701      {
702        sq.real(zero);
703        sq.imag(sqrt(-disk.real()));
704      }
705      else
706        sq = (gmp_complex)sqrt(disk.real());
707    }
708    else
709      sq = sqrt(disk);
710    *r[k+1] = sq - h1;
711    sq += h1;
712    *r[k] = (gmp_complex)0.0-sq;
713    if(sq.imag()==zero)
714    {
715      k = j;
716      j++;
717    }
718    else
719    {
720      j = k;
721      k--;
722    }
723  }
724  else
725  {
726    *r[k]= (gmp_complex)0.0-(*a[0] / *a[1]);
727    if(r[k]->imag()==zero)
728      j++;
729    else
730      k--;
731  }
732}
733
734void rootContainer::sortroots(gmp_complex **ro, int r, int c, bool isf)
735{
736  int j;
737
738  for (j=0; j<r; j++) // the real roots
739    sortre(ro, j, r, 1);
740  if (c>=tdg) return;
741  if (isf)
742  {
743    for (j=c; j+2<tdg; j+=2) // the complex roots for a real poly
744      sortre(ro, j, tdg-1, 2);
745  }
746  else
747  {
748    for (j=c; j+1<tdg; j++) // the complex roots for a general poly
749      sortre(ro, j, tdg-1, 1);
750  }
751}
752
753void rootContainer::sortre(gmp_complex **r, int l, int u, int inc)
754{
755  int pos,i;
756  gmp_complex *x,*y;
757
758  pos = l;
759  x = r[pos];
760  for (i=l+inc; i<=u; i+=inc)
761  {
762    if (r[i]->real()<x->real())
763    {
764      pos = i;
765      x = r[pos];
766    }
767  }
768  if (pos>l)
769  {
770    if (inc==1)
771    {
772      for (i=pos; i>l; i--)
773        r[i] = r[i-1];
774      r[l] = x;
775    }
776    else
777    {
778      y = r[pos+1];
779      for (i=pos+1; i+1>l; i--)
780        r[i] = r[i-2];
781      if (x->imag()>y->imag())
782      {
783        r[l] = x;
784        r[l+1] = y;
785      }
786      else
787      {
788        r[l] = y;
789        r[l+1] = x;
790      }
791    }
792  }
793  else if ((inc==2)&&(x->imag()<r[l+1]->imag()))
794  {
795    r[l] = r[l+1];
796    r[l+1] = x;
797  }
798}
799
800void rootContainer::computefx(gmp_complex **a, gmp_complex x, int m,
801                  gmp_complex &f0, gmp_complex &f1, gmp_complex &f2,
802                  gmp_float &ex, gmp_float &ef)
803{
804  int k;
805
806  f0= *a[m];
807  ef= abs(f0);
808  f1= gmp_complex(0.0);
809  f2= f1;
810  ex= abs(x);
811
812  for ( k= m-1; k >= 0; k-- )
813  {
814    f2 = ( x * f2 ) + f1;
815    f1 = ( x * f1 ) + f0;
816    f0 = ( x * f0 ) + *a[k];
817    ef = abs( f0 ) + ( ex * ef );
818  }
819}
820
821void rootContainer::computegx(gmp_complex **a, gmp_complex x, int m,
822                  gmp_complex &f0, gmp_complex &f1, gmp_complex &f2,
823                  gmp_float &ex, gmp_float &ef)
824{
825  int k;
826
827  f0= *a[0];
828  ef= abs(f0);
829  f1= gmp_complex(0.0);
830  f2= f1;
831  ex= abs(x);
832
833  for ( k= 1; k <= m; k++ )
834  {
835    f2 = ( x * f2 ) + f1;
836    f1 = ( x * f1 ) + f0;
837    f0 = ( x * f0 ) + *a[k];
838    ef = abs( f0 ) + ( ex * ef );
839  }
840}
841
842//-----------------------------------------------------------------------------
843//-------------- rootArranger -------------------------------------------------
844//-----------------------------------------------------------------------------
845
846//-> rootArranger::rootArranger(...)
847rootArranger::rootArranger( rootContainer ** _roots,
848                            rootContainer ** _mu,
849                            const int _howclean )
850  : roots(_roots), mu(_mu), howclean(_howclean)
851{
852  found_roots=false;
853}
854//<-
855
856//-> void rootArranger::solve_all()
857void rootArranger::solve_all()
858{
859  int i;
860  found_roots= true;
861
862  // find roots of polys given by coeffs in roots
863  rc= roots[0]->getAnzElems();
864  for ( i= 0; i < rc; i++ )
865    if ( !roots[i]->solver( howclean ) )
866    {
867      found_roots= false;
868      return;
869    }
870  // find roots of polys given by coeffs in mu
871  mc= mu[0]->getAnzElems();
872  for ( i= 0; i < mc; i++ )
873    if ( ! mu[i]->solver( howclean ) )
874    {
875      found_roots= false;
876      return;
877    }
878}
879//<-
880
881//-> void rootArranger::arrange()
882void rootArranger::arrange()
883{
884  gmp_complex tmp,zwerg;
885  int anzm= mu[0]->getAnzElems();
886  int anzr= roots[0]->getAnzRoots();
887  int xkoord, r, rtest, xk, mtest;
888  bool found;
889  //gmp_complex mprec(1.0/pow(10,gmp_output_digits-5),1.0/pow(10,gmp_output_digits-5));
890  gmp_float mprec(1.0/pow(10.0,(int)(gmp_output_digits/3)));
891
892  for ( xkoord= 0; xkoord < anzm; xkoord++ ) {    // für x1,x2, x1,x2,x3, x1,x2,...,xn
893    for ( r= 0; r < anzr; r++ ) {                 // für jede Nullstelle
894      // (x1-koordinate) * evp[1] + (x2-koordinate) * evp[2] +
895      //                                  ... + (xkoord-koordinate) * evp[xkoord]
896      tmp= gmp_complex();
897      for ( xk =0; xk <= xkoord; xk++ )
898      {
899        tmp -= (*roots[xk])[r] * mu[xkoord]->evPointCoord(xk+1); //xk+1
900      }
901      found= false;
902      for ( rtest= r; rtest < anzr; rtest++ ) {   // für jede Nullstelle
903        zwerg = tmp - (*roots[xk])[rtest] * mu[xkoord]->evPointCoord(xk+1); // xk+1, xkoord+2
904        for ( mtest= 0; mtest < anzr; mtest++ )
905        {
906          //          if ( tmp == (*mu[xkoord])[mtest] )
907          //          {
908          if ( ((zwerg.real() <= (*mu[xkoord])[mtest].real() + mprec) &&
909                (zwerg.real() >= (*mu[xkoord])[mtest].real() - mprec)) &&
910               ((zwerg.imag() <= (*mu[xkoord])[mtest].imag() + mprec) &&
911                (zwerg.imag() >= (*mu[xkoord])[mtest].imag() - mprec)) )
912           {
913             roots[xk]->swapRoots( r, rtest );
914             found= true;
915             break;
916           }
917        }
918      } // rtest
919      if ( !found )
920      {
921        Warn("rootArranger::arrange: No match? coord %d, root %d.",xkoord,r);
922#ifdef mprDEBUG_PROT
923        WarnS("One of these ...");
924        for ( rtest= r; rtest < anzr; rtest++ )
925        {
926          tmp= gmp_complex();
927          for ( xk =0; xk <= xkoord; xk++ )
928          {
929            tmp-= (*roots[xk])[r] * mu[xkoord]->evPointCoord(xk+1);
930          }
931          tmp-= (*roots[xk])[rtest] * mu[xkoord]->evPointCoord(xk+1); // xkoord+2
932          Warn("  %s",complexToStr(tmp,gmp_output_digits+1),rtest);
933        }
934        WarnS(" ... must match to one of these:");
935        for ( mtest= 0; mtest < anzr; mtest++ )
936        {
937          Warn("                  %s",complexToStr((*mu[xkoord])[mtest],gmp_output_digits+1));
938        }
939#endif
940      }
941    } // r
942  } // xkoord
943}
944//<-
945
946//-----------------------------------------------------------------------------
947//-------------- simplex ----- ------------------------------------------------
948//-----------------------------------------------------------------------------
949
950//  #ifdef mprDEBUG_PROT
951//  #define error(a) a
952//  #else
953//  #define error(a)
954//  #endif
955
956#define error(a) a
957
958#define MAXPOINTS      1000
959
960//-> simplex::*
961//
962simplex::simplex( int rows, int cols )
963   : LiPM_cols(cols), LiPM_rows(rows)
964{
965  int i;
966
967  LiPM_rows=LiPM_rows+3;
968  LiPM_cols=LiPM_cols+2;
969
970  LiPM = (mprfloat **)omAlloc( LiPM_rows * sizeof(mprfloat *) );  // LP matrix
971  for( i= 0; i < LiPM_rows; i++ )
972  {
973    // Mem must be allocated aligned, also for type double!
974    LiPM[i] = (mprfloat *)omAlloc0Aligned( LiPM_cols * sizeof(mprfloat) );
975  }
976
977  iposv = (int *)omAlloc0( 2*LiPM_rows*sizeof(int) );
978  izrov = (int *)omAlloc0( 2*LiPM_rows*sizeof(int) );
979
980  m=n=m1=m2=m3=icase=0;
981
982#ifdef mprDEBUG_ALL
983  Print("LiPM size: %d, %d\n",LiPM_rows,LiPM_cols);
984#endif
985}
986
987simplex::~simplex()
988{
989  // clean up
990  int i;
991  for( i= 0; i < LiPM_rows; i++ )
992  {
993    omFreeSize( (ADDRESS) LiPM[i], LiPM_cols * sizeof(mprfloat) );
994  }
995  omFreeSize( (ADDRESS) LiPM, LiPM_rows * sizeof(mprfloat *) );
996
997  omFreeSize( (ADDRESS) iposv, 2*LiPM_rows*sizeof(int) );
998  omFreeSize( (ADDRESS) izrov, 2*LiPM_rows*sizeof(int) );
999}
1000
1001BOOLEAN simplex::mapFromMatrix( matrix m )
1002{
1003  int i,j;
1004//    if ( MATROWS( m ) > LiPM_rows ||  MATCOLS( m ) > LiPM_cols ) {
1005//      WarnS("");
1006//      return FALSE;
1007//    }
1008
1009  number coef;
1010  for ( i= 1; i <= MATROWS( m ); i++ )
1011  {
1012     for ( j= 1; j <= MATCOLS( m ); j++ )
1013     {
1014        if ( MATELEM(m,i,j) != NULL )
1015        {
1016           coef= pGetCoeff( MATELEM(m,i,j) );
1017           if ( coef != NULL && !nIsZero(coef) )
1018              LiPM[i][j]= (double)(*(gmp_float*)coef);
1019           //#ifdef mpr_DEBUG_PROT
1020           //Print("%f ",LiPM[i][j]);
1021           //#endif
1022        }
1023     }
1024     //     PrintLn();
1025  }
1026
1027  return TRUE;
1028}
1029
1030matrix simplex::mapToMatrix( matrix m )
1031{
1032  int i,j;
1033//    if ( MATROWS( m ) < LiPM_rows-3 ||  MATCOLS( m ) < LiPM_cols-2 ) {
1034//      WarnS("");
1035//      return NULL;
1036//    }
1037
1038//Print(" %d x %d\n",MATROWS( m ),MATCOLS( m ));
1039
1040  number coef;
1041  gmp_float * bla;
1042  for ( i= 1; i <= MATROWS( m ); i++ )
1043  {
1044    for ( j= 1; j <= MATCOLS( m ); j++ )
1045    {
1046       pDelete( &(MATELEM(m,i,j)) );
1047       MATELEM(m,i,j)= NULL;
1048//Print(" %3.0f ",LiPM[i][j]);
1049       if ( LiPM[i][j] != 0.0 )
1050       {
1051          bla= new gmp_float(LiPM[i][j]);
1052          coef= (number)bla;
1053          MATELEM(m,i,j)= pOne();
1054          pSetCoeff( MATELEM(m,i,j), coef );
1055       }
1056    }
1057//PrintLn();
1058  }
1059
1060  return m;
1061}
1062
1063intvec * simplex::posvToIV()
1064{
1065   int i;
1066   intvec * iv = new intvec( m );
1067   for ( i= 1; i <= m; i++ )
1068   {
1069      IMATELEM(*iv,i,1)= iposv[i];
1070   }
1071   return iv;
1072}
1073
1074intvec * simplex::zrovToIV()
1075{
1076   int i;
1077   intvec * iv = new intvec( n );
1078   for ( i= 1; i <= n; i++ )
1079   {
1080      IMATELEM(*iv,i,1)= izrov[i];
1081   }
1082   return iv;
1083}
1084
1085void simplex::compute()
1086{
1087  int i,ip,ir,is,k,kh,kp,m12,nl1,nl2;
1088  int *l1,*l2,*l3;
1089  mprfloat q1, bmax;
1090
1091  if ( m != (m1+m2+m3) )
1092  {
1093    // error: bad input
1094    error(WarnS("simplex::compute: Bad input constraint counts!");)
1095    icase=-2;
1096    return;
1097  }
1098
1099  l1= (int *) omAlloc0( (n+1) * sizeof(int) );
1100  l2= (int *) omAlloc0( (m+1) * sizeof(int) );
1101  l3= (int *) omAlloc0( (m+1) * sizeof(int) );
1102
1103  nl1= n;
1104  for ( k=1; k<=n; k++ ) l1[k]=izrov[k]=k;
1105  nl2=m;
1106  for ( i=1; i<=m; i++ )
1107  {
1108    if ( LiPM[i+1][1] < 0.0 )
1109    {
1110      // error: bad input
1111      error(WarnS("simplex::compute: Bad input tableau!");)
1112      error(Warn("simplex::compute: in input Matrix row %d, column 1, value %f",i+1,LiPM[i+1][1]);)
1113      icase=-2;
1114      // free mem l1,l2,l3;
1115      omFreeSize( (ADDRESS) l3, (m+1) * sizeof(int) );
1116      omFreeSize( (ADDRESS) l2, (m+1) * sizeof(int) );
1117      omFreeSize( (ADDRESS) l1, (n+1) * sizeof(int) );
1118      return;
1119    }
1120    l2[i]= i;
1121    iposv[i]= n+i;
1122  }
1123  for ( i=1; i<=m2; i++) l3[i]= 1;
1124  ir= 0;
1125  if (m2+m3)
1126  {
1127    ir=1;
1128    for ( k=1; k <= (n+1); k++ )
1129    {
1130      q1=0.0;
1131      for ( i=m1+1; i <= m; i++ ) q1+= LiPM[i+1][k];
1132      LiPM[m+2][k]= -q1;
1133    }
1134
1135    do
1136    {
1137      simp1(LiPM,m+1,l1,nl1,0,&kp,&bmax);
1138      if ( bmax <= SIMPLEX_EPS && LiPM[m+2][1] < -SIMPLEX_EPS )
1139      {
1140        icase= -1; // no solution found
1141        // free mem l1,l2,l3;
1142        omFreeSize( (ADDRESS) l3, (m+1) * sizeof(int) );
1143        omFreeSize( (ADDRESS) l2, (m+1) * sizeof(int) );
1144        omFreeSize( (ADDRESS) l1, (n+1) * sizeof(int) );
1145        return;
1146      }
1147      else if ( bmax <= SIMPLEX_EPS && LiPM[m+2][1] <= SIMPLEX_EPS )
1148      {
1149        m12= m1+m2+1;
1150        if ( m12 <= m )
1151        {
1152          for ( ip= m12; ip <= m; ip++ )
1153          {
1154            if ( iposv[ip] == (ip+n) )
1155            {
1156              simp1(LiPM,ip,l1,nl1,1,&kp,&bmax);
1157              if ( fabs(bmax) >= SIMPLEX_EPS)
1158                goto one;
1159            }
1160          }
1161        }
1162        ir= 0;
1163        --m12;
1164        if ( m1+1 <= m12 )
1165          for ( i=m1+1; i <= m12; i++ )
1166            if ( l3[i-m1] == 1 )
1167              for ( k=1; k <= n+1; k++ )
1168                LiPM[i+1][k] = -(LiPM[i+1][k]);
1169        break;
1170      }
1171      //#if DEBUG
1172      //print_bmat( a, m+2, n+3);
1173      //#endif
1174      simp2(LiPM,n,l2,nl2,&ip,kp,&q1);
1175      if ( ip == 0 )
1176      {
1177        icase = -1; // no solution found
1178        // free mem l1,l2,l3;
1179        omFreeSize( (ADDRESS) l3, (m+1) * sizeof(int) );
1180        omFreeSize( (ADDRESS) l2, (m+1) * sizeof(int) );
1181        omFreeSize( (ADDRESS) l1, (n+1) * sizeof(int) );
1182        return;
1183      }
1184    one: simp3(LiPM,m+1,n,ip,kp);
1185      // #if DEBUG
1186      // print_bmat(a,m+2,n+3);
1187      // #endif
1188      if ( iposv[ip] >= (n+m1+m2+1))
1189      {
1190        for ( k= 1; k <= nl1; k++ )
1191          if ( l1[k] == kp ) break;
1192        --nl1;
1193        for ( is=k; is <= nl1; is++ ) l1[is]= l1[is+1];
1194        ++(LiPM[m+2][kp+1]);
1195        for ( i= 1; i <= m+2; i++ ) LiPM[i][kp+1] = -(LiPM[i][kp+1]);
1196      }
1197      else
1198      {
1199        if ( iposv[ip] >= (n+m1+1) )
1200        {
1201          kh= iposv[ip]-m1-n;
1202          if ( l3[kh] )
1203          {
1204            l3[kh]= 0;
1205            ++(LiPM[m+2][kp+1]);
1206            for ( i=1; i<= m+2; i++ )
1207              LiPM[i][kp+1] = -(LiPM[i][kp+1]);
1208          }
1209        }
1210      }
1211      is= izrov[kp];
1212      izrov[kp]= iposv[ip];
1213      iposv[ip]= is;
1214    } while (ir);
1215  }
1216  /* end of phase 1, have feasible sol, now optimize it */
1217  loop
1218  {
1219    // #if DEBUG
1220    // print_bmat( a, m+1, n+5);
1221    // #endif
1222    simp1(LiPM,0,l1,nl1,0,&kp,&bmax);
1223    if (bmax <= /*SIMPLEX_EPS*/0.0)
1224    {
1225      icase=0; // finite solution found
1226      // free mem l1,l2,l3
1227      omFreeSize( (ADDRESS) l3, (m+1) * sizeof(int) );
1228      omFreeSize( (ADDRESS) l2, (m+1) * sizeof(int) );
1229      omFreeSize( (ADDRESS) l1, (n+1) * sizeof(int) );
1230      return;
1231    }
1232    simp2(LiPM,n,l2,nl2,&ip,kp,&q1);
1233    if (ip == 0)
1234    {
1235      //printf("Unbounded:");
1236      // #if DEBUG
1237      //       print_bmat( a, m+1, n+1);
1238      // #endif
1239      icase=1;                /* unbounded */
1240      // free mem
1241      omFreeSize( (ADDRESS) l3, (m+1) * sizeof(int) );
1242      omFreeSize( (ADDRESS) l2, (m+1) * sizeof(int) );
1243      omFreeSize( (ADDRESS) l1, (n+1) * sizeof(int) );
1244      return;
1245    }
1246    simp3(LiPM,m,n,ip,kp);
1247    is= izrov[kp];
1248    izrov[kp]= iposv[ip];
1249    iposv[ip]= is;
1250  }/*for ;;*/
1251}
1252
1253void simplex::simp1( mprfloat **a, int mm, int ll[], int nll, int iabf, int *kp, mprfloat *bmax )
1254{
1255  int k;
1256  mprfloat test;
1257
1258  if( nll <= 0)
1259  {                        /* init'tion: fixed */
1260    *bmax = 0.0;
1261    return;
1262  }
1263  *kp=ll[1];
1264  *bmax=a[mm+1][*kp+1];
1265  for (k=2;k<=nll;k++)
1266  {
1267    if (iabf == 0)
1268    {
1269      test=a[mm+1][ll[k]+1]-(*bmax);
1270      if (test > 0.0)
1271      {
1272        *bmax=a[mm+1][ll[k]+1];
1273        *kp=ll[k];
1274      }
1275    }
1276    else
1277    {                        /* abs values: have fixed it */
1278      test=fabs(a[mm+1][ll[k]+1])-fabs(*bmax);
1279      if (test > 0.0)
1280      {
1281        *bmax=a[mm+1][ll[k]+1];
1282        *kp=ll[k];
1283      }
1284    }
1285  }
1286}
1287
1288void simplex::simp2( mprfloat **a, int n, int l2[], int nl2, int *ip, int kp, mprfloat *q1 )
1289{
1290  int k,ii,i;
1291  mprfloat qp,q0,q;
1292
1293  *ip= 0;
1294  for ( i=1; i <= nl2; i++ )
1295  {
1296    if ( a[l2[i]+1][kp+1] < -SIMPLEX_EPS )
1297    {
1298      *q1= -a[l2[i]+1][1] / a[l2[i]+1][kp+1];
1299      *ip= l2[i];
1300      for ( i= i+1; i <= nl2; i++ )
1301      {
1302        ii= l2[i];
1303        if (a[ii+1][kp+1] < -SIMPLEX_EPS)
1304        {
1305          q= -a[ii+1][1] / a[ii+1][kp+1];
1306          if (q - *q1 < -SIMPLEX_EPS)
1307          {
1308            *ip=ii;
1309            *q1=q;
1310          }
1311          else if (q - *q1 < SIMPLEX_EPS)
1312          {
1313            for ( k=1; k<= n; k++ )
1314            {
1315              qp= -a[*ip+1][k+1]/a[*ip+1][kp+1];
1316              q0= -a[ii+1][k+1]/a[ii+1][kp+1];
1317              if ( q0 != qp ) break;
1318            }
1319            if ( q0 < qp ) *ip= ii;
1320          }
1321        }
1322      }
1323    }
1324  }
1325}
1326
1327void simplex::simp3( mprfloat **a, int i1, int k1, int ip, int kp )
1328{
1329  int kk,ii;
1330  mprfloat piv;
1331
1332  piv= 1.0 / a[ip+1][kp+1];
1333  for ( ii=1; ii <= i1+1; ii++ )
1334  {
1335    if ( ii -1 != ip )
1336    {
1337      a[ii][kp+1] *= piv;
1338      for ( kk=1; kk <= k1+1; kk++ )
1339        if ( kk-1 != kp )
1340          a[ii][kk] -= a[ip+1][kk] * a[ii][kp+1];
1341    }
1342  }
1343  for ( kk=1; kk<= k1+1; kk++ )
1344    if ( kk-1 != kp ) a[ip+1][kk] *= -piv;
1345  a[ip+1][kp+1]= piv;
1346}
1347//<-
1348
1349//-----------------------------------------------------------------------------
1350
1351//#endif // HAVE_MPR
1352
1353// local Variables: ***
1354// folded-file: t ***
1355// compile-command-1: "make installg" ***
1356// compile-command-2: "make install" ***
1357// End: ***
Note: See TracBrowser for help on using the repository browser.