Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/tensorflow/python/autograph/operators/logical.py: 42%
40 statements
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
« prev ^ index » next coverage.py v7.4.0, created at 2024-01-03 07:57 +0000
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."""
17from tensorflow.python.framework import tensor_util
18from tensorflow.python.ops import cond as tf_cond
19from tensorflow.python.ops import gen_math_ops
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)
29def _tf_not(a):
30 """Implementation of the "not_" operator for TensorFlow."""
31 return gen_math_ops.logical_not(a)
34def _py_not(a):
35 """Default Python implementation of the "not_" operator."""
36 return not a
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)
47def _tf_lazy_and(cond, b):
48 """Lazy-eval equivalent of "and" for Tensors."""
49 # TODO(mdan): Enforce cond is scalar here?
50 return tf_cond.cond(cond, b, lambda: cond)
53def _py_lazy_and(cond, b):
54 """Lazy-eval equivalent of "and" in Python."""
55 return cond and b()
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)
66def _tf_lazy_or(cond, b):
67 """Lazy-eval equivalent of "or" for Tensors."""
68 # TODO(mdan): Enforce cond is scalar here?
69 return tf_cond.cond(cond, lambda: cond, b)
72def _py_lazy_or(cond, b):
73 """Lazy-eval equivalent of "or" in Python."""
74 return cond or b()
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)
84def _tf_equal(a, b):
85 """Overload of "equal" for Tensors."""
86 return gen_math_ops.equal(a, b)
89def _py_equal(a, b):
90 """Overload of "equal" that falls back to Python's default implementation."""
91 return a == b
94def not_eq(a, b):
95 """Functional form of "not-equal"."""
96 return not_(eq(a, b))