Ch3.1.1 Part 3: Vector Multiplication

Vector Multiplication: Pointwise

  • Pointwise multiplication (element-by-element) is one way to multiply vectors.
  • “Like elements” are multiplied, obtaining a vector result.

\[ \mathbf{u}. * \mathbf{v} = \begin{bmatrix} 3 \\ 0 \\ 2 \end{bmatrix} . * \begin{bmatrix} 4 \\ 4 \\ 4 \end{bmatrix} =\begin{bmatrix} 12 \\ 0 \\ 8 \end{bmatrix} \]

  • The [.*] notation above is borrowed from MATLAB.
  • R uses [*] notation; however, writing it this way in a math class is not consistent with existing mathematical conventions.

Definition of Pointwise Vector Product

  • Let \( \mathbf{u} \) and \( \mathbf{v} \) be two vectors of length \( n \):

\[ \begin{aligned} \mathbf{u} & = \begin{bmatrix} u_1, & u_2, & \ldots, & u_n \end{bmatrix} \\ \mathbf{v} & = \begin{bmatrix} v_1, & v_2, & \ldots, & v_n \end{bmatrix} \end{aligned} \]

  • Then \( \mathbf{u}.* \mathbf{v} \) is a vector-valued output defined as:

\[ \mathbf{u}. * \mathbf{v} = \begin{bmatrix} u_1 \\ u_2 \\ \vdots \\ u_n \end{bmatrix} . * \begin{bmatrix} v_1 \\ v_2 \\ \vdots \\ v_n \end{bmatrix} =\begin{bmatrix} u_1 v_1 \\ u_2 v_2 \\ \vdots \\ u_n v_n \end{bmatrix} \]

Example 1: Pointwise Multiplication

  • Math result:

\[ \begin{bmatrix} 1 \\ -2 \\ 3 \end{bmatrix} . * \begin{bmatrix} 4 \\ 5 \\ 6 \end{bmatrix} = ~~ ? \]

  • R result:
u <- c(1,-2,3); v <- c(4,5,6)
u * v = ?

Your answer here!

Example 1: Pointwise Multiplication

  • Math result:

\[ \begin{bmatrix} 1 \\ -2 \\ 3 \end{bmatrix} . * \begin{bmatrix} 4 \\ 5 \\ 6 \end{bmatrix} =\begin{bmatrix} 4 \\ -10 \\ 18 \end{bmatrix} \]

  • R result:
u <- c(1,-2,3); v <- c(4,5,6)
u * v
[1]   4 -10  18

Example 2: Pointwise Multiplication

  • Math result:

\[ \begin{bmatrix} 1 \\ -2 \\ 3 \end{bmatrix} . * \begin{bmatrix} 1 \\ -2 \\ 3 \end{bmatrix} = ~~ ? \]

  • R result:
u <- c(1,-2,3)
u * u = ?

Your answer here!

Example 2: Pointwise Multiplication

  • Math result:

\[ \begin{bmatrix} 1 \\ -2 \\ 3 \end{bmatrix} . * \begin{bmatrix} 1 \\ -2 \\ 3 \end{bmatrix} =\begin{bmatrix} 1 \\ 4 \\ 9 \end{bmatrix} \]

  • R result:
u <- c(1,-2,3)
u * u
[1] 1 4 9
u^2
[1] 1 4 9

Example 3: Pointwise Multiplication

  • Math result:

\[ \begin{bmatrix} 2 \\ 3 \\ 4 \end{bmatrix} . * \begin{bmatrix} 5 \\ 6 \end{bmatrix} = ~~ \mathrm{Error} \]

  • R result:
  • Shorter vector recycles
u <- c(2,3,4)
v <- c(5,6)
u * v
[1] 10 18 20

Vector Multiplication: Dot Product

  • The dot product (inner product) is another way of multiplying two vectors.
  • This is pointwise multiplication together with addition.
  • The output of the dot product is a scalar value.
  • Useful for systems of linear equations.

\[ \matrix{ x_1 & + & 2x_2 & = & 5 \cr 3x_1 & - & 4 x_2 & = & 6 } ~~ \Leftrightarrow \begin{bmatrix} 1 & 2 \\ 3 & -4 \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \end{bmatrix} = \begin{bmatrix} 5 \\ 6 \end{bmatrix} \]

Definition of Vector Inner (Dot) Product

  • The inner (dot) product is a scalar-valued product.
  • Let \( \mathbf{u} \) and \( \mathbf{v} \) be two vectors of length \( n \):

\[ \begin{aligned} \mathbf{u} & = \begin{bmatrix} u_1, & u_2, & \ldots, & u_n \end{bmatrix} \\ \mathbf{v} & = \begin{bmatrix} v_1, & v_2, & \ldots, & v_n \end{bmatrix} \end{aligned} \]

  • Then the inner product (dot product) of \( \mathbf{u} \) and \( \mathbf{v} \) is defined as:

\[ \mathbf{u} \cdot \mathbf{v} = \sum_{i = 1}^n u_i v_i \]

Example: Vector Inner Product

  • Let \( \mathbf{u} \) and \( \mathbf{v} \) be the vectors

\[ \begin{aligned} \mathbf{u} & = \begin{bmatrix} 2, & 0, & 3 \end{bmatrix} \\ \mathbf{v} & = \begin{bmatrix} -1, & 1, & 4 \end{bmatrix} \end{aligned} \]

  • Then

\[ \mathbf{u} \cdot \mathbf{v} = (2)(-1) + (0)(1) + (3)(4) = 10 \]

Example 1: Inner Product

  • Math result:

\[ \begin{bmatrix} 1 \\ -2 \\ 3 \end{bmatrix} \cdot \begin{bmatrix} 4 \\ 5 \\ 6 \end{bmatrix} = ~~ ? \]

  • R result:
u <- c(1,-2,3); v <- c(4,5,6)
u %*% v = ?

Your answer here!

Example 1: Inner Product

  • Math result:

\[ \begin{bmatrix} 1 \\ -2 \\ 3 \end{bmatrix} \cdot \begin{bmatrix} 4 \\ 5 \\ 6 \end{bmatrix} = 12 \]

  • R result:
u <- c(1,-2,3); v <- c(4,5,6)
u %*% v
     [,1]
[1,]   12

Example 2: Inner Product

  • Math result:

\[ \begin{bmatrix} 1 \\ -2 \\ 3 \end{bmatrix} \cdot \begin{bmatrix} 1 \\ -2 \\ 3 \end{bmatrix} = ~~ ? \]

  • R result:
u <- c(1,-2,3)
u * u = ?

Your answer here!

Example 2: Inner Product

  • Math result:

\[ \begin{bmatrix} 1 \\ -2 \\ 3 \end{bmatrix} \cdot \begin{bmatrix} 1 \\ -2 \\ 3 \end{bmatrix} = 14 \]

u <- c(1,-2,3)
u %*% u
     [,1]
[1,]   14
u^2
[1] 1 4 9
sum(u^2)
[1] 14

Example 3: No Recycling (Inner Product)

  • Math result:

\[ \begin{bmatrix} 2 \\ 3 \\ 4 \end{bmatrix} . * \begin{bmatrix} 5 \\ 6 \end{bmatrix} = ~~ \mathrm{Error} \]

  • R result:
  • No recycling
u <- c(2,3,4)
v <- c(5,6)
u %*% v
Error in u %*% v : non-conformable arguments

Example1 : Orthogonal Vectors

  • If \( \mathbf{u} \cdot \mathbf{v} = 0 \), then the vectors are said to be orthogonal.
  • Consider \( \mathbf{u} = [2,-1] \) and \( \mathbf{v}=[3,6] \).
  • Then

\[ \mathbf{u} \cdot \mathbf{v} = \begin{bmatrix} 2 \\ -1 \end{bmatrix} \cdot \begin{bmatrix} 3 \\ 6 \end{bmatrix} = 0 \]

  • Thus \( \mathbf{u} \) and \( \mathbf{v} \) are orthogonal.
  • Can also write \( \mathbf{u} \perp \mathbf{v} \).
  • Weblink

Example 2: Orthogonal Vectors

  • Show orthogonal: \( \mathbf{u} = [2,1,1] \), \( \mathbf{v}=[-3,4,2] \).

\[ \begin{bmatrix} 2 \\ 1 \\ 1 \end{bmatrix} \cdot \begin{bmatrix} -3 \\ 4 \\ 2 \end{bmatrix} = (2)(-3) + (1)(4) + (1)(2) =0 \]

  • Since inner product is zero, \( \mathbf{u} \perp \mathbf{v} \).
u <- c(2,1,1); w <- c(-3,4,2)
u %*% w
     [,1]
[1,]    0