Gradient
Lesson 3.2MathCode~50 min

The dot product

One number that says how much two arrows agree. The core operation of AI.

Module 3 · 0/7 done

Multiply, then add

The dot product takes two vectors of the same length and gives back a single number: multiply the matching entries, then add everything up.

a⋅b=∑i=1nai bi=a1b1+a2b2+⋯+anbn\blue{\mathbf{a}} \cdot \amber{\mathbf{b}} = \sum_{i=1}^{n} \blue{a_i}\,\amber{b_i} = \blue{a_1}\amber{b_1} + \blue{a_2}\amber{b_2} + \dots + \blue{a_n}\amber{b_n}
a213b0.52.21.1a×b12.23.3Σ →6.5
Fig 3.4Pair up, multiply, add.

You use this all the time without the name. If you buy 2 apples at 0.50, 1 loaf at 2.20 and 3 milks at 1.10, your bill is the dot product of quantities and prices.

Try itA shopping bill
// The dot product as a shopping bill: quantities · prices
const quantities = [2, 1, 3]        // apples, bread, milk
const prices     = [0.5, 2.2, 1.1]

let bill = 0
for (let i = 0; i < quantities.length; i++) {
  bill += quantities[i] * prices[i]
}
console.log("total:", bill)
console.log("same thing with the built-in:", vec.dot(quantities, prices))
Press Run (⌘/Ctrl + Enter) to see output here.
Work it out
What is [1,2,3]⋅[4,5,6][1, 2, 3] \cdot [4, 5, 6]?

Complete the challenge above to continue.