Skip to content

Commit 3f960de

Browse files
authored
Add aliases for arithmetic inequality function equivalents in NumPy (#533)
* feat(numpy logical aliases): add aliases for logical function equivalents in NumPy (#462) * fix(logical): bitwise and logical are distinct operators * doc(aliases): Document the new NumPy inequality aliases in the same style as existing bitwise ones. * refactor(numpy-aliases): collect all NumPy aliases in one block at the end of the file
1 parent 89fe939 commit 3f960de

File tree

2 files changed

+45
-15
lines changed

2 files changed

+45
-15
lines changed

doc/library/tensor/basic.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,6 +1292,30 @@ The six usual equality and inequality operators share the same interface.
12921292

12931293
Returns a variable representing the result of logical inequality (a!=b).
12941294

1295+
.. function:: greater(a, b)
1296+
1297+
Alias for `gt`. greater is the NumPy name.
1298+
1299+
.. function:: greater_equal(a, b)
1300+
1301+
Alias for `ge`. greater_equal is the NumPy name.
1302+
1303+
.. function:: less(a, b)
1304+
1305+
Alias for `lt`. less is the NumPy name.
1306+
1307+
.. function:: less_equal(a, b)
1308+
1309+
Alias for `le`. less_equal is the NumPy name.
1310+
1311+
.. function:: equal(a, b)
1312+
1313+
Alias for `eq`. equal is the NumPy name.
1314+
1315+
.. function:: not_equal(a, b)
1316+
1317+
Alias for `neq`. not_equal is the NumPy name.
1318+
12951319
.. function:: isnan(a)
12961320

12971321
Returns a variable representing the comparison of ``a`` elements with nan.

pytensor/tensor/math.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,32 +1013,21 @@ def and_(a, b):
10131013
"""bitwise a & b"""
10141014

10151015

1016-
bitwise_and = and_ # numpy name for it
1017-
1018-
10191016
@scalar_elemwise
10201017
def or_(a, b):
10211018
"""bitwise a | b"""
10221019

10231020

1024-
bitwise_or = or_ # numpy name for it
1025-
1026-
10271021
@scalar_elemwise
10281022
def xor(a, b):
10291023
"""bitwise a ^ b"""
10301024

10311025

1032-
bitwise_xor = xor # numpy name for it
1033-
1034-
10351026
@scalar_elemwise
10361027
def invert(a):
10371028
"""bitwise ~a"""
10381029

10391030

1040-
bitwise_not = invert # numpy alias for it
1041-
10421031
##########################
10431032
# Math
10441033
##########################
@@ -1167,10 +1156,6 @@ def sqr(a):
11671156
"""square of a"""
11681157

11691158

1170-
# alias to sqr, included to maintain similarity with numpy interface
1171-
square = sqr
1172-
1173-
11741159
def cov(m, y=None, rowvar=True, bias=False, ddof=None, fweights=None, aweights=None):
11751160
"""Calculate the covariance matrix.
11761161
@@ -2956,6 +2941,21 @@ def vectorize_node_to_matmul(op, node, batched_x, batched_y):
29562941
return vectorize_node_fallback(op, node, batched_x, batched_y)
29572942

29582943

2944+
# NumPy logical aliases
2945+
square = sqr
2946+
2947+
bitwise_and = and_
2948+
bitwise_or = or_
2949+
bitwise_xor = xor
2950+
bitwise_not = invert
2951+
2952+
greater = gt
2953+
greater_equal = ge
2954+
less = lt
2955+
less_equal = le
2956+
equal = eq
2957+
not_equal = neq
2958+
29592959
__all__ = [
29602960
"max_and_argmax",
29612961
"max",
@@ -2966,11 +2966,17 @@ def vectorize_node_to_matmul(op, node, batched_x, batched_y):
29662966
"smallest",
29672967
"largest",
29682968
"lt",
2969+
"less",
29692970
"gt",
2971+
"greater",
29702972
"le",
2973+
"less_equal",
29712974
"ge",
2975+
"greater_equal",
29722976
"eq",
2977+
"equal",
29732978
"neq",
2979+
"not_equal",
29742980
"isnan",
29752981
"isinf",
29762982
"allclose",

0 commit comments

Comments
 (0)