## Transpose

The transpose of a matrix means turning rows (columns) into columns (rows).

For instance, if the matrix is a vector,

Generalizing, if A is of order **m** x **n** its transpose, A’ (or A⊤ ) is a matrix of order **n** x **m**.

To obtain the transpose of a matrix, we use the transpose operator **t()**

For instance:

1 |
> B |

1 2 3 4 |
[,1] [,2] [,3] [1,] 1 2 3 [2,] 0 4 5 [3,] 0 0 2 |

1 |
> t(B) |

1 2 3 4 |
[,1] [,2] [,3] [1,] 1 0 0 [2,] 2 4 0 [3,] 3 5 2 |

*Tip: Transposition is reflexive: *(A’)’ = A

## Trace

The trace of a

squarematrix is defined as the sum of the diagonal elements.

*For a matrix A of order m x m:*

For example, for

1 2 3 4 |
[,1] [,2] [,3] [1,] 1 2 3 [2,] 0 4 5 [3,] 0 0 2 |

the trace is given by:

1 |
tr(A) = a[1,1]+a[2,2]+ a[3,3] = 1 + 4 + 2 = 7 |

In R, we use a combination of the operators **sum()** and **diag()**.

1 2 3 |
tr<-sum(diag(A)) [1] 7 |

Note that the command will work even on rectangular matrices. For a m × n matrix, where n < m, the command will sum the diagonal elements of the first n rows in the matrix.

## Addition and Subtraction

Any two matrices (or vectors) can be added or subtracted if they are of the same order **m** x **n**. In this case, we say that the matrices are **conformable** for addition.

Because of the matrices are of the same order, the resulting matrix is also of the order **m** x **n**.

The elements of the new matrix are given by the sum (+) or difference (-) of the corresponding elements in the original matrices:

1 |
C = A+B has elements c[i,j] = a[i,j] + b[i,j] for all i,j. |

1 |
C = A−B has elements c[i,j] = a[i,j] - b[i,j] for all i,j. |

Let’s start with an example with vectors.

Let **v1** be a **column vector** (*this is the vector “by default”, unless otherwise stated*) with values: 1,3,2, and **v2** another column vector with values: 2,8,9

1 2 |
v1 <- c(1,3,2) [1] 1 3 2 |

1 2 |
v2 <- c(2,8,9) [1] 2 8 9 |

1 2 3 |
v <- a+b [1] 3 11 11 |

A formal representation of a sum of matrices could be this:

In R, to add two conformable matrices, A and B, we do:

1 |
C<-A+B |

For instance, let’s create the following matrices:

1 2 3 4 5 6 |
A<-matrix(c(1,3,1,0,1,2),nrow=3,ncol=2,byrow=T) [,1] [,2] [1,] 1 3 [2,] 1 0 [3,] 1 2 |

1 2 3 4 5 6 |
B<-matrix(c(0,0,7,5,2,1),nrow=3,ncol=2,byrow=T) [,1] [,2] [1,] 0 0 [2,] 7 5 [3,] 2 1 |

And now, if we sum both matrices,

We will get;

1 2 3 4 5 6 |
A+B [,1] [,2] [1,] 1 3 [2,] 8 5 [3,] 3 3 |

To subtract B from A:

1 |
C<-A-B |

1 2 3 4 5 |
A-B [,1] [,2] [1,] 1 3 [2,] -6 -5 [3,] -1 1 |

#### Matrix addition and subtraction have several important properties.

- Matrix addition complies with the commutative law: A+B = B+A.
- Matrix subtraction does not comply with the commutative law: A − B <> B − A.
- Matrix addition complies with the associative law: (A + B) + C = A + (B + C) = A + B + C.
- The transpose of a sum of matrices is equal to the sum of the transposes: (A + B)’ = A’ + B’.
- The transpose of a difference between matrices is equal to the difference between the transposes: (A − B)’ = A’ − B’.
- The trace of a matrix sum is equal to the sum of the traces: tr(A + B) = tr(A) + tr(B). Similarly, tr(A − B) = tr(A) − tr(B).

## Multiplication

R supports all of the major forms of matrix multiplication.

### Scalar Multiplication

For a number k and a matrix A, the product kA is the matrix obtained by multiplying each element in A by k.

Thus, the result of this operation is a new matrix of the same order as the original matrix.

Formally:

Let k denote a scalar and let B=kA, where A is of order **m** x **n**. Then

- B is of order
**m**x**n**. - b[i,j] =k x a[i,j] for all i,j.

For example,

A

1 2 3 4 |
[,1] [,2] [1,] 1 3 [2,] 1 0 [3,] 1 2 |

If k=5, we should have the following result:

1 2 3 4 5 6 |
B<-k*A [,1] [,2] [1,] 5 15 [2,] 5 0 [3,] 5 10 |

#### Scalar multiplication has several important properties:

k(A + B) = kA + kB

(kA)’ = kA’

### Inner Products

**Inner** or **dot products** involve the multiplication of two vectors.

Let **u** a *row vector* of order * 1 x n*, and

**v**a

*column vector*of order

*hen the inner product*

**n x 1, t****uv**sometimes also written as <

**u**,

**v**>, is defined by:

In other words, the inner product is the sum of the products between corresponding elements in the vectors:

The first element in the row vector is multiplied by the first element of the column vector, the second element of the row vector is multiplied by the second element of the column vector, etc. All of these product terms are summed so that a single value is returned by the inner product.

*Note: It is required that both vectors have the same number of elements. *

For example:

Consider the row vector

1 2 3 |
u [,1] [,2] [,3] [1,] 1 3 2 |

and the column vector

1 2 |
v [1] 2 8 9 |

The inner product < **u**, **v** >, exists because both have the same number of elements (**u** has 3 columns and **v** has 3 rows). This inner product is given by:

1 |
u%*%v |

1 2 |
[,1] [1,] 44 |

That is:

1 |
(1 × 2) + (3 × 8) + (2 x 9) = 44. |

### Matrix multiplication

Two matrices, A and B, can be multiplied if A is of the order **m** x ** n** and B is of the order

**x**

*n***p**.

In other words, matrix multiplication is only possible when the second dimension of A equals the first dimension of B (**n**). Thus, the resulting matrix, C, is of the order **m** x **p**.

Formally:

We can say that c[i,j] is equal to the inner product of the *i-th* row in A and the *j-th *column in B.

In R, a matrix product can be performed using

1 |
C<-A%*%B |

Let’s see an example. Consider the following matrices:

1 2 3 4 5 |
A <- matrix(c(1, 3, 2, 2, 8, 9), ncol = 2) [,1] [,2] [1,] 1 2 [2,] 3 8 [3,] 2 9 |

1 2 3 4 |
B <- matrix(c(5, 8, 4, 2), ncol = 2) [,1] [,2] [1,] 5 4 [2,] 8 2 |

First, we verify that A and B are **conformable** for multiplication.

The matrix A is of order 3 x **2,** while the matrix B is of order **2** x 2. Since A has as many columns as B has rows, the matrix product is defined. Now, let’s multiply both matrices:

1 2 3 4 5 |
C<-A%*%B [,1] [,2] [1,] 21 8 [2,] 79 28 [3,] 82 26 |

What happened here:

#### Matrix multiplication has several important properties:

- The product of a matrix, A, and a conformable identity matrix is the matrix itself: IA = AI = A.
- Matrix multiplication follows the associative law: A(BC) = (AB)C = ABC.
- Matrix multiplication follows the distributive law: A(B+C) = AB+AC.
- The transpose of a product is equal to the product of the transposes in reversed order: (AB)’ = B’A’.
- If the matrix products AB and BA are both defined, then the trace of these products is identical: tr(AB) = tr(BA). If B = A’, this implies tr(AA’) = tr(A’A).
- If the product of a matrix with itself is equal to the matrix, we call this matrix an idempotent matrix: AA = A.

### Kronecker Products

**Kronecker products** or **direct products** are another variation of matrix multiplication with important statistical applications.

A ⊗ B, takes each element of the matrix A to create scalar products with the second matrix B.

For example, if A is of order 3×2 and B is of order 2×3, then A⊗B is given by

The dimension of the new matrix is (assuming A=**m**x**n** and B=**p**x**q**):

1 |
C=(mp)x(nq) |

To obtain Kronecker products in R:

1 |
C<-kronecker(A,B) |

For instance:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
A [,1] [,2] [1,] 1 2 [2,] 3 8 [3,] 2 9 B [,1] [,2] [1,] 5 4 [2,] 8 2 kronecker(A,B) [,1] [,2] [,3] [,4] [1,] 5 4 10 8 [2,] 8 2 16 4 [3,] 15 12 40 32 [4,] 24 6 64 16 [5,] 10 8 45 36 [6,] 16 4 72 18 |