# Dot Product and Orthogonality

Computer Science I · Algebra · https://tryals.app/learn/computer-science-i/dot-product-and-orthogonality

## Multiplying Vectors

The **dot product** takes two vectors and returns a single number (a scalar). For $u = (u_1, u_2)$ and $v = (v_1, v_2)$:

$u \cdot v = u_1 v_1 + u_2 v_2$

Multiply matching components and add.

Its geometric meaning is $u \cdot v = \|u\|\|v\|\cos\theta$, where $\theta$ is the angle between them. It measures how aligned two vectors are:

| Alignment | Dot Product | Angle $\theta$ |
| --- | --- | --- |
| **Same direction** | Positive | $\theta < 90^\circ$ |
| **Perpendicular** | Zero | $\theta = 90^\circ$ |
| **Opposite** | Negative | $\theta > 90^\circ$ |

## Orthogonality and Angles

Two nonzero vectors are **orthogonal** (perpendicular) exactly when $u \cdot v = 0$. No angle computation is needed.

The dot product recovers length via $u \cdot u = \|u\|^2$. You can find the **angle** between vectors using:

$\cos\theta = \frac{u \cdot v}{\|u\|\|v\|}$

In machine learning, **cosine similarity** uses this exact formula to measure how similar two feature vectors are for search.

> **Pitfall:** The dot product returns a _number_, never a vector. Orthogonal always means a dot product of zero.

## Practice questions

3 of this lesson's 10 practice questions, with answers. The full set is in the app.

### 1. A recommender compares users by the cosine of the angle between their rating vectors rather than Euclidean distance. What problem does that solve?

A. Cosine similarity handles negative ratings and distance does not
B. Cosine similarity is faster to compute at scale
C. Generous and harsh raters get vectors pointing the same way
D. Euclidean distance is undefined when ratings are missing

**Answer:** C. Generous and harsh raters get vectors pointing the same way

**Why:** One user's vector is essentially a scaled version of the other's — same direction, greater magnitude — so the angle says 'identical taste' and the distance says 'far apart'. Magnitude here encodes generosity rather than preference, and cosine is the decision to ignore it.

Page: https://tryals.app/practice/computer-science-i/dot-product-and-orthogonality/a-recommender-compares-users-by-the-cosine-of-the-angle-between-their

### 2. The dot product returns a single number rather than a vector of products. Why is that the useful output?

A. Because a vector output would not be defined in dimensions above three
B. Because the scalar is easier to compute than the componentwise product
C. Because vectors of the same length cannot be multiplied componentwise
D. Because it measures how much two vectors share a direction

**Answer:** D. Because it measures how much two vectors share a direction

**Why:** Alignment is one number, so the operation that measures it returns one. The componentwise product is a legitimate operation; it simply answers a different question.

Page: https://tryals.app/practice/computer-science-i/dot-product-and-orthogonality/the-dot-product-returns-a-single-number-rather-than-a-vector-of

### 3. Comparing documents by Euclidean distance between their word-count vectors makes the comparison independent of document length.

**Answer:** False

**Why:** False. Doubling every count doubles the vector's magnitude, so Euclidean distance grows even though the document is about the same thing. Cosine similarity looks only at the angle and is unaffected, which is why it is the standard for text, comparing what a document is about rather than how much of it there is.

Page: https://tryals.app/practice/computer-science-i/dot-product-and-orthogonality/comparing-documents-by-euclidean-distance-between-their-word-count
