/usr/lib/python2.7/dist-packages/ufl/conditional.py is in python-ufl 2017.2.0.0-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 | # -*- coding: utf-8 -*-
"""This module defines classes for conditional expressions."""
# Copyright (C) 2008-2016 Martin Sandve Alnæs
#
# This file is part of UFL.
#
# UFL is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# UFL is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with UFL. If not, see <http://www.gnu.org/licenses/>.
from ufl.log import warning, error
from ufl.utils.py23 import as_native_strings
from ufl.core.expr import ufl_err_str
from ufl.core.ufl_type import ufl_type
from ufl.core.operator import Operator
from ufl.constantvalue import as_ufl
from ufl.precedence import parstr
from ufl.exprequals import expr_equals
from ufl.checks import is_true_ufl_scalar
# --- Condition classes ---
# TODO: Would be nice with some kind of type system to show that this
# is a boolean type not a float type
@ufl_type(is_abstract=True, is_scalar=True)
class Condition(Operator):
__slots__ = ()
def __init__(self, operands):
Operator.__init__(self, operands)
def __bool__(self):
# Showing explicit error here to protect against misuse
error("UFL conditions cannot be evaluated as bool in a Python context.")
__nonzero__ = __bool__
@ufl_type(is_abstract=True, num_ops=2)
class BinaryCondition(Condition):
__slots__ = as_native_strings(('_name',))
def __init__(self, name, left, right):
left = as_ufl(left)
right = as_ufl(right)
Condition.__init__(self, (left, right))
self._name = name
if name in ('!=', '=='):
# Since equals and not-equals are used for comparing
# representations, we have to allow any shape here. The
# scalar properties must be checked when used in
# conditional instead!
pass
elif name in ('&&', '||'):
# Binary operators acting on boolean expressions allow
# only conditions
for arg in (left, right):
if not isinstance(arg, Condition):
error("Expecting a Condition, not %s." % ufl_err_str(arg))
else:
# Binary operators acting on non-boolean expressions allow
# only scalars
if left.ufl_shape != () or right.ufl_shape != ():
error("Expecting scalar arguments.")
if left.ufl_free_indices != () or right.ufl_free_indices != ():
error("Expecting scalar arguments.")
def __str__(self):
return "%s %s %s" % (parstr(self.ufl_operands[0], self),
self._name, parstr(self.ufl_operands[1], self))
# Not associating with __eq__, the concept of equality with == is
# reserved for object equivalence for use in set and dict.
@ufl_type()
class EQ(BinaryCondition):
__slots__ = ()
def __init__(self, left, right):
BinaryCondition.__init__(self, "==", left, right)
def evaluate(self, x, mapping, component, index_values):
a = self.ufl_operands[0].evaluate(x, mapping, component, index_values)
b = self.ufl_operands[1].evaluate(x, mapping, component, index_values)
return bool(a == b)
def __bool__(self):
return expr_equals(self.ufl_operands[0], self.ufl_operands[1])
__nonzero__ = __bool__
# Not associating with __ne__, the concept of equality with == is
# reserved for object equivalence for use in set and dict.
@ufl_type()
class NE(BinaryCondition):
__slots__ = ()
def __init__(self, left, right):
BinaryCondition.__init__(self, "!=", left, right)
def evaluate(self, x, mapping, component, index_values):
a = self.ufl_operands[0].evaluate(x, mapping, component, index_values)
b = self.ufl_operands[1].evaluate(x, mapping, component, index_values)
return bool(a != b)
def __bool__(self):
return not expr_equals(self.ufl_operands[0], self.ufl_operands[1])
__nonzero__ = __bool__
@ufl_type(binop="__le__")
class LE(BinaryCondition):
__slots__ = ()
def __init__(self, left, right):
BinaryCondition.__init__(self, "<=", left, right)
def evaluate(self, x, mapping, component, index_values):
a = self.ufl_operands[0].evaluate(x, mapping, component, index_values)
b = self.ufl_operands[1].evaluate(x, mapping, component, index_values)
return bool(a <= b)
@ufl_type(binop="__ge__")
class GE(BinaryCondition):
__slots__ = ()
def __init__(self, left, right):
BinaryCondition.__init__(self, ">=", left, right)
def evaluate(self, x, mapping, component, index_values):
a = self.ufl_operands[0].evaluate(x, mapping, component, index_values)
b = self.ufl_operands[1].evaluate(x, mapping, component, index_values)
return bool(a >= b)
@ufl_type(binop="__lt__")
class LT(BinaryCondition):
__slots__ = ()
def __init__(self, left, right):
BinaryCondition.__init__(self, "<", left, right)
def evaluate(self, x, mapping, component, index_values):
a = self.ufl_operands[0].evaluate(x, mapping, component, index_values)
b = self.ufl_operands[1].evaluate(x, mapping, component, index_values)
return bool(a < b)
@ufl_type(binop="__gt__")
class GT(BinaryCondition):
__slots__ = ()
def __init__(self, left, right):
BinaryCondition.__init__(self, ">", left, right)
def evaluate(self, x, mapping, component, index_values):
a = self.ufl_operands[0].evaluate(x, mapping, component, index_values)
b = self.ufl_operands[1].evaluate(x, mapping, component, index_values)
return bool(a > b)
@ufl_type()
class AndCondition(BinaryCondition):
__slots__ = ()
def __init__(self, left, right):
BinaryCondition.__init__(self, "&&", left, right)
def evaluate(self, x, mapping, component, index_values):
a = self.ufl_operands[0].evaluate(x, mapping, component, index_values)
b = self.ufl_operands[1].evaluate(x, mapping, component, index_values)
return bool(a and b)
@ufl_type()
class OrCondition(BinaryCondition):
__slots__ = ()
def __init__(self, left, right):
BinaryCondition.__init__(self, "||", left, right)
def evaluate(self, x, mapping, component, index_values):
a = self.ufl_operands[0].evaluate(x, mapping, component, index_values)
b = self.ufl_operands[1].evaluate(x, mapping, component, index_values)
return bool(a or b)
@ufl_type(num_ops=1)
class NotCondition(Condition):
__slots__ = ()
def __init__(self, condition):
Condition.__init__(self, (condition,))
if not isinstance(condition, Condition):
error("Expecting a condition.")
def evaluate(self, x, mapping, component, index_values):
a = self.ufl_operands[0].evaluate(x, mapping, component, index_values)
return bool(not a)
def __str__(self):
return "!(%s)" % (str(self.ufl_operands[0]),)
# --- Conditional expression (condition ? true_value : false_value) ---
@ufl_type(num_ops=3, inherit_shape_from_operand=1,
inherit_indices_from_operand=1)
class Conditional(Operator):
__slots__ = ()
def __init__(self, condition, true_value, false_value):
if not isinstance(condition, Condition):
error("Expectiong condition as first argument.")
true_value = as_ufl(true_value)
false_value = as_ufl(false_value)
tsh = true_value.ufl_shape
fsh = false_value.ufl_shape
if tsh != fsh:
error("Shape mismatch between conditional branches.")
tfi = true_value.ufl_free_indices
ffi = false_value.ufl_free_indices
if tfi != ffi:
error("Free index mismatch between conditional branches.")
if isinstance(condition, (EQ, NE)):
if not all((condition.ufl_operands[0].ufl_shape == (),
condition.ufl_operands[0].ufl_free_indices == (),
condition.ufl_operands[1].ufl_shape == (),
condition.ufl_operands[1].ufl_free_indices == ())):
error("Non-scalar == or != is not allowed.")
Operator.__init__(self, (condition, true_value, false_value))
def evaluate(self, x, mapping, component, index_values):
c = self.ufl_operands[0].evaluate(x, mapping, component, index_values)
if c:
a = self.ufl_operands[1]
else:
a = self.ufl_operands[2]
return a.evaluate(x, mapping, component, index_values)
def __str__(self):
return "%s ? %s : %s" % tuple(parstr(o, self) for o in self.ufl_operands)
# --- Specific functions higher level than a conditional ---
@ufl_type(is_scalar=True, num_ops=1)
class MinValue(Operator):
"UFL operator: Take the minimum of two values."
__slots__ = ()
def __init__(self, left, right):
Operator.__init__(self, (left, right))
if not (is_true_ufl_scalar(left) and is_true_ufl_scalar(right)):
error("Expecting scalar arguments.")
def evaluate(self, x, mapping, component, index_values):
a, b = self.ufl_operands
a = a.evaluate(x, mapping, component, index_values)
b = b.evaluate(x, mapping, component, index_values)
try:
res = min(a, b)
except ValueError:
warning('Value error in evaluation of min() of %s and %s.' % self.ufl_operands)
raise
return res
def __str__(self):
return "min_value(%s, %s)" % self.ufl_operands
@ufl_type(is_scalar=True, num_ops=1)
class MaxValue(Operator):
"UFL operator: Take the maximum of two values."
__slots__ = ()
def __init__(self, left, right):
Operator.__init__(self, (left, right))
if not (is_true_ufl_scalar(left) and is_true_ufl_scalar(right)):
error("Expecting scalar arguments.")
def evaluate(self, x, mapping, component, index_values):
a, b = self.ufl_operands
a = a.evaluate(x, mapping, component, index_values)
b = b.evaluate(x, mapping, component, index_values)
try:
res = max(a, b)
except ValueError:
warning('Value error in evaluation of max() of %s and %s.' % self.ufl_operands)
raise
return res
def __str__(self):
return "max_value(%s, %s)" % self.ufl_operands
|