Matrix Multiplication Animation

Choosing some matrices to multiply

set.seed (12345)
print (x <- matrix (sample (0:3, 24, replace = TRUE), 4, 6), cw = 10)
##      [,1] [,2] [,3] [,4] [,5] [,6]
## [1,]    2    1    2    2    1    1
## [2,]    3    0    3    0    1    1
## [3,]    3    1    0    1    0    3
## [4,]    3    2    0    1    3    2
print (y <- matrix (sample (0:3, 18, replace = TRUE), 6, 3), cw = 10)
##      [,1] [,2] [,3]
## [1,]    2    3    3
## [2,]    1    0    3
## [3,]    2    0    2
## [4,]    2    2    0
## [5,]    0    1    3
## [6,]    1    1    1
x %*% y
##      [,1] [,2] [,3]
## [1,]   14   12   17
## [2,]   13   11   19
## [3,]   12   14   15
## [4,]   12   16   26

A function to draw matrices in a plot

matrixDraw <- function (x, cw = 6, lc = c(0,0), id = c(1, 1, 1)) {
n <- nrow (x)
m <- ncol (x)
for (j in (1 : m)) {
  for (i in (n : 1)) {
        ni <- (n - i) + 1
        ccol <- "WHITE"
        if ((j == id [2]) && (ni == id [3]) && (id [3] != 0)) ccol <- "RED"
        if ((j == id [2]) && (ni != id [3]) && (id [3] != 0)) ccol <- "GREEN"
        if ((j != id [3]) && (ni == id [1]) && (id [3] != 0)) ccol <- "GREEN"
        if ((j == id [3]) && (ni == id [1]) && (id [3] != 0)) ccol <- "RED"
        if ((j == id [2]) && (ni == id [1]) && (id [3] == 0)) ccol <- "LIGHTBLUE"
        rect (lc[1] + (j - 1) * cw, lc[2] + (i - 1) * cw, lc[1] + j * cw, lc[2] + i * cw, col = ccol)
        text (lc[1] + (j - 1) * cw + cw / 2, lc[2] + (i - 1) * cw + cw / 2, as.character (x[ni, j]))
        }
    }
}

Example of drawing our matrices

plot (0 : 100, type = "n", axes = FALSE, xlab = "", ylab = "", asp = 1)
matrixDraw (x, cw = 10, lc = c(20,20), id = c(0,0,0))

plot (0 : 100, type = "n", axes = FALSE, xlab = "", ylab = "", asp = 1)
matrixDraw (y, cw = 10, lc = c(20,20), id = c(0,0,0))

A function to animate matrix multiplication —— The formula we are implementing is \[ z_{ij}=\sum_{k=1}^K x_{ik}y_{kj}. \]

matrixMult <- function (x, y, cw = 6) {
    rx <- nrow (x)
    cx <- ncol (x)
    ry <- nrow (y)
    cy <- ncol (y)
    for (i in (1 : rx)) {
        for (j in (1 : cy)) {
            for (k in (1 : cx)) {
                plot (0 : 100, type = "n", axes = FALSE, xlab = "", ylab = "", asp = 1)
                matrixDraw (x, cw = cw, lc = c (cw, 100 - (rx + 1) * cw), id = c (i, 0, k))
                matrixDraw (y, cw = cw, lc = c ((cx + 2) * cw, 100 - (ry + 1) * cw), id = c (0, j, k))
                z <- x %*% y
                z[i, j] <- sum (x[i, 1 : k] * y [1 : k, j])
                z[(1 : rx) > i, ] <- " "
                z[i, (1 : cy) > j] <- " "
                text ((cx + cy + 3) * cw, 100 - (rx - 1) * cw, "=")
                matrixDraw (z, cw = cw, lc = c ((cx + cy + 4) * cw, 100 - (rx + 1) * cw), id = c(i, j, 0))
                Sys.sleep (1)
            }
        }
    }
}

Animating multiplication of our matrices

par (mar = rep(3, 4), bg = "white")
matrixMult (x, y, cw = 6)

Alternative formulas

\[ z_{i\bullet}=\sum_{k=1}^K x_{ik}y_{k\bullet}. \] \[ z_{\bullet j}=\sum_{k=1}^K x_{\bullet k}y_{kj}. \] \[ Z=\sum_{k=1}^K x_{\bullet k}y_{k\bullet} \] Each of these formulas has its own animation.