Skip to content
Easy Framework

Element-wise Operations

Apply the same polynomial independently to every pair of corresponding values in two tensors.

Your task

Implement elementwise_poly(a, b) using the expanded expression:

a**2 + 2*a*b + b**2

The floating-point inputs have the same shape, dtype and device. Return that shape, dtype and device, leaving both inputs unchanged. No reduction over an axis or matrix multiplication is needed.

For a=[1, -2] and b=[3, 1], return [16, 1]:

  • First position: 1**2 + 2*1*3 + 3**2 = 16.
  • Second position: (-2)**2 + 2*(-2)*1 + 1**2 = 1.

The same rule applies to matrices and higher-dimensional tensors. Each output uses only a and b at that position. In both frameworks, * multiplies values at matching positions, while **2 squares each value. Python's ^ is not a power operator.

Algebraically the polynomial is (a + b)**2, but this exercise asks for the expanded form to practice elementwise addition, multiplication and powers. Floating-point evaluation can give slightly different rounding for the two forms.