Generated on for Gecode by doxygen 1.17.0
exp-log.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 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.dev
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining
14 * a copy of this software and associated documentation files (the
15 * "Software"), to deal in the Software without restriction, including
16 * without limitation the rights to use, copy, modify, merge, publish,
17 * distribute, sublicense, and/or sell copies of the Software, and to
18 * permit persons to whom the Software is furnished to do so, subject to
19 * the following conditions:
20 *
21 * The above copyright notice and this permission notice shall be
22 * included in all copies or substantial portions of the Software.
23 *
24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 *
32 */
33
34#include <mpfr.h>
35
36namespace Gecode { namespace Float { namespace Transcendental {
37
38 // Round endpoints explicitly; compiler optimizations can ignore changes to
39 // the hardware rounding mode.
40 forceinline FloatVal
41 fixed_base_pow(FloatNum base, const FloatVal& exponent) {
42 mpfr_t b, x, y;
43 mpfr_init2(b, std::numeric_limits<FloatNum>::digits);
44 mpfr_init2(x, std::numeric_limits<FloatNum>::digits);
45 mpfr_init2(y, std::numeric_limits<FloatNum>::digits);
46 mpfr_set_d(b, base, GMP_RNDN);
47 mpfr_set_d(x, base > 1.0 ? exponent.min() : exponent.max(), GMP_RNDN);
48 mpfr_pow(y, b, x, GMP_RNDD);
49 FloatNum lower = mpfr_get_d(y, GMP_RNDD);
50 mpfr_set_d(x, base > 1.0 ? exponent.max() : exponent.min(), GMP_RNDN);
51 mpfr_pow(y, b, x, GMP_RNDU);
52 FloatNum upper = mpfr_get_d(y, GMP_RNDU);
53 mpfr_clear(b);
54 mpfr_clear(x);
55 mpfr_clear(y);
56 return FloatVal(lower, upper);
57 }
58
60 fixed_base_log(FloatNum base, const FloatVal& value) {
61 FloatVal numerator = log(value);
62 FloatVal denominator = log(FloatVal(base));
63 const FloatNum n[] = {numerator.min(), numerator.max()};
64 const FloatNum d[] = {denominator.min(), denominator.max()};
65 FloatNum lower = std::numeric_limits<FloatNum>::infinity();
66 FloatNum upper = -lower;
67 mpfr_t x, y, q;
68 mpfr_init2(x, std::numeric_limits<FloatNum>::digits);
69 mpfr_init2(y, std::numeric_limits<FloatNum>::digits);
70 mpfr_init2(q, std::numeric_limits<FloatNum>::digits);
71 // log(base) does not contain zero. Divide all four endpoint pairs with
72 // outward rounding, including when 0 < base < 1.
73 for (int i=0; i<2; i++) {
74 mpfr_set_d(x, n[i], GMP_RNDN);
75 for (int j=0; j<2; j++) {
76 mpfr_set_d(y, d[j], GMP_RNDN);
77 mpfr_div(q, x, y, GMP_RNDD);
78 lower = std::min(lower, mpfr_get_d(q, GMP_RNDD));
79 mpfr_div(q, x, y, GMP_RNDU);
80 upper = std::max(upper, mpfr_get_d(q, GMP_RNDU));
81 }
82 }
83 mpfr_clear(x);
84 mpfr_clear(y);
85 mpfr_clear(q);
86 return FloatVal(lower, upper);
87 }
88
89 /*
90 * Bounds consistent exponential operator
91 *
92 */
93
94 template<class A, class B>
98
99 template<class A, class B>
101 Exp<A,B>::post(Home home, A x0, B x1) {
102 if (x0 == x1) {
103 return ES_FAILED;
104 } else {
105 GECODE_ME_CHECK(x1.gq(home,0.0));
106 }
107 GECODE_ME_CHECK(x1.eq(home,exp(x0.domain())));
108 if (x1.max() == 0.0)
109 return ES_FAILED;
110 GECODE_ME_CHECK(x0.eq(home,log(x1.domain())));
111 (void) new (home) Exp<A,B>(home,x0,x1);
112 return ES_OK;
113 }
114
115
116 template<class A, class B>
120
121 template<class A, class B>
122 Actor*
124 return new (home) Exp<A,B>(home,*this);
125 }
126
127 template<class A, class B>
130 GECODE_ME_CHECK(x1.eq(home,exp(x0.domain())));
131 if (x1.max() == 0.0)
132 return ES_FAILED;
133 GECODE_ME_CHECK(x0.eq(home,log(x1.domain())));
134 return x0.assigned() ? home.ES_SUBSUMED(*this) : ES_FIX;
135 }
136
137
138 /*
139 * Bounds consistent logarithm operator with base
140 *
141 */
142
143 template<class A, class B>
145 Pow<A,B>::Pow(Home home, FloatNum base0, A x0, B x1)
147 base(base0) {}
148
149 template<class A, class B>
152 if (base <= 0) return ES_FAILED;
153 if (base == 1.0) {
154 GECODE_ME_CHECK(x1.eq(home,1.0));
155 return ES_OK;
156 }
157 GECODE_ME_CHECK(x1.gq(home,0.0));
158 if (x1.max() == 0.0)
159 return ES_FAILED;
160 GECODE_ME_CHECK(x0.eq(home,fixed_base_log(base,x1.domain())));
161 GECODE_ME_CHECK(x1.eq(home,fixed_base_pow(base,x0.domain())));
162 (void) new (home) Pow<A,B>(home,base,x0,x1);
163 return ES_OK;
164 }
165
166 template<class A, class B>
171
172 template<class A, class B>
173 Actor*
175 return new (home) Pow<A,B>(home,*this);
176 }
177
178 template<class A, class B>
181 if (x1.max() == 0.0)
182 return ES_FAILED;
183 GECODE_ME_CHECK(x0.eq(home,fixed_base_log(base,x1.domain())));
184 GECODE_ME_CHECK(x1.eq(home,fixed_base_pow(base,x0.domain())));
185 return x0.assigned() ? home.ES_SUBSUMED(*this) : ES_NOFIX;
186 }
187
188}}}
189
190// STATISTICS: float-prop
191
Base-class for both propagators and branchers.
Definition core.hpp:635
Float value type.
Definition float.hh:329
friend FloatVal max(const FloatVal &x, const FloatVal &y)
Definition val.hpp:387
friend FloatVal min(const FloatVal &x, const FloatVal &y)
Definition val.hpp:399
static ExecStatus post(Home home, A x0, B x1)
Post propagator for .
Definition exp-log.hpp:101
virtual Actor * copy(Space &home)
Create copy during cloning.
Definition exp-log.hpp:123
Exp(Space &home, Exp &p)
Constructor for cloning p.
Definition exp-log.hpp:118
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition exp-log.hpp:129
static ExecStatus post(Home home, FloatNum base, A x0, B x1)
Post propagator for .
Definition exp-log.hpp:151
Pow(Space &home, Pow &p)
Constructor for cloning p.
Definition exp-log.hpp:168
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition exp-log.hpp:180
virtual Actor * copy(Space &home)
Create copy during cloning.
Definition exp-log.hpp:174
Home class for posting propagators
Definition core.hpp:863
MixBinaryPropagator(Space &home, MixBinaryPropagator &p)
Definition pattern.hpp:597
friend class Space
Definition core.hpp:1075
ExecStatus ES_SUBSUMED(Propagator &p)
Propagator p is subsumed
Definition core.hpp:3780
int ModEventDelta
Modification event deltas.
Definition core.hpp:94
#define GECODE_ME_CHECK(me)
Check whether modification event me is failed, and forward failure.
Definition macros.hpp:52
double FloatNum
Floating point number base type.
Definition float.hh:106
Transcendental propagators
FloatVal fixed_base_log(FloatNum base, const FloatVal &value)
Definition exp-log.hpp:60
FloatVal fixed_base_pow(FloatNum base, const FloatVal &exponent)
Definition exp-log.hpp:41
Floating point numbers.
const Gecode::PropCond PC_FLOAT_BND
Propagate when minimum or maximum of a view changes.
Definition var-type.hpp:300
Gecode toplevel namespace
void log(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
void exp(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
ExecStatus
Definition core.hpp:479
@ ES_OK
Execution is okay.
Definition core.hpp:483
@ ES_FIX
Propagation has computed fixpoint.
Definition core.hpp:484
@ ES_FAILED
Execution has resulted in failure.
Definition core.hpp:481
@ ES_NOFIX
Propagation has not computed fixpoint.
Definition core.hpp:482
#define forceinline
Definition config.hpp:144