Exercise 2: Why (2,3)+(3,) Broadcasts But (2,3)+(2,) Doesn't — Possible Solution ==================================================================== THE COMPATIBILITY RULE, PER THIS CHAPTER ------------------------------ Per this chapter, "comparing shapes from the trailing dimension backward, each pair of dimensions must either match exactly or one of them must be 1." WORKING THROUGH (2, 3) AND (3,) ------------------------------ Align the two shapes from the trailing (rightmost) dimension backward: (2, 3) (3,) The trailing dimension of the first shape is 3; the trailing (and only) dimension of the second shape is also 3 — these match exactly. The first shape has a second dimension (2) that the second shape simply doesn't have at all; per NumPy's own broadcasting rule, a missing dimension on the shorter shape is treated as if it were 1, which satisfies the rule's own "or one of them must be 1" clause. Both positions check out, so the shapes are compatible, and per this chapter's own example, "matrix + row" broadcasts row across each row of matrix successfully. WORKING THROUGH (2, 3) AND (2,) ------------------------------ Align these two shapes the same way, from the trailing dimension backward: (2, 3) (2,) The trailing dimension of the first shape is 3; the trailing (and only) dimension of the second shape is 2. Checking the rule: do they match exactly? No, 3 ≠ 2. Is either one equal to 1? No — neither 3 nor 2 is 1. Since neither condition in the rule is satisfied at this position, the two shapes fail the compatibility check, and broadcasting cannot proceed without first reshaping the (2,) array into a shape where its dimensions actually satisfy the rule (for example, reshaping it to (2, 1) would make it broadcastable against (2, 3), since a trailing 1 always satisfies the "or one of them must be 1" clause). WHY THE DIRECTION OF COMPARISON MATTERS ------------------------------ The rule works specifically from the trailing dimension backward, not from the front. A (2,) array's single dimension gets compared against (2, 3)'s own trailing dimension (3), not its leading dimension (2) — which is exactly why (2,) fails against (2, 3) even though the number 2 appears in both shapes; the rule never actually compares those two matching 2s directly, because they occupy different positions once alignment starts from the right-hand end. WHY THIS WORKS AS AN ANSWER ------------------------------ It applies the chapter's own trailing-dimension-backward alignment rule step by step to both example pairs, showing exactly which dimensions get compared to which, and explains why (2, 3)+(3,) satisfies the rule while (2, 3)+(2,) does not, including why the superficial visual match between the two 2s in the second example is a coincidence the rule never actually credits.