00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #ifndef _BERNSTEINPOLY_H
00034 #define _BERNSTEINPOLY_H
00035
00036 #include <vector>
00037 #include <iostream>
00038
00039 namespace Go {
00042
00043
00044
00054 class BernsteinPoly {
00055 public:
00057 BernsteinPoly() { }
00061 explicit BernsteinPoly(const std::vector<double>& coefs)
00062 : coefs_(coefs) { }
00067 template <typename ForwardIterator>
00068 BernsteinPoly(ForwardIterator begin, ForwardIterator end)
00069 : coefs_(begin, end) { }
00072 explicit BernsteinPoly(double coef)
00073 : coefs_(std::vector<double>(1, coef)) { }
00074
00077 int degree() const
00078 { return coefs_.size() - 1; }
00079
00081 const double& operator[] (int num) const
00082 { return coefs_[num]; }
00084 double& operator[] (int num)
00085 { return coefs_[num]; }
00087 std::vector<double>::iterator coefsBegin()
00088 { return coefs_.begin(); }
00090 std::vector<double>::const_iterator coefsBegin() const
00091 { return coefs_.begin(); }
00093 std::vector<double>::iterator coefsEnd()
00094 { return coefs_.end(); }
00096 std::vector<double>::const_iterator coefsEnd() const
00097 { return coefs_.end(); }
00098
00103 double operator() (double t) const;
00104
00110 bool isZero(const double eps = 0.0) const;
00120 bool isStrictlyPositive(const double eps = 0.0) const;
00121
00128 double norm() const;
00132 void normalize();
00133
00138 BernsteinPoly deriv(int der) const;
00139
00142 double integral(double a = 0.0, double b = 1.0) const;
00143
00152 double blossom(const std::vector<double>& tvec) const;
00160 BernsteinPoly pickInterval(double a, double b) const;
00161
00165 void degreeElevate(int d);
00166
00168 BernsteinPoly& operator*= (const BernsteinPoly& poly);
00170 BernsteinPoly& operator*= (double c);
00171
00173 BernsteinPoly& operator+= (const BernsteinPoly& poly);
00175 BernsteinPoly& operator+= (double c);
00176
00178 BernsteinPoly& operator-= (const BernsteinPoly& poly);
00180 BernsteinPoly& operator-= (double c);
00181
00183 BernsteinPoly& operator/= (double c);
00184
00186 void read(std::istream& is);
00188 void write(std::ostream& os) const;
00189
00190 private:
00191 std::vector<double> coefs_;
00192 };
00193
00194
00196 inline BernsteinPoly operator* (const BernsteinPoly& p1,
00197 const BernsteinPoly& p2)
00198 {
00199 BernsteinPoly tmp = p1;
00200 tmp *= p2;
00201 return tmp;
00202 }
00203
00204
00206 inline BernsteinPoly operator* (const BernsteinPoly& p,
00207 double c)
00208 {
00209 BernsteinPoly tmp = p;
00210 tmp *= c;
00211 return tmp;
00212 }
00213
00214
00216 inline BernsteinPoly operator* (double c,
00217 const BernsteinPoly& p)
00218 {
00219 BernsteinPoly tmp = p;
00220 tmp *= c;
00221 return tmp;
00222 }
00223
00224
00226 inline BernsteinPoly operator+ (const BernsteinPoly& p1,
00227 const BernsteinPoly& p2)
00228 {
00229 BernsteinPoly tmp = p1;
00230 tmp += p2;
00231 return tmp;
00232 }
00233
00234
00236 inline BernsteinPoly operator+ (double c,
00237 const BernsteinPoly& p)
00238 {
00239 BernsteinPoly tmp = p;
00240 tmp += c;
00241 return tmp;
00242 }
00243
00244
00246 inline BernsteinPoly operator+ (const BernsteinPoly& p,
00247 double c)
00248 {
00249 BernsteinPoly tmp = p;
00250 tmp += c;
00251 return tmp;
00252 }
00253
00254
00256 inline BernsteinPoly operator- (const BernsteinPoly& p1,
00257 const BernsteinPoly& p2)
00258 {
00259 BernsteinPoly tmp = p1;
00260 tmp -= p2;
00261 return tmp;
00262 }
00263
00264
00266 inline BernsteinPoly operator- (double c,
00267 const BernsteinPoly& p)
00268 {
00269 BernsteinPoly tmp(c);
00270 tmp -= p;
00271 return tmp;
00272 }
00273
00274
00276 inline BernsteinPoly operator- (const BernsteinPoly& p,
00277 double c)
00278 {
00279 BernsteinPoly tmp = p;
00280 tmp -= c;
00281 return tmp;
00282 }
00283
00284
00286 inline BernsteinPoly operator/ (const BernsteinPoly& p,
00287 double c)
00288 {
00289 BernsteinPoly tmp = p;
00290 tmp /= c;
00291 return tmp;
00292 }
00293
00294
00296 }
00297
00298
00300 namespace std {
00303
00304
00305
00307 inline std::istream& operator >> (std::istream& is,
00308 Go::BernsteinPoly& p)
00309 {
00310 p.read(is);
00311 return is;
00312 }
00313
00314
00316 inline std::ostream& operator << (std::ostream& os,
00317 const Go::BernsteinPoly& p)
00318 {
00319 p.write(os);
00320 return os;
00321 }
00322
00323
00325 }
00326
00327
00328 #endif // _BERNSTEINPOLY_H
00329
00330