Ch3.1.1 Part 4: Matrix Multiplication

Standard Matrix-Vector Multiplication

  • An \( m \times n \) matrix \( A \) can multiply an \( n \times 1 \) vector \( \mathbf{x} \).
  • The inner dimension \( n \) must agree.
  • The result is an \( m \times 1 \) vector \( \mathbf{b} \) (the outer dimensions).

\[ \begin{aligned} A &= \begin{bmatrix} a_{11} & a_{12} & a_{13} \\ a_{21} & a_{22} & a_{23} \end{bmatrix}, ~ \mathbf{x} = \begin{bmatrix} x_1 \\ x_2 \\ x_3 \end{bmatrix}, ~ \mathbf{b} = \begin{bmatrix} b_1 \\ b_2 \end{bmatrix} \\ \\ A \mathbf{x} & = \begin{bmatrix} a_{11} & a_{12} & a_{13} \\ a_{21} & a_{22} & a_{23} \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \\ x_3 \end{bmatrix} = \begin{bmatrix} b_1 \\ b_2 \end{bmatrix} = \mathbf{b} \end{aligned} \]

Example 1: Standard Product

  • We compute this vector \( \mathbf{b} \) one entry at a time.
  • To find the \( i \)-th entry \( b_i \) of \( \mathbf{b} \), take the inner product of the \( i \)-th row of \( A \) with \( \mathbf{b} \); see weblink.

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

Example 2: Standard Product

  • Compute the matrix-vector product:

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

 A <- matrix(c(1,3,4,0,-2,1),2,3)
 x <- c(5,1,-1)
 A %*% x

Example 2: Standard Product

\[ \begin{bmatrix} 1 & 4 & -2 \\ 3 & 0 & 1 \end{bmatrix} \begin{bmatrix} 5 \\ 1 \\ -1 \end{bmatrix} = \begin{bmatrix} (1)(5) + (4)(1) + (-2)(-1) \\ (3)(5) + (0)(1) + (1)(-1) \end{bmatrix} = \begin{bmatrix} 11 \\ 14 \end{bmatrix} \]

 A <- matrix(c(1,3,4,0,-2,1),2,3)
 x <- c(5,1,-1)
 A %*% x
      [,1]
 [1,]   11
 [2,]   14

Example 3: Standard Product

  • Compute the matrix-vector product:

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

 A <- matrix(c(1,-2,0,4,3,1),3,2)
 x <- c(5,2)
 A %*% x

Example 3: Standard Product

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

 A <- matrix(c(1,-2,0,4,3,1),3,2)
 x <- c(5,2)
 A %*% x
      [,1]
 [1,]   13
 [2,]   -4
 [3,]    2

Example 1: Pointwise Product

  • Pointwise matrix-vector products can be computed.
  • The columns of matrix are multiplied with vector.
  • Recycling occurs if lengths are different.

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

 A <- matrix(c(2,0,-1,1,3,4),2,3)
 x <- c(1,3,5)
 A * x
      [,1] [,2] [,3]
 [1,]    2   -5    9
 [2,]    0    1   20

Example 2: Pointwise Product

  • Compute the pointwise matrix-vector product:

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

 A <- matrix(c(2,0,-1,1,3,4),2,3)
 x <- c(1,3)
 A * x
      [,1] [,2] [,3]
 [1,]    2   -1    3
 [2,]    0    3   12

Example 3: Pointwise Product

  • Compute the pointwise matrix-vector product:

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

  • R result:

    A <- matrix(c(1,1,1,1,1,1),2,3)
    x <- c(2,3,4,5)
    A * x
    
      [,1] [,2] [,3]
    [1,]    2    4    2
    [2,]    3    5    3
    

Standard Matrix-Matrix Multiplication

  • An \( m \times n \) matrix \( A \) can multiply an \( n \times r \) matrix \( B \).
  • The inner dimension \( n \) must agree.
  • The result is an \( m \times r \) matrix \( C \) (the outer dimensions).
  • We write \( AB = C \).
  • Typically, \( AB \neq BA \), with different dimensions.

\[ A= \begin{bmatrix} a_{11} & a_{12} & \cdots & a_{1n} \\ a_{21} & a_{22} & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1} & a_{m2} & \cdots & a_{mn} \\ \end{bmatrix}, ~~ B= \begin{bmatrix} b_{11} & b_{12} & \cdots & b_{1r} \\ b_{21} & b_{22} & \cdots & b_{2r} \\ \vdots & \vdots & \ddots & \vdots \\ b_{n1} & b_{m2} & \cdots & b_{nr} \\ \end{bmatrix} \]

Example 1: Standard Matrix-Matrix Product

  • We compute this matrix \( C \) one entry at a time.
  • To find the \( c_{ij} \) entry of \( C \), take the inner product of the \( i \)-th row of \( A \) with the \( j \)-th row of \( B \); see weblink.

\[ \begin{aligned} AB &= \begin{bmatrix} 2 & -1 & 3 \\ 0 & 1 & 4 \end{bmatrix} \begin{bmatrix} 1 & 5 \\ 3 & 8 \\ 2 & 0 \end{bmatrix} \\ &= \begin{bmatrix} (2)(1) + (-1)(3) + (3)(2) & (2)(5) + (-1)(8) + (3)(0) \\ (0)(1) + (1)(3) + (4)(2) & (0)(5) + (1)(8) + (4)(0) \end{bmatrix} \\ & = \begin{bmatrix} 5 & 2 \\ 11 & 8 \end{bmatrix} \end{aligned} \]

Example 1: Standard Matrix-Matrix Product

  • Compute the matrix-matrix product \( AB \):

\[ AB = \begin{bmatrix} 2 & -1 & 3 \\ 0 & 1 & 4 \end{bmatrix} \begin{bmatrix} 1 & 5 \\ 3 & 8 \\ 2 & 0 \end{bmatrix} = ? \]

 A <- matrix(c(2,0,-1,1,3,4),2,3)
 B <- matrix(c(1,3,2,5,8,0),3,2)
 A %*% B = ?

 Your answer here!

Example 1: Standard Matrix-Matrix Product

  • Compute the matrix-matrix product \( AB \):

\[ AB = \begin{bmatrix} 2 & -1 & 3 \\ 0 & 1 & 4 \end{bmatrix} \begin{bmatrix} 1 & 5 \\ 3 & 8 \\ 2 & 0 \end{bmatrix} = \begin{bmatrix} 5 & 2 \\ 11 & 8 \end{bmatrix} \]

 A <- matrix(c(2,0,-1,1,3,4),2,3)
 B <- matrix(c(1,3,2,5,8,0),3,2)
 A %*% B
      [,1] [,2]
 [1,]    5    2
 [2,]   11    8

Example 2: Standard Product

  • Compute the matrix-matrix product \( BA \):

\[ \begin{aligned} BA &= \begin{bmatrix} 1 & 5 \\ 3 & 8 \\ 2 & 0 \end{bmatrix} \begin{bmatrix} 2 & -1 & 3 \\ 0 & 1 & 4 \end{bmatrix} = ~~? \end{aligned} \]

 B <- matrix(c(1,3,2,5,8,0),3,2)
 A <- matrix(c(2,0,-1,1,3,4),2,3)
 B %*% A = ?

Your answer here!

Example 2: Standard Product

\[ \begin{aligned} BA &= \begin{bmatrix} 1 & 5 \\ 3 & 8 \\ 2 & 0 \end{bmatrix} \begin{bmatrix} 2 & -1 & 3 \\ 0 & 1 & 4 \end{bmatrix} \\ &= \begin{bmatrix} (1)(2) + (5)(0) & (1)(-1) + (5)(1) & (1)(3) + (5)(4) \\ (3)(2) + (8)(0) & (3)(-1) + (8)(1) & (3)(3) + (8)(4) \\ (2)(2) + (0)(0) & (2)(-1) + (0)(1) & (2)(3) + (0)(4) \end{bmatrix} \\ & = \begin{bmatrix} 2 & 4 & 23 \\ 6 & 5 & 41 \\ 4 & -2 & 6 \end{bmatrix} \end{aligned} \]

Example 2: Standard Product

\[ BA = \begin{bmatrix} 1 & 5 \\ 3 & 8 \\ 2 & 0 \end{bmatrix} \begin{bmatrix} 2 & -1 & 3 \\ 0 & 1 & 4 \end{bmatrix} = \begin{bmatrix} 2 & 4 & 23 \\ 6 & 5 & 41 \\ 4 & -2 & 6 \end{bmatrix} \]

 B <- matrix(c(1,3,2,5,8,0),3,2)
 A <- matrix(c(2,0,-1,1,3,4),2,3)
 B %*% A
      [,1] [,2] [,3]
 [1,]    2    4   23
 [2,]    6    5   41
 [3,]    4   -2    6

Example 2: AB not equal to BA

  • Info for \( AB \)
(C <- A %*% B)
     [,1] [,2]
[1,]    5    2
[2,]   11    8
dim(C)
[1] 2 2
  • Info for \( BA \)
(D <- B %*% A)
     [,1] [,2] [,3]
[1,]    2    4   23
[2,]    6    5   41
[3,]    4   -2    6
dim(D)
[1] 3 3

Matrix Product: MATLAB & Python

  • MATLAB is easier…

title

title

Pointwise Matrix-Matrix Multiplication

  • A \( m \times n \) matrix \( A \) can pointwise multiply \( m \times n \) matrix \( B \).
  • Note that both dimensions must agree.
  • Here it will always be true that \( A.*B = B.*A \).

\[ A= \begin{bmatrix} a_{11} & a_{12} & \cdots & a_{1n} \\ a_{21} & a_{22} & \cdots & a_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ a_{m1} & a_{m2} & \cdots & a_{mn} \\ \end{bmatrix}, ~~ B= \begin{bmatrix} b_{11} & b_{12} & \cdots & b_{1n} \\ b_{21} & b_{22} & \cdots & b_{2n} \\ \vdots & \vdots & \ddots & \vdots \\ b_{m1} & b_{m2} & \cdots & b_{mn} \\ \end{bmatrix} \]

Pointwise Matrix Product

  • Here are A and B:
     [,1] [,2] [,3]
[1,]    2   -1    3
[2,]    3    2    4
     [,1] [,2] [,3]
[1,]    1    2    8
[2,]    4    5    0
  • R results:
A * B
     [,1] [,2] [,3]
[1,]    2   -2   24
[2,]   12   10    0
B * A
     [,1] [,2] [,3]
[1,]    2   -2   24
[2,]   12   10    0

Pointwise Product: No Recycling

  • Here are A and B:
     [,1] [,2] [,3]
[1,]    2   -1    3
[2,]    3    1    4
     [,1] [,2]
[1,]    1    5
[2,]    4    8
[3,]    2    0
  • R results:
dim(A)
[1] 2 3
dim(B)
[1] 3 2
A * B
Error in A * B : non-conformable arrays

Pointwise Product: MATLAB & Python

  • MATLAB & Python results.
  • MATLAB is easier.

title

title