Skip to content

Commit 0018263

Browse files
Optimise the _ensure_not_equal function
Co-authored-by: Ricardo Vieira <[email protected]>
1 parent 6518da2 commit 0018263

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

pytensor/tensor/einsum.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -421,10 +421,12 @@ def _ensure_not_equal(elements):
421421
"""
422422
Ensures that any pair in a list of elements are not the same object. If a pair of elements is found to be equal, then one of them is converted to a copy.
423423
"""
424-
for i in range(len(elements)):
425-
for j in range(i + 1, len(elements)):
426-
if elements[i] == elements[j]:
427-
elements[j] = elements[j].copy()
424+
elements = list(elements)
425+
n_elem = len(elements)
426+
for i, elem1 in enumerate(elements[:-1]):
427+
for j, elem2 in enumerate(elements[i + 1:], start=i+1):
428+
if elem1 is elem2:
429+
elements[j] = elem1.copy()
428430
return elements
429431

430432

0 commit comments

Comments
 (0)