this repo has no description
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 2/* 3 * Main authors: 4 * Christian Schulte <schulte@gecode.org> 5 * Guido Tack <tack@gecode.org> 6 * Vincent Barichard <Vincent.Barichard@univ-angers.fr> 7 * 8 * Copyright: 9 * Christian Schulte, 2004 10 * Guido Tack, 2006 11 * Vincent Barichard, 2012 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 38namespace Gecode { namespace Float { namespace Arithmetic { 39 40 /* 41 * Positive bounds consistent squaring 42 * 43 */ 44 template<class VA, class VB> 45 forceinline 46 SqrPlus<VA,VB>::SqrPlus(Home home, VA x0, VB x1) 47 : MixBinaryPropagator<VA,PC_FLOAT_BND,VB,PC_FLOAT_BND>(home,x0,x1) {} 48 49 template<class VA, class VB> 50 forceinline ExecStatus 51 SqrPlus<VA,VB>::post(Home home, VA x0, VB x1) { 52 if (x0 == x1) { 53 if (x0.assigned()) 54 return ((x0.val() == 0) || (x0.val() == 1))? ES_OK : ES_FAILED; 55 } else { 56 GECODE_ME_CHECK(x0.eq(home,sqrt(x1.val()))); 57 GECODE_ME_CHECK(x1.eq(home,sqr(x0.val()))); 58 } 59 60 (void) new (home) SqrPlus<VA,VB>(home,x0,x1); 61 return ES_OK; 62 } 63 64 template<class VA, class VB> 65 forceinline 66 SqrPlus<VA,VB>::SqrPlus(Space& home, SqrPlus<VA,VB>& p) 67 : MixBinaryPropagator<VA,PC_FLOAT_BND,VB,PC_FLOAT_BND>(home,p) {} 68 69 template<class VA, class VB> 70 Actor* 71 SqrPlus<VA,VB>::copy(Space& home) { 72 return new (home) SqrPlus<VA,VB>(home,*this); 73 } 74 75 template<class VA, class VB> 76 ExecStatus 77 SqrPlus<VA,VB>::propagate(Space& home, const ModEventDelta&) { 78 if (x0 == x1) { 79 if (x0.max() < 1) GECODE_ME_CHECK(x0.eq(home,0)); 80 else if (x0.min() > 0) GECODE_ME_CHECK(x0.eq(home,1)); 81 if (x0.assigned()) 82 return ((x0.val() == 0) || (x0.val() == 1))? home.ES_SUBSUMED(*this) : ES_FAILED; 83 } else { 84 GECODE_ME_CHECK(x0.eq(home,sqrt(x1.val()))); 85 GECODE_ME_CHECK(x1.eq(home,sqr(x0.val()))); 86 if (x0.assigned() || x1.assigned()) return home.ES_SUBSUMED(*this); 87 } 88 89 return ES_FIX; 90 } 91 92 93 /* 94 * Bounds consistent squaring 95 * 96 */ 97 98 template<class View> 99 forceinline 100 Sqr<View>::Sqr(Home home, View x0, View x1) 101 : BinaryPropagator<View,PC_FLOAT_BND>(home,x0,x1) {} 102 103 template<class View> 104 forceinline ExecStatus 105 Sqr<View>::post(Home home, View x0, View x1) { 106 GECODE_ME_CHECK(x1.gq(home,0)); 107 if (x0 == x1) { 108 if (x0.assigned()) 109 return ((x0.val() == 0) || (x0.val() == 1))? ES_OK : ES_FAILED; 110 GECODE_ME_CHECK(x1.lq(home,1)); 111 return SqrPlus<FloatView,FloatView>::post(home,x0,x1); 112 } else { 113 if (x0.min() >= 0) 114 return SqrPlus<FloatView,FloatView>::post(home,x0,x1); 115 if (x0.max() <= 0) 116 return SqrPlus<MinusView,FloatView>::post(home,MinusView(x0),x1); 117 GECODE_ME_CHECK(x1.eq(home,sqr(x0.val()))); 118 (void) new (home) Sqr<View>(home,x0,x1); 119 } 120 return ES_OK; 121 } 122 123 template<class View> 124 forceinline 125 Sqr<View>::Sqr(Space& home, Sqr<View>& p) 126 : BinaryPropagator<View,PC_FLOAT_BND>(home,p) {} 127 128 template<class View> 129 Actor* 130 Sqr<View>::copy(Space& home) { 131 return new (home) Sqr<View>(home,*this); 132 } 133 134 template<class View> 135 ExecStatus 136 Sqr<View>::propagate(Space& home, const ModEventDelta&) { 137 assert(x1.min() >= 0); 138 if (x0.min() >= 0) 139 GECODE_REWRITE(*this,(SqrPlus<FloatView,FloatView>::post(home(*this),x0,x1))); 140 if (x0.max() <= 0) 141 GECODE_REWRITE(*this,(SqrPlus<MinusView,FloatView>::post(home(*this), 142 MinusView(x0),x1))); 143 144 GECODE_ME_CHECK(x1.eq(home,sqr(x0.val()))); 145 Rounding r; 146 FloatVal z = sqrt(x1.val()); 147 if (x0.min() > -r.sqrt_up(x1.min())) 148 GECODE_ME_CHECK(x0.eq(home,z)); 149 else if (x0.max() < r.sqrt_down(x1.min())) 150 GECODE_ME_CHECK(x0.eq(home,-z)); 151 else 152 GECODE_ME_CHECK(x0.eq(home,hull(z,-z))); 153 154 return ES_NOFIX; 155 } 156 157 158 /* 159 * Bounds consistent square root operator 160 * 161 */ 162 163 template<class A, class B> 164 forceinline 165 Sqrt<A,B>::Sqrt(Home home, A x0, B x1) 166 : MixBinaryPropagator<A,PC_FLOAT_BND,B,PC_FLOAT_BND>(home,x0,x1) {} 167 168 template<class A, class B> 169 ExecStatus 170 Sqrt<A,B>::post(Home home, A x0, B x1) { 171 GECODE_ME_CHECK(x0.gq(home,0)); 172 if (x0 == x1) { 173 if (x0.assigned()) 174 return ((x0.val() == 0) || (x0.val() == 1))? ES_OK : ES_FAILED; 175 GECODE_ME_CHECK(x0.lq(home,1)); 176 (void) new (home) Sqrt<A,B>(home,x0,x1); 177 } else { 178 GECODE_ME_CHECK(x1.eq(home,sqrt(x0.val()))); 179 (void) new (home) Sqrt<A,B>(home,x0,x1); 180 } 181 return ES_OK; 182 } 183 184 template<class A, class B> 185 forceinline 186 Sqrt<A,B>::Sqrt(Space& home, Sqrt<A,B>& p) 187 : MixBinaryPropagator<A,PC_FLOAT_BND,B,PC_FLOAT_BND>(home,p) {} 188 189 template<class A, class B> 190 Actor* 191 Sqrt<A,B>::copy(Space& home) { 192 return new (home) Sqrt<A,B>(home,*this); 193 } 194 195 template<class A, class B> 196 ExecStatus 197 Sqrt<A,B>::propagate(Space& home, const ModEventDelta&) { 198 if (x0 == x1) { 199 if (x0.max() < 1) GECODE_ME_CHECK(x0.eq(home,0)); 200 else if (x0.min() > 0) GECODE_ME_CHECK(x0.eq(home,1)); 201 if (x0.assigned()) 202 return ((x0.val() == 0) || (x0.val() == 1))? home.ES_SUBSUMED(*this) : ES_FAILED; 203 } else { 204 GECODE_ME_CHECK(x0.eq(home,sqr(x1.val()))); 205 GECODE_ME_CHECK(x1.eq(home,sqrt(x0.val()))); 206 if (x0.assigned() || x1.assigned()) return home.ES_SUBSUMED(*this); 207 } 208 209 return ES_FIX; 210 } 211 212}}} 213 214// STATISTICS: float-prop 215