Generated on Tue Apr 18 10:21:42 2017 for Gecode by doxygen 1.6.3

trigonometric.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  *  Last modified:
00012  *     $Date: 2016-04-19 17:19:45 +0200 (Tue, 19 Apr 2016) $ by $Author: schulte $
00013  *     $Revision: 14967 $
00014  *
00015  *  This file is part of Gecode, the generic constraint
00016  *  development environment:
00017  *     http://www.gecode.org
00018  *
00019  *  Permission is hereby granted, free of charge, to any person obtaining
00020  *  a copy of this software and associated documentation files (the
00021  *  "Software"), to deal in the Software without restriction, including
00022  *  without limitation the rights to use, copy, modify, merge, publish,
00023  *  distribute, sublicense, and/or sell copies of the Software, and to
00024  *  permit persons to whom the Software is furnished to do so, subject to
00025  *  the following conditions:
00026  *
00027  *  The above copyright notice and this permission notice shall be
00028  *  included in all copies or substantial portions of the Software.
00029  *
00030  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00031  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00032  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00033  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00034  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00035  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00036  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00037  *
00038  */
00039 
00040 #include "test/float.hh"
00041 #include <gecode/minimodel.hh>
00042 
00043 #ifdef GECODE_HAS_MPFR
00044 
00045 #include <cmath>
00046 #include <algorithm>
00047 
00048 namespace Test { namespace Float {
00049 
00051    namespace Trigonometric {
00052 
00054      class SinXY : public Test {
00055      public:
00057        SinXY(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
00058          : Test("Trigonometric::Sin::XY::"+s,2,d,st,CPLT_ASSIGNMENT,false) {}
00060        virtual MaybeType solution(const Assignment& x) const {
00061          return eq(sin(x[0]), x[1]);
00062        }
00064        virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
00065          if (flip())
00066            Gecode::sin(home, x[0], x[1]);
00067          else
00068            Gecode::rel(home, sin(x[0]) == x[1]);
00069        }
00070      };
00071 
00073      class SinXYSol : public Test {
00074      public:
00076        SinXYSol(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
00077        : Test("Trigonometric::Sin::XY::Sol::"+s,2,d,st,EXTEND_ASSIGNMENT,false) {}
00079        virtual MaybeType solution(const Assignment& x) const {
00080          return eq(sin(x[0]), x[1]);
00081        }
00083        virtual bool extendAssignement(Assignment& x) const {
00084          Gecode::FloatVal d = sin(x[0]);
00085          if (Gecode::Float::subset(d, dom)) {
00086            x.set(1, d);
00087            return true;
00088          } else {
00089            return false;
00090          }
00091        }
00093        virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
00094          Gecode::sin(home, x[0], x[1]);
00095        }
00096      };
00097 
00099      class SinXX : public Test {
00100      public:
00102        SinXX(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
00103          : Test("Trigonometric::Sin::XX::"+s,1,d,st,CPLT_ASSIGNMENT,false) {}
00105        virtual MaybeType solution(const Assignment& x) const {
00106          return eq(sin(x[0]), x[0]);
00107        }
00109        virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
00110          Gecode::sin(home, x[0], x[0]);
00111        }
00112      };
00113 
00115      class CosXY : public Test {
00116      public:
00118        CosXY(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
00119        : Test("Trigonometric::Cos::XY::"+s,2,d,st,CPLT_ASSIGNMENT,false) {}
00121        virtual MaybeType solution(const Assignment& x) const {
00122          return eq(cos(x[0]), x[1]);
00123        }
00125        virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
00126          if (flip())
00127            Gecode::cos(home, x[0], x[1]);
00128          else
00129            Gecode::rel(home, cos(x[0]) == x[1]);
00130        }
00131      };
00132 
00134      class CosXYSol : public Test {
00135      public:
00137        CosXYSol(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
00138        : Test("Trigonometric::Cos::XY::Sol::"+s,2,d,st,EXTEND_ASSIGNMENT,false) {}
00140        virtual MaybeType solution(const Assignment& x) const {
00141          return eq(cos(x[0]), x[1]);
00142        }
00144        virtual bool extendAssignement(Assignment& x) const {
00145          Gecode::FloatVal d = cos(x[0]);
00146          if (Gecode::Float::subset(d, dom)) {
00147            x.set(1, d);
00148            return true;
00149          } else {
00150            return false;
00151          }
00152        }
00154        virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
00155          Gecode::cos(home, x[0], x[1]);
00156        }
00157      };
00158 
00160      class CosXX : public Test {
00161      public:
00163        CosXX(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
00164        : Test("Trigonometric::Cos::XX::"+s,1,d,st,CPLT_ASSIGNMENT,false) {}
00166        virtual MaybeType solution(const Assignment& x) const {
00167          return eq(cos(x[0]), x[0]);
00168        }
00170        virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
00171          Gecode::cos(home, x[0], x[0]);
00172        }
00173      };
00174 
00176      class TanXY : public Test {
00177      public:
00179        TanXY(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
00180        : Test("Trigonometric::Tan::XY::"+s,2,d,st,CPLT_ASSIGNMENT,false) {}
00182        virtual MaybeType solution(const Assignment& x) const {
00183          return eq(tan(x[0]), x[1]);
00184        }
00186        virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
00187          if (flip())
00188            Gecode::tan(home, x[0], x[1]);
00189          else
00190            Gecode::rel(home, tan(x[0]) == x[1]);
00191        }
00192      };
00193 
00195      class TanXYSol : public Test {
00196      public:
00198        TanXYSol(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
00199        : Test("Trigonometric::Tan::XY::Sol::"+s,2,d,st,EXTEND_ASSIGNMENT,false) {}
00201        virtual MaybeType solution(const Assignment& x) const {
00202          return eq(tan(x[0]), x[1]);
00203        }
00205        virtual bool extendAssignement(Assignment& x) const {
00206          Gecode::FloatVal d = tan(x[0]);
00207          if (Gecode::Float::subset(d, dom)) {
00208            x.set(1, d);
00209            return true;
00210          } else {
00211            return false;
00212          }
00213        }
00215        virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
00216          Gecode::tan(home, x[0], x[1]);
00217        }
00218      };
00219 
00221      class TanXX : public Test {
00222      public:
00224        TanXX(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
00225        : Test("Trigonometric::Tan::XX::"+s,1,d,st,CPLT_ASSIGNMENT,false) {}
00227        virtual MaybeType solution(const Assignment& x) const {
00228          return eq(tan(x[0]), x[0]);
00229        }
00231        virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
00232          Gecode::tan(home, x[0], x[0]);
00233        }
00234      };
00235 
00237      class ASinXY : public Test {
00238      public:
00240        ASinXY(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
00241        : Test("Trigonometric::ASin::XY::"+s,2,d,st,CPLT_ASSIGNMENT,false) {}
00243        virtual MaybeType solution(const Assignment& x) const {
00244          if ((x[0].min() > 1.0) || (x[0].max() < -1.0))
00245            return MT_FALSE;
00246          return eq(asin(x[0]), x[1]);
00247        }
00249        virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
00250          if (flip())
00251            Gecode::asin(home, x[0], x[1]);
00252          else
00253            Gecode::rel(home, asin(x[0]) == x[1]);
00254        }
00255      };
00256 
00258      class ASinXYSol : public Test {
00259      public:
00261        ASinXYSol(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
00262        : Test("Trigonometric::ASin::XY::Sol::"+s,2,d,st,EXTEND_ASSIGNMENT,false) {}
00264        virtual MaybeType solution(const Assignment& x) const {
00265          if ((x[0].min() > 1.0) || (x[0].max() < -1.0))
00266            return MT_FALSE;
00267          return eq(asin(x[0]), x[1]);
00268        }
00270        virtual bool extendAssignement(Assignment& x) const {
00271          if ((x[0].min() > 1.0) || (x[0].max() < -1.0))
00272            return false;
00273          Gecode::FloatVal d = asin(x[0]);
00274          if (Gecode::Float::subset(d, dom)) {
00275            x.set(1, d);
00276            return true;
00277          } else {
00278            return false;
00279          }
00280        }
00282        virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
00283          Gecode::asin(home, x[0], x[1]);
00284        }
00285      };
00286 
00288      class ASinXX : public Test {
00289      public:
00291        ASinXX(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
00292        : Test("Trigonometric::ASin::XX::"+s,1,d,st,CPLT_ASSIGNMENT,false) {}
00294        virtual MaybeType solution(const Assignment& x) const {
00295          if ((x[0].min() > 1.0) || (x[0].max() < -1.0))
00296            return MT_FALSE;
00297          return eq(asin(x[0]), x[0]);
00298        }
00300        virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
00301          Gecode::asin(home, x[0], x[0]);
00302        }
00303      };
00304 
00306      class ACosXY : public Test {
00307      public:
00309        ACosXY(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
00310        : Test("Trigonometric::ACos::XY::"+s,2,d,st,CPLT_ASSIGNMENT,false) {}
00312        virtual MaybeType solution(const Assignment& x) const {
00313          if ((x[0].min() > 1.0) || (x[0].max() < -1.0))
00314            return MT_FALSE;
00315          return eq(acos(x[0]), x[1]);
00316        }
00318        virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
00319          if (flip())
00320            Gecode::acos(home, x[0], x[1]);
00321          else
00322            Gecode::rel(home, acos(x[0]) == x[1]);
00323        }
00324      };
00325 
00327      class ACosXYSol : public Test {
00328      public:
00330        ACosXYSol(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
00331        : Test("Trigonometric::ACos::XY::Sol::"+s,2,d,st,EXTEND_ASSIGNMENT,false) {}
00333        virtual MaybeType solution(const Assignment& x) const {
00334          if ((x[0].min() > 1.0) || (x[0].max() < -1.0))
00335            return MT_FALSE;
00336          return eq(acos(x[0]), x[1]);
00337        }
00339        virtual bool extendAssignement(Assignment& x) const {
00340          if ((x[0].min() > 1.0) || (x[0].max() < -1.0))
00341            return false;
00342          Gecode::FloatVal d = acos(x[0]);
00343          if (Gecode::Float::subset(d, dom)) {
00344            x.set(1, d);
00345            return true;
00346          } else {
00347            return false;
00348          }
00349        }
00351        virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
00352          Gecode::acos(home, x[0], x[1]);
00353        }
00354      };
00355 
00357      class ACosXX : public Test {
00358      public:
00360        ACosXX(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
00361        : Test("Trigonometric::ACos::XX::"+s,1,d,st,CPLT_ASSIGNMENT,false) {}
00363        virtual MaybeType solution(const Assignment& x) const {
00364          if ((x[0].min() > 1.0) || (x[0].max() < -1.0))
00365            return MT_FALSE;
00366          return eq(acos(x[0]), x[0]);
00367        }
00369        virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
00370          Gecode::acos(home, x[0], x[0]);
00371        }
00372      };
00373 
00375      class ATanXY : public Test {
00376      public:
00378        ATanXY(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
00379        : Test("Trigonometric::ATan::XY::"+s,2,d,st,CPLT_ASSIGNMENT,false) {}
00381        virtual MaybeType solution(const Assignment& x) const {
00382          return eq(atan(x[0]), x[1]);
00383        }
00385        virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
00386          if (flip())
00387            Gecode::atan(home, x[0], x[1]);
00388          else
00389            Gecode::rel(home, atan(x[0]) == x[1]);
00390        }
00391      };
00392 
00394      class ATanXYSol : public Test {
00395      public:
00397        ATanXYSol(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
00398        : Test("Trigonometric::ATan::XY::Sol::"+s,2,d,st,EXTEND_ASSIGNMENT,false) {}
00400        virtual MaybeType solution(const Assignment& x) const {
00401          return eq(atan(x[0]), x[1]);
00402        }
00404        virtual bool extendAssignement(Assignment& x) const {
00405          Gecode::FloatVal d = atan(x[0]);
00406          if (Gecode::Float::subset(d, dom)) {
00407            x.set(1, d);
00408            return true;
00409          } else {
00410            return false;
00411          }
00412        }
00414        virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
00415          Gecode::atan(home, x[0], x[1]);
00416        }
00417      };
00418 
00420      class ATanXX : public Test {
00421      public:
00423        ATanXX(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
00424        : Test("Trigonometric::ATan::XX::"+s,1,d,st,CPLT_ASSIGNMENT,false) {}
00426        virtual MaybeType solution(const Assignment& x) const {
00427          return eq(atan(x[0]), x[0]);
00428        }
00430        virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
00431          Gecode::atan(home, x[0], x[0]);
00432        }
00433      };
00434 
00435      const Gecode::FloatNum step = 0.15;
00436      const Gecode::FloatNum step2 = 2*step;
00437      Gecode::FloatVal a(-8,5);
00438      Gecode::FloatVal b(9,12);
00439      Gecode::FloatVal c(-8,8);
00440 
00441      SinXY sin_xy_a("A",a,step);
00442      SinXY sin_xy_b("B",b,step);
00443      SinXY sin_xy_c("C",c,step);
00444 
00445      SinXYSol sin_xy_sol_a("A",a,step);
00446      SinXYSol sin_xy_sol_b("B",b,step);
00447      SinXYSol sin_xy_sol_c("C",c,step);
00448 
00449      SinXX sin_xx_a("A",a,step);
00450      SinXX sin_xx_b("B",b,step);
00451      SinXX sin_xx_c("C",c,step);
00452 
00453      CosXY cos_xy_a("A",a,step);
00454      CosXY cos_xy_b("B",b,step);
00455      CosXY cos_xy_c("C",c,step);
00456 
00457      CosXYSol cos_xy_sol_a("A",a,step);
00458      CosXYSol cos_xy_sol_b("B",b,step);
00459      CosXYSol cos_xy_sol_c("C",c,step);
00460 
00461      CosXX cos_xx_a("A",a,step);
00462      CosXX cos_xx_b("B",b,step);
00463      CosXX cos_xx_c("C",c,step);
00464 
00465      TanXY tan_xy_a("A",a,step);
00466      TanXY tan_xy_b("B",b,step);
00467      TanXY tan_xy_c("C",c,step);
00468 
00469      TanXYSol tan_xy_sol_a("A",a,step);
00470      TanXYSol tan_xy_sol_b("B",b,step);
00471      TanXYSol tan_xy_sol_c("C",c,step);
00472 
00473      TanXX tan_xx_a("A",a,step);
00474      TanXX tan_xx_b("B",b,step);
00475      TanXX tan_xx_c("C",c,step);
00476 
00477      ASinXY asin_xy_a("A",a,step);
00478      ASinXY asin_xy_b("B",b,step);
00479      ASinXY asin_xy_c("C",c,step);
00480 
00481      ASinXYSol asin_xy_sol_a("A",a,step);
00482      ASinXYSol asin_xy_sol_b("B",b,step);
00483      ASinXYSol asin_xy_sol_c("C",c,step);
00484 
00485      ASinXX asin_xx_a("A",a,step);
00486      ASinXX asin_xx_b("B",b,step);
00487      ASinXX asin_xx_c("C",c,step);
00488 
00489      ACosXY acos_xy_a("A",a,step);
00490      ACosXY acos_xy_b("B",b,step);
00491      ACosXY acos_xy_c("C",c,step);
00492 
00493      ACosXYSol acos_xy_sol_a("A",a,step);
00494      ACosXYSol acos_xy_sol_b("B",b,step);
00495      ACosXYSol acos_xy_sol_c("C",c,step);
00496 
00497      ACosXX acos_xx_a("A",a,step);
00498      ACosXX acos_xx_b("B",b,step);
00499      ACosXX acos_xx_c("C",c,step);
00500 
00501      ATanXY atan_xy_a("A",a,step);
00502      ATanXY atan_xy_b("B",b,step);
00503      ATanXY atan_xy_c("C",c,step);
00504 
00505      ATanXYSol atan_xy_sol_a("A",a,step);
00506      ATanXYSol atan_xy_sol_b("B",b,step);
00507      ATanXYSol atan_xy_sol_c("C",c,step);
00508 
00509      ATanXX atan_xx_a("A",a,step);
00510      ATanXX atan_xx_b("B",b,step);
00511      ATanXX atan_xx_c("C",c,step);
00512 
00514 
00515    }
00516 }}
00517 
00518 #endif
00519 // STATISTICS: test-float