// @(#)root/mathmore:$Id$
// Authors: L. Moneta, A. Zsenei   08/2005

 /**********************************************************************
  *                                                                    *
  * Copyright (c) 2004 ROOT Foundation,  CERN/PH-SFT                   *
  *                                                                    *
  * This library is free software; you can redistribute it and/or      *
  * modify it under the terms of the GNU General Public License        *
  * as published by the Free Software Foundation; either version 2     *
  * of the License, or (at your option) any later version.             *
  *                                                                    *
  * This library is distributed in the hope that it will be useful,    *
  * but WITHOUT ANY WARRANTY; without even the implied warranty of     *
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU   *
  * General Public License for more details.                           *
  *                                                                    *
  * You should have received a copy of the GNU General Public License  *
  * along with this library (see file COPYING); if not, write          *
  * to the Free Software Foundation, Inc., 59 Temple Place, Suite      *
  * 330, Boston, MA 02111-1307 USA, or contact the author.             *
  *                                                                    *
  **********************************************************************/

// Header file for class Polynomial
//
// Created by: Lorenzo Moneta  at Wed Nov 10 17:46:19 2004
//
// Last update: Wed Nov 10 17:46:19 2004
//
#ifndef ROOT_Math_Polynomial
#define ROOT_Math_Polynomial

#include <complex>

#include "Math/ParamFunction.h"

// #ifdef _WIN32
// #pragma warning(disable : 4250)
// #endif

namespace ROOT {
namespace Math {

//_____________________________________________________________________________________
  /**
     Parametric Function class describing polynomials of order n.

     <em>P(x) = p[0] + p[1]*x + p[2]*x**2 + ....... + p[n]*x**n</em>

     The class implements also the derivatives, \a dP(x)/dx and the \a dP(x)/dp[i].

     The class provides also the method to find the roots of the polynomial.
     It uses analytical methods up to quartic polynomials.

     Implements both the Parameteric function interface and the gradient interface
     since it provides the analytical gradient with respect to x


     @ingroup ParamFunc
  */

class Polynomial : public ParamFunction<IParamGradFunction>,
                   public IGradientOneDim
{


public:

 typedef  ParamFunction<IParamGradFunction> ParFunc;
   /**
      Construct a Polynomial function of order n.
      The number of Parameters is n+1.
   */

   Polynomial(unsigned int n = 0);

   /**
      Construct a Polynomial of degree  1 : a*x + b
   */
   Polynomial(double a, double b);

   /**
      Construct a Polynomial of degree  2 : a*x**2 + b*x + c
   */
   Polynomial(double a, double b, double c);

   /**
      Construct a Polynomial of degree  3 : a*x**3 + b*x**2 + c*x + d
   */
   Polynomial(double a, double b, double c, double d);

   /**
      Construct a Polynomial of degree  4 : a*x**4 + b*x**3 + c*x**2 + dx  + e
   */
   Polynomial(double a, double b, double c, double d, double e);


   virtual ~Polynomial() {}

   // use default copy-ctor and assignment operators



//   using ParamFunction::operator();


   /**
      Find the polynomial roots.
      For n <= 4, the roots are found analytically while for larger order an iterative numerical method is used
      The numerical method used is from GSL (see <A HREF="http://www.gnu.org/software/gsl/manual/gsl-ref_6.html#SEC53" )
   */
   const std::vector<std::complex <double> > & FindRoots();


   /**
      Find the only the real polynomial roots.
      For n <= 4, the roots are found analytically while for larger order an iterative numerical method is used
      The numerical method used is from GSL (see <A HREF="http://www.gnu.org/software/gsl/manual/gsl-ref_6.html#SEC53" )
   */
   std::vector<double > FindRealRoots();


   /**
      Find the polynomial roots using always an iterative numerical methods
      The numerical method used is from GSL (see <A HREF="http://www.gnu.org/software/gsl/manual/gsl-ref_6.html#SEC53" )
   */
   const std::vector<std::complex <double> > & FindNumRoots();

   /**
      Order of Polynomial
   */
   unsigned int Order() const { return fOrder; }


   IGenFunction * Clone() const;

   /**
       Optimized method to evaluate at the same time the function value and derivative at a point x.
       Implement the interface specified bby ROOT::Math::IGradientOneDim.
       In the case of polynomial there is no advantage to compute both at the same time
   */
   void FdF (double x, double & f, double & df) const {
      f = (*this)(x);
      df = Derivative(x);
   }


private:

   double DoEvalPar ( double x, const double * p ) const ;

   double DoDerivative (double x) const ;

   double DoParameterDerivative(double x, const double * p, unsigned int ipar) const;


   // cache order = number of params - 1)
   unsigned int fOrder;

   // cache Parameters for Gradient
   mutable std::vector<double> fDerived_params;

   // roots

   std::vector< std::complex < double > > fRoots;

};

} // namespace Math
} // namespace ROOT


#endif /* ROOT_Math_Polynomial */
 Polynomial.h:1
 Polynomial.h:2
 Polynomial.h:3
 Polynomial.h:4
 Polynomial.h:5
 Polynomial.h:6
 Polynomial.h:7
 Polynomial.h:8
 Polynomial.h:9
 Polynomial.h:10
 Polynomial.h:11
 Polynomial.h:12
 Polynomial.h:13
 Polynomial.h:14
 Polynomial.h:15
 Polynomial.h:16
 Polynomial.h:17
 Polynomial.h:18
 Polynomial.h:19
 Polynomial.h:20
 Polynomial.h:21
 Polynomial.h:22
 Polynomial.h:23
 Polynomial.h:24
 Polynomial.h:25
 Polynomial.h:26
 Polynomial.h:27
 Polynomial.h:28
 Polynomial.h:29
 Polynomial.h:30
 Polynomial.h:31
 Polynomial.h:32
 Polynomial.h:33
 Polynomial.h:34
 Polynomial.h:35
 Polynomial.h:36
 Polynomial.h:37
 Polynomial.h:38
 Polynomial.h:39
 Polynomial.h:40
 Polynomial.h:41
 Polynomial.h:42
 Polynomial.h:43
 Polynomial.h:44
 Polynomial.h:45
 Polynomial.h:46
 Polynomial.h:47
 Polynomial.h:48
 Polynomial.h:49
 Polynomial.h:50
 Polynomial.h:51
 Polynomial.h:52
 Polynomial.h:53
 Polynomial.h:54
 Polynomial.h:55
 Polynomial.h:56
 Polynomial.h:57
 Polynomial.h:58
 Polynomial.h:59
 Polynomial.h:60
 Polynomial.h:61
 Polynomial.h:62
 Polynomial.h:63
 Polynomial.h:64
 Polynomial.h:65
 Polynomial.h:66
 Polynomial.h:67
 Polynomial.h:68
 Polynomial.h:69
 Polynomial.h:70
 Polynomial.h:71
 Polynomial.h:72
 Polynomial.h:73
 Polynomial.h:74
 Polynomial.h:75
 Polynomial.h:76
 Polynomial.h:77
 Polynomial.h:78
 Polynomial.h:79
 Polynomial.h:80
 Polynomial.h:81
 Polynomial.h:82
 Polynomial.h:83
 Polynomial.h:84
 Polynomial.h:85
 Polynomial.h:86
 Polynomial.h:87
 Polynomial.h:88
 Polynomial.h:89
 Polynomial.h:90
 Polynomial.h:91
 Polynomial.h:92
 Polynomial.h:93
 Polynomial.h:94
 Polynomial.h:95
 Polynomial.h:96
 Polynomial.h:97
 Polynomial.h:98
 Polynomial.h:99
 Polynomial.h:100
 Polynomial.h:101
 Polynomial.h:102
 Polynomial.h:103
 Polynomial.h:104
 Polynomial.h:105
 Polynomial.h:106
 Polynomial.h:107
 Polynomial.h:108
 Polynomial.h:109
 Polynomial.h:110
 Polynomial.h:111
 Polynomial.h:112
 Polynomial.h:113
 Polynomial.h:114
 Polynomial.h:115
 Polynomial.h:116
 Polynomial.h:117
 Polynomial.h:118
 Polynomial.h:119
 Polynomial.h:120
 Polynomial.h:121
 Polynomial.h:122
 Polynomial.h:123
 Polynomial.h:124
 Polynomial.h:125
 Polynomial.h:126
 Polynomial.h:127
 Polynomial.h:128
 Polynomial.h:129
 Polynomial.h:130
 Polynomial.h:131
 Polynomial.h:132
 Polynomial.h:133
 Polynomial.h:134
 Polynomial.h:135
 Polynomial.h:136
 Polynomial.h:137
 Polynomial.h:138
 Polynomial.h:139
 Polynomial.h:140
 Polynomial.h:141
 Polynomial.h:142
 Polynomial.h:143
 Polynomial.h:144
 Polynomial.h:145
 Polynomial.h:146
 Polynomial.h:147
 Polynomial.h:148
 Polynomial.h:149
 Polynomial.h:150
 Polynomial.h:151
 Polynomial.h:152
 Polynomial.h:153
 Polynomial.h:154
 Polynomial.h:155
 Polynomial.h:156
 Polynomial.h:157
 Polynomial.h:158
 Polynomial.h:159
 Polynomial.h:160
 Polynomial.h:161
 Polynomial.h:162
 Polynomial.h:163
 Polynomial.h:164
 Polynomial.h:165
 Polynomial.h:166
 Polynomial.h:167
 Polynomial.h:168
 Polynomial.h:169
 Polynomial.h:170
 Polynomial.h:171
 Polynomial.h:172
 Polynomial.h:173
 Polynomial.h:174