Home | Doxygen Documentation | Tutorials | Developer Tools (restricted)

app-bholger/gfem/polyCoeff.h
Go to the documentation of this file.
00001 #pragma once
00002 #include "basics/typedefs.hh"
00003 #include "convol.h"
00004 #include <memory>
00005 #include "stdio.h"
00006 #include "assert.h"
00007 #include "algorithm"
00008 
00009 namespace concepts {
00010 
00011 class PolyCoeff {
00012 public:
00013   PolyCoeff(int n)
00014     : n(n)
00015   {
00016     coeffs = new Real[n];
00017   }
00018   
00019   ~PolyCoeff() {
00020     delete[] coeffs;
00021   }
00022   
00023   PolyCoeff(const PolyCoeff& other) : n(0), coeffs(NULL) {
00024     *this = other;
00025   }
00026 
00027   PolyCoeff& operator=(const PolyCoeff& other) {
00028     reserve(other.size());
00029 
00030     int min_n = std::min(n, other.size());
00031 
00032     for(int i = 0; i < min_n; ++i) {
00033       coeffs[i] = other.coeffs[i];
00034     }
00035 
00036     for(int i = min_n; i < n; ++i) {
00037       coeffs[i] = 0;
00038     }
00039 
00040     return *this;
00041   }
00042 
00043   Real eval(Real x) const {
00044     Real val = 0;
00045 
00046     for(int i=n; i--;) {
00047       val *= x;
00048       val += coeffs[i];
00049     }
00050     
00051     return val;
00052   }
00053 
00054   PolyCoeff& operator*=(Real coeff) {
00055     for(int i=0; i < size(); ++i) {
00056       coeffs[i] *= coeff;
00057     }
00058 
00059     return *this;
00060   }
00061 
00062   PolyCoeff& operator-=(const PolyCoeff& other) {
00063     assert(other.size() == size());
00064 
00065     for(int i=0; i < size(); ++i) {
00066       coeffs[i] -= other.coeffs[i];
00067     }
00068 
00069     return *this;
00070   }
00071   
00074   PolyCoeff operator*(const PolyCoeff& other) {
00075     return PolyCoeff(*conv(other));
00076   }
00077 
00078   int size() const {
00079     return n;
00080   }
00081 
00085   std::auto_ptr<PolyCoeff> mul(const PolyCoeff& other) {
00086     return conv(other);
00087   }
00088   
00089   std::auto_ptr<PolyCoeff> derivative() const {
00090     PolyCoeff* res = new PolyCoeff(n - 1);
00091 
00092     for(int i = 1; i < n; ++i) {
00093       res->coeffs[i-1] = coeffs[i] * i;
00094     }
00095 
00096     return std::auto_ptr<PolyCoeff>(res);
00097   }
00098 
00100   std::auto_ptr<PolyCoeff> conv(const PolyCoeff& other) {
00101     PolyCoeff* res = new PolyCoeff(n + other.n - 1);
00102 
00103     LinearConvolution(coeffs, other.coeffs, res->coeffs, n, other.n);
00104 
00105     return std::auto_ptr<PolyCoeff>(res);
00106   }
00107 
00111   void scalex(Real scale) {
00112     Real pow_scale = 1;
00113     for(int i=0; i < n; ++i) {
00114       coeffs[i] *= pow_scale;
00115 
00116       pow_scale *= scale;
00117     }
00118   }
00119 
00123   void scale(Real scale) {
00124     for(int i=0; i < n; ++i) {
00125       coeffs[i] *= scale;
00126     }
00127   }
00128 
00129   void resize(int n_new) {
00130     if(n == n_new)
00131       return;
00132 
00133     Real* coeff_new = new Real[n_new];
00134 
00135     int min_n = std::min(n, n_new);
00136     for(int i=0; i < min_n; ++i) {
00137       coeff_new[i] = coeffs[i];
00138     }
00139 
00140     for(int i=min_n; i < n_new; ++i) {
00141       coeff_new[i] = 0;
00142     }
00143 
00144     n = n_new;
00145     delete[] coeffs;
00146     coeffs = coeff_new;
00147   }
00148 
00149   void reserve(int n_new) {
00150     if(n < n_new) {
00151       Real* coeff_new = new Real[n_new];
00152 
00153       for(int i=0; i < n; ++i) {
00154         coeff_new[i] = coeffs[i];
00155       }
00156       n = n_new;
00157       delete[] coeffs;
00158       coeffs = coeff_new;
00159     }
00160   }
00161 
00162   void print(const char* name = "") const {
00163     printf("%s n=%d: ", name, n);
00164 
00165     for(int i=0; i < n; ++i) {
00166       //printf("%5.3f ", coeffs[i]);
00167       printf("%5.3f x^%d  ", getCoeff(i), i);
00168     }
00169 
00170     puts("");
00171   }
00172 
00173   void printMatlab(const char* name = "") const {
00174     printf("%s [", name);
00175 
00176     for(int i=0; i < n; ++i) {
00177       printf("%5.3f ", getCoeff(n-i-1));
00178     }
00179 
00180     puts("]");
00181   }
00182 
00183   inline Real& getCoeff(int i) {
00184     return coeffs[i];
00185   }
00186 
00187   inline Real getCoeff(int i) const {
00188     return coeffs[i];
00189   }
00190 
00191   inline Real& operator[](int i) {
00192     return getCoeff(i);
00193   }
00194 
00195   inline Real operator[](int i) const {
00196     return getCoeff(i);
00197   }
00198 
00199   Real* getCoeffs() {
00200     return coeffs;
00201   }
00202 
00203   const Real* getCoeffs() const {
00204     return coeffs;
00205   }
00206 
00207 private:
00208   int n;
00209   Real* coeffs;
00210 };
00211 
00212 } // namespace

Home | Doxygen Documentation | Tutorials | Developer Tools (restricted)