source/numeric/diff_test.cxx

Go to the documentation of this file.
00001 /*************************************************************************
00002  *
00003  *  The Contents of this file are made available subject to
00004  *  the terms of GNU Lesser General Public License Version 2.1.
00005  *
00006  *
00007  *    GNU Lesser General Public License Version 2.1
00008  *    =============================================
00009  *    Copyright 2005 by Kohei Yoshida.
00010  *    1039 Kingsway Dr., Apex, NC 27502, USA
00011  *
00012  *    This library is free software; you can redistribute it and/or
00013  *    modify it under the terms of the GNU Lesser General Public
00014  *    License version 2.1, as published by the Free Software Foundation.
00015  *
00016  *    This library is distributed in the hope that it will be useful,
00017  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00019  *    Lesser General Public License for more details.
00020  *
00021  *    You should have received a copy of the GNU Lesser General Public
00022  *    License along with this library; if not, write to the Free Software
00023  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00024  *    MA  02111-1307  USA
00025  *
00026  ************************************************************************/
00027 
00028 #include "numeric/diff.hxx"
00029 #include "numeric/funcobj.hxx"
00030 #include <vector>
00031 #include <string>
00032 #include <iostream>
00033 #include <cmath>
00034 #include <memory>
00035 
00036 using ::std::vector;
00037 using ::std::cout;
00038 using ::std::endl;
00039 using ::std::string;
00040 using ::std::auto_ptr;
00041 
00042 using namespace scsolver::numeric;
00043 
00044 class TestFailed {};
00045 
00046 class BaseTestFunc : public BaseFuncObj
00047 {
00048 public:
00049     BaseTestFunc(size_t varIndex) : BaseFuncObj(varIndex)
00050     {
00051     }
00052 
00053     virtual ~BaseTestFunc() throw() {}
00054     virtual double evalDef1() const = 0;
00055     virtual double evalDef2() const = 0;
00056 
00057 private:
00058     BaseTestFunc();
00059 };
00060 
00061 class TestFunc : public BaseTestFunc
00062 {
00063 public:
00064     TestFunc() : BaseTestFunc(1)
00065     {
00066     }
00067 
00068     virtual ~TestFunc() throw()
00069     {
00070     }
00071 
00072         virtual const string getFuncString() const
00073         {
00074                 return string("f(x) = 2x^2 + 5x");
00075         }
00076 
00077 //  virtual const vector<double>& getVars() const
00078 //  {
00079 //      return m_Vars;
00080 //  }
00081 //
00082 //  virtual void setVars(const vector<double>& vars)
00083 //  {
00084 //      vector<double> tmp(vars);
00085 //      m_Vars.swap(tmp);
00086 //  }
00087 //
00088 //  virtual void setVar(size_t index, double var)
00089 //  {
00090 //      m_Vars.at(index) = var;
00091 //  }
00092 
00093     virtual double eval() const
00094     {
00095 //      double x = m_Vars.at(0);
00096         double x = getVar(0);
00097         return (2*x + 5)*x;
00098     }
00099 
00100     virtual double evalDef1() const
00101     {
00102 //      double x = m_Vars.at(0);
00103         double x = getVar(0);
00104         return 4*x + 5;
00105     }
00106 
00107     virtual double evalDef2() const
00108     {
00109         return 4;
00110     }
00111 
00112 //private:
00113 //    vector<double> m_Vars;
00114 };
00115 
00116 class TestFunc2 : public BaseTestFunc
00117 {
00118 public:
00119     TestFunc2() : BaseTestFunc(1)
00120     {
00121     }
00122 
00123     virtual ~TestFunc2() throw()
00124     {
00125     }
00126 
00127         virtual const string getFuncString() const
00128         {
00129                 return string("f(x) = 2x^3 + 5x^2 - 2x");
00130         }
00131 
00132         virtual double eval() const
00133         {
00134         // ( (2*x + 5) * x - 2)*x
00135 //      double x = m_Vars.at( 0 );
00136         double x = getVar(0);
00137         double f = ((2*x + 5)*x - 2)*x;
00138         return f;
00139         }
00140 
00141     virtual double evalDef1() const
00142     {
00143         // 6x^2 + 10x - 2 = (6x + 10) * x - 2
00144 //      double x = m_Vars.at(0);
00145         double x = getVar(0);
00146         double f = (6*x + 10) * x - 2;
00147         return f;
00148     }
00149 
00150     virtual double evalDef2() const
00151     {
00152 //      double x = m_Vars.at(0);
00153         double x = getVar(0);
00154         return 12*x + 10;
00155     }
00156 
00157 //    virtual const vector<double>& getVars() const
00158 //    {
00159 //        return m_Vars;
00160 //    }
00161 //
00162 //    virtual void setVars(const vector<double>& vars)
00163 //    {
00164 //        vector<double> tmp(vars);
00165 //        m_Vars.swap(tmp);
00166 //    }
00167 //
00168 //    virtual void setVar(size_t index, double var)
00169 //    {
00170 //        m_Vars.at(index) = var;
00171 //    }
00172 //
00173 //private:
00174 //    vector<double> m_Vars;
00175 };
00176 
00177 void checkDelta(double delta)
00178 {
00179     cout << "delta = " << delta << endl;
00180     if (delta > 0.05)
00181         throw TestFailed();
00182 }
00183 
00184 void test(BaseTestFunc* pF)
00185 {
00186     static const double vars[] = {0, 1.5, 10.0, 20.5, 50.0};
00187     for (int i = 0; i < 5; ++i)
00188     {
00189         double var = vars[i];
00190         cout << "----------------------------------------" << endl;
00191         cout << " x = " << var << endl;
00192 //      vector<double> cnX;
00193 //      cnX.push_back(var);
00194         auto_ptr<NumericalDiffer> pDiff( new NumericalDiffer );
00195 //      pDiff->setVariables( cnX );
00196         pDiff->setVariable(var);
00197         pDiff->setPrecision(5);
00198         SingleVarFuncObj& rSingleFuncObj = pF->getSingleVarFuncObj(0);
00199         pDiff->setFuncObject(&rSingleFuncObj);
00200         double answer = pDiff->run();
00201         double delta = fabs(answer - pF->evalDef1())/fabs(answer);
00202         checkDelta(delta);
00203         
00204         pDiff->setSecondOrder(true);
00205         answer = pDiff->run();
00206         delta = (answer - pF->evalDef2())/answer;
00207         checkDelta(delta);
00208     }
00209 }
00210 
00211 int main()
00212 {
00213     try
00214     {
00215         auto_ptr<TestFunc> pF( new TestFunc );
00216         test(pF.get());
00217         auto_ptr<TestFunc2> pF2( new TestFunc2 );
00218         test(pF2.get());
00219         cout << "Test passed!" << endl;
00220     }
00221     catch (const TestFailed& )
00222     {
00223         cout << "Test failed" << endl;
00224     }
00225 }
00226 
00227 
00228 
00229 
00230 
00231 
00232 
00233 
00234 
00235 
00236 
00237 
00238 
00239 
00240 
00241 

Generated on Mon Jul 28 09:13:20 2008 for scsolver by  doxygen 1.5.3