Open the context
Choose the project folder and name the files or behavior involved.
Infer a shared tensor shape by comparing trailing dimensions and expanding unit axes. Check scalar shapes and zero-sized axes with unit dimensions, rejecting incompatible, negative or fractional dimensions.
Choose the project folder and name the files or behavior involved.
Describe the input, expected output and boundaries that must stay intact.
Review the proposed edits and any commands before relying on their result.
Compare actual behavior with the expected result. If a check fails, investigate it before applying more changes.
from itertools import zip_longest
def broadcast_shape(a, b):
a, b = tuple(a), tuple(b)
if any(type(d) is not int or d < 0 for d in a + b):
raise ValueError("dimension.invalid")
result = []
for left, right in zip_longest(reversed(a), reversed(b), fillvalue=1):
if left == right or right == 1:
result.append(left)
elif left == 1:
result.append(right)
else:
raise ValueError("shape.incompatible")
return tuple(reversed(result))The example is a starting point for a real project, not a claim that generated code is already tested. Adjust paths, dependencies and expected behavior to match your application.