casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Polynomial.h
Go to the documentation of this file.
00001 //# Polynomial.h: A one dimensional polynomial class
00002 //# Copyright (C) 1994,1995,1996,2001,2002,2005
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id: Polynomial.h 21024 2011-03-01 11:46:18Z gervandiepen $
00027 
00028 #ifndef SCIMATH_POLYNOMIAL_H
00029 #define SCIMATH_POLYNOMIAL_H
00030 
00031 //# Includes
00032 #include <casa/aips.h>
00033 #include <scimath/Functionals/PolynomialParam.h>
00034 #include <scimath/Functionals/Function1D.h>
00035 #include <scimath/Mathematics/AutoDiff.h>
00036 #include <scimath/Mathematics/AutoDiffMath.h>
00037 
00038 namespace casa { //# NAMESPACE CASA - BEGIN
00039 
00040 //# Forward declarations
00041 
00042 // <summary> A one dimensional polynomial class
00043 // </summary>
00044 
00045 // <reviewed reviewer="tcornwel" date="1996/02/22" tests="tPolynomial"
00046 // demos="">
00047 // </reviewed>
00048 
00049 // <prerequisite>
00050 //   <li> <linkto class=Function>Function</linkto>
00051 // </prerequisite>
00052 //
00053 // <synopsis> 
00054 // A Polynomial<T> contains a set of coefficients; its fundamental operations
00055 // is evaluating itself at some "x". The number of coefficients is the order
00056 // of the polynomial plus one, so is the number of available parameters. 
00057 //
00058 // <note role=tip>
00059 // The present implementation merely stores the coefficients in a Block. In the
00060 // unlikely case that we need to deal with polynomials with many zero
00061 // coefficients, a more efficient representation would be possible.
00062 // </note>
00063 // </synopsis> 
00064 //
00065 // <example>
00066 // <srcblock>
00067 //  Polynomial<Float> pf(3); // Third order polynomial - coeffs 0 by default
00068 //  pf.setCoefficient(1, 1.0);
00069 //  pf[2] = 2.0;
00070 //  pf.setCoefficient(3, 3.0);  // 3x^3 + 2x^2 + x
00071 //  pf(2); // == 34
00072 // </srcblock>
00073 // </example>
00074 
00075 // <templating arg=T>
00076 //  <li> T should have standard numerical operators. Current
00077 //      implementation only tested for real types (and their AutoDiffs).
00078 // </templating>
00079 
00080 // <thrown>
00081 //    <li> Assertion in debug mode if attempt is made to address incorrect
00082 //              coefficients
00083 // </thrown>
00084 
00085 // <todo asof="1995/08/25">
00086 //   <li> Global functions to make various ``special'' polynomials of various
00087 //   orders will be useful eventually.
00088 // </todo>
00089 
00090 template<class T> class Polynomial: public PolynomialParam<T> {
00091 public:
00092   //# Enumerations
00093   
00094   //# Constructors
00095   // Constructs a zero'th order polynomial, with a coeficcient of 0.0.
00096   Polynomial() : PolynomialParam<T>() {}
00097   // Makes a polynomial of the given order, with all coeficcients set to
00098   // zero. 
00099   explicit Polynomial(uInt order) : PolynomialParam<T>(order) {}
00100   // Copy constructor/assignment (deep copy)
00101   // <group>
00102   Polynomial(const Polynomial<T> &other) : PolynomialParam<T>(other) {}
00103   template <class W>
00104   Polynomial(const Polynomial<W> &other) : PolynomialParam<T>(other) {}
00105   Polynomial<T> &operator=(const Polynomial<T> &other) {
00106     PolynomialParam<T>::operator=(other); return *this; }
00107   // </group>
00108   
00109   // Destructor
00110   virtual ~Polynomial() {}
00111   
00112   //# Operators    
00113   // Evaluate the polynomial at <src>x</src>.
00114   virtual T eval(typename Function1D<T>::FunctionArg x) const;
00115   
00116   //# Member functions
00117   // Return the polynomial which is the derivative of this one. <em>e.g.,</em>
00118   // <src> 2+4x+5x^2 --> 0+4+10x </src>.
00119   Polynomial<T> derivative() const;
00120   
00121   // Return a copy of this object from the heap. The caller is responsible for
00122   // deleting the pointer.
00123   // <group>
00124   virtual Function<T> *clone() const { return new Polynomial<T>(*this); }
00125   virtual Function<typename FunctionTraits<T>::DiffType> *cloneAD() const {
00126     return new Polynomial<typename FunctionTraits<T>::DiffType>(*this); }
00127   virtual Function<typename FunctionTraits<T>::BaseType> *cloneNonAD() const {
00128     return new Polynomial<typename FunctionTraits<T>::BaseType>(*this); }
00129   // </group>
00130 
00131   //# Make members of parent classes known.
00132 protected:
00133   using PolynomialParam<T>::param_p;
00134 public:
00135   using PolynomialParam<T>::nparameters;
00136   using PolynomialParam<T>::order;
00137 
00138 };
00139 
00140 #define Polynomial_PS Polynomial
00141 
00142 // <summary> Partial specialization of Polynomial for <src>AutoDiff</src>
00143 // </summary>
00144 
00145 // <synopsis>
00146 // <note role=warning> The name <src>Polynomial_PS</src> is only for cxx2html
00147 // documentation problems. Use <src>Polynomial</src> in your code.</note>
00148 // </synopsis>
00149 
00150 template <class T> class Polynomial_PS<AutoDiff<T> > : 
00151 public PolynomialParam<AutoDiff<T> > {
00152 public:
00153   //# Constructors
00154   // Constructs one dimensional Polynomials.
00155   // <group>
00156   Polynomial_PS() : PolynomialParam<AutoDiff<T> >() {}
00157   explicit Polynomial_PS(uInt order) :
00158     PolynomialParam<AutoDiff<T> >(order) {}
00159   // </group>
00160 
00161   // Copy constructor (deep copy)
00162   // <group>
00163   Polynomial_PS(const Polynomial_PS<AutoDiff<T> > &other) :
00164     PolynomialParam<AutoDiff<T> >(other) {}
00165   template <class W>
00166   Polynomial_PS(const Polynomial_PS<W> &other) :
00167     PolynomialParam<AutoDiff<T> >(other) {}
00168   // </group>
00169 
00170   // Copy assignment (deep copy)
00171   Polynomial_PS<AutoDiff<T> > &
00172     operator=(const Polynomial_PS<AutoDiff<T> > &other) {
00173     PolynomialParam<AutoDiff<T> >::operator=(other); return *this; }
00174     
00175   // Destructor
00176   virtual ~Polynomial_PS() {}
00177 
00178   //# Operators    
00179   // Evaluate the polynomial and its derivatives at <src>x</src> <em>wrt</em>
00180   // to the coefficients.
00181   // <group>
00182   virtual AutoDiff<T> eval(typename Function<AutoDiff<T> >::FunctionArg x) const;
00183   // </group>
00184 
00185   //# Member functions
00186   // Return a copy of this object from the heap. The caller is responsible 
00187   // for deleting this pointer.
00188   // <group>
00189   virtual Function<AutoDiff<T> > *clone() const {
00190     return new Polynomial<AutoDiff<T> >(*this); }
00191   virtual Function<typename FunctionTraits<AutoDiff<T> >::DiffType>
00192     *cloneAD() const {
00193     return new Polynomial<typename FunctionTraits<AutoDiff<T> >::DiffType>
00194       (*this); }
00195   virtual Function<typename FunctionTraits<AutoDiff<T> >::BaseType>
00196     *cloneNonAD() const {
00197     return new Polynomial<typename FunctionTraits<AutoDiff<T> >::BaseType>
00198       (*this); }
00199   // </group>
00200 
00201   //# Make members of parent classes known.
00202 protected:
00203   using PolynomialParam<AutoDiff<T> >::param_p;
00204 public:
00205   using PolynomialParam<AutoDiff<T> >::nparameters;
00206   using PolynomialParam<AutoDiff<T> >::order;
00207 };
00208 
00209 #undef Polynomial_PS
00210 
00211 
00212 } //# NAMESPACE CASA - END
00213 
00214 #ifndef CASACORE_NO_AUTO_TEMPLATES
00215 #include <scimath/Functionals/Polynomial.tcc>
00216 #include <scimath/Functionals/Polynomial2.tcc>
00217 #endif //# CASACORE_NO_AUTO_TEMPLATES
00218 #endif