Xmipp  v3.23.11-Nereus
Public Member Functions | Public Attributes | Protected Member Functions | List of all members
InterPolynomial Class Reference

#include <IntPoly.h>

Inheritance diagram for InterPolynomial:
Inheritance graph
[legend]
Collaboration diagram for InterPolynomial:
Collaboration graph
[legend]

Public Member Functions

double * NewtonCoefficient (double *)
 
void ComputeLagrangeBasis (double *, unsigned nPtsTotal)
 
void GenerateBasis (double rho, double rhosmall, Matrix data, ObjectiveFunction *of)
 
 InterPolynomial (unsigned _deg, double rho, Vector vBase, Matrix data, ObjectiveFunction *of)
 
int findAGoodPointToReplace (int excludeFromT, double rho, Vector pointToAdd, double *modelStep=NULL)
 
void replace (int t, Vector pointToAdd, double valueF)
 
int maybeAdd (Vector pointToAdd, unsigned k, double rho, double valueF)
 
void updateM (Vector newPoint, double valueF)
 
int checkIfValidityIsInBound (Vector dd, unsigned k, double bound, double rho)
 
int getGoodInterPolationSites (Matrix d, int k, double rho, Vector *v=NULL)
 
double interpError (Vector Point)
 
void translate (int k)
 
void translate (Vector translation)
 
 ~InterPolynomial ()
 
 InterPolynomial (const InterPolynomial &A)
 
InterPolynomialoperator= (const InterPolynomial &A)
 
InterPolynomial clone ()
 
void copyFrom (InterPolynomial a)
 
void copyFrom (Polynomial a)
 
 InterPolynomial (unsigned dim, unsigned deg)
 
- Public Member Functions inherited from Polynomial
 Polynomial ()
 
 Polynomial (unsigned Dim, unsigned deg=0, double *data=0)
 
 Polynomial (unsigned Dim, double val)
 
 Polynomial (MultInd &)
 
 Polynomial (char *name)
 
unsigned dim ()
 
unsigned deg ()
 
unsigned sz ()
 
 operator double * () const
 
 ~Polynomial ()
 
 Polynomial (const Polynomial &A)
 
Polynomialoperator= (const Polynomial &A)
 
Polynomial clone ()
 
void copyFrom (Polynomial a)
 
Polynomial operator* (const double)
 
Polynomial operator/ (const double)
 
Polynomial operator+ (Polynomial)
 
Polynomial operator- (Polynomial)
 
Polynomial operator- (void)
 
Polynomial operator+ (void)
 
Polynomial operator+= (Polynomial)
 
Polynomial operator-= (Polynomial)
 
Polynomial operator*= (const double)
 
Polynomial operator/= (const double)
 
double shiftedEval (Vector Point, double minusVal)
 
double operator() (Vector)
 
Polynomial derivate (int i)
 
void gradient (Vector P, Vector G)
 
void gradientHessian (Vector P, Vector G, Matrix H)
 
void translate (Vector translation)
 
int operator== (const Polynomial q)
 
int equals (Polynomial q)
 
void print ()
 
void save (char *name)
 
void setFlag (unsigned int val)
 
void unsetFlag (unsigned int val)
 
unsigned queryFlag (unsigned int val)
 

Public Attributes

double M
 
unsigned nPtsUsed
 
unsigned nUpdateOfM
 
PolynomialNewtonBasis
 
VectorNewtonPoints
 
Vector vBase
 
double * valuesF
 
int kbest
 

Protected Member Functions

void destroyCurrentBuffer ()
 
- Protected Member Functions inherited from Polynomial
void init (int _dim, int _deg, double *data=NULL)
 
void destroyCurrentBuffer ()
 

Additional Inherited Members

- Static Public Attributes inherited from Polynomial
static const unsigned int NicePrint = 1
 
static const unsigned int Warning = 2
 
static const unsigned int Normalized = 4
 
static unsigned int flags = Polynomial::Warning||Polynomial::NicePrint
 
static Polynomial emptyPolynomial
 
- Protected Types inherited from Polynomial
typedef struct Polynomial::PolynomialDataTag PolynomialData
 
- Protected Attributes inherited from Polynomial
PolynomialDatad
 

Detailed Description

Definition at line 35 of file IntPoly.h.

Constructor & Destructor Documentation

◆ InterPolynomial() [1/3]

InterPolynomial::InterPolynomial ( unsigned  _deg,
double  rho,
Vector  vBase,
Matrix  data,
ObjectiveFunction of 
)

Definition at line 228 of file IntPoly.cpp.

230  : Polynomial(data.nColumn()-2, _deg), M(0.0), nUpdateOfM(0), NewtonBasis(NULL), NewtonPoints(NULL), kbest(-1)
231 {
232  nPtsUsed=choose( _deg+d->dim,d->dim );
233  if (data.nLine()==0)
234  {
235  printf( "InterPolynomial::InterPolynomial( double *) : No data\n");
236  getchar(); exit(-1);
237  }
238 
239  // Generate basis
240  if (_vBase==Vector::emptyVector)
241  {
242  double rhosmall=rho*ROOT2*.5;
243  GenerateBasis(rho,rhosmall, data,NULL);
244  if (!NewtonBasis)
245  {
246  GenerateBasis(rho,rho*.5, data,of);
247  }
248  } else
249  {
250  int i,ii,j=0,k,mdim=dim();
251  double norm, *p, *pb=_vBase;
252  KeepBests kb(nPtsUsed*2,mdim);
253 // fprintf(stderr,"Value Objective=%e\n", vBase);
254 
255  ii=data.lineIndex(_vBase);
256  if (ii!=-1)
257  {
258  p=data[ii];
259  kb.add(0.0,p[mdim],p); j++;
260  }
261 
262  i=data.nLine();
263  while (i--)
264  {
265  if (i==ii) continue;
266  p=data[i];
267  if (p[mdim+1]) continue;
268  norm=0; k=mdim;
269  while (k--) norm+=sqr(p[k]-pb[k]);
270  if ((rho==0.0)||
271  ((norm<=4.00*rho*rho))//&&(norm>.25*rhomin*rhomin))
272  )
273  {
274  kb.add(norm,p[mdim], p); j++;
275  }
276  }
277  if (j<(int)nPtsUsed)
278  {
279  printf("Not Enough points in DB to build interpolation polynomial\n");
280  return;
281  }
282  // we have retained only the 2*nPtsUsed best points:
283  j=mmin(j,(int)(2*nPtsUsed));
284  NewtonPoints=new Vector[j];
285  valuesF=(double*)malloc(j*sizeof(double));
286  for (i=0; i<j; i++)
287  {
288  valuesF[i]=kb.getValue(i);
289  NewtonPoints[i]=Vector(mdim, kb.getOptValue(i));
290  }
291  //kbest=findK(valuesF,j,NULL,NewtonPoints);
292  //vBase=NewtonPoints[kbest].clone();
293  vBase=_vBase;
294  for (i=0; i<j; i++) NewtonPoints[i]-=vBase;
296  }
297  if (NewtonBasis==NULL) return;
298 
299 // test();
300 
301  // Compute Interpolant
302 // double *NewtonCoefPoly=NewtonCoefficient(_Yp);
303  double *NewtonCoefPoly=valuesF;
304 
305  double *coc= NewtonCoefPoly+nPtsUsed-1;
306  Polynomial *ppc= NewtonBasis+nPtsUsed-1;
307  this->copyFrom((Polynomial)(*coc * *ppc)); //take highest degree
308 
309  int i=nPtsUsed-1;
310  if (i)
311  while(i--)
312  (*this) += *(--coc) * *(--ppc);
313  // No reallocation here because of the order of
314  // the summation
315  //free(NewtonCoefPoly);
316 }
#define ROOT2
Definition: tools.h:45
unsigned long choose(unsigned n, unsigned k)
Definition: tools.cpp:38
Vector * NewtonPoints
Definition: IntPoly.h:47
void GenerateBasis(double rho, double rhosmall, Matrix data, ObjectiveFunction *of)
Definition: IntPoly.cpp:324
double rho
Definition: Vector.h:37
unsigned dim()
Definition: Poly.h:62
double mmin(const double t1, const double t2)
Definition: tools.h:69
T norm(const std::vector< T > &v)
Definition: vector_ops.h:399
int nColumn()
Definition: Matrix.h:84
#define i
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
int nLine()
Definition: Matrix.h:83
double sqr(const double &t)
Definition: tools.h:99
void ComputeLagrangeBasis(double *, unsigned nPtsTotal)
Definition: IntPoly.cpp:127
int lineIndex(Vector r, int nn=0)
Definition: Matrix.cpp:1168
unsigned nUpdateOfM
Definition: IntPoly.h:40
double * valuesF
Definition: IntPoly.h:48
void copyFrom(InterPolynomial a)
Definition: IntPoly.cpp:876
#define j
PolynomialData * d
Definition: Poly.h:49
unsigned nPtsUsed
Definition: IntPoly.h:40
static Vector emptyVector
Definition: Vector.h:119
double M
Definition: IntPoly.h:39
Polynomial * NewtonBasis
Definition: IntPoly.h:43
Polynomial()
Definition: Poly.h:54

◆ ~InterPolynomial()

InterPolynomial::~InterPolynomial ( )

Definition at line 819 of file IntPoly.cpp.

820 {
822 }
void destroyCurrentBuffer()
Definition: IntPoly.cpp:805

◆ InterPolynomial() [2/3]

InterPolynomial::InterPolynomial ( const InterPolynomial A)

Definition at line 824 of file IntPoly.cpp.

825 {
826  // shallow copy for inter poly.
827  d=A.d;
830 // ValuesF=A.ValuesF;
831  M=A.M;
832  nPtsUsed=A.nPtsUsed;
834  (d->ref_count)++;
835 
836 }
Vector * NewtonPoints
Definition: IntPoly.h:47
unsigned nUpdateOfM
Definition: IntPoly.h:40
PolynomialData * d
Definition: Poly.h:49
unsigned nPtsUsed
Definition: IntPoly.h:40
double M
Definition: IntPoly.h:39
Polynomial * NewtonBasis
Definition: IntPoly.h:43

◆ InterPolynomial() [3/3]

InterPolynomial::InterPolynomial ( unsigned  dim,
unsigned  deg 
)

Definition at line 861 of file IntPoly.cpp.

861  : Polynomial(_dim, _deg), M(0.0), nUpdateOfM(0)
862 {
863  nPtsUsed=choose( _deg+_dim,_dim );
866 }
unsigned long choose(unsigned n, unsigned k)
Definition: tools.cpp:38
Vector * NewtonPoints
Definition: IntPoly.h:47
Definition: Vector.h:37
unsigned nUpdateOfM
Definition: IntPoly.h:40
unsigned nPtsUsed
Definition: IntPoly.h:40
double M
Definition: IntPoly.h:39
Polynomial * NewtonBasis
Definition: IntPoly.h:43
Polynomial()
Definition: Poly.h:54

Member Function Documentation

◆ checkIfValidityIsInBound()

int InterPolynomial::checkIfValidityIsInBound ( Vector  dd,
unsigned  k,
double  bound,
double  rho 
)

Definition at line 684 of file IntPoly.cpp.

687 {
688 
689  // check validity around x_k
690  // bound is epsilon in the paper
691  // return index of the worst point of J
692  // if (j==-1) then everything OK : next : trust region step
693  // else model step: replace x_j by x_k+d where d
694  // is calculated with LAGMAX
695 
696  unsigned i,N=nPtsUsed, n=dim();
697  int j;
699  Vector *xx=NewtonPoints, xk=xx[k],vd;
700  Vector Distance(N);
701  double *dist=Distance, *dd=dist, distMax, vmax, tmp;
702  Matrix H(n,n);
703  Vector GXk(n); //,D(n);
704 
705  for (i=0; i<N; i++) *(dd++)=xk.euclidianDistance(xx[i]);
706 
707  while (true)
708  {
709  dd=dist; j=-1; distMax=2*rho;
710  for (i=0; i<N; i++)
711  {
712  if (*dd>distMax) { j=i; distMax=*dd; };
713  dd++;
714  }
715  if (j<0) return -1;
716 
717  // to prevent to choose the same point once again:
718  dist[j]=0;
719 
720  pp[j].gradientHessian(xk,GXk,H);
721 // d=H.multiply(xk);
722 // d.add(G);
723 
724  tmp=M*distMax*distMax*distMax;
725 
726  if (tmp*rho*(GXk.euclidianNorm()+0.5*rho*H.frobeniusNorm())>=bound)
727  {
728 /* vd=L2NormMinimizer(pp[j], xk, rho);
729  vd+=xk;
730  vmax=condorAbs(pp[j](vd));
731 
732  Vector vd2=L2NormMinimizer(pp[j], xk, rho);
733  vd2+=xk;
734  double vmax2=condorAbs(pp[j](vd));
735 
736  if (vmax<vmax2) { vmax=vmax2; vd=vd2; }
737 */
738  vd=LAGMAXModified(GXk,H,rho,vmax);
739 // tmp=vd.euclidianNorm();
740  vd+=xk;
741  vmax=condorAbs(pp[j](vd));
742 
743  if (tmp*vmax>=bound) break;
744  }
745  }
746  if (j>=0) ddv.copyFrom(vd);
747  return j;
748 }
Vector * NewtonPoints
Definition: IntPoly.h:47
double rho
#define pp(s, x)
Definition: ml2d.cpp:473
Definition: Vector.h:37
unsigned dim()
Definition: Poly.h:62
#define i
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
Vector LAGMAXModified(Vector G, Matrix H, double rho, double &VMAX)
Definition: MSSolver.cpp:40
void gradientHessian(Vector P, Vector G, Matrix H)
Definition: Poly.cpp:515
double condorAbs(const double t1)
Definition: tools.h:47
Definition: Matrix.h:38
#define j
unsigned nPtsUsed
Definition: IntPoly.h:40
double M
Definition: IntPoly.h:39
Polynomial * NewtonBasis
Definition: IntPoly.h:43
int * n

◆ clone()

InterPolynomial InterPolynomial::clone ( )

Definition at line 868 of file IntPoly.cpp.

869 {
870  // a deep copy
871  InterPolynomial m(d->dim, d->deg);
872  m.copyFrom(*this);
873  return m;
874 }
int m
PolynomialData * d
Definition: Poly.h:49

◆ ComputeLagrangeBasis()

void InterPolynomial::ComputeLagrangeBasis ( double *  yy,
unsigned  nPtsTotal 
)

Definition at line 127 of file IntPoly.cpp.

128 {
129 #ifdef NOOBJECTIVEFUNCTION
130  const double eps = 1e-6;
131 #else
132  const double eps = 1e-10;
133 #endif
134  const double good = 1 ;
135  unsigned dim=d->dim,i;
136 
137  Vector *xx=NewtonPoints, xtemp;
138  Polynomial *pp= new Polynomial[nPtsUsed], *qq=pp;
139  NewtonBasis=pp;
140 
141  if (!pp)
142  {
143  printf("ComputeNewtonBasis( ... ) : Alloc for polynomials failed\n");
144  getchar(); exit(251);
145  }
146 
147  MultInd I(dim);
148  for (i=0; i<nPtsUsed; i++)
149  {
150  *(qq++)=Polynomial(I);
151  I++;
152  }
153 
154  unsigned k, kmax;
155  double v, vmax, vabs;
156 #ifdef VERBOSE
157  printf("Constructing first quadratic ... (N=%i)\n",nPtsUsed);
158 #endif
159  for (i=0; i<nPtsUsed; i++)
160  {
161 #ifdef VERBOSE
162  printf(".");
163 #endif
164  vmax = vabs = 0;
165  kmax = i;
166  if (i==0)
167  {
168  // to be sure point 0 is always taken:
169  vmax=(pp[0])( xx[0] );
170  } else
171  for (k=i; k<nPtsTotal; k++) // Pivoting
172  {
173  v=(pp[i])( xx[k] );
174  if (fabs(v) > vabs)
175  {
176  vmax = v;
177  vabs = condorAbs(v);
178  kmax = k;
179  }
180  if (fabs(v) > good ) break;
181  }
182 
183  // Now, check ...
184  if (fabs(vmax) < eps)
185  {
186  printf("Cannot construct Lagrange basis");
187  delete[] NewtonBasis;
188  NewtonBasis=NULL;
189  return;
190  }
191 
192  // exchange component i and k of NewtonPoints
193  // fast because of shallow copy
194  xtemp =xx[kmax];
195  xx[kmax]=xx[i];
196  xx[i] =xtemp;
197 
198  // exchange component i and k of newtonData
199  v =yy[kmax];
200  yy[kmax]=yy[i];
201  yy[i] =v;
202 
203  pp[i]/=vmax;
204  for (k=0; k<i; k++) pp[k] -= (pp[k])( xx[i] ) * pp[i];
205  for (k=i+1; k<nPtsUsed; k++) pp[k] -= (pp[k])( xx[i] ) * pp[i];
206 
207  // Next polynomial, break if necessary
208  }
209 #ifdef VERBOSE
210  printf("\n");
211 #endif
212 }
Vector * NewtonPoints
Definition: IntPoly.h:47
#define pp(s, x)
Definition: ml2d.cpp:473
Definition: Vector.h:37
unsigned dim()
Definition: Poly.h:62
cmache_1 eps
#define i
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
double condorAbs(const double t1)
Definition: tools.h:47
PolynomialData * d
Definition: Poly.h:49
unsigned nPtsUsed
Definition: IntPoly.h:40
Polynomial * NewtonBasis
Definition: IntPoly.h:43
Polynomial()
Definition: Poly.h:54

◆ copyFrom() [1/2]

void InterPolynomial::copyFrom ( InterPolynomial  a)

Definition at line 876 of file IntPoly.cpp.

877 {
878  if (m.d->dim!=d->dim)
879  {
880  printf("poly: copyFrom: dim do not agree\n");
881  getchar(); exit(254);
882  }
883  if (m.d->deg!=d->deg)
884  {
885  printf("poly: copyFrom: degree do not agree\n");
886  getchar(); exit(254);
887  }
889  M=m.M;
890 // nPtsUsed=m.nPtsUsed; // not useful because dim and degree already agree.
891  nUpdateOfM=m.nUpdateOfM;
892 // ValuesF
893 
894  int i=nPtsUsed;
895  while (i--)
896  {
897 // NewtonBasis[i]=m.NewtonBasis[i];
898 // NewtonPoints[i]=m.NewtonPoints[i];
899  NewtonBasis[i]=m.NewtonBasis[i].clone();
900  NewtonPoints[i]=m.NewtonPoints[i].clone();
901  }
902 }
Vector * NewtonPoints
Definition: IntPoly.h:47
#define i
unsigned nUpdateOfM
Definition: IntPoly.h:40
int m
PolynomialData * d
Definition: Poly.h:49
unsigned nPtsUsed
Definition: IntPoly.h:40
double M
Definition: IntPoly.h:39
Polynomial * NewtonBasis
Definition: IntPoly.h:43
void copyFrom(Polynomial a)
Definition: Poly.cpp:125

◆ copyFrom() [2/2]

void InterPolynomial::copyFrom ( Polynomial  a)

Definition at line 904 of file IntPoly.cpp.

905 {
907 }
int m
void copyFrom(Polynomial a)
Definition: Poly.cpp:125

◆ destroyCurrentBuffer()

void InterPolynomial::destroyCurrentBuffer ( )
protected

Definition at line 805 of file IntPoly.cpp.

806 {
807  if (!d) return;
808  if (d->ref_count==1)
809  {
810  if (NewtonBasis)
811  {
812  delete[] NewtonBasis;
813  delete[] NewtonPoints;
814  free(valuesF);
815  }
816  }
817 }
Vector * NewtonPoints
Definition: IntPoly.h:47
double * valuesF
Definition: IntPoly.h:48
free((char *) ob)
PolynomialData * d
Definition: Poly.h:49
Polynomial * NewtonBasis
Definition: IntPoly.h:43

◆ findAGoodPointToReplace()

int InterPolynomial::findAGoodPointToReplace ( int  excludeFromT,
double  rho,
Vector  pointToAdd,
double *  modelStep = NULL 
)

Definition at line 551 of file IntPoly.cpp.

553 {
554  //not tested
555 
556  // excludeFromT is set to k if not success from optim and we want
557  // to be sure that we keep the best point
558  // excludeFromT is set to -1 if we can replace the point x_k by
559  // pointToAdd(=x_k+d) because of the success of optim.
560 
561  // chosen t: the index of the point inside the newtonPoints
562  // which will be replaced.
563 
564  Vector *xx=NewtonPoints;
565  Vector XkHat;
566  if (excludeFromT>=0) XkHat=xx[excludeFromT];
567  else XkHat=pointToAdd;
568 
569  int t=-1, i, N=nPtsUsed;
570  double a, aa, maxa=-1.0, maxdd=0;
572 
573  // if (excludeFromT>=0) maxa=1.0;
574 
575  for (i=0; i<N; i++)
576  {
577  if (i==excludeFromT) continue;
578  aa=XkHat.euclidianDistance(xx[i]);
579  if (aa==0.0) return -1;
580  a=aa/rho;
581  // because of the next line, rho is important:
582  a=mmax(a*a*a,1.0);
583  a*=condorAbs(pp[i] (pointToAdd));
584 
585  if (a>maxa)
586  {
587  t=i; maxa=a; maxdd=aa;
588  }
589  }
590  if (maxd) *maxd=maxdd;
591  return t;
592 }
Vector * NewtonPoints
Definition: IntPoly.h:47
int * mmax
double rho
#define pp(s, x)
Definition: ml2d.cpp:473
Definition: Vector.h:37
double euclidianDistance(Vector v)
Definition: Vector.cpp:244
#define i
double condorAbs(const double t1)
Definition: tools.h:47
unsigned nPtsUsed
Definition: IntPoly.h:40
Polynomial * NewtonBasis
Definition: IntPoly.h:43
doublereal * a

◆ GenerateBasis()

void InterPolynomial::GenerateBasis ( double  rho,
double  rhosmall,
Matrix  data,
ObjectiveFunction of 
)

Definition at line 324 of file IntPoly.cpp.

327 {
328  if (deg()!=2)
329  {
330  printf("The GenerateBasis function only works for interpolation polynomials of degree 2.\n");
331  exit(244);
332  }
333  int i,j,k,l,dim=data.nColumn()-2, N=(dim+1)*(dim+2)/2,
334  *failed=(int*)malloc(N*sizeof(int)),nToCompute, nl=data.nLine(),best;
335  Vector Base=data.getLine(0,dim);
336  VectorInt vi(dim);
337  valuesF=(double*)malloc((N+dim)*sizeof(double));
338  double dBase=((double**)data)[0][dim],*vrho=valuesF+N,
339  *base=Base,*dcur,*p,*pb, norm, normbest, r; // value objective function
340 
341  NewtonPoints=new Vector[N];
342  Vector *cp, cur; // cp: current Point
343  NewtonPoints[N-1]=Base.clone(); valuesF[N-1]=dBase;
344 
345  // generate perfect sampling site
346  for (i=0; i<dim; i++)
347  {
348  cur=Base.clone();
349  cur[i]+=rho;
350  NewtonPoints[i]=cur;
351  vi[i]=i;
352  }
353 
354  nToCompute=0; cp=NewtonPoints;
355  // try to find good points in DB, which are near perfect sampling sites
356  for (i=0; i<dim; i++)
357  {
358  normbest=INF; pb=cp[i];
359  for (j=1; j<nl; j++)
360  {
361  p=data[j];
362  if (p[dim+1]) continue;
363  norm=0; k=dim;
364  while (k--) norm+=sqr(p[k]-pb[k]);
365  if (normbest>norm) { normbest=norm; best=j; }
366  }
367  if (normbest<rhosmall*rhosmall)
368  {
369  cp[i]=data.getLine(best,dim);
370  valuesF[i]=((double**)data)[best][dim];
371  } else
372  {
373  cur=cp[nToCompute]; cp[nToCompute]=cp[i]; cp[i]=cur;
374  l=vi[nToCompute]; vi[nToCompute]=vi[i]; vi[i]=l;
375  valuesF[i]=valuesF[nToCompute];
376  nToCompute++;
377  }
378  }
379 
380  if ((!of)&&(nToCompute)) { free(failed); free(valuesF); delete[] NewtonPoints; return; }
381 
382  // if some points have not been found in DB, start evaluation
383  while (nToCompute>0)
384  {
385  // parallelize here !
386  calculateNParallelJob(nToCompute,valuesF,NewtonPoints,of,failed);
387 
388  k=0;
389  for (j=0; j<nToCompute; j++)
390  {
391  of->saveValue(cp[j],valuesF[j],failed[j]);
392  if (failed[j])
393  {
394  dcur=cp[j];
395  for (i=0; i<dim; i++)
396  {
397  dcur[i]=base[i]+(base[i]-dcur[i])*.7;
398  }
399 
400  // group all missing values at the beginning (around NewtonPoints+dim)
401  cur=cp[k]; cp[k]=cp[j]; cp[j]=cur;
402  l=vi[k]; vi[k]=vi[j]; vi[j]=l;
403  valuesF[j]=valuesF[k];
404  k++;
405  }
406  }
407  nToCompute=k;
408  }
409 
410  // re-order vectors (based on vi)
411  for (j=0; j<dim-1; j++)
412  {
413  k=j; while (vi[k]!=j) k++;
414  if (k==j) continue;
415  cur=cp[k]; cp[k]=cp[j]; cp[j]=cur;
416  l=vi[k]; vi[k]=vi[j]; vi[j]=l;
417  r=valuesF[k]; valuesF[k]=valuesF[j]; valuesF[j]=r;
418  }
419 
420  // select again some good sampling sites
421  cp=NewtonPoints+dim;
422  for (j=0; j<dim; j++)
423  {
424  cur=Base.clone();
425  dcur=NewtonPoints[j];
426  r=dcur[j]-base[j];
427  if (valuesF[j]<dBase) { cur[j]+=r*2.0; vrho[j]=r; }
428  else { cur[j]-=r; vrho[j]=-r; }
429  *(cp++)=cur;
430  }
431  for (j=0; j<dim; j++)
432  {
433  for (k=0; k<j; k++)
434  {
435  cur=Base.clone();
436  cur[j]+=vrho[j];
437  cur[k]+=vrho[k];
438  *(cp++)=cur;
439  }
440  }
441 
442  nToCompute=0;
443  cp=NewtonPoints+dim;
444  // try to find good points in DB, which are near perfect sampling sites
445  for (i=0; i<N-dim-1; i++)
446  {
447  normbest=INF; pb=cp[i];
448  for (j=1; j<nl; j++)
449  {
450  p=data[j];
451  if (p[dim+1]) continue;
452  norm=0; k=dim;
453  while (k--) norm+=sqr(p[k]-pb[k]);
454  if (normbest>norm) { normbest=norm; best=j; }
455  }
456  if (normbest<rhosmall*rhosmall)
457  {
458  cp[i]=data.getLine(best,dim);
459  valuesF[i+dim]=((double**)data)[best][dim];
460  } else
461  {
462  cur=cp[nToCompute];
463  cp[nToCompute]=cp[i];
464  cp[i]=cur;
465  valuesF[i+dim]=valuesF[nToCompute+dim];
466  nToCompute++;
467  }
468  }
469 
470  if ((!of)&&(nToCompute)) { free(failed); free(valuesF); delete[] NewtonPoints; return; }
471 
472  while (nToCompute>0)
473  {
474  // parallelize here !
475  calculateNParallelJob(nToCompute,valuesF+dim,cp,of,failed);
476 
477  k=0;
478  for (j=0; j<nToCompute; j++)
479  {
480  of->saveValue(cp[j],valuesF[j+dim],failed[j]);
481  if (failed[j])
482  {
483  dcur=cp[j];
484  for (i=0; i<dim; i++)
485  {
486  dcur[i]=base[i]+(base[i]-dcur[i])*.65;
487  }
488 
489  // group all missing values at the beginning (around NewtonPoints+dim)
490  cur=cp[k];
491  cp[k]=cp[j];
492  cp[j]=cur;
493  valuesF[j+dim]=valuesF[k+dim];
494  k++;
495  }
496  }
497  nToCompute=k;
498 
499  }
500  free(failed);
501 
502  // "insertion sort" to always place best points first
503  for (i=0; i<N-1; i++)
504  {
505  kbest=findK(valuesF+i,N-i,of,NewtonPoints+i)+i;
506 
507  // to accept infeasible points for i>=1 :
508  of=NULL;
509 
512  }
513  kbest=0;
514  vBase=NewtonPoints[0].clone();
515  for (i=0; i<N; i++) NewtonPoints[i]-=vBase;
517 }
virtual void saveValue(Vector tmp, double valueOF, int nerror)
Vector * NewtonPoints
Definition: IntPoly.h:47
int calculateNParallelJob(int n, double *vf, Vector *cp, ObjectiveFunction *of, int *notsuccess)
Definition: parallel.cpp:43
double rho
Definition: Vector.h:37
unsigned dim()
Definition: Poly.h:62
unsigned deg()
Definition: Poly.h:63
T norm(const std::vector< T > &v)
Definition: vector_ops.h:399
int nColumn()
Definition: Matrix.h:84
Vector clone()
Definition: Vector.cpp:207
#define i
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
int nLine()
Definition: Matrix.h:83
double sqr(const double &t)
Definition: tools.h:99
void ComputeLagrangeBasis(double *, unsigned nPtsTotal)
Definition: IntPoly.cpp:127
double * valuesF
Definition: IntPoly.h:48
Vector vBase
Definition: IntPoly.h:47
free((char *) ob)
int findK(double *ValuesF, int n, ObjectiveFunction *of, Vector *points)
Definition: IntPoly.cpp:214
#define j
Vector getLine(int i, int n=0, int startCol=0)
Definition: Matrix.cpp:1094
#define INF
Definition: svm.cpp:43

◆ getGoodInterPolationSites()

int InterPolynomial::getGoodInterPolationSites ( Matrix  d,
int  k,
double  rho,
Vector v = NULL 
)

Definition at line 750 of file IntPoly.cpp.

753 {
754  //not tested
755 
756  unsigned i, N=nPtsUsed, n=dim();
757  int ii, j,r=0;
759  Vector *xx=NewtonPoints, xk,vd;
760  Vector Distance(N);
761  double *dist=Distance, *dd=dist, distMax, vmax;
762  Matrix H(n,n);
763  Vector GXk(n);
764  if (k>=0) xk=xx[k]; else xk=*v;
765 
766  for (i=0; i<N; i++) *(dd++)=xk.euclidianDistance(xx[i]);
767 
768  for (ii=0; ii<d.nLine(); ii++)
769  {
770  dd=dist; j=-1;
771  distMax=-1.0;
772  for (i=0; i<N; i++)
773  {
774  if (*dd>distMax) { j=i; distMax=*dd; };
775  dd++;
776  }
777  // to prevent to choose the same point once again:
778  dist[j]=-1.0;
779 
780  if (distMax>2*rho) r++;
781  pp[j].gradientHessian(xk,GXk,H);
782  vd=LAGMAXModified(GXk,H,rho,vmax);
783  vd+=xk;
784 
785  d.setLine(ii,vd);
786  }
787  return r;
788 }
Vector * NewtonPoints
Definition: IntPoly.h:47
double rho
#define pp(s, x)
Definition: ml2d.cpp:473
Definition: Vector.h:37
unsigned dim()
Definition: Poly.h:62
double euclidianDistance(Vector v)
Definition: Vector.cpp:244
#define i
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
int nLine()
Definition: Matrix.h:83
Vector LAGMAXModified(Vector G, Matrix H, double rho, double &VMAX)
Definition: MSSolver.cpp:40
void gradientHessian(Vector P, Vector G, Matrix H)
Definition: Poly.cpp:515
Definition: Matrix.h:38
#define j
unsigned nPtsUsed
Definition: IntPoly.h:40
void setLine(int i, Vector v, int n=0)
Definition: Matrix.cpp:1125
Polynomial * NewtonBasis
Definition: IntPoly.h:43
int * n

◆ interpError()

double InterPolynomial::interpError ( Vector  Point)

Definition at line 536 of file IntPoly.cpp.

537 {
538  unsigned i=nPtsUsed;
539  double sum=0,a;
541  Vector *xx=NewtonPoints;
542 
543  while (i--)
544  {
545  a=Point.euclidianDistance(xx[i]);
546  sum+=condorAbs(pp[i]( Point ))*a*a*a;
547  }
548  return M*sum;
549 }
Vector * NewtonPoints
Definition: IntPoly.h:47
#define pp(s, x)
Definition: ml2d.cpp:473
Definition: Vector.h:37
double euclidianDistance(Vector v)
Definition: Vector.cpp:244
#define i
double condorAbs(const double t1)
Definition: tools.h:47
unsigned nPtsUsed
Definition: IntPoly.h:40
double M
Definition: IntPoly.h:39
Polynomial * NewtonBasis
Definition: IntPoly.h:43
doublereal * a

◆ maybeAdd()

int InterPolynomial::maybeAdd ( Vector  pointToAdd,
unsigned  k,
double  rho,
double  valueF 
)

Definition at line 649 of file IntPoly.cpp.

652 {
653 
654  unsigned i,N=nPtsUsed;
655  int j = 0;
656  Vector *xx=NewtonPoints, xk=xx[k];
657  double distMax=-1.0,dd;
658  /*
659  Polynomial *pp=NewtonBasis;
660  Vector *xx=NewtonPoints, xk=xx[k],vd;
661  Matrix H(n,n);
662  Vector GXk(n); //,D(n);
663 */
664  // find worst point/newton poly
665 
666  for (i=0; i<N; i++)
667  {
668  dd=xk.euclidianDistance(xx[i]);
669  if (dd>distMax) { j=i; distMax=dd; };
670  }
671  dd=xk.euclidianDistance(pointToAdd);
672 
673  // no tested:
674 
675  if (condorAbs(NewtonBasis[j](pointToAdd))*distMax*distMax*distMax/(dd*dd*dd)>1.0)
676  {
677  printf("good point found.\n");
678  replace(j, pointToAdd, valueF);
679  return 1;
680  }
681  return 0;
682 }
Vector * NewtonPoints
Definition: IntPoly.h:47
Definition: Vector.h:37
double euclidianDistance(Vector v)
Definition: Vector.cpp:244
#define i
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
void replace(int t, Vector pointToAdd, double valueF)
Definition: IntPoly.cpp:622
double condorAbs(const double t1)
Definition: tools.h:47
#define j
unsigned nPtsUsed
Definition: IntPoly.h:40
Polynomial * NewtonBasis
Definition: IntPoly.h:43

◆ NewtonCoefficient()

double* InterPolynomial::NewtonCoefficient ( double *  )

◆ operator=()

InterPolynomial & InterPolynomial::operator= ( const InterPolynomial A)

Definition at line 838 of file IntPoly.cpp.

839 {
840  // shallow copy
841  if (this != &A)
842  {
844 
845  d=A.d;
848 // ValuesF=A.ValuesF;
849  M=A.M;
850  nPtsUsed=A.nPtsUsed;
852  kbest=A.kbest;
853  vBase=A.vBase;
854  valuesF=A.valuesF;
855 
856  (d->ref_count) ++ ;
857  }
858  return *this;
859 }
Vector * NewtonPoints
Definition: IntPoly.h:47
void destroyCurrentBuffer()
Definition: IntPoly.cpp:805
unsigned nUpdateOfM
Definition: IntPoly.h:40
double * valuesF
Definition: IntPoly.h:48
Vector vBase
Definition: IntPoly.h:47
PolynomialData * d
Definition: Poly.h:49
unsigned nPtsUsed
Definition: IntPoly.h:40
double M
Definition: IntPoly.h:39
Polynomial * NewtonBasis
Definition: IntPoly.h:43

◆ replace()

void InterPolynomial::replace ( int  t,
Vector  pointToAdd,
double  valueF 
)

Definition at line 622 of file IntPoly.cpp.

623 {
624  //not tested
625  updateM(pointToAdd, valueF);
626  if (t<0) return;
627 
628  Vector *xx=NewtonPoints;
630  int i, N=nPtsUsed;
631  double t2=(pp[t]( pointToAdd ));
632 
633  if (t2==0) return;
634 
635  t1=pp[t]/=t2;
636 
637  for (i=0; i<t; i++) pp[i]-= pp[i]( pointToAdd )*t1;
638  for (i=t+1; i<N; i++) pp[i]-= pp[i]( pointToAdd )*t1;
639  xx[t].copyFrom(pointToAdd);
640 
641  // update the coefficents of general poly.
642 
643  valueF-=(*this)(pointToAdd);
644  if (condorAbs(valueF)>1e-11) (*this)+=valueF*pp[t];
645 
646 // test();
647 }
Vector * NewtonPoints
Definition: IntPoly.h:47
void updateM(Vector newPoint, double valueF)
Definition: IntPoly.cpp:519
#define pp(s, x)
Definition: ml2d.cpp:473
Definition: Vector.h:37
#define i
double condorAbs(const double t1)
Definition: tools.h:47
unsigned nPtsUsed
Definition: IntPoly.h:40
Polynomial * NewtonBasis
Definition: IntPoly.h:43
void copyFrom(Vector r, int _n=0)
Definition: Vector.cpp:215

◆ translate() [1/2]

void InterPolynomial::translate ( int  k)

◆ translate() [2/2]

void InterPolynomial::translate ( Vector  translation)

Definition at line 790 of file IntPoly.cpp.

791 {
792  if (translation==Vector::emptyVector) return;
793  vBase+=translation;
794  Polynomial::translate(translation);
795  int i=nPtsUsed;
796  while (i--) NewtonBasis[i].translate(translation);
797  i=nPtsUsed;
798  while (i--) if (NewtonPoints[i]==translation) NewtonPoints[i].zero();
799  else NewtonPoints[i]-=translation;
800 }
Vector * NewtonPoints
Definition: IntPoly.h:47
void translate(Vector translation)
Definition: Poly.cpp:563
#define i
void zero(int _i=0, int _n=0)
Definition: Vector.cpp:93
Vector vBase
Definition: IntPoly.h:47
unsigned nPtsUsed
Definition: IntPoly.h:40
static Vector emptyVector
Definition: Vector.h:119
Polynomial * NewtonBasis
Definition: IntPoly.h:43

◆ updateM()

void InterPolynomial::updateM ( Vector  newPoint,
double  valueF 
)

Definition at line 519 of file IntPoly.cpp.

520 {
521  //not tested
522  unsigned i=nPtsUsed;
523  double sum=0,a;
525  Vector *xx=NewtonPoints;
526 
527  while (i--)
528  {
529  a=newPoint.euclidianDistance(xx[i]);
530  sum+=condorAbs(pp[i]( newPoint ))*a*a*a;
531  }
532  M=mmax(M, condorAbs((*this)(newPoint)-valueF)/sum);
533  nUpdateOfM++;
534 }
Vector * NewtonPoints
Definition: IntPoly.h:47
int * mmax
#define pp(s, x)
Definition: ml2d.cpp:473
Definition: Vector.h:37
double euclidianDistance(Vector v)
Definition: Vector.cpp:244
#define i
unsigned nUpdateOfM
Definition: IntPoly.h:40
double condorAbs(const double t1)
Definition: tools.h:47
unsigned nPtsUsed
Definition: IntPoly.h:40
double M
Definition: IntPoly.h:39
Polynomial * NewtonBasis
Definition: IntPoly.h:43
doublereal * a

Member Data Documentation

◆ kbest

int InterPolynomial::kbest

Definition at line 49 of file IntPoly.h.

◆ M

double InterPolynomial::M

Definition at line 39 of file IntPoly.h.

◆ NewtonBasis

Polynomial* InterPolynomial::NewtonBasis

Definition at line 43 of file IntPoly.h.

◆ NewtonPoints

Vector* InterPolynomial::NewtonPoints

Definition at line 47 of file IntPoly.h.

◆ nPtsUsed

unsigned InterPolynomial::nPtsUsed

Definition at line 40 of file IntPoly.h.

◆ nUpdateOfM

unsigned InterPolynomial::nUpdateOfM

Definition at line 40 of file IntPoly.h.

◆ valuesF

double* InterPolynomial::valuesF

Definition at line 48 of file IntPoly.h.

◆ vBase

Vector InterPolynomial::vBase

Definition at line 47 of file IntPoly.h.


The documentation for this class was generated from the following files: