47 if (_dim) n=
d->
n=
choose( _dim+_deg, _dim );
54 if (n==0) {
d->
coeff=NULL;
return; };
56 d->
coeff=(
double*)malloc(n*
sizeof(
double));
57 if (
d->
coeff==NULL) { printf(
"memory allocation error\n"); getchar(); exit(253); }
59 if (data) memcpy(
d->
coeff, data,
d->
n*
sizeof(
double));
60 else memset(
d->
coeff, 0,
d->
n*
sizeof(
double));
129 printf(
"poly: copyFrom: dim do not agree");
130 getchar(); exit(254);
134 unsigned N1=
sz(), N2=m.
sz();
140 memcpy((*
this),m,N2*
sizeof(
double));
149 while (i--) *(tq++) = *(tp++) * t;
158 printf(
"op/(Poly,double): Division by zero\n");
165 while (i--) *(tq++) = *(tp++) / t;
173 printf(
"Poly::op+ : Different dimension\n");
178 unsigned N1=
sz(), N2=q.
sz(), Ni=
mmin(N1,N2);
179 double *tr = r, *tp = (*this), *tq = q;
180 while (Ni--) *(tr++) = *(tp++) + *(tq++);
183 memcpy(tr,tq,(N2-N1)*
sizeof(
double));
194 printf(
"Poly::op- : Different dimension\n");
199 unsigned N1=
sz(), N2=q.
sz(), Ni=
mmin(N1,N2);
200 double *tr = r, *tp = (*this), *tq = q;
201 while (Ni--) *(tr++) = *(tp++) - *(tq++);
204 N2-=N1;
while (N2--) *(tr++)=-(*(tq++));
212 double *tp = (*this);
214 if (!Ni || !tp)
return *
this;
218 while( Ni-- ) *(tq++) = -(*(tp++));
227 printf(
"Poly::op+= : Different dimension\n");
232 unsigned N1=
sz(), N2=p.
sz(), Ni=
mmin(N1,N2);
238 double *tt = (*this),*tp = p;
240 while (Ni--) *(tt++) += *(tp++);
244 memcpy(tt,tp,(N2-N1)*
sizeof(
double));
256 printf(
"Poly::op-= : Different dimension\n");
261 unsigned N1=
sz(), N2=p.
sz(), Ni=
mmin(N1,N2);
267 double *tt = (*this),*tp = p;
269 while (Ni--) *(tt++) -= *(tp++);
273 N2-=N1;
while (N2--) *(tt++)=-(*(tp++));
283 double *tp = (*this);
285 while (i--) *(tp++) *=t;
294 printf(
"Poly::op/= : Division by zero\n");
299 double *tp = (*this);
301 while (i--) *(tp++) /=t;
307 if (
d==q.
d)
return 1;
311 double *tp = (*this),*tq = q;
314 if ( *(tp++) != *(tq++) )
return 0;
323 double *tt = (*this);
327 if ( !N || !tt ) { printf(
"[Void polynomial]\n");
return; }
329 if (*tt) { IsFirst=
false; printf(
"%f", *tt); }
332 for (
unsigned i = 1;
i < N;
i++,tt++,++I)
340 if (*tt<0) printf(
"-");
345 printf(
"+%f x^",*tt); I.
print();
352 if (*tt<0) printf(
"-");
else printf(
"+");
357 printf(
"+%f x^",*tt); I.
print();
382 double tmp1=
d->
coeff[0], tmp2;
401 double *rbufp = rbuf;
402 unsigned lsize = 100;
409 if ( dim != (
unsigned)Point.
sz() )
411 printf(
"Polynomial::operator()( Vector& ) : Improper size\n");
419 printf(
"Polynomial::operator()( Vector& ) : evaluating void polynomial\n");
428 printf(
"Polynomial::operator()( Vector& ) : Warning -> 100 variables\n");
432 rbufp = (
double*)malloc(lsize*
sizeof(
double));
436 printf(
"Polynomial::operator()( Vector& ) : Cannot allocate <rbufp>\n");
437 getchar(); exit( -1 );
445 double *cc = (*this), *P=Point;
449 memset(rbufp,0,dim*
sizeof(
double));
458 r=r0; rptr=rbufp+lc; j=dim-lc;
459 while (j--) { r+=*rptr; *(rptr++)=0; }
463 r=r0; rptr=rbufp; i=(int)dim;
464 while (i--) r+=*(rptr++);
477 double *tS=(*this), *tD=r;
478 unsigned j=
sz(),
k, *cc, sum,
479 *allExpo=(
unsigned*)I, *expo=allExpo+i, *firstOfJ=(
unsigned*)J;
487 sum=0; cc=allExpo;
k=
dim;
488 while (
k--) sum+=*(cc++);
489 if (sum)
k=
choose( sum-1+dim, dim );
else k=0;
491 while (!(J==I)) {
k++; J++; }
494 tD[
k]=(*tS) * (double)*expo;
509 memcpy(r,
d->
coeff+1,i*
sizeof(
double));
526 memcpy(r,c,dim*
sizeof(
double));
528 for (i=0; i<
dim; i++)
531 for (j=i+1; j<
dim; j++)
532 h[i][j]=h[j][i]=*(c++);
554 h[
i][
j]=h[
j][
i]=
a(P);
567 printf(
"Translation only for polynomial of degree lower than 3.\n");
568 getchar(); exit(255);
570 d->
coeff[0]=(*this)(translation);
571 if (
d->
deg==1)
return;
576 memcpy(((
double*)
d->
coeff)+1, (
double*)G, dim*
sizeof(
double));
581 std::ofstream ofp(name, std::ios::out | std::ios::binary);
582 ofp.write(reinterpret_cast<const char*>(&
d->
dim),
sizeof(
int));
583 ofp.write(reinterpret_cast<const char*>(&
d->
deg),
sizeof(
int));
584 ofp.write(reinterpret_cast<const char*>(
d->
coeff),
d->
n*
sizeof(
double));
591 std::ifstream ifp(name,
std::ios::in | std::ios::binary);
592 ifp.read(reinterpret_cast<char*>(&_dim),
sizeof(
int));
593 ifp.read(reinterpret_cast<char*>(&_deg),
sizeof(
int));
595 ifp.read(reinterpret_cast<char*>(
d->
coeff),
d->
n*
sizeof(
double));
static const unsigned int Warning
unsigned long choose(unsigned n, unsigned k)
MultIndCache cacheMultInd
void translate(Vector translation)
Matrix multiply(Matrix B)
Polynomial operator-=(Polynomial)
static unsigned int flags
int equals(const Vector Q)
static const unsigned int Normalized
Polynomial & operator=(const Polynomial &A)
MultInd * get(unsigned _dim, unsigned _deg)
Polynomial operator/(const double)
void setSize(int _nLine, int _nColumn)
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
Polynomial operator*=(const double)
static const unsigned int NicePrint
Polynomial operator*(const double)
void gradientHessian(Vector P, Vector G, Matrix H)
double operator()(Vector)
void init(int _dim, int _deg, double *data=NULL)
Polynomial derivate(int i)
Polynomial operator+(void)
unsigned * indexesOfCoefInLexOrder()
static Vector emptyVector
double shiftedEval(Vector Point, double minusVal)
void gradient(Vector P, Vector G)
Polynomial operator-(void)
Polynomial operator+=(Polynomial)
unsigned queryFlag(unsigned int val)
void copyFrom(Polynomial a)
static Polynomial emptyPolynomial
void destroyCurrentBuffer()
Polynomial operator/=(const double)