File indexing completed on 2025-01-05 04:12:53

0001 /******************************************************************************
0002  *  polynom.h : polynom class. Implements the following operations:           *
0003  *              - the sum, the difference and the product between polynoms ;  *
0004  *              - the multiplication and the division by a scalar ;           *
0005  *              - the derivative and the integrate.                           *
0006  *                                                                            *
0007  *  Copyright 2005, Jean-Paul Petillon                                        *
0008  *   This program is free software; you can redistribute it and/or modify     *
0009  *   it under the terms of the GNU General Public License as published by     *
0010  *   the Free Software Foundation; either version 2 of the License, or        *
0011  *   (at your option) any later version.                                      *
0012  *                                                                            *
0013  ******************************************************************************/
0014 #ifndef POLYNOM_H
0015 #define POLYNOM_H
0016 
0017 template<class S> class polynom {
0018 public:
0019   polynom(int n);                       // constructor
0020   polynom(const polynom& P);            // copy constructor
0021  ~polynom();                            // destructor
0022   S& operator[] (int i);                // ith coefficient access (read/write)
0023   S operator() (S x);                   // value at x
0024   polynom& operator=(const polynom& P); // set a polynom equal to another one
0025   polynom  operator+(const polynom& P); // sum of 2 polynoms
0026   polynom  operator-(const polynom& P); // difference between 2 polynoms
0027   polynom  operator*(const polynom& P); // product of 2 polynoms
0028   polynom  operator*(const S& x);       // product by a scalar
0029   polynom  operator/(const S& x);       // division by a scalar
0030   polynom  Derivative();                // derivative
0031   polynom  Integrate();                 // integrate
0032   int GetDegree();
0033   void SetDegree(int n);
0034 private:
0035   S* C;                                 // coefficients
0036   S NulC;                               // null coefficient (for i>n)
0037   int n;                                // degree of polynom (table size is n+1)
0038 };
0039 
0040 // constructor
0041 template<class S> polynom<S>::polynom(int n)
0042 {
0043   C = new S[n+1];
0044   for (int i=0; i<=n; i++) C[i]=0.0;
0045   NulC = 0.0;
0046   this->n = n;
0047 };
0048 //------------------------------------------------------------------------------
0049 // copy constructor
0050 template<class S> polynom<S>::polynom(const polynom& P) {
0051   n = P.n;
0052   C = new S[n+1];
0053   for (int i=0; i<=n; i++) C[i]=P.C[i];
0054 }
0055 //------------------------------------------------------------------------------
0056 // destructor
0057 template<class S> polynom<S>::~polynom()
0058 {
0059   delete[] C;
0060 };
0061 //------------------------------------------------------------------------------
0062 // coefficient access (read or write)
0063 template<class S> S& polynom<S>::operator[] (int i)
0064 {
0065   return i<=n ? C[i] : NulC;
0066 };
0067 //------------------------------------------------------------------------------
0068 // value at x
0069 template<class S> S polynom<S>::operator() (S x)
0070 {
0071   S r=0.0;
0072   S xpi=1.0;  // x at the power i
0073   for (int i=0; i<=n; i++) {
0074     r+=C[i]*xpi;
0075     xpi*=x;
0076   }
0077   return r;
0078 };
0079 //------------------------------------------------------------------------------
0080 // set a polynom equal to another one
0081 template<class S> polynom<S>& polynom<S>::operator=(const polynom& P)
0082 {
0083   if (n!=P.n) { // reallocate memory if different size
0084     delete[] C;
0085     n = P.n;
0086     C = new S[n+1];
0087     //this->n = n;
0088   }
0089   for (int i=0; i<=n; i++) C[i]=P.C[i];
0090   return *this;
0091 };
0092 //------------------------------------------------------------------------------
0093 // polynom sum
0094 template<class S> polynom<S> polynom<S>::operator+(const polynom& P)
0095 {
0096   polynom r(n>P.n?n:P.n);
0097   for (int i=0; i<=  n; i++) r.C[i]+=  C[i];
0098   for (int i=0; i<=P.n; i++) r.C[i]+=P.C[i];
0099   return r;
0100 };
0101 //------------------------------------------------------------------------------
0102 // polynom difference
0103 template<class S> polynom<S> polynom<S>::operator-(const polynom& P)
0104 {
0105   polynom r(n>P.n?n:P.n);
0106   for (int i=0; i<=  n; i++) r.C[i]+=  C[i];
0107   for (int i=0; i<=P.n; i++) r.C[i]-=P.C[i];
0108   return r;
0109 };
0110 //------------------------------------------------------------------------------
0111 // polynom product
0112 template<class S> polynom<S> polynom<S>::operator*(const polynom<S>& P)
0113 {
0114   polynom r(n+P.n);
0115   for (int i=0; i<=r.n; i++) {
0116     r.C[i]=0;
0117     for (int j=(0>i-n?0:i-n); j<=(i<P.n?i:P.n); j++) r.C[i]+=C[i-j]*P.C[j];
0118   }
0119   return r;
0120 };
0121 //------------------------------------------------------------------------------
0122 // scalar multipication
0123 template<class S> polynom<S> polynom<S>::operator*(const S& x)
0124 {
0125   polynom r(n);
0126   for (int i=0; i<=n; i++) r.C[i]=C[i]*x;
0127   return r;
0128 };
0129 //------------------------------------------------------------------------------
0130 // scalar division
0131 template<class S> polynom<S> polynom<S>::operator/(const S& x)
0132 {
0133   polynom<S> r(n);
0134   for (int i=0; i<=n; i++) r.C[i]=C[i]/x;
0135   return r;
0136 };
0137 //------------------------------------------------------------------------------
0138 // derivative
0139 template<class S> polynom<S> polynom<S>::Derivative()
0140 {
0141   polynom<S> r(n-1);
0142   for (int i=0; i<=r.n; i++) r.C[i]=(i+1)*C[i+1];
0143   return r;
0144 };
0145 //------------------------------------------------------------------------------
0146 // integrate
0147 template<class S> polynom<S> polynom<S>::Integrate()
0148 {
0149   polynom<S> r(n+1);
0150   r.C[0]=0;
0151   for (int i=1; i<=r.n; i++) r.C[i]=C[i-1]/i;
0152   return r;
0153 };
0154 //------------------------------------------------------------------------------
0155 template<class S> int polynom<S>::GetDegree()
0156 {
0157   return n;
0158 }
0159 //------------------------------------------------------------------------------
0160 template<class S> void polynom<S>::SetDegree(int n)
0161 {
0162   if (this->n != n) {
0163     delete[] C;
0164     C = new double[n+1];
0165     this->n = n;
0166     for (int i=0; i<=n; i++) C[i]=0.0;
0167   }
0168 }
0169 
0170 #endif // ifndef POLYNOM_H