Generated on Tue Feb 11 2025 17:33:26 for Gecode by doxygen 1.12.0
trigonometric.cpp
Go to the documentation of this file.
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Christian Schulte <schulte@gecode.org>
5 * Vincent Barichard <Vincent.Barichard@univ-angers.fr>
6 *
7 * Copyright:
8 * Christian Schulte, 2005
9 * Vincent Barichard, 2012
10 *
11 * This file is part of Gecode, the generic constraint
12 * development environment:
13 * http://www.gecode.org
14 *
15 * Permission is hereby granted, free of charge, to any person obtaining
16 * a copy of this software and associated documentation files (the
17 * "Software"), to deal in the Software without restriction, including
18 * without limitation the rights to use, copy, modify, merge, publish,
19 * distribute, sublicense, and/or sell copies of the Software, and to
20 * permit persons to whom the Software is furnished to do so, subject to
21 * the following conditions:
22 *
23 * The above copyright notice and this permission notice shall be
24 * included in all copies or substantial portions of the Software.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 *
34 */
35
36#include "test/float.hh"
37#include <gecode/minimodel.hh>
38
39#ifdef GECODE_HAS_MPFR
40
41#include <cmath>
42#include <algorithm>
43
44namespace Test { namespace Float {
45
47 namespace Trigonometric {
48
50 class SinXY : public Test {
51 public:
53 SinXY(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
54 : Test("Trigonometric::Sin::XY::"+s,2,d,st,CPLT_ASSIGNMENT,false) {}
56 virtual MaybeType solution(const Assignment& x) const {
57 return eq(sin(x[0]), x[1]);
58 }
60 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
61 if (flip())
62 Gecode::sin(home, x[0], x[1]);
63 else
64 Gecode::rel(home, sin(x[0]) == x[1]);
65 }
66 };
67
69 class SinXYSol : public Test {
70 public:
72 SinXYSol(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
73 : Test("Trigonometric::Sin::XY::Sol::"+s,2,d,st,EXTEND_ASSIGNMENT,false) {}
75 virtual MaybeType solution(const Assignment& x) const {
76 return eq(sin(x[0]), x[1]);
77 }
79 virtual bool extendAssignement(Assignment& x) const {
80 Gecode::FloatVal d = sin(x[0]);
81 if (Gecode::Float::subset(d, dom)) {
82 x.set(1, d);
83 return true;
84 } else {
85 return false;
86 }
87 }
89 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) {
90 Gecode::sin(home, x[0], x[1]);
91 }
92 };
93
95 class SinXX : public Test {
96 public:
98 SinXX(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
99 : Test("Trigonometric::Sin::XX::"+s,1,d,st,CPLT_ASSIGNMENT,false) {}
101 virtual MaybeType solution(const Assignment& x) const {
102 return eq(sin(x[0]), x[0]);
103 }
106 Gecode::sin(home, x[0], x[0]);
107 }
108 };
109
111 class CosXY : public Test {
112 public:
114 CosXY(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
115 : Test("Trigonometric::Cos::XY::"+s,2,d,st,CPLT_ASSIGNMENT,false) {}
117 virtual MaybeType solution(const Assignment& x) const {
118 return eq(cos(x[0]), x[1]);
119 }
122 if (flip())
123 Gecode::cos(home, x[0], x[1]);
124 else
125 Gecode::rel(home, cos(x[0]) == x[1]);
126 }
127 };
128
130 class CosXYSol : public Test {
131 public:
133 CosXYSol(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
134 : Test("Trigonometric::Cos::XY::Sol::"+s,2,d,st,EXTEND_ASSIGNMENT,false) {}
136 virtual MaybeType solution(const Assignment& x) const {
137 return eq(cos(x[0]), x[1]);
138 }
140 virtual bool extendAssignement(Assignment& x) const {
141 Gecode::FloatVal d = cos(x[0]);
142 if (Gecode::Float::subset(d, dom)) {
143 x.set(1, d);
144 return true;
145 } else {
146 return false;
147 }
148 }
151 Gecode::cos(home, x[0], x[1]);
152 }
153 };
154
156 class CosXX : public Test {
157 public:
159 CosXX(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
160 : Test("Trigonometric::Cos::XX::"+s,1,d,st,CPLT_ASSIGNMENT,false) {}
162 virtual MaybeType solution(const Assignment& x) const {
163 return eq(cos(x[0]), x[0]);
164 }
167 Gecode::cos(home, x[0], x[0]);
168 }
169 };
170
172 class TanXY : public Test {
173 public:
175 TanXY(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
176 : Test("Trigonometric::Tan::XY::"+s,2,d,st,CPLT_ASSIGNMENT,false) {}
178 virtual MaybeType solution(const Assignment& x) const {
179 return eq(tan(x[0]), x[1]);
180 }
183 if (flip())
184 Gecode::tan(home, x[0], x[1]);
185 else
186 Gecode::rel(home, tan(x[0]) == x[1]);
187 }
188 };
189
191 class TanXYSol : public Test {
192 public:
194 TanXYSol(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
195 : Test("Trigonometric::Tan::XY::Sol::"+s,2,d,st,EXTEND_ASSIGNMENT,false) {}
197 virtual MaybeType solution(const Assignment& x) const {
198 return eq(tan(x[0]), x[1]);
199 }
201 virtual bool extendAssignement(Assignment& x) const {
202 Gecode::FloatVal d = tan(x[0]);
203 if (Gecode::Float::subset(d, dom)) {
204 x.set(1, d);
205 return true;
206 } else {
207 return false;
208 }
209 }
212 Gecode::tan(home, x[0], x[1]);
213 }
214 };
215
217 class TanXX : public Test {
218 public:
220 TanXX(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
221 : Test("Trigonometric::Tan::XX::"+s,1,d,st,CPLT_ASSIGNMENT,false) {}
223 virtual MaybeType solution(const Assignment& x) const {
224 return eq(tan(x[0]), x[0]);
225 }
228 Gecode::tan(home, x[0], x[0]);
229 }
230 };
231
233 class ASinXY : public Test {
234 public:
236 ASinXY(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
237 : Test("Trigonometric::ASin::XY::"+s,2,d,st,CPLT_ASSIGNMENT,false) {}
239 virtual MaybeType solution(const Assignment& x) const {
240 if ((x[0].min() > 1.0) || (x[0].max() < -1.0))
241 return MT_FALSE;
242 return eq(asin(x[0]), x[1]);
243 }
246 if (flip())
247 Gecode::asin(home, x[0], x[1]);
248 else
249 Gecode::rel(home, asin(x[0]) == x[1]);
250 }
251 };
252
254 class ASinXYSol : public Test {
255 public:
257 ASinXYSol(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
258 : Test("Trigonometric::ASin::XY::Sol::"+s,2,d,st,EXTEND_ASSIGNMENT,false) {}
260 virtual MaybeType solution(const Assignment& x) const {
261 if ((x[0].min() > 1.0) || (x[0].max() < -1.0))
262 return MT_FALSE;
263 return eq(asin(x[0]), x[1]);
264 }
266 virtual bool extendAssignement(Assignment& x) const {
267 if ((x[0].min() > 1.0) || (x[0].max() < -1.0))
268 return false;
269 Gecode::FloatVal d = asin(x[0]);
270 if (Gecode::Float::subset(d, dom)) {
271 x.set(1, d);
272 return true;
273 } else {
274 return false;
275 }
276 }
279 Gecode::asin(home, x[0], x[1]);
280 }
281 };
282
284 class ASinXX : public Test {
285 public:
287 ASinXX(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
288 : Test("Trigonometric::ASin::XX::"+s,1,d,st,CPLT_ASSIGNMENT,false) {}
290 virtual MaybeType solution(const Assignment& x) const {
291 if ((x[0].min() > 1.0) || (x[0].max() < -1.0))
292 return MT_FALSE;
293 return eq(asin(x[0]), x[0]);
294 }
297 Gecode::asin(home, x[0], x[0]);
298 }
299 };
300
302 class ACosXY : public Test {
303 public:
305 ACosXY(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
306 : Test("Trigonometric::ACos::XY::"+s,2,d,st,CPLT_ASSIGNMENT,false) {}
308 virtual MaybeType solution(const Assignment& x) const {
309 if ((x[0].min() > 1.0) || (x[0].max() < -1.0))
310 return MT_FALSE;
311 return eq(acos(x[0]), x[1]);
312 }
315 if (flip())
316 Gecode::acos(home, x[0], x[1]);
317 else
318 Gecode::rel(home, acos(x[0]) == x[1]);
319 }
320 };
321
323 class ACosXYSol : public Test {
324 public:
326 ACosXYSol(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
327 : Test("Trigonometric::ACos::XY::Sol::"+s,2,d,st,EXTEND_ASSIGNMENT,false) {}
329 virtual MaybeType solution(const Assignment& x) const {
330 if ((x[0].min() > 1.0) || (x[0].max() < -1.0))
331 return MT_FALSE;
332 return eq(acos(x[0]), x[1]);
333 }
335 virtual bool extendAssignement(Assignment& x) const {
336 if ((x[0].min() > 1.0) || (x[0].max() < -1.0))
337 return false;
338 Gecode::FloatVal d = acos(x[0]);
339 if (Gecode::Float::subset(d, dom)) {
340 x.set(1, d);
341 return true;
342 } else {
343 return false;
344 }
345 }
348 Gecode::acos(home, x[0], x[1]);
349 }
350 };
351
353 class ACosXX : public Test {
354 public:
356 ACosXX(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
357 : Test("Trigonometric::ACos::XX::"+s,1,d,st,CPLT_ASSIGNMENT,false) {}
359 virtual MaybeType solution(const Assignment& x) const {
360 if ((x[0].min() > 1.0) || (x[0].max() < -1.0))
361 return MT_FALSE;
362 return eq(acos(x[0]), x[0]);
363 }
366 Gecode::acos(home, x[0], x[0]);
367 }
368 };
369
371 class ATanXY : public Test {
372 public:
374 ATanXY(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
375 : Test("Trigonometric::ATan::XY::"+s,2,d,st,CPLT_ASSIGNMENT,false) {}
377 virtual MaybeType solution(const Assignment& x) const {
378 return eq(atan(x[0]), x[1]);
379 }
382 if (flip())
383 Gecode::atan(home, x[0], x[1]);
384 else
385 Gecode::rel(home, atan(x[0]) == x[1]);
386 }
387 };
388
390 class ATanXYSol : public Test {
391 public:
393 ATanXYSol(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
394 : Test("Trigonometric::ATan::XY::Sol::"+s,2,d,st,EXTEND_ASSIGNMENT,false) {}
396 virtual MaybeType solution(const Assignment& x) const {
397 return eq(atan(x[0]), x[1]);
398 }
400 virtual bool extendAssignement(Assignment& x) const {
401 Gecode::FloatVal d = atan(x[0]);
402 if (Gecode::Float::subset(d, dom)) {
403 x.set(1, d);
404 return true;
405 } else {
406 return false;
407 }
408 }
411 Gecode::atan(home, x[0], x[1]);
412 }
413 };
414
416 class ATanXX : public Test {
417 public:
419 ATanXX(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st)
420 : Test("Trigonometric::ATan::XX::"+s,1,d,st,CPLT_ASSIGNMENT,false) {}
422 virtual MaybeType solution(const Assignment& x) const {
423 return eq(atan(x[0]), x[0]);
424 }
427 Gecode::atan(home, x[0], x[0]);
428 }
429 };
430
431 const Gecode::FloatNum step = 0.15;
436
440
444
448
452
456
460
464
468
472
476
480
484
488
492
496
500
504
508
510
511 }
512}}
513
514#endif
515// STATISTICS: test-float
Node * x
Pointer to corresponding Boolean expression node.
Float value type.
Definition float.hh:334
Float variable array.
Definition float.hh:1030
Computation spaces.
Definition core.hpp:1742
Base class for assignments
Definition float.hh:80
static MaybeType eq(Gecode::FloatVal x, Gecode::FloatVal y)
Whether x and y are equal.
Definition float.hpp:268
bool flip(void)
Flip a coin and return true or false randomly.
Definition float.hpp:273
Gecode::FloatVal dom
Domain of variables.
Definition float.hh:249
Test for acosinus constraint with shared variables
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
ACosXX(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum st)
Create and register test.
Test for acosinus constraint where solution is ensured
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
ACosXYSol(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum st)
Create and register test.
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
virtual bool extendAssignement(Assignment &x) const
Extend assignment x.
Test for acosinus constraint
ACosXY(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum st)
Create and register test.
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
Test for asinus constraint with shared variables
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
ASinXX(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum st)
Create and register test.
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
Test for asinus constraint where solution is ensured
ASinXYSol(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum st)
Create and register test.
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
virtual bool extendAssignement(Assignment &x) const
Extend assignment x.
Test for asinus constraint
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
ASinXY(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum st)
Create and register test.
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
Test for atangent constraint with shared variables
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
ATanXX(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum st)
Create and register test.
Test for atangent constraint where solution is ensured
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
virtual bool extendAssignement(Assignment &x) const
Extend assignment x.
ATanXYSol(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum st)
Create and register test.
Test for atangent constraint
ATanXY(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum st)
Create and register test.
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
Test for cosinus constraint with shared variables
CosXX(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum st)
Create and register test.
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
Test for cosinus constraint where solution is ensured
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
CosXYSol(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum st)
Create and register test.
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
virtual bool extendAssignement(Assignment &x) const
Extend assignment x.
Test for cosinus constraint
CosXY(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum st)
Create and register test.
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
Test for sinus constraint with shared variables
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
SinXX(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum st)
Create and register test.
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
Test for sinus constraint where solution is ensured
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
virtual bool extendAssignement(Assignment &x) const
Extend assignment x.
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
SinXYSol(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum st)
Create and register test.
Test for sinus constraint
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
SinXY(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum st)
Create and register test.
Test for tangent constraint with shared variables
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
TanXX(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum st)
Create and register test.
Test for tangent constraint where solution is ensured
virtual bool extendAssignement(Assignment &x) const
Extend assignment x.
TanXYSol(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum st)
Create and register test.
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
Test for tangent constraint
virtual MaybeType solution(const Assignment &x) const
Test whether x is solution
virtual void post(Gecode::Space &home, Gecode::FloatVarArray &x)
Post constraint on x.
TanXY(const std::string &s, const Gecode::FloatVal &d, Gecode::FloatNum st)
Create and register test.
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVar x1)
Post propagator for .
Definition rel.cpp:68
double FloatNum
Floating point number base type.
Definition float.hh:106
bool subset(const FloatVal &x, const FloatVal &y)
Definition val.hpp:490
void sin(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
void cos(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
void acos(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
void atan(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
void asin(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
void tan(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
TanXX tan_xx_c("C", c, step)
ACosXYSol acos_xy_sol_c("C", c, step)
CosXYSol cos_xy_sol_a("A", a, step)
ASinXX asin_xx_b("B", b, step)
CosXY cos_xy_a("A", a, step)
ACosXX acos_xx_b("B", b, step)
CosXX cos_xx_a("A", a, step)
CosXYSol cos_xy_sol_b("B", b, step)
ATanXYSol atan_xy_sol_b("B", b, step)
TanXYSol tan_xy_sol_b("B", b, step)
Gecode::FloatVal c(-8, 8)
ACosXY acos_xy_c("C", c, step)
SinXYSol sin_xy_sol_c("C", c, step)
TanXY tan_xy_a("A", a, step)
SinXY sin_xy_c("C", c, step)
ATanXY atan_xy_c("C", c, step)
SinXY sin_xy_a("A", a, step)
ATanXX atan_xx_c("C", c, step)
TanXYSol tan_xy_sol_a("A", a, step)
TanXYSol tan_xy_sol_c("C", c, step)
CosXX cos_xx_b("B", b, step)
ATanXYSol atan_xy_sol_c("C", c, step)
SinXYSol sin_xy_sol_b("B", b, step)
CosXYSol cos_xy_sol_c("C", c, step)
SinXX sin_xx_c("C", c, step)
CosXY cos_xy_b("B", b, step)
Gecode::FloatVal a(-8, 5)
TanXX tan_xx_a("A", a, step)
ACosXYSol acos_xy_sol_b("B", b, step)
SinXY sin_xy_b("B", b, step)
const Gecode::FloatNum step2
ATanXY atan_xy_b("B", b, step)
ASinXX asin_xx_a("A", a, step)
ASinXYSol asin_xy_sol_a("A", a, step)
ACosXY acos_xy_a("A", a, step)
ATanXYSol atan_xy_sol_a("A", a, step)
TanXY tan_xy_c("C", c, step)
CosXX cos_xx_c("C", c, step)
ACosXYSol acos_xy_sol_a("A", a, step)
SinXX sin_xx_a("A", a, step)
TanXY tan_xy_b("B", b, step)
SinXX sin_xx_b("B", b, step)
ASinXY asin_xy_c("C", c, step)
TanXX tan_xx_b("B", b, step)
const Gecode::FloatNum step
ACosXX acos_xx_c("C", c, step)
ASinXX asin_xx_c("C", c, step)
ACosXY acos_xy_b("B", b, step)
ASinXYSol asin_xy_sol_b("B", b, step)
Gecode::FloatVal b(9, 12)
ATanXX atan_xx_b("B", b, step)
ACosXX acos_xx_a("A", a, step)
SinXYSol sin_xy_sol_a("A", a, step)
ASinXY asin_xy_b("B", b, step)
ASinXYSol asin_xy_sol_c("C", c, step)
CosXY cos_xy_c("C", c, step)
ATanXX atan_xx_a("A", a, step)
ASinXY asin_xy_a("A", a, step)
ATanXY atan_xy_a("A", a, step)
@ EXTEND_ASSIGNMENT
Definition float.hh:64
@ CPLT_ASSIGNMENT
Definition float.hh:62
MaybeType
Type for comparisons and solutions.
Definition float.hh:51
General test support.
Definition afc.cpp:39