From decc255e4953ddb2b462a9697b25bb1ce6f57612 Mon Sep 17 00:00:00 2001 From: Louis Maddox Date: Wed, 6 Dec 2023 19:16:06 +0000 Subject: [PATCH 1/4] feat(numpy logical aliases): add aliases for logical function equivalents in NumPy (#462) --- pytensor/tensor/math.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/pytensor/tensor/math.py b/pytensor/tensor/math.py index 0f035272af..b187f04662 100644 --- a/pytensor/tensor/math.py +++ b/pytensor/tensor/math.py @@ -2937,6 +2937,18 @@ def matmul(x1: "ArrayLike", x2: "ArrayLike", dtype: Optional["DTypeLike"] = None return out +# NumPy logical aliases +greater = gt +greater_equal = ge +less = lt +less_equal = le +equal = eq +not_equal = neq +logical_and = bitwise_and +logical_or = bitwise_or +logical_not = bitwise_not +logical_xor = bitwise_xor + __all__ = [ "max_and_argmax", "max", @@ -2947,23 +2959,33 @@ def matmul(x1: "ArrayLike", x2: "ArrayLike", dtype: Optional["DTypeLike"] = None "smallest", "largest", "lt", + "less", "gt", + "greater", "le", + "less_equal", "ge", + "greater_equal", "eq", + "equal", "neq", + "not_equal", "isnan", "isinf", "allclose", "isclose", "and_", "bitwise_and", + "logical_and", "or_", "bitwise_or", + "logical_or", "xor", "bitwise_xor", + "logical_xor", "invert", "bitwise_not", + "logical_not", "abs", "exp", "exp2", From 9edd5d9e1fa3e165f4f405751c8f766d09cdb633 Mon Sep 17 00:00:00 2001 From: Louis Maddox Date: Wed, 6 Dec 2023 19:30:54 +0000 Subject: [PATCH 2/4] fix(logical): bitwise and logical are distinct operators --- pytensor/tensor/math.py | 8 -------- 1 file changed, 8 deletions(-) diff --git a/pytensor/tensor/math.py b/pytensor/tensor/math.py index b187f04662..dbe3c822d5 100644 --- a/pytensor/tensor/math.py +++ b/pytensor/tensor/math.py @@ -2944,10 +2944,6 @@ def matmul(x1: "ArrayLike", x2: "ArrayLike", dtype: Optional["DTypeLike"] = None less_equal = le equal = eq not_equal = neq -logical_and = bitwise_and -logical_or = bitwise_or -logical_not = bitwise_not -logical_xor = bitwise_xor __all__ = [ "max_and_argmax", @@ -2976,16 +2972,12 @@ def matmul(x1: "ArrayLike", x2: "ArrayLike", dtype: Optional["DTypeLike"] = None "isclose", "and_", "bitwise_and", - "logical_and", "or_", "bitwise_or", - "logical_or", "xor", "bitwise_xor", - "logical_xor", "invert", "bitwise_not", - "logical_not", "abs", "exp", "exp2", From 1430e0d734df5deb3c7a20711d9b1b52d64bd7fa Mon Sep 17 00:00:00 2001 From: Louis Maddox Date: Wed, 6 Dec 2023 19:47:19 +0000 Subject: [PATCH 3/4] doc(aliases): Document the new NumPy inequality aliases in the same style as existing bitwise ones. --- doc/library/tensor/basic.rst | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/doc/library/tensor/basic.rst b/doc/library/tensor/basic.rst index 911583da92..b6f4122aaf 100644 --- a/doc/library/tensor/basic.rst +++ b/doc/library/tensor/basic.rst @@ -1292,6 +1292,30 @@ The six usual equality and inequality operators share the same interface. Returns a variable representing the result of logical inequality (a!=b). +.. function:: greater(a, b) + + Alias for `gt`. greater is the NumPy name. + +.. function:: greater_equal(a, b) + + Alias for `ge`. greater_equal is the NumPy name. + +.. function:: less(a, b) + + Alias for `lt`. less is the NumPy name. + +.. function:: less_equal(a, b) + + Alias for `le`. less_equal is the NumPy name. + +.. function:: equal(a, b) + + Alias for `eq`. equal is the NumPy name. + +.. function:: not_equal(a, b) + + Alias for `neq`. not_equal is the NumPy name. + .. function:: isnan(a) Returns a variable representing the comparison of ``a`` elements with nan. From 37da12f0600406682a4df47effb203cfdda1f1da Mon Sep 17 00:00:00 2001 From: Louis Maddox Date: Thu, 7 Dec 2023 18:02:27 +0000 Subject: [PATCH 4/4] refactor(numpy-aliases): collect all NumPy aliases in one block at the end of the file --- pytensor/tensor/math.py | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/pytensor/tensor/math.py b/pytensor/tensor/math.py index dbe3c822d5..7e6877c571 100644 --- a/pytensor/tensor/math.py +++ b/pytensor/tensor/math.py @@ -1012,32 +1012,21 @@ def and_(a, b): """bitwise a & b""" -bitwise_and = and_ # numpy name for it - - @scalar_elemwise def or_(a, b): """bitwise a | b""" -bitwise_or = or_ # numpy name for it - - @scalar_elemwise def xor(a, b): """bitwise a ^ b""" -bitwise_xor = xor # numpy name for it - - @scalar_elemwise def invert(a): """bitwise ~a""" -bitwise_not = invert # numpy alias for it - ########################## # Math ########################## @@ -1166,10 +1155,6 @@ def sqr(a): """square of a""" -# alias to sqr, included to maintain similarity with numpy interface -square = sqr - - def cov(m, y=None, rowvar=True, bias=False, ddof=None, fweights=None, aweights=None): """Calculate the covariance matrix. @@ -2938,6 +2923,13 @@ def matmul(x1: "ArrayLike", x2: "ArrayLike", dtype: Optional["DTypeLike"] = None # NumPy logical aliases +square = sqr + +bitwise_and = and_ +bitwise_or = or_ +bitwise_xor = xor +bitwise_not = invert + greater = gt greater_equal = ge less = lt