Loading...
Loading...
You are given a list of integers shapeA and a list of integers valuesA that describe tensor A, and a list of integers shapeB and a list of integers valuesB that describe tensor B. shapeA gives the size of A along each axis and valuesA lists the entries of A in row major order, so the last axis varies fastest. shapeB and valuesB describe B the same way.
The two tensors are summed with broadcasting: Prepend 1s to the shorter shape until both shapes have the same length. On each axis, if the two sizes are equal, the result keeps that size. If one of the two sizes is 1, the tensor with size 1 reuses its single entry along that axis. If neither condition holds, the shapes are incompatible. The result shape takes the larger of the two sizes on every aligned axis, and each result entry is the sum of the matching entry of A and the matching entry of B.
Return the entries of the summed result in row major order. If the shapes are incompatible, return an empty list.
You can assume valuesA.length equals the product of the sizes in shapeA, and valuesB.length equals the product of the sizes in shapeB.
shapeA.length ≤5shapeB.length ≤5shapeA[i] ≤5 for every valid index ishapeB[i] ≤5 for every valid index ivaluesA.length equals the product of the sizes in shapeA.valuesB.length equals the product of the sizes in shapeB.valuesA.length ≤3125valuesB.length ≤3125valuesA[i] ≤1000 for every valid index ivaluesB[i] ≤1000 for every valid index iB has size 1 on the only axis, so its single entry is reused at both positions. The result shape is [2].
A is [[1,2,3],[4,5,6]]. B has shape [3], which aligns with the last axis of A, and its three entries are reused in both rows. The result shape is [2, 3].
The shorter shape [2] becomes [1, 2], so B's two entries line up with the last axis and are reused across the first axis. The result shape is [2, 2].
The shapes are incompatible.