PPolynomial.inl 13.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
/*
Copyright (c) 2006, Michael Kazhdan and Matthew Bolitho
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer. Redistributions in binary form must reproduce
the above copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the distribution. 

Neither the name of the Johns Hopkins University nor the names of its contributors
may be used to endorse or promote products derived from this software without specific
prior written permission. 

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES 
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE  GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
*/

#include "Factor.h"

////////////////////////
// StartingPolynomial //
////////////////////////
template<int Degree>
template<int Degree2>
StartingPolynomial<Degree+Degree2> StartingPolynomial<Degree>::operator * (const StartingPolynomial<Degree2>& p) const{
37 38 39 40 41
  StartingPolynomial<Degree+Degree2> sp;
  if(start>p.start){sp.start=start;}
  else{sp.start=p.start;}
  sp.p=this->p*p.p;
  return sp;
42 43 44
}
template<int Degree>
StartingPolynomial<Degree> StartingPolynomial<Degree>::scale(double s) const{
45 46 47 48
  StartingPolynomial q;
  q.start=start*s;
  q.p=p.scale(s);
  return q;
49 50 51
}
template<int Degree>
StartingPolynomial<Degree> StartingPolynomial<Degree>::shift(double s) const{
52 53 54 55
  StartingPolynomial q;
  q.start=start+s;
  q.p=p.shift(s);
  return q;
56 57 58 59 60
}


template<int Degree>
int StartingPolynomial<Degree>::operator < (const StartingPolynomial<Degree>& sp) const{
61 62
  if(start<sp.start){return 1;}
  else{return 0;}
63 64 65
}
template<int Degree>
int StartingPolynomial<Degree>::Compare(const void* v1,const void* v2){
66 67 68 69
  double d=(static_cast<const StartingPolynomial*>(v1))->start-(static_cast<const StartingPolynomial*>(v2))->start;
  if    (d<0) {return -1;}
  else if (d>0) {return  1;}
  else      {return  0;}
70 71 72 73 74 75 76
}

/////////////////
// PPolynomial //
/////////////////
template<int Degree>
PPolynomial<Degree>::PPolynomial(void){
77 78
  polyCount=0;
  polys=NULL;
79 80 81
}
template<int Degree>
PPolynomial<Degree>::PPolynomial(const PPolynomial<Degree>& p){
82 83 84 85
  polyCount=0;
  polys=NULL;
  set(p.polyCount);
  memcpy(polys,p.polys,sizeof( StartingPolynomial<Degree> )*p.polyCount);
86 87 88 89
}

template<int Degree>
PPolynomial<Degree>::~PPolynomial(void){
90 91 92
  if(polyCount){free(polys);}
  polyCount=0;
  polys=NULL;
93 94 95
}
template<int Degree>
void PPolynomial<Degree>::set(StartingPolynomial<Degree>* sps,int count){
96 97 98 99 100 101 102 103 104
  int i,c=0;
  set(count);
  qsort(sps,count,sizeof(StartingPolynomial<Degree>),StartingPolynomial<Degree>::Compare);
  for( i=0 ; i<count ; i++ )
  {
    if( !c || sps[i].start!=polys[c-1].start ) polys[c++] = sps[i];
    else{polys[c-1].p+=sps[i].p;}
  }
  reset( c );
105 106 107 108 109 110 111
}
template <int Degree>
int PPolynomial<Degree>::size(void) const{return int(sizeof(StartingPolynomial<Degree>)*polyCount);}

template<int Degree>
void PPolynomial<Degree>::set( size_t size )
{
112 113 114 115 116 117 118 119
  if(polyCount){free(polys);}
  polyCount=0;
  polys=NULL;
  polyCount=size;
  if(size){
    polys=static_cast<StartingPolynomial<Degree>*>(malloc(sizeof(StartingPolynomial<Degree>)*size));
    memset(polys,0,sizeof(StartingPolynomial<Degree>)*size);
  }
120 121 122 123
}
template<int Degree>
void PPolynomial<Degree>::reset( size_t newSize )
{
124 125 126 127 128
  polyCount=newSize;
  StartingPolynomial<Degree>* tmp = static_cast<StartingPolynomial<Degree>*>(realloc(polys,sizeof(StartingPolynomial<Degree>)*newSize));
  if (!tmp)
    free(polys);
  polys=tmp;
129 130 131 132
}

template<int Degree>
PPolynomial<Degree>& PPolynomial<Degree>::operator = (const PPolynomial<Degree>& p){
133 134 135
  set(p.polyCount);
  memcpy(polys,p.polys,sizeof(StartingPolynomial<Degree>)*p.polyCount);
  return *this;
136 137 138 139 140
}

template<int Degree>
template<int Degree2>
PPolynomial<Degree>& PPolynomial<Degree>::operator  = (const PPolynomial<Degree2>& p){
141 142 143 144 145 146
  set(p.polyCount);
  for(int i=0;i<int(polyCount);i++){
    polys[i].start=p.polys[i].start;
    polys[i].p=p.polys[i].p;
  }
  return *this;
147 148 149 150 151
}

template<int Degree>
double PPolynomial<Degree>::operator ()( double t ) const
{
152 153 154
  double v=0;
  for( int i=0 ; i<int(polyCount) && t>polys[i].start ; i++ ) v+=polys[i].p(t);
  return v;
155 156 157 158 159
}

template<int Degree>
double PPolynomial<Degree>::integral( double tMin , double tMax ) const
{
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
  int m=1;
  double start,end,s,v=0;
  start=tMin;
  end=tMax;
  if(tMin>tMax){
    m=-1;
    start=tMax;
    end=tMin;
  }
  for(int i=0;i<int(polyCount) && polys[i].start<end;i++){
    if(start<polys[i].start){s=polys[i].start;}
    else{s=start;}
    v+=polys[i].p.integral(s,end);
  }
  return v*m;
175 176 177 178 179
}
template<int Degree>
double PPolynomial<Degree>::Integral(void) const{return integral(polys[0].start,polys[polyCount-1].start);}
template<int Degree>
PPolynomial<Degree> PPolynomial<Degree>::operator + (const PPolynomial<Degree>& p) const{
180 181 182 183 184
  PPolynomial q;
  int i,j;
  size_t idx=0;
  q.set(polyCount+p.polyCount);
  i=j=-1;
185

186 187 188 189 190 191 192 193
  while(idx<q.polyCount){
    if    (j>=int(p.polyCount)-1)       {q.polys[idx]=  polys[++i];}
    else if (i>=int(  polyCount)-1)       {q.polys[idx]=p.polys[++j];}
    else if(polys[i+1].start<p.polys[j+1].start){q.polys[idx]=  polys[++i];}
    else                    {q.polys[idx]=p.polys[++j];}
    idx++;
  }
  return q;
194 195 196
}
template<int Degree>
PPolynomial<Degree> PPolynomial<Degree>::operator - (const PPolynomial<Degree>& p) const{
197 198 199 200 201
  PPolynomial q;
  int i,j;
  size_t idx=0;
  q.set(polyCount+p.polyCount);
  i=j=-1;
202

203 204 205 206 207 208 209 210
  while(idx<q.polyCount){
    if    (j>=int(p.polyCount)-1)       {q.polys[idx]=  polys[++i];}
    else if (i>=int(  polyCount)-1)       {q.polys[idx].start=p.polys[++j].start;q.polys[idx].p=p.polys[j].p*(-1.0);}
    else if(polys[i+1].start<p.polys[j+1].start){q.polys[idx]=  polys[++i];}
    else                    {q.polys[idx].start=p.polys[++j].start;q.polys[idx].p=p.polys[j].p*(-1.0);}
    idx++;
  }
  return q;
211 212 213
}
template<int Degree>
PPolynomial<Degree>& PPolynomial<Degree>::addScaled(const PPolynomial<Degree>& p,double scale){
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
  int i,j;
  StartingPolynomial<Degree>* oldPolys=polys;
  size_t idx=0,cnt=0,oldPolyCount=polyCount;
  polyCount=0;
  polys=NULL;
  set(oldPolyCount+p.polyCount);
  i=j=-1;
  while(cnt<polyCount){
    if    (j>=int( p.polyCount)-1)        {polys[idx]=oldPolys[++i];}
    else if (i>=int(oldPolyCount)-1)        {polys[idx].start= p.polys[++j].start;polys[idx].p=p.polys[j].p*scale;}
    else if (oldPolys[i+1].start<p.polys[j+1].start){polys[idx]=oldPolys[++i];}
    else                      {polys[idx].start= p.polys[++j].start;polys[idx].p=p.polys[j].p*scale;}
    if(idx && polys[idx].start==polys[idx-1].start) {polys[idx-1].p+=polys[idx].p;}
    else{idx++;}
    cnt++;
  }
  free(oldPolys);
  reset(idx);
  return *this;
233 234 235 236
}
template<int Degree>
template<int Degree2>
PPolynomial<Degree+Degree2> PPolynomial<Degree>::operator * (const PPolynomial<Degree2>& p) const{
237 238 239
  PPolynomial<Degree+Degree2> q;
  StartingPolynomial<Degree+Degree2> *sp;
  int i,j,spCount=int(polyCount*p.polyCount);
240

241 242 243 244 245 246 247 248 249
  sp=(StartingPolynomial<Degree+Degree2>*)malloc(sizeof(StartingPolynomial<Degree+Degree2>)*spCount);
  for(i=0;i<int(polyCount);i++){
    for(j=0;j<int(p.polyCount);j++){
      sp[i*p.polyCount+j]=polys[i]*p.polys[j];
    }
  }
  q.set(sp,spCount);
  free(sp);
  return q;
250 251 252 253
}
template<int Degree>
template<int Degree2>
PPolynomial<Degree+Degree2> PPolynomial<Degree>::operator * (const Polynomial<Degree2>& p) const{
254 255 256 257 258 259 260
  PPolynomial<Degree+Degree2> q;
  q.set(polyCount);
  for(int i=0;i<int(polyCount);i++){
    q.polys[i].start=polys[i].start;
    q.polys[i].p=polys[i].p*p;
  }
  return q;
261 262 263 264
}
template<int Degree>
PPolynomial<Degree> PPolynomial<Degree>::scale( double s ) const
{
265 266 267 268
  PPolynomial q;
  q.set(polyCount);
  for(size_t i=0;i<polyCount;i++){q.polys[i]=polys[i].scale(s);}
  return q;
269 270 271 272
}
template<int Degree>
PPolynomial<Degree> PPolynomial<Degree>::shift( double s ) const
{
273 274 275 276
  PPolynomial q;
  q.set(polyCount);
  for(size_t i=0;i<polyCount;i++){q.polys[i]=polys[i].shift(s);}
  return q;
277 278 279
}
template<int Degree>
PPolynomial<Degree-1> PPolynomial<Degree>::derivative(void) const{
280 281 282 283 284 285 286
  PPolynomial<Degree-1> q;
  q.set(polyCount);
  for(size_t i=0;i<polyCount;i++){
    q.polys[i].start=polys[i].start;
    q.polys[i].p=polys[i].p.derivative();
  }
  return q;
287 288 289
}
template<int Degree>
PPolynomial<Degree+1> PPolynomial<Degree>::integral(void) const{
290 291 292 293 294 295 296 297 298
  int i;
  PPolynomial<Degree+1> q;
  q.set(polyCount);
  for(i=0;i<int(polyCount);i++){
    q.polys[i].start=polys[i].start;
    q.polys[i].p=polys[i].p.integral();
    q.polys[i].p-=q.polys[i].p(q.polys[i].start);
  }
  return q;
299 300 301 302 303 304 305 306
}
template<int Degree>
PPolynomial<Degree>& PPolynomial<Degree>::operator  += ( double s ) {polys[0].p+=s;}
template<int Degree>
PPolynomial<Degree>& PPolynomial<Degree>::operator  -= ( double s ) {polys[0].p-=s;}
template<int Degree>
PPolynomial<Degree>& PPolynomial<Degree>::operator  *= ( double s )
{
307 308
  for(int i=0;i<int(polyCount);i++){polys[i].p*=s;}
  return *this;
309 310 311 312
}
template<int Degree>
PPolynomial<Degree>& PPolynomial<Degree>::operator  /= ( double s )
{
313 314
  for(size_t i=0;i<polyCount;i++){polys[i].p/=s;}
  return *this;
315 316 317 318
}
template<int Degree>
PPolynomial<Degree> PPolynomial<Degree>::operator + ( double s ) const
{
319 320 321
  PPolynomial q=*this;
  q+=s;
  return q;
322 323 324 325
}
template<int Degree>
PPolynomial<Degree> PPolynomial<Degree>::operator - ( double s ) const
{
326 327 328
  PPolynomial q=*this;
  q-=s;
  return q;
329 330 331 332
}
template<int Degree>
PPolynomial<Degree> PPolynomial<Degree>::operator * ( double s ) const
{
333 334 335
  PPolynomial q=*this;
  q*=s;
  return q;
336 337 338 339
}
template<int Degree>
PPolynomial<Degree> PPolynomial<Degree>::operator / ( double s ) const
{
340 341 342
  PPolynomial q=*this;
  q/=s;
  return q;
343 344 345 346
}

template<int Degree>
void PPolynomial<Degree>::printnl(void) const{
347
  Polynomial<Degree> p;
348

349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
  if(!polyCount){
    Polynomial<Degree> p;
    printf("[-Infinity,Infinity]\n");
  }
  else{
    for(size_t i=0;i<polyCount;i++){
      printf("[");
      if    (polys[i  ].start== DBL_MAX){printf("Infinity,");}
      else if (polys[i  ].start==-DBL_MAX){printf("-Infinity,");}
      else                {printf("%f,",polys[i].start);}
      if(i+1==polyCount)          {printf("Infinity]\t");}
      else if (polys[i+1].start== DBL_MAX){printf("Infinity]\t");}
      else if (polys[i+1].start==-DBL_MAX){printf("-Infinity]\t");}
      else                {printf("%f]\t",polys[i+1].start);}
      p=p+polys[i].p;
      p.printnl();
    }
  }
  printf("\n");
368 369 370 371
}
template< >
PPolynomial< 0 > PPolynomial< 0 >::BSpline( double radius )
{
372 373
  PPolynomial q;
  q.set(2);
374

375 376
  q.polys[0].start=-radius;
  q.polys[1].start= radius;
377

378 379 380
  q.polys[0].p.coefficients[0]= 1.0;
  q.polys[1].p.coefficients[0]=-1.0;
  return q;
381
}
382
template<int Degree >
383 384
PPolynomial< Degree > PPolynomial<Degree>::BSpline( double radius )
{
385
  return PPolynomial< Degree-1 >::BSpline().MovingAverage( radius );
386 387 388 389
}
template<int Degree>
PPolynomial<Degree+1> PPolynomial<Degree>::MovingAverage( double radius )
{
390 391 392
  PPolynomial<Degree+1> A;
  Polynomial<Degree+1> p;
  StartingPolynomial<Degree+1>* sps;
393

394
  sps=static_cast<StartingPolynomial<Degree+1>*>(malloc(sizeof(StartingPolynomial<Degree+1>)*polyCount*2));
395

396 397 398 399 400 401 402 403 404 405
  for(int i=0;i<int(polyCount);i++){
    sps[2*i  ].start=polys[i].start-radius;
    sps[2*i+1].start=polys[i].start+radius;
    p=polys[i].p.integral()-polys[i].p.integral()(polys[i].start);
    sps[2*i  ].p=p.shift(-radius);
    sps[2*i+1].p=p.shift( radius)*-1;
  }
  A.set(sps,int(polyCount*2));
  free(sps);
  return A*1.0/(2*radius);
406 407 408
}
template<int Degree>
void PPolynomial<Degree>::getSolutions(double c,std::vector<double>& roots,double EPS,double min,double max) const{
409 410
  Polynomial<Degree> p;
  std::vector<double> tempRoots;
411

412 413 414 415 416 417 418 419 420 421 422 423
  p.setZero();
  for(size_t i=0;i<polyCount;i++){
    p+=polys[i].p;
    if(polys[i].start>max){break;}
    if(i<polyCount-1 && polys[i+1].start<min){continue;}
    p.getSolutions(c,tempRoots,EPS);
    for(size_t j=0;j<tempRoots.size();j++){
      if(tempRoots[j]>polys[i].start && (i+1==polyCount || tempRoots[j]<=polys[i+1].start)){
        if(tempRoots[j]>min && tempRoots[j]<max){roots.push_back(tempRoots[j]);}
      }
    }
  }
424 425 426 427
}

template<int Degree>
void PPolynomial<Degree>::write(FILE* fp,int samples,double min,double max) const{
428 429 430 431 432 433
  fwrite(&samples,sizeof(int),1,fp);
  for(int i=0;i<samples;i++){
    double x=min+i*(max-min)/(samples-1);
    float v=(*this)(x);
    fwrite(&v,sizeof(float),1,fp);
  }
434
}