Generated on Tue May 22 09:39:49 2018 for Gecode by doxygen 1.6.3

transcendental.cpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Christian Schulte <schulte@gecode.org>
00005  *     Vincent Barichard <Vincent.Barichard@univ-angers.fr>
00006  *
00007  *  Copyright:
00008  *     Christian Schulte, 2005
00009  *     Vincent Barichard, 2012
00010  *
00011  *  This file is part of Gecode, the generic constraint
00012  *  development environment:
00013  *     http://www.gecode.org
00014  *
00015  *  Permission is hereby granted, free of charge, to any person obtaining
00016  *  a copy of this software and associated documentation files (the
00017  *  "Software"), to deal in the Software without restriction, including
00018  *  without limitation the rights to use, copy, modify, merge, publish,
00019  *  distribute, sublicense, and/or sell copies of the Software, and to
00020  *  permit persons to whom the Software is furnished to do so, subject to
00021  *  the following conditions:
00022  *
00023  *  The above copyright notice and this permission notice shall be
00024  *  included in all copies or substantial portions of the Software.
00025  *
00026  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00027  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00028  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00029  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00030  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00031  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00032  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00033  *
00034  */
00035 
00036 #include "test/float.hh"
00037 #include <gecode/minimodel.hh>
00038 
00039 #ifdef GECODE_HAS_MPFR
00040 
00041 #include <cmath>
00042 #include <algorithm>
00043 
00044 namespace Test { namespace Float {
00045 
00047    namespace Transcendental {
00048 
00050      class ExpXY : public Test {
00051      public:
00053        ExpXY(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
00054          : Test("Transcendental::Exp::XY::"+s,2,d,st,CPLT_ASSIGNMENT,false) {}
00056        virtual MaybeType solution(const Assignment& x) const {
00057          return eq(exp(x[0]), x[1]);
00058        }
00060        virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
00061          if (flip())
00062            Gecode::exp(home, x[0], x[1]);
00063          else
00064            Gecode::rel(home, exp(x[0]) == x[1]);
00065        }
00066      };
00067 
00069      class ExpXYSol : public Test {
00070      public:
00072        ExpXYSol(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
00073        : Test("Transcendental::Exp::XY::Sol::"+s,2,d,st,EXTEND_ASSIGNMENT,false) {}
00075        virtual MaybeType solution(const Assignment& x) const {
00076          return eq(exp(x[0]), x[1]);
00077        }
00079        virtual bool extendAssignement(Assignment& x) const {
00080          Gecode::FloatVal d = exp(x[0]);
00081          if (Gecode::Float::subset(d, dom)) {
00082            x.set(1, d);
00083            return true;
00084          } else {
00085            return false;
00086          }
00087        }
00089        virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
00090          Gecode::exp(home, x[0], x[1]);
00091        }
00092      };
00093 
00095      class ExpXX : public Test {
00096      public:
00098        ExpXX(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
00099          : Test("Transcendental::Exp::XX::"+s,1,d,st,CPLT_ASSIGNMENT,false) {}
00101        virtual MaybeType solution(const Assignment& x) const {
00102          return eq(exp(x[0]), x[0]);
00103        }
00105        virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
00106          Gecode::exp(home, x[0], x[0]);
00107        }
00108      };
00109 
00111      class LogXY : public Test {
00112      public:
00114        LogXY(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
00115          : Test("Transcendental::Log::XY::"+s,2,d,st,CPLT_ASSIGNMENT,false) {}
00117        virtual MaybeType solution(const Assignment& x) const {
00118          if (x[0].max() < 0.0)
00119            return MT_FALSE;
00120          return eq(log(x[0]), x[1]);
00121        }
00123        virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
00124          if (flip())
00125            Gecode::log(home, x[0], x[1]);
00126          else
00127            Gecode::rel(home, log(x[0]) == x[1]);
00128        }
00129      };
00130 
00132      class LogXYSol : public Test {
00133      public:
00135        LogXYSol(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
00136        : Test("Transcendental::Log::XY::Sol::"+s,2,d,st,EXTEND_ASSIGNMENT,false) {}
00138        virtual MaybeType solution(const Assignment& x) const {
00139          if (x[0].max() < 0.0)
00140            return MT_FALSE;
00141          return eq(log(x[0]), x[1]);
00142        }
00144        virtual bool extendAssignement(Assignment& x) const {
00145          if (x[0].max() < 0.0) return false;
00146          Gecode::FloatVal d = log(x[0]);
00147          if (Gecode::Float::subset(d, dom)) {
00148            x.set(1, d);
00149            return true;
00150          } else {
00151            return false;
00152          }
00153        }
00155        virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
00156          Gecode::log(home, x[0], x[1]);
00157        }
00158      };
00159 
00161      class LogXX : public Test {
00162      public:
00164        LogXX(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
00165        : Test("Transcendental::Log::XX::"+s,1,d,st,CPLT_ASSIGNMENT,false) {}
00167        virtual MaybeType solution(const Assignment& x) const {
00168          if (x[0].max() < 0.0)
00169            return MT_FALSE;
00170          return eq(log(x[0]), x[0]);
00171        }
00173        virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
00174          Gecode::log(home, x[0], x[0]);
00175        }
00176      };
00177 
00179      class LogNXY : public Test {
00180        Gecode::FloatNum base;
00181      public:
00183        LogNXY(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum _base, Gecode::FloatNum st)
00184        : Test("Transcendental::Log::N::"+str(_base)+"::XY::"+s,2,d,st,CPLT_ASSIGNMENT,false), base(_base) {}
00186        virtual MaybeType solution(const Assignment& x) const {
00187          if ((x[0].max() <= 0.0) || (base <= 0.0))
00188            return MT_FALSE;
00189          return eq(log(x[0]) / log(base), x[1]);
00190        }
00192        virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
00193          Gecode::log(home, base, x[0], x[1]);
00194        }
00195      };
00196 
00198      class LogNXYSol : public Test {
00199        Gecode::FloatNum base;
00200      public:
00202        LogNXYSol(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum _base, Gecode::FloatNum st)
00203        : Test("Transcendental::Log::N::"+str(_base)+"::XY::Sol::"+s,2,d,st,EXTEND_ASSIGNMENT,false), base(_base) {}
00205        virtual MaybeType solution(const Assignment& x) const {
00206          if ((x[0].max() <= 0.0) || (base <= 0.0))
00207            return MT_FALSE;
00208          return eq(log(x[0]) / log(base), x[1]);
00209        }
00211        virtual bool extendAssignement(Assignment& x) const {
00212          if ((x[0].max() <= 0.0) || (base <= 0.0))
00213            return false;
00214          Gecode::FloatVal d = log(x[0])/log(base);
00215          if (Gecode::Float::subset(d, dom)) {
00216            x.set(1, d);
00217            return true;
00218          } else {
00219            return false;
00220          }
00221        }
00223        virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
00224          Gecode::log(home, base, x[0], x[1]);
00225        }
00226      };
00227 
00229      class LogNXX : public Test {
00230        Gecode::FloatNum base;
00231      public:
00233        LogNXX(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum _base, Gecode::FloatNum st)
00234          : Test("Transcendental::Log::N::"+str(_base)+"::XX::"+s,1,d,st,CPLT_ASSIGNMENT,false), base(_base) {}
00236        virtual MaybeType solution(const Assignment& x) const {
00237          if ((x[0].max() <= 0.0) || (base <= 0.0))
00238            return MT_FALSE;
00239          return eq(log(x[0]) / log(base), x[0]);
00240        }
00242        virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
00243          Gecode::log(home, base, x[0], x[0]);
00244        }
00245      };
00246 
00248      class PowXY : public Test {
00249        Gecode::FloatNum base;
00250      public:
00252        PowXY(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum _base, Gecode::FloatNum st)
00253        : Test("Transcendental::Pow::N::"+str(_base)+"::XY::"+s,2,d,st,CPLT_ASSIGNMENT,false), base(_base) {}
00255        virtual MaybeType solution(const Assignment& x) const {
00256          if (base <= 0.0)
00257            return MT_FALSE;
00258          return eq(exp(x[0] * log(base)), x[1]);
00259        }
00261        virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
00262          Gecode::pow(home, base, x[0], x[1]);
00263        }
00264      };
00265 
00267      class PowXYSol : public Test {
00268        Gecode::FloatNum base;
00269      public:
00271        PowXYSol(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum _base, Gecode::FloatNum st)
00272        : Test("Transcendental::Pow::N::"+str(_base)+"::XY::Sol::"+s,2,d,st,EXTEND_ASSIGNMENT,false), base(_base) {}
00274        virtual MaybeType solution(const Assignment& x) const {
00275          if (base <= 0.0)
00276            return MT_FALSE;
00277          return eq(exp(x[0] * log(base)), x[1]);
00278        }
00280        virtual bool extendAssignement(Assignment& x) const {
00281          if (base <= 0.0) return false;
00282          Gecode::FloatVal d = exp(x[0]*log(base));
00283          if (Gecode::Float::subset(d, dom)) {
00284            x.set(1, d);
00285            return true;
00286          } else {
00287            return false;
00288          }
00289        }
00291        virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
00292          Gecode::pow(home, base, x[0], x[1]);
00293        }
00294      };
00295 
00297      class PowXX : public Test {
00298        Gecode::FloatNum base;
00299      public:
00301        PowXX(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum _base, Gecode::FloatNum st)
00302        : Test("Transcendental::Pow::N::"+str(_base)+"::XX::"+s,1,d,st,CPLT_ASSIGNMENT,false), base(_base) {}
00304        virtual MaybeType solution(const Assignment& x) const {
00305          if ((x[0].max() <= 0.0) || (base <= 0.0))
00306            return MT_FALSE;
00307          return eq(exp(x[0] * log(base)), x[0]);
00308        }
00310        virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
00311          Gecode::pow(home, base, x[0], x[0]);
00312        }
00313      };
00314 
00315      const Gecode::FloatNum step = 0.15;
00316      const Gecode::FloatNum step2 = 2*step;
00317      Gecode::FloatVal a(-8,5);
00318      Gecode::FloatVal b(9,12);
00319      Gecode::FloatVal c(-8,8);
00320 
00321      ExpXY exp_xy_a("A",a,step);
00322      ExpXY exp_xy_b("B",b,step);
00323      ExpXY exp_xy_c("C",c,step);
00324 
00325      ExpXYSol exp_xy_sol_a("A",a,step);
00326      ExpXYSol exp_xy_sol_b("B",b,step);
00327      ExpXYSol exp_xy_sol_c("C",c,step);
00328 
00329      ExpXX exp_xx_a("A",a,step);
00330      ExpXX exp_xx_b("B",b,step);
00331      ExpXX exp_xx_c("C",c,step);
00332 
00333      LogXY log_xy_a("A",a,step);
00334      LogXY log_xy_b("B",b,step);
00335      LogXY log_xy_c("C",c,step);
00336 
00337      LogXYSol log_xy_sol_a("A",a,step);
00338      LogXYSol log_xy_sol_b("B",b,step);
00339      LogXYSol log_xy_sol_c("C",c,step);
00340 
00341      LogXX log_xx_a("A",a,step);
00342      LogXX log_xx_b("B",b,step);
00343      LogXX log_xx_c("C",c,step);
00344 
00345      LogNXY logn_xy_a_1("A",a,-1.5,step);
00346      LogNXY logn_xy_b_1("B",b,-1.5,step);
00347      LogNXY logn_xy_c_1("C",c,-1.5,step);
00348 
00349      LogNXYSol logn_xy_sol_a_1("A",a,-1.5,step);
00350      LogNXYSol logn_xy_sol_b_1("B",b,-1.5,step);
00351      LogNXYSol logn_xy_sol_c_1("C",c,-1.5,step);
00352 
00353      LogNXX logn_xx_a_1("A",a,-1.5,step);
00354      LogNXX logn_xx_b_1("B",b,-1.5,step);
00355      LogNXX logn_xx_c_1("C",c,-1.5,step);
00356 
00357      LogNXY logn_xy_a_2("A",a,1.5,step);
00358      LogNXY logn_xy_b_2("B",b,1.5,step);
00359      LogNXY logn_xy_c_2("C",c,1.5,step);
00360 
00361      LogNXYSol logn_xy_sol_a_2("A",a,1.5,step);
00362      LogNXYSol logn_xy_sol_b_2("B",b,1.5,step);
00363      LogNXYSol logn_xy_sol_c_2("C",c,1.5,step);
00364 
00365      LogNXX logn_xx_a_2("A",a,1.5,step);
00366      LogNXX logn_xx_b_2("B",b,1.5,step);
00367      LogNXX logn_xx_c_2("C",c,1.5,step);
00368 
00369      LogNXY logn_xy_a_3("A",a,0,step);
00370      LogNXY logn_xy_b_3("B",b,0,step);
00371      LogNXY logn_xy_c_3("C",c,0,step);
00372 
00373      LogNXYSol logn_xy_sol_a_3("A",a,0,step);
00374      LogNXYSol logn_xy_sol_b_3("B",b,0,step);
00375      LogNXYSol logn_xy_sol_c_3("C",c,0,step);
00376 
00377      LogNXX logn_xx_a_3("A",a,0,step);
00378      LogNXX logn_xx_b_3("B",b,0,step);
00379      LogNXX logn_xx_c_3("C",c,0,step);
00380 
00381      PowXY pow_xy_a_1("A",a,-1.5,step);
00382      PowXY pow_xy_b_1("B",b,-1.5,step);
00383      PowXY pow_xy_c_1("C",c,-1.5,step);
00384 
00385      PowXYSol pow_xy_sol_a_1("A",a,-1.5,step);
00386      PowXYSol pow_xy_sol_b_1("B",b,-1.5,step);
00387      PowXYSol pow_xy_sol_c_1("C",c,-1.5,step);
00388 
00389      PowXX pow_xx_a_1("A",a,-1.5,step);
00390      PowXX pow_xx_b_1("B",b,-1.5,step);
00391      PowXX pow_xx_c_1("C",c,-1.5,step);
00392 
00393      PowXY pow_xy_a_2("A",a,1.5,step);
00394      PowXY pow_xy_b_2("B",b,1.5,step);
00395      PowXY pow_xy_c_2("C",c,1.5,step);
00396 
00397      PowXYSol pow_xy_sol_a_2("A",a,1.5,step);
00398      PowXYSol pow_xy_sol_b_2("B",b,1.5,step);
00399      PowXYSol pow_xy_sol_c_2("C",c,1.5,step);
00400 
00401      PowXX pow_xx_a_2("A",a,1.5,step);
00402      PowXX pow_xx_b_2("B",b,1.5,step);
00403      PowXX pow_xx_c_2("C",c,1.5,step);
00404 
00405      PowXY pow_xy_a_3("A",a,0,step);
00406      PowXY pow_xy_b_3("B",b,0,step);
00407      PowXY pow_xy_c_3("C",c,0,step);
00408 
00409      PowXYSol pow_xy_sol_a_3("A",a,0,step);
00410      PowXYSol pow_xy_sol_b_3("B",b,0,step);
00411      PowXYSol pow_xy_sol_c_3("C",c,0,step);
00412 
00413      PowXX pow_xx_a_3("A",a,0,step);
00414      PowXX pow_xx_b_3("B",b,0,step);
00415      PowXX pow_xx_c_3("C",c,0,step);
00416 
00418 
00419    }
00420 }}
00421 
00422 #endif
00423 // STATISTICS: test-float