# Dot product and its representation as sum.

If I define the dot product $u\cdot v$ where $u,v\in \mathbb{R}^n$ as $u\cdot v= |u||v|\cos \theta$ where $\theta$ is the angle between $u$ and $v$. How can I get that $$u\cdot v= u_1v_1+u_2v_2+u_3v_3$$ with $u=u_1e_1+u_2e_2+\cdots+u_ne_n$ and $v=v_1e_1+v_2e_2+\cdots+v_ne_n$?

$e_1... e_n$ is the canonical base.

If we think in $\mathbb{R}^2$ we can suppose that, $u$ have an angle of $\alpha$ with the X axis, $v$ have an angle $\beta$ with the $X$ axis, and $\alpha\geq\beta$. Then $\theta=\alpha-\beta$. We get

$$\cos(\alpha-\beta)=\cos\alpha\cos\beta+\sin\alpha\sin\beta$$

Using the relation $cos\alpha= \displaystyle\frac{u_1}{|u|}, \sin \alpha=\displaystyle\frac{u_2}{|u|}$ and analogous relation for $v$

$$\cos\theta= \displaystyle\frac{u_1}{|u|}\displaystyle\frac{v_1}{|v|}+\displaystyle\frac{u_2}{|u|}\displaystyle\frac{v_2}{|v|}$$

multiplying by |u||v| we get the result.

Can we generalize this idea to $\mathbb{R}^n$?

Thanks!

The dot product defined as $$u\cdot v = \|u\|\cdot\|v\|\cos\theta$$ is just the (signed) length of the projection of $u$ on $v$ (or viceversa). To find such projection, it is sufficient to compute the $\lambda\in\mathbb{R}$ such that $\|u-\lambda v\|$ is minimal. Notice that: $$\|u-\lambda v\|^2 = \sum_{i=1}^{n}(u_i-\lambda v_i)^2=p(\lambda)$$ is a quadratic polynomial in $\lambda$ and: $$p(\lambda) = \|v\|^2\,\lambda^2 - 2\left(\sum_{i=1}^n u_i v_i\right)\lambda + \|u\|^2,$$ hence the minimum for $p(\lambda)$ is attained when: $$\lambda = \frac{\sum_{i=1}^n u_i v_i}{\|v\|^2}$$ so the (signed) length of the projection of $u$ on $v$ is just: $$\sum_{i=1}^{n} u_i v_i$$ as wanted.
Letting $\vec u = (u_1, u_2, \dots, u_n)$ and $\vec v = (v_1, v_2, \dots, v_n)$,
then $\displaystyle |u|^2 = \sum_{i=1}^n u_i^2$, $\displaystyle |v|^2 = \sum_{i=1}^n v_i^2$, and $\displaystyle |u-v|^2 = \sum_{i=1}^n (u_i - v_i)^2$.
By the law of cosines, $\cos \theta = \dfrac{|u|^2 + |v|^2 - |u-v|^2}{2\,|u|\,|v|}$. So \begin{align} u \circ v &= |u|\,|v|\,\cos \theta \\ &= \dfrac 12(|u|^2 + |v|^2 - |u-v|^2) \\ &= \dfrac 12\left( \sum_{i=1}^n u_i^2 + \sum_{i=1}^n v_i^2 - \sum_{i=1}^n (u_i^2 + v_i^2 - 2u_iv_i)\right) \\ &= \sum_{i=1}^n u_iv_i \end{align}