1# Copyright 2018 The TensorFlow Authors. All Rights Reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14# ============================================================================== 15"""Logical boolean operators: not, and, or.""" 16 17from tensorflow.python.framework import tensor_util 18from tensorflow.python.ops import control_flow_ops 19from tensorflow.python.ops import gen_math_ops 20 21 22def not_(a): 23 """Functional form of "not".""" 24 if tensor_util.is_tf_type(a): 25 return _tf_not(a) 26 return _py_not(a) 27 28 29def _tf_not(a): 30 """Implementation of the "not_" operator for TensorFlow.""" 31 return gen_math_ops.logical_not(a) 32 33 34def _py_not(a): 35 """Default Python implementation of the "not_" operator.""" 36 return not a 37 38 39def and_(a, b): 40 """Functional form of "and". Uses lazy evaluation semantics.""" 41 a_val = a() 42 if tensor_util.is_tf_type(a_val): 43 return _tf_lazy_and(a_val, b) 44 return _py_lazy_and(a_val, b) 45 46 47def _tf_lazy_and(cond, b): 48 """Lazy-eval equivalent of "and" for Tensors.""" 49 # TODO(mdan): Enforce cond is scalar here? 50 return control_flow_ops.cond(cond, b, lambda: cond) 51 52 53def _py_lazy_and(cond, b): 54 """Lazy-eval equivalent of "and" in Python.""" 55 return cond and b() 56 57 58def or_(a, b): 59 """Functional form of "or". Uses lazy evaluation semantics.""" 60 a_val = a() 61 if tensor_util.is_tf_type(a_val): 62 return _tf_lazy_or(a_val, b) 63 return _py_lazy_or(a_val, b) 64 65 66def _tf_lazy_or(cond, b): 67 """Lazy-eval equivalent of "or" for Tensors.""" 68 # TODO(mdan): Enforce cond is scalar here? 69 return control_flow_ops.cond(cond, lambda: cond, b) 70 71 72def _py_lazy_or(cond, b): 73 """Lazy-eval equivalent of "or" in Python.""" 74 return cond or b() 75 76 77def eq(a, b): 78 """Functional form of "equal".""" 79 if tensor_util.is_tf_type(a) or tensor_util.is_tf_type(b): 80 return _tf_equal(a, b) 81 return _py_equal(a, b) 82 83 84def _tf_equal(a, b): 85 """Overload of "equal" for Tensors.""" 86 return gen_math_ops.equal(a, b) 87 88 89def _py_equal(a, b): 90 """Overload of "equal" that falls back to Python's default implementation.""" 91 return a == b 92 93 94def not_eq(a, b): 95 """Functional form of "not-equal".""" 96 return not_(eq(a, b)) 97