Introduction Matrix Multiplication in R
Matrix multiplication is a fundamental operation in linear algebra, and R provides efficient functions. The matrix multiplication in R can be done easily. For this purpose, the %*% operator is used for general matrix multiplication. An $n\times 1$ or $1 \times n$ vector (also called matrix) may be used as an $ n$ vector. In other words, vectors that occur in matrix multiplication expressions are automatically promoted to row (or column) vectors, whichever is multiplicatively coherent, if possible.
Scalar Multiplication
The * operator may be used for multiplying a matrix by a scalar quantity. The scalar value is multiplied by each element of the matrix.
m <- matrix(1:9, nrow = 3) m <- 2 * m m
From the above output, it can be seen that each element of the original matrix is multiplied by 2.
Element-wise Multiplication
If $A$ and $B$ are two square matrices of the same size, then the element-wise multiplication between matrices $A$ and $B$ can be performed using the * operator. In element-wise multiplication of the matrices, the corresponding elements of both matrices will be multiplied (provided that the matrices have the same dimension).
A <- matrix(1:9, nrow = 3) A ## Ouput [,1] [,2] [,3] [1,] 1 4 7 [2,] 2 5 8 [3,] 3 6 9 B <- matrix(10:18, nrow = 3) B ## Output [,1] [,2] [,3] [1,] 10 13 16 [2,] 11 14 17 [3,] 12 15 18 A * B ## Output [,1] [,2] [,3] [1,] 10 52 112 [2,] 22 70 136 [3,] 36 90 162
Matrix Multiplication in R
The matrix multiplication in R can be done easily. The general multiplication of matrices (matrix product) can be performed using the %*% operator. The matrix multiplication must satisfy the condition that the number of columns in the first matrix is equal to the number of rows in the second matrix. Suppose, if matrix $A$ has $m$ rows and $n$ columns and matrix $B$ has $n$ rows and $x$ columns, then the multiplication of these matrices will result in with dimension of $n times x$. Consider the following example of matrix multiplication in R language.
A <- matrix(1:9, nrow = 3) B <- matrix(10:18, nrow = 3) A %*% B
Note the difference in output between A*B
and A%*%B
.
Suppose, $x$ is a vector, then the quadratic form of the matrices is
x <- c(5, 6, 7) A <- matrix(1:9, nrow = 3) x %% A %% x ## Output [,1] [1,] 1764
Splitting the above multiplication procedure, one can easily understand how the matrices and vectors are multiplied.
x%*%A ## Output [,1] [,2] [,3] [1,] 38 92 146 x%*%A%*%x ## Output [,1] [1,] 1764
The crossprod() in R
The function crossprod()
forms “crossproducts” meaning that crossprod(X, y)
is the same as t(X) %*% y
. The crossprod()
operation is more efficient than the t(X) %*%y
.
crossprod(x, A) [,1] [,2] [,3] [1,] 38 92 146
The cross product of $x$, $A$, the` (crossprod(x, A)
) is equivalent to x%*%A
, and crossprod(x%*%A, x)
is equivalent to x%*%A%*%x
.
Multiplication of Large Matrices
For larger matrices, the Matrix
package may be used for optimized performance. The Matrix
package also helps for working with sparse matrices or matrices with special structures.
Some Important Points about Matrices
- Be careful about matrix dimensions to avoid errors.
- Be careful about the use of operators * and %*%.
- Be careful about the order of the matrices during multiplication (
A%*%B
, orB%*%A
). - Explore other matrix operations like addition, subtraction, and transposition using R functions.
- The
dim()
function helps identify the dimensions of a matrix. - For larger matrices, consider using the
solve()
function for matrix inversion or theeigen()
function for eigenvalue decomposition.