Skip to section
Foundationsfor rotation-free search
Section 4 of 528% of course
Contents
Chapter 1 Section 1.4 52 min

Part I · Mathematical foundations

Multiply matching positions

Align two lists and distinguish coordinatewise multiplication from a final sum.

Two lists share a position system

Let a = [2, −1, 3, 1] and b = [3, 0, 1, −1]. If both lists use the same position meanings (like if both lists represent 4 hours of measurements), then position i in list a naturally pairs up with position i in list b.

Matching-position multiplication
position i0123left value aᵢ2-131right value bᵢ301-1product aᵢbᵢ603-1

If we multiply those matching positions together, we get another list: [6, 0, 3, −1]. It's exactly the same length as our inputs.

[a0, a1, …] ⊙ [b0, b1, …] = [a0b0, a1b1, …]

You might sometimes see the symbol ⊙ used for this. The symbol isn't what matters here—it's the alignment rule: match up the same index with the same index.

Worked example

Keep products aligned

For a = [−2, 4, 5] and b = [6, 3, −1], the coordinate products are:

[−2×6, 4×3, 5×(−1)] = [−12, 12, −5]

Make sure not to pair the −2 with the 3 or the −1; those values live at different indices.

Length agreement matters

If one list has four entries and another has only three, "multiply matching positions" leaves us stuck: what matches the extra position? Mathematics usually demands either equal lengths or an explicit padding rule.

In software terms, equal length is a strict precondition for this operation. A robust function will check this precondition up front, or use a data type that guarantees it. If your code quietly stops at the shorter input, it might return an answer, but it's not the math operation we just defined.

Check your understanding

What is the coordinatewise product of [1, 2, 3] and [4, −1, 2]?

Code and notation say the same thing

def coordinate_product(a, b):
    if len(a) != len(b):
        raise ValueError("length mismatch")
    product = [0] * len(a)
    for i in range(len(a)):
        product[i] = a[i] * b[i]
    return product
pi = aibi   for 0 ≤ i < D

Predict the output before calculating

We can know the shape of the operation's output without knowing any of the actual values. Two compatible length-D inputs will always produce one length-D output. Building this habit—predicting types and shapes before doing the arithmetic—is a great way to catch mistakes early. A single number simply can't be a coordinatewise product, because it would have lost those D separate positions.

Worked example

Zeros reveal independence

Let a=[5,6,7] and b=[0,1,0]. Their coordinatewise product is [0,6,0]. The output at position 1 depends only on the inputs at position 1. Changing a0 won't affect that output at all. Each coordinate is its own independent multiplication.

Check your understanding

A coordinatewise product consumes two length-64 lists. What can you know before reading their values?

Section summary

  • Matching coordinates share the exact same index.
  • Coordinatewise multiplication gives us one product per index.
  • The result remains a list until another operation (like a sum) reduces it.

Repository layer · second pass

Why must coordinatewise multiplication preserve alignment?

Coordinatewise multiplication creates a new list. At each position i, it multiplies the two values already aligned at i. There is no cross-position mixing and no final reduction. This makes it different from both a Cartesian product, which considers every pair, and a dot product, which adds the matching products.

Alignment is a semantic contract, not merely a length check. Two length-768 arrays can be multiplied mechanically while representing incompatible coordinate systems. Later, matching embedding model and preprocessing versions will be as important as matching array length.

Reasoning chain

  1. 1

    Verify equal lengths.

  2. 2

    Verify that equal positions have compatible meaning.

  3. 3

    For each i, compute aᵢbᵢ.

  4. 4

    Store each product back at position i.

  5. 5

    Do not add unless a later operation explicitly requests reduction.

Worked trace

Keep the intermediate list visible

  1. Align A = [2, −1, 3] with B = [4, 5, −2].
  2. Position 0 contributes 2×4 = 8.
  3. Position 1 contributes −1×5 = −5.
  4. Position 2 contributes 3×−2 = −6.

Result. A ⊙ B = [8, −5, −6], still a length-three list.

Executable lens · Python

Make the hidden state visible

a = [2, -1, 3]
b = [4, 5, -2]
products = [left * right for left, right in zip(a, b, strict=True)]
assert products == [8, -5, -6]

Retype this example, predict each intermediate value, and then change one input that touches a boundary.

Misconception clinic

Tempting mistakes

  • Calling [8, −5, −6] the dot product; the dot product is its sum.
  • Using zip without checking equal lengths in code that could silently truncate.

Retrieval and transfer

Close the book first

  1. Compute [0, 2, −3, 4] ⊙ [9, −1, 2, 0].
  2. Give a case where equal-length lists should not be aligned semantically.
  3. Write a loop that rejects unequal lengths before multiplying.