| 1 |
/* |
| 2 |
Copyright (c) 2006, Michael Kazhdan and Matthew Bolitho |
| 3 |
All rights reserved. |
| 4 |
|
| 5 |
Redistribution and use in source and binary forms, with or without modification, |
| 6 |
are permitted provided that the following conditions are met: |
| 7 |
|
| 8 |
Redistributions of source code must retain the above copyright notice, this list of |
| 9 |
conditions and the following disclaimer. Redistributions in binary form must reproduce |
| 10 |
the above copyright notice, this list of conditions and the following disclaimer |
| 11 |
in the documentation and/or other materials provided with the distribution. |
| 12 |
|
| 13 |
Neither the name of the Johns Hopkins University nor the names of its contributors |
| 14 |
may be used to endorse or promote products derived from this software without specific |
| 15 |
prior written permission. |
| 16 |
|
| 17 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 18 |
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO THE IMPLIED WARRANTIES |
| 19 |
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
| 20 |
SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 |
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 22 |
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 23 |
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 24 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 25 |
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
| 26 |
DAMAGE. |
| 27 |
*/ |
| 28 |
|
| 29 |
//////////////// |
| 30 |
// Polynomial // |
| 31 |
//////////////// |
| 32 |
template<int Degree> |
| 33 |
Polynomial<Degree>::Polynomial(void){memset(coefficients,0,sizeof(double)*(Degree+1));} |
| 34 |
template<int Degree> |
| 35 |
template<int Degree2> |
| 36 |
Polynomial<Degree>::Polynomial(const Polynomial<Degree2>& P){ |
| 37 |
memset(coefficients,0,sizeof(double)*(Degree+1)); |
| 38 |
for(int i=0;i<=Degree && i<=Degree2;i++){coefficients[i]=P.coefficients[i];} |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
template<int Degree> |
| 43 |
template<int Degree2> |
| 44 |
Polynomial<Degree>& Polynomial<Degree>::operator = (const Polynomial<Degree2> &p){ |
| 45 |
int d=Degree<Degree2?Degree:Degree2; |
| 46 |
memset(coefficients,0,sizeof(double)*(Degree+1)); |
| 47 |
memcpy(coefficients,p.coefficients,sizeof(double)*(d+1)); |
| 48 |
return *this; |
| 49 |
} |
| 50 |
|
| 51 |
template<int Degree> |
| 52 |
Polynomial<Degree+1> Polynomial<Degree>::integral(void) const{ |
| 53 |
Polynomial<Degree+1> p; |
| 54 |
p.coefficients[0]=0; |
| 55 |
for(int i=0;i<=Degree;i++){p.coefficients[i+1]=coefficients[i]/(i+1);} |
| 56 |
return p; |
| 57 |
} |
| 58 |
|
| 59 |
template< > inline double Polynomial< 0 >::operator() ( double t ) const { return coefficients[0]; } |
| 60 |
template< > inline double Polynomial< 1 >::operator() ( double t ) const { return coefficients[0]+coefficients[1]*t; } |
| 61 |
template< > inline double Polynomial< 2 >::operator() ( double t ) const { return coefficients[0]+(coefficients[1]+coefficients[2]*t)*t; } |
| 62 |
template< int Degree > |
| 63 |
double Polynomial<Degree>::operator() ( double t ) const{ |
| 64 |
double v=coefficients[Degree]; |
| 65 |
for( int d=Degree-1 ; d>=0 ; d-- ) v = v*t + coefficients[d]; |
| 66 |
return v; |
| 67 |
} |
| 68 |
template<int Degree> |
| 69 |
double Polynomial<Degree>::integral( double tMin , double tMax ) const |
| 70 |
{ |
| 71 |
double v=0; |
| 72 |
double t1,t2; |
| 73 |
t1=tMin; |
| 74 |
t2=tMax; |
| 75 |
for(int i=0;i<=Degree;i++){ |
| 76 |
v+=coefficients[i]*(t2-t1)/(i+1); |
| 77 |
if(t1!=-DBL_MAX && t1!=DBL_MAX){t1*=tMin;} |
| 78 |
if(t2!=-DBL_MAX && t2!=DBL_MAX){t2*=tMax;} |
| 79 |
} |
| 80 |
return v; |
| 81 |
} |
| 82 |
template<int Degree> |
| 83 |
int Polynomial<Degree>::operator == (const Polynomial& p) const{ |
| 84 |
for(int i=0;i<=Degree;i++){if(coefficients[i]!=p.coefficients[i]){return 0;}} |
| 85 |
return 1; |
| 86 |
} |
| 87 |
template<int Degree> |
| 88 |
int Polynomial<Degree>::operator != (const Polynomial& p) const{ |
| 89 |
for(int i=0;i<=Degree;i++){if(coefficients[i]==p.coefficients[i]){return 0;}} |
| 90 |
return 1; |
| 91 |
} |
| 92 |
template<int Degree> |
| 93 |
int Polynomial<Degree>::isZero(void) const{ |
| 94 |
for(int i=0;i<=Degree;i++){if(coefficients[i]!=0){return 0;}} |
| 95 |
return 1; |
| 96 |
} |
| 97 |
template<int Degree> |
| 98 |
void Polynomial<Degree>::setZero(void){memset(coefficients,0,sizeof(double)*(Degree+1));} |
| 99 |
|
| 100 |
template<int Degree> |
| 101 |
Polynomial<Degree>& Polynomial<Degree>::addScaled(const Polynomial& p,double s){ |
| 102 |
for(int i=0;i<=Degree;i++){coefficients[i]+=p.coefficients[i]*s;} |
| 103 |
return *this; |
| 104 |
} |
| 105 |
template<int Degree> |
| 106 |
Polynomial<Degree>& Polynomial<Degree>::operator += (const Polynomial<Degree>& p){ |
| 107 |
for(int i=0;i<=Degree;i++){coefficients[i]+=p.coefficients[i];} |
| 108 |
return *this; |
| 109 |
} |
| 110 |
template<int Degree> |
| 111 |
Polynomial<Degree>& Polynomial<Degree>::operator -= (const Polynomial<Degree>& p){ |
| 112 |
for(int i=0;i<=Degree;i++){coefficients[i]-=p.coefficients[i];} |
| 113 |
return *this; |
| 114 |
} |
| 115 |
template<int Degree> |
| 116 |
Polynomial<Degree> Polynomial<Degree>::operator + (const Polynomial<Degree>& p) const{ |
| 117 |
Polynomial q; |
| 118 |
for(int i=0;i<=Degree;i++){q.coefficients[i]=(coefficients[i]+p.coefficients[i]);} |
| 119 |
return q; |
| 120 |
} |
| 121 |
template<int Degree> |
| 122 |
Polynomial<Degree> Polynomial<Degree>::operator - (const Polynomial<Degree>& p) const{ |
| 123 |
Polynomial q; |
| 124 |
for(int i=0;i<=Degree;i++) {q.coefficients[i]=coefficients[i]-p.coefficients[i];} |
| 125 |
return q; |
| 126 |
} |
| 127 |
template<int Degree> |
| 128 |
void Polynomial<Degree>::Scale(const Polynomial& p,double w,Polynomial& q){ |
| 129 |
for(int i=0;i<=Degree;i++){q.coefficients[i]=p.coefficients[i]*w;} |
| 130 |
} |
| 131 |
template<int Degree> |
| 132 |
void Polynomial<Degree>::AddScaled(const Polynomial& p1,double w1,const Polynomial& p2,double w2,Polynomial& q){ |
| 133 |
for(int i=0;i<=Degree;i++){q.coefficients[i]=p1.coefficients[i]*w1+p2.coefficients[i]*w2;} |
| 134 |
} |
| 135 |
template<int Degree> |
| 136 |
void Polynomial<Degree>::AddScaled(const Polynomial& p1,double w1,const Polynomial& p2,Polynomial& q){ |
| 137 |
for(int i=0;i<=Degree;i++){q.coefficients[i]=p1.coefficients[i]*w1+p2.coefficients[i];} |
| 138 |
} |
| 139 |
template<int Degree> |
| 140 |
void Polynomial<Degree>::AddScaled(const Polynomial& p1,const Polynomial& p2,double w2,Polynomial& q){ |
| 141 |
for(int i=0;i<=Degree;i++){q.coefficients[i]=p1.coefficients[i]+p2.coefficients[i]*w2;} |
| 142 |
} |
| 143 |
|
| 144 |
template<int Degree> |
| 145 |
void Polynomial<Degree>::Subtract(const Polynomial &p1,const Polynomial& p2,Polynomial& q){ |
| 146 |
for(int i=0;i<=Degree;i++){q.coefficients[i]=p1.coefficients[i]-p2.coefficients[i];} |
| 147 |
} |
| 148 |
template<int Degree> |
| 149 |
void Polynomial<Degree>::Negate(const Polynomial& in,Polynomial& out){ |
| 150 |
out=in; |
| 151 |
for(int i=0;i<=Degree;i++){out.coefficients[i]=-out.coefficients[i];} |
| 152 |
} |
| 153 |
|
| 154 |
template<int Degree> |
| 155 |
Polynomial<Degree> Polynomial<Degree>::operator - (void) const{ |
| 156 |
Polynomial q=*this; |
| 157 |
for(int i=0;i<=Degree;i++){q.coefficients[i]=-q.coefficients[i];} |
| 158 |
return q; |
| 159 |
} |
| 160 |
template<int Degree> |
| 161 |
template<int Degree2> |
| 162 |
Polynomial<Degree+Degree2> Polynomial<Degree>::operator * (const Polynomial<Degree2>& p) const{ |
| 163 |
Polynomial<Degree+Degree2> q; |
| 164 |
for(int i=0;i<=Degree;i++){for(int j=0;j<=Degree2;j++){q.coefficients[i+j]+=coefficients[i]*p.coefficients[j];}} |
| 165 |
return q; |
| 166 |
} |
| 167 |
|
| 168 |
template<int Degree> |
| 169 |
Polynomial<Degree>& Polynomial<Degree>::operator += ( double s ) |
| 170 |
{ |
| 171 |
coefficients[0]+=s; |
| 172 |
return *this; |
| 173 |
} |
| 174 |
template<int Degree> |
| 175 |
Polynomial<Degree>& Polynomial<Degree>::operator -= ( double s ) |
| 176 |
{ |
| 177 |
coefficients[0]-=s; |
| 178 |
return *this; |
| 179 |
} |
| 180 |
template<int Degree> |
| 181 |
Polynomial<Degree>& Polynomial<Degree>::operator *= ( double s ) |
| 182 |
{ |
| 183 |
for(int i=0;i<=Degree;i++){coefficients[i]*=s;} |
| 184 |
return *this; |
| 185 |
} |
| 186 |
template<int Degree> |
| 187 |
Polynomial<Degree>& Polynomial<Degree>::operator /= ( double s ) |
| 188 |
{ |
| 189 |
for(int i=0;i<=Degree;i++){coefficients[i]/=s;} |
| 190 |
return *this; |
| 191 |
} |
| 192 |
template<int Degree> |
| 193 |
Polynomial<Degree> Polynomial<Degree>::operator + ( double s ) const |
| 194 |
{ |
| 195 |
Polynomial<Degree> q=*this; |
| 196 |
q.coefficients[0]+=s; |
| 197 |
return q; |
| 198 |
} |
| 199 |
template<int Degree> |
| 200 |
Polynomial<Degree> Polynomial<Degree>::operator - ( double s ) const |
| 201 |
{ |
| 202 |
Polynomial q=*this; |
| 203 |
q.coefficients[0]-=s; |
| 204 |
return q; |
| 205 |
} |
| 206 |
template<int Degree> |
| 207 |
Polynomial<Degree> Polynomial<Degree>::operator * ( double s ) const |
| 208 |
{ |
| 209 |
Polynomial q; |
| 210 |
for(int i=0;i<=Degree;i++){q.coefficients[i]=coefficients[i]*s;} |
| 211 |
return q; |
| 212 |
} |
| 213 |
template<int Degree> |
| 214 |
Polynomial<Degree> Polynomial<Degree>::operator / ( double s ) const |
| 215 |
{ |
| 216 |
Polynomial q; |
| 217 |
for( int i=0 ; i<=Degree ; i++ ) q.coefficients[i] = coefficients[i]/s; |
| 218 |
return q; |
| 219 |
} |
| 220 |
template<int Degree> |
| 221 |
Polynomial<Degree> Polynomial<Degree>::scale( double s ) const |
| 222 |
{ |
| 223 |
Polynomial q=*this; |
| 224 |
double s2=1.0; |
| 225 |
for(int i=0;i<=Degree;i++){ |
| 226 |
q.coefficients[i]*=s2; |
| 227 |
s2/=s; |
| 228 |
} |
| 229 |
return q; |
| 230 |
} |
| 231 |
template<int Degree> |
| 232 |
Polynomial<Degree> Polynomial<Degree>::shift( double t ) const |
| 233 |
{ |
| 234 |
Polynomial<Degree> q; |
| 235 |
for(int i=0;i<=Degree;i++){ |
| 236 |
double temp=1; |
| 237 |
for(int j=i;j>=0;j--){ |
| 238 |
q.coefficients[j]+=coefficients[i]*temp; |
| 239 |
temp*=-t*j; |
| 240 |
temp/=(i-j+1); |
| 241 |
} |
| 242 |
} |
| 243 |
return q; |
| 244 |
} |
| 245 |
template<int Degree> |
| 246 |
void Polynomial<Degree>::printnl(void) const{ |
| 247 |
for(int j=0;j<=Degree;j++){ |
| 248 |
printf("%6.4f x^%d ",coefficients[j],j); |
| 249 |
if(j<Degree && coefficients[j+1]>=0){printf("+");} |
| 250 |
} |
| 251 |
printf("\n"); |
| 252 |
} |
| 253 |
|
| 254 |
template< > |
| 255 |
inline int Polynomial< 1 >::getSolutions( double c , double* roots , double EPS ) const |
| 256 |
{ |
| 257 |
std::complex< double > _roots[4]; |
| 258 |
int _rCount = Factor( coefficients[1] , coefficients[0]-c , _roots , EPS ); |
| 259 |
int rCount = 0; |
| 260 |
for( int i=0 ; i<_rCount ; i++ ) if( fabs( _roots[i].imag() )<=EPS ) roots[rCount++] = _roots[i].real(); |
| 261 |
return rCount; |
| 262 |
} |
| 263 |
|
| 264 |
template< > |
| 265 |
inline int Polynomial< 2 >::getSolutions( double c , double* roots , double EPS ) const |
| 266 |
{ |
| 267 |
std::complex< double > _roots[4]; |
| 268 |
int _rCount = Factor( coefficients[2] , coefficients[1] , coefficients[0]-c , _roots , EPS ); |
| 269 |
int rCount = 0; |
| 270 |
for( int i=0 ; i<_rCount ; i++ ) if( fabs( _roots[i].imag() )<=EPS ) roots[rCount++] = _roots[i].real(); |
| 271 |
return rCount; |
| 272 |
} |
| 273 |
|
| 274 |
template< > |
| 275 |
inline int Polynomial< 3 >::getSolutions( double c , double* roots , double EPS ) const |
| 276 |
{ |
| 277 |
std::complex< double > _roots[4]; |
| 278 |
int _rCount = Factor( coefficients[3] , coefficients[2] , coefficients[1] , coefficients[0]-c , _roots , EPS ); |
| 279 |
int rCount = 0; |
| 280 |
for( int i=0 ; i<_rCount ; i++ ) if( fabs( _roots[i].imag() )<=EPS ) roots[rCount++] = _roots[i].real(); |
| 281 |
return rCount; |
| 282 |
} |
| 283 |
|
| 284 |
#if 0 |
| 285 |
template< > |
| 286 |
inline int Polynomial< 4 >::getSolutions( double c , double* roots , double EPS ) const |
| 287 |
{ |
| 288 |
std::complex< double > _roots[4]; |
| 289 |
int _rCount = Factor( coefficients[4] , coefficients[3] , coefficients[2] , coefficients[1] , coefficients[0]-c , _roots , EPS ); |
| 290 |
int rCount = 0; |
| 291 |
for( int i=0 ; i<_rCount ; i++ ) if( fabs( _roots[i].imag() )<=EPS ) roots[rCount++] = _roots[i].real(); |
| 292 |
return rCount; |
| 293 |
} |
| 294 |
#endif |
| 295 |
|
| 296 |
template< int Degree > |
| 297 |
int Polynomial<Degree>::getSolutions( double c , double* roots , double EPS ) const |
| 298 |
{ |
| 299 |
MK_THROW( "Can't solve polynomial of degree: " , Degree ); |
| 300 |
return 0; |
| 301 |
} |
| 302 |
|
| 303 |
// The 0-th order B-spline |
| 304 |
template< > |
| 305 |
inline Polynomial< 0 > Polynomial< 0 >::BSplineComponent( int i ) |
| 306 |
{ |
| 307 |
Polynomial p; |
| 308 |
p.coefficients[0] = 1.; |
| 309 |
return p; |
| 310 |
} |
| 311 |
|
| 312 |
// The Degree-th order B-spline |
| 313 |
template< int Degree > |
| 314 |
Polynomial< Degree > Polynomial< Degree >::BSplineComponent( int i ) |
| 315 |
{ |
| 316 |
// B_d^i(x) = \int_x^1 B_{d-1}^{i}(y) dy + \int_0^x B_{d-1}^{i-1} y dy |
| 317 |
// = \int_0^1 B_{d-1}^{i}(y) dy - \int_0^x B_{d-1}^{i}(y) dy + \int_0^x B_{d-1}^{i-1} y dy |
| 318 |
Polynomial p; |
| 319 |
if( i<Degree ) |
| 320 |
{ |
| 321 |
Polynomial< Degree > _p = Polynomial< Degree-1 >::BSplineComponent( i ).integral(); |
| 322 |
p -= _p; |
| 323 |
p.coefficients[0] += _p(1); |
| 324 |
} |
| 325 |
if( i>0 ) |
| 326 |
{ |
| 327 |
Polynomial< Degree > _p = Polynomial< Degree-1 >::BSplineComponent( i-1 ).integral(); |
| 328 |
p += _p; |
| 329 |
} |
| 330 |
return p; |
| 331 |
} |
| 332 |
|
| 333 |
|
| 334 |
// The 0-th order B-spline values |
| 335 |
template< > inline void Polynomial< 0 >::BSplineComponentValues( double x , double* values ){ values[0] = 1.; } |
| 336 |
// The Degree-th order B-spline |
| 337 |
template< int Degree > void Polynomial< Degree >::BSplineComponentValues( double x , double* values ) |
| 338 |
{ |
| 339 |
const double Scale = 1./Degree; |
| 340 |
Polynomial< Degree-1 >::BSplineComponentValues( x , values+1 ); |
| 341 |
values[0] = values[1] * (1.-x) * Scale; |
| 342 |
for( int i=1 ; i<Degree ; i++ ) |
| 343 |
{ |
| 344 |
double x1 = (x-i+Degree) , x2 = (-x+i+1); |
| 345 |
values[i] = ( values[i]*x1 + values[i+1]*x2 ) * Scale; |
| 346 |
} |
| 347 |
values[Degree] *= x * Scale; |
| 348 |
} |
| 349 |
|
| 350 |
// Using the recurrence formulation for Pascal's triangle |
| 351 |
template< > inline void Polynomial< 0 >::BinomialCoefficients( int bCoefficients[1] ){ bCoefficients[0] = 1; } |
| 352 |
|
| 353 |
template< int Degree > void Polynomial< Degree >::BinomialCoefficients( int bCoefficients[Degree+1] ) |
| 354 |
{ |
| 355 |
Polynomial< Degree-1 >::BinomialCoefficients( bCoefficients ); |
| 356 |
int leftValue = 0; |
| 357 |
for( int i=0 ; i<Degree ; i++ ) |
| 358 |
{ |
| 359 |
int temp = bCoefficients[i]; |
| 360 |
bCoefficients[i] += leftValue; |
| 361 |
leftValue = temp; |
| 362 |
} |
| 363 |
bCoefficients[Degree] = 1; |
| 364 |
} |