BWAPI
SPAR/AIModule/BWTA/vendors/CGAL/CGAL/Polynomial/Interpolator.h
Go to the documentation of this file.
00001 // Copyright (c) 2008 Max-Planck-Institute Saarbruecken (Germany)
00002 //
00003 // This file is part of CGAL (www.cgal.org); you can redistribute it and/or
00004 // modify it under the terms of the GNU Lesser General Public License as
00005 // published by the Free Software Foundation; version 2.1 of the License.
00006 // See the file LICENSE.LGPL distributed with CGAL.
00007 //
00008 // Licensees holding a valid commercial license may use this file in
00009 // accordance with the commercial license agreement provided with the software.
00010 //
00011 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 //
00014 // $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.5-branch/Polynomial/include/CGAL/Polynomial/Interpolator.h $
00015 // $Id: Interpolator.h 47300 2008-12-09 10:48:07Z hemmer $
00016 //
00017 //
00018 // Author(s)     : Michael Hemmer <hemmer@informatik.uni-mainz.de> 
00019 
00020 #ifndef CGAL_POLYNOMIAL_INTERPOLATE_H
00021 #define CGAL_POLYNOMIAL_INTERPOLATE_H
00022 
00023 CGAL_BEGIN_NAMESPACE
00024 namespace CGALi {
00025 
00026 // Class for interpolation of univariate or multivariate polynomials. 
00027 // The template argument must be a model of concept Polynomial_d
00028 // 
00029 //
00030 template <class Polynomial_d_>
00031 class Interpolator{
00032   typedef CGAL::Polynomial_traits_d<Polynomial_d_> PT;
00033     
00034 public:
00035   typedef typename PT::Polynomial_d Polynomial_d; 
00036   typedef typename PT::Coefficient_type Coeff; 
00037   typedef typename PT::Innermost_coefficient_type IC;
00038 
00039 private: 
00040   typedef typename CGAL::Coercion_traits<Coeff,IC>::Cast IC2Coeff;
00041   typedef typename PT::Construct_polynomial Construct;
00042 
00043   std::vector<IC> xvals; 
00044   std::vector<Coeff> yvals; 
00045   std::vector<Coeff> b; 
00046     
00047   bool valid; 
00048   Polynomial_d interpolant; 
00049 
00050 
00051   Coeff eval_newton(int n, IC z)
00052   {
00053     Coeff p(b[n]);
00054     for (int i = n-1; i >=0; i--){
00055       Coeff tmp(IC2Coeff()((z - xvals[i])));
00056       p = p * tmp + b[i];
00057     }
00058     return p;
00059   }
00060 
00061 
00062   Polynomial_d eval_newton_poly(int n)
00063   {    
00064     CGAL_precondition(n >=0);
00065     Polynomial_d p(Construct()(b[n]));
00066     for (int i = n-1; i >=0; i--) {
00067       Polynomial_d tmp = Construct()(IC2Coeff()(-xvals[i]),Coeff(1));
00068       p = (p * tmp) + b[i];
00069     }
00070     return p;
00071   }
00072     
00073 public:
00074   Interpolator(){};
00075   
00076   // constructor from an InputIterator range with value type std::pair<IC,Coeff> 
00077   template<class InputIterator>
00078   Interpolator(InputIterator begin, InputIterator end){
00079     for(InputIterator it = begin; it != end; it++){
00080       add_interpolation_point(*it);
00081     }
00082   }
00083     
00084   /*
00085     Interpolator(std::vector<IC> xvals_, std::vector<Coeff> yvals_)
00086     : valid(false) {
00087     CGAL_precondition(xvals_.size() != 0);
00088     CGAL_precondition(xvals_.size() == yvals_.size());
00089     for(unsigned i = 0; i < xvals_.size();  i++){
00090     add_interpolation_point(xvals_[i],yvals_[i]);
00091     }
00092     }
00093   */
00094     
00095   // void add_interpolation_point(std::pair<IC,Coeff> point){
00096   //    add_interpolation_point(point.first, point.second);
00097   // }
00098     
00099   // void add_interpolation_point(IC xval, Coeff yval){
00100   void add_interpolation_point(std::pair<IC,Coeff> point){
00101     valid = false;
00102 //        CGAL_precondition(0 == std::count(xval, xvals.begin(), yvals.end()));
00103     xvals.push_back(point.first);
00104     yvals.push_back(point.second);
00105         
00106     Coeff num, den;
00107     int k = xvals.size() - 1; 
00108     if(k == 0){
00109       b.push_back(yvals[0]);
00110     }else{
00111       num = yvals[k] - eval_newton(k-1,xvals[k]);    
00112       den = Coeff(1);            
00113       for (int j = 0; j < k; j++) {
00114         // (k-j) if xvals's are sequential
00115         den *= (xvals[k] - xvals[j]);
00116       }
00117       b.push_back(num / den);
00118     }
00119   }
00120     
00121   Polynomial_d get_interpolant(){ 
00122     if (xvals.size() == 0) return Polynomial_d(0);
00123     // TODO: compute new interpolant from old interpolant ?
00124     if(!valid)
00125       interpolant = eval_newton_poly(xvals.size()-1);
00126     return interpolant; 
00127   }
00128     
00129 };
00130 
00131 } // namespace CGALi
00132 CGAL_END_NAMESPACE
00133 
00134 #endif // CGAL_POLYNOMIAL_INTERPOLATE_H 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines