source/numeric/bisectionsearch_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 2008, 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/bisectionsearch.hxx"
00029 #include "numeric/funcobj.hxx"
00030 #include "numeric/testtool.hxx"
00031 #include <cmath>
00032 
00033 using namespace ::scsolver::numeric;
00034 using namespace ::std;
00035 
00036 class TestFunc1 : public SingleVarTestFuncBase
00037 {
00038 public:
00039     virtual double eval() const
00040     {
00041         double var = getVar();
00042         return (var - 2.0)*(var*2.0 + 5.0) + 10.0;
00043     }
00044 
00045     virtual const string getFuncString() const
00046     {
00047         // 2*x^2 + x
00048         return string("(x - 2) * (2x + 5) + 10");
00049     }
00050 };
00051 
00052 class TestFunc2 : public SingleVarTestFuncBase
00053 {
00054 public:
00055     virtual double eval() const
00056     {
00057         double var = getVar();
00058         return ::std::sin(var) * var;
00059     }
00060 
00064     virtual const string getFuncString() const
00065     {
00066         return string("sin(x) * x");
00067     }
00068 };
00069 
00070 class TestFunc3 : public SingleVarTestFuncBase
00071 {
00072 public:
00073     virtual double eval() const
00074     {
00075         double x = getVar();
00076         return x*(2*x-20)*(x*x+5*x+10);
00077     }
00078 
00082     virtual const string getFuncString() const
00083     {
00084         return string("x*(2*x-20)*(x*x+5*x+10)");
00085     }
00086 };
00087 
00088 class TestFunc4 : public SingleVarTestFuncBase
00089 {
00090 public:
00091     virtual double eval() const
00092     {
00093         using namespace std;
00094         double x = getVar();
00095         return ((x-2)*(x*2+8) + 45)*(2*x+3)*cos(x/2);
00096     }
00097 
00101     virtual const string getFuncString() const
00102     {
00103         return string("((x-2)*(x*2+8) + 45)*(2*x+3)*cos(x/2)");
00104     }
00105 };
00106 
00107 class TestFunc5 : public SingleVarTestFuncBase
00108 {
00109 public:
00110     virtual double eval() const
00111     {
00112         using namespace std;
00113         double x = getVar();
00114         return (x-2)*(x-2)*(x-2)*(x-2) + (x-6)*(x-6);
00115     }
00116 
00120     virtual const::std::string getFuncString() const
00121     {
00122         return string("(x - 2)^4 + (x - 6)^2");
00123     }
00124 };
00125 
00126 class TestFunc6 : public SingleVarTestFuncBase
00127 {
00128 public:
00129     virtual double eval() const
00130     {
00131         double x = getVar();
00132         return x*x;
00133     }
00134 
00138     virtual const::std::string getFuncString() const
00139     {
00140         return string("x^2");
00141     }
00142 };
00143 
00144 class TestFunc7 : public SingleVarTestFuncBase
00145 {
00146 public:
00147     virtual double eval() const
00148     {
00149         double x = getVar();
00150         return (x-1)*(x-1);
00151     }
00152 
00156     virtual const::std::string getFuncString() const
00157     {
00158         return string("(x-1)^2");
00159     }
00160 };
00161 
00162 class TestFunc8 : public SingleVarTestFuncBase
00163 {
00164 public:
00165     virtual double eval() const
00166     {
00167         double x = getVar();
00168         return (x-2)*(x-2)*(x-2)*(x-2) + 0.006715617*x*x;
00169     }
00170 
00174     virtual const::std::string getFuncString() const
00175     {
00176         return string("(x-2)^4 + (0.081948871*x)^2");
00177     }
00178 };
00179 
00180 int main()
00181 {
00182     runSingleVarTestFunc(new BisectionSearch(-100, 100), new TestFunc1);
00183     runSingleVarTestFunc(new BisectionSearch(-10, -1),   new TestFunc2);
00184     runSingleVarTestFunc(new BisectionSearch(-100, 100), new TestFunc3);
00185     runSingleVarTestFunc(new BisectionSearch(-100, 100), new TestFunc4);
00186     runSingleVarTestFunc(new BisectionSearch(-100, 100), new TestFunc5);
00187     runSingleVarTestFunc(new BisectionSearch(-100, 50),  new TestFunc6);
00188     runSingleVarTestFunc(new BisectionSearch(-100, 50),  new TestFunc7);
00189     runSingleVarTestFunc(new BisectionSearch(-100, 50),  new TestFunc8);
00190 }
00191 

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