Generated on Sat Feb 7 2015 02:01:17 for Gecode by doxygen 1.8.9.1
sincos.hpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Vincent Barichard <Vincent.Barichard@univ-angers.fr>
5  *
6  * Copyright:
7  * Vincent Barichard, 2012
8  *
9  * Last modified:
10  * $Date: 2013-02-04 21:28:39 +0100 (Mon, 04 Feb 2013) $ by $Author: schulte $
11  * $Revision: 13262 $
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 namespace Gecode { namespace Float { namespace Trigonometric {
39 
40 
41  /*
42  * ASin projection function
43  *
44  */
45 template<class V>
46 void aSinProject(Rounding& r, const V& aSinIv, FloatNum& iv_min, FloatNum& iv_max, int& n_min, int& n_max) {
47  #define I0__PI_2I FloatVal(0,pi_half_upper())
48  #define IPI_2__PII FloatVal(pi_half_lower(),pi_upper())
49  #define IPI__3PI_2I FloatVal(pi_lower(),3*pi_half_upper())
50  #define I3PI_2__2PII FloatVal(3*pi_half_lower(),pi_twice_upper())
51  #define POS(X) ((I0__PI_2I.in(X))?0: (IPI_2__PII.in(X))?1: (IPI__3PI_2I.in(X))?2: 3 )
52  #define ASININF_DOWN r.asin_down(aSinIv.min())
53  #define ASINSUP_UP r.asin_up(aSinIv.max())
54 
55  // 0 <=> in [0;PI/2]
56  // 1 <=> in [PI/2;PI]
57  // 2 <=> in [PI;3*PI/2]
58  // 3 <=> in [3*PI/2;2*PI]
59  switch ( POS(iv_min) )
60  {
61  case 0:
62  if (r.sin_down(iv_min) > aSinIv.max()) { n_min++; iv_min = -ASINSUP_UP; }
63  else if (r.sin_up(iv_min) < aSinIv.min()) { iv_min = ASININF_DOWN; }
64  break;
65  case 1:
66  if (r.sin_down(iv_min) > aSinIv.max()) { n_min++; iv_min = -ASINSUP_UP; }
67  else if (r.sin_up(iv_min) < aSinIv.min()) { n_min+=2; iv_min = ASININF_DOWN; }
68  break;
69  case 2:
70  if (r.sin_down(iv_min) > aSinIv.max()) { n_min++; iv_min = -ASINSUP_UP; }
71  else if (r.sin_up(iv_min) < aSinIv.min()) { n_min+=2; iv_min = ASININF_DOWN; }
72  break;
73  case 3:
74  if (r.sin_down(iv_min) > aSinIv.max()) { n_min+=3; iv_min = -ASINSUP_UP; }
75  else if (r.sin_up(iv_min) < aSinIv.min()) { n_min+=2; iv_min = ASININF_DOWN; }
76  break;
77  default:
79  break;
80  }
81 
82  // 0 <=> in [0;PI/2]
83  // 1 <=> in [PI/2;PI]
84  // 2 <=> in [PI;3*PI/2]
85  // 3 <=> in [3*PI/2;2*PI]
86  switch ( POS(iv_max) )
87  {
88  case 0:
89  if (r.sin_down(iv_max) > aSinIv.max()) { iv_max = ASINSUP_UP; }
90  else if (r.sin_up(iv_max) < aSinIv.min()) { n_max--; iv_max = -ASININF_DOWN; }
91  break;
92  case 1:
93  if (r.sin_down(iv_max) > aSinIv.max()) { iv_max = ASINSUP_UP; }
94  else if (r.sin_up(iv_max) < aSinIv.min()) { n_max++; iv_max = -ASININF_DOWN; }
95  break;
96  case 2:
97  if (r.sin_down(iv_max) > aSinIv.max()) { iv_max = ASINSUP_UP; }
98  else if (r.sin_up(iv_max) < aSinIv.min()) { n_max++; iv_max = -ASININF_DOWN; }
99  break;
100  case 3:
101  if (r.sin_down(iv_max) > aSinIv.max()) { n_max+=2; iv_max = ASINSUP_UP; }
102  else if (r.sin_up(iv_max) < aSinIv.min()) { n_max++; iv_max = -ASININF_DOWN; }
103  break;
104  default:
105  GECODE_NEVER;
106  break;
107  }
108  #undef ASININF_DOWN
109  #undef ASINSUP_UP
110  #undef POS
111  #undef I0__PI_2I
112  #undef IPI_2__PII
113  #undef IPI__3PI_2I
114  #undef I3PI_2__2PII
115 }
116 
117 /*
118  * Bounds consistent sinus operator
119  *
120  */
121 
122  template<class A, class B>
124  Sin<A,B>::Sin(Home home, A x0, B x1)
125  : MixBinaryPropagator<A,PC_FLOAT_BND,B,PC_FLOAT_BND>(home,x0,x1) {}
126 
127  template<class A, class B>
128  ExecStatus
129  Sin<A,B>::post(Home home, A x0, B x1) {
130  if (same(x0,x1)) {
131  GECODE_ME_CHECK(x0.eq(home,0.0));
132  } else {
133  GECODE_ME_CHECK(x1.gq(home,-1.0));
134  GECODE_ME_CHECK(x1.lq(home,1.0));
135  (void) new (home) Sin<A,B>(home,x0,x1);
136  }
137 
138  return ES_OK;
139  }
140 
141 
142  template<class A, class B>
144  Sin<A,B>::Sin(Space& home, bool share, Sin<A,B>& p)
145  : MixBinaryPropagator<A,PC_FLOAT_BND,B,PC_FLOAT_BND>(home,share,p) {}
146 
147  template<class A, class B>
148  Actor*
149  Sin<A,B>::copy(Space& home, bool share) {
150  return new (home) Sin<A,B>(home,share,*this);
151  }
152 
153  template<class A, class B>
154  ExecStatus
156  GECODE_ME_CHECK(x1.eq(home,sin(x0.val())));
157  Rounding r;
158  int n_min = 2*static_cast<int>(r.div_up(x0.min(), pi_twice_upper()));
159  int n_max = 2*static_cast<int>(r.div_up(x0.max(), pi_twice_upper()));
160  if (x0.min() < 0) n_min-=2;
161  if (x0.max() < 0) n_max-=2;
162  FloatNum iv_min = r.sub_down(x0.min(),r.mul_down(n_min, pi_upper()));
163  FloatNum iv_max = r.sub_up (x0.max(),r.mul_down(n_max, pi_upper()));
164  aSinProject(r,x1,iv_min,iv_max,n_min,n_max);
165  FloatNum n_iv_min = r.add_down(iv_min,r.mul_down(n_min, pi_upper()));
166  FloatNum n_iv_max = r.add_up (iv_max,r.mul_down(n_max, pi_upper()));
167  if (n_iv_min > n_iv_max) return ES_FAILED;
168  GECODE_ME_CHECK(x0.eq(home,FloatVal(n_iv_min,n_iv_max)));
169  GECODE_ME_CHECK(x1.eq(home,sin(x0.val()))); // Redo sin because with x0 reduction, sin may be more accurate
170  return (x0.assigned()) ? home.ES_SUBSUMED(*this) : ES_FIX;
171  }
172 
173  /*
174  * Bounds consistent cosinus operator
175  *
176  */
177 
178  template<class A, class B>
180  Cos<A,B>::Cos(Home home, A x0, B x1)
181  : MixBinaryPropagator<A,PC_FLOAT_BND,B,PC_FLOAT_BND>(home,x0,x1) {}
182 
183  template<class A, class B>
184  ExecStatus
185  Cos<A,B>::post(Home home, A x0, B x1) {
186  if (same(x0,x1)) {
187  GECODE_ME_CHECK(x0.gq(home,0.7390851332151));
188  GECODE_ME_CHECK(x0.lq(home,0.7390851332152));
189  bool mod;
190  do {
191  mod = false;
192  GECODE_ME_CHECK_MODIFIED(mod,x0.eq(home,cos(x0.val())));
193  } while (mod);
194  } else {
195  GECODE_ME_CHECK(x1.gq(home,-1.0));
196  GECODE_ME_CHECK(x1.lq(home,1.0));
197  (void) new (home) Cos<A,B>(home,x0,x1);
198  }
199  return ES_OK;
200  }
201 
202 
203  template<class A, class B>
205  Cos<A,B>::Cos(Space& home, bool share, Cos<A,B>& p)
206  : MixBinaryPropagator<A,PC_FLOAT_BND,B,PC_FLOAT_BND>(home,share,p) {}
207 
208  template<class A, class B>
209  Actor*
210  Cos<A,B>::copy(Space& home, bool share) {
211  return new (home) Cos<A,B>(home,share,*this);
212  }
213 
214  template<class A, class B>
215  ExecStatus
217  GECODE_ME_CHECK(x1.eq(home,cos(x0.val())));
218  Rounding r;
219  FloatVal x0Trans = x0.val() + FloatVal::pi_half();
220  int n_min = 2*static_cast<int>(r.div_up(x0Trans.min(), pi_twice_upper()));
221  int n_max = 2*static_cast<int>(r.div_up(x0Trans.max(), pi_twice_upper()));
222  if (x0Trans.min() < 0) n_min-=2;
223  if (x0Trans.max() < 0) n_max-=2;
224  FloatNum iv_min = r.sub_down(x0Trans.min(),r.mul_down(n_min, pi_upper()));
225  FloatNum iv_max = r.sub_up (x0Trans.max(),r.mul_down(n_max, pi_upper()));
226  aSinProject(r,x1,iv_min,iv_max,n_min,n_max);
227  FloatNum n_iv_min = r.add_down(iv_min,r.mul_down(n_min, pi_upper()));
228  FloatNum n_iv_max = r.add_up (iv_max,r.mul_down(n_max, pi_upper()));
229  if (n_iv_min > n_iv_max) return ES_FAILED;
230  GECODE_ME_CHECK(x0.eq(home,FloatVal(n_iv_min,n_iv_max) - FloatVal::pi_half()));
231  GECODE_ME_CHECK(x1.eq(home,cos(x0.val()))); // Redo sin because with x0 reduction, sin may be more accurate
232  return (x0.assigned()) ? home.ES_SUBSUMED(*this) : ES_FIX;
233  }
234 
235 }}}
236 
237 // STATISTICS: float-prop
238 
Propagator for bounds consistent cosinus operator
void mod(Home home, IntVar x0, IntVar x1, IntVar x2, IntConLevel icl)
Post propagator for .
Definition: arithmetic.cpp:213
ExecStatus ES_SUBSUMED(Propagator &p)
Definition: core.hpp:2973
static ExecStatus post(Home home, A x0, B x1)
Post propagator for .
Definition: sincos.hpp:185
#define ASINSUP_UP
Sin(Space &home, bool share, Sin &p)
Constructor for cloning p.
Definition: sincos.hpp:144
void aSinProject(Rounding &r, const V &aSinIv, FloatNum &iv_min, FloatNum &iv_max, int &n_min, int &n_max)
Definition: sincos.hpp:46
static ExecStatus post(Home home, A x0, B x1)
Post propagator for .
Definition: sincos.hpp:129
Propagation has computed fixpoint.
Definition: core.hpp:528
Computation spaces.
Definition: core.hpp:1362
#define GECODE_ME_CHECK_MODIFIED(modified, me)
Check whether me is failed or modified, and forward failure.
Definition: macros.hpp:57
Base-class for both propagators and branchers.
Definition: core.hpp:666
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
bool same(const ConstView< ViewA > &, const ConstView< ViewB > &)
Test whether two views are the same.
Definition: view.hpp:603
FloatNum sin_up(FloatNum x)
Return upper bound of sine of x (domain: )
#define ASININF_DOWN
Execution has resulted in failure.
Definition: core.hpp:525
FloatNum pi_twice_upper(void)
Return upper bound of .
Definition: num.hpp:61
NNF * r
Right subtree.
Definition: bool-expr.cpp:246
#define POS(X)
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: sincos.hpp:216
#define GECODE_ME_CHECK(me)
Check whether modification event me is failed, and forward failure.
Definition: macros.hpp:45
Floating point rounding policy.
Definition: float.hh:137
void cos(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition: arithmetic.cpp:196
virtual Actor * copy(Space &home, bool share)
Create copy during cloning.
Definition: sincos.hpp:210
Float value type.
Definition: float.hh:321
Mixed binary propagator.
Definition: propagator.hpp:203
static FloatVal pi_half(void)
Return .
Definition: val.hpp:113
ExecStatus
Definition: core.hpp:523
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition: sincos.hpp:155
#define forceinline
Definition: config.hpp:132
Propagator for bounds consistent sinus operator
Execution is okay.
Definition: core.hpp:527
const Gecode::PropCond PC_FLOAT_BND
Propagate when minimum or maximum of a view changes.
Definition: var-type.hpp:292
Gecode toplevel namespace
void sin(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition: arithmetic.cpp:182
friend FloatVal max(const FloatVal &x, const FloatVal &y)
Definition: val.hpp:390
int ModEventDelta
Modification event deltas.
Definition: core.hpp:173
friend FloatVal min(const FloatVal &x, const FloatVal &y)
Definition: val.hpp:402
Home class for posting propagators
Definition: core.hpp:717
double FloatNum
Floating point number base type.
Definition: float.hh:108
#define GECODE_NEVER
Assert that this command is never executed.
Definition: macros.hpp:60
Cos(Space &home, bool share, Cos &p)
Constructor for cloning p.
Definition: sincos.hpp:205
FloatNum sin_down(FloatNum x)
Return lower bound of sine of x (domain: )
virtual Actor * copy(Space &home, bool share)
Create copy during cloning.
Definition: sincos.hpp:149
FloatNum pi_upper(void)
Return upper bound of .
Definition: num.hpp:53