6 Matrix arithmetic
Andry + Fenitra = Tolotra
Above is an example of cross-field use of mathematical notation1 (\(+\) and \(=\)) in the context of romance and acts of love. While they may not be as rigorously defined as in mathematics, the statement above means something.
In the previous chapter, a new kind of mathematical object called a matrix (Chapter 5) was introduced. What would the sum of two matrices \(A + B\) or the product of two matrices mean? Because mathematics is (arguably) a bit more rigorous than romance, mathematicians defined rigorously what performing operations with matrices means. This chapter focuses on what is called element-wise operations. The next chapter (Chapter 7) will delve into a special type of matrix multiplication.
The simplest operation is to add, subtract, multiply, or divide each element of the matrix by a scalar. In such element-wise operations, standard operators are used. The grocery matrix from the previous section (Chapter 5) can be reused.
\[ A = \begin{pmatrix} 6 & 3 & 0.5 \\ 3 & 1 & 0 \\ 0 & 10 & 0 \\ 1 & 0 & 2 \end{pmatrix} \]
and multiplied by 2
\[ 2 \times A = \begin{pmatrix} 2 \times 6 & 2 \times 3 & 2 \times 0.5 \\ 2 \times 3 & 2 \times 1 & 2 \times 0 \\ 2 \times 0 & 2 \times 10 & 2 \times 0 \\ 2 \times 1 & 2 \times 0 & 2 \times 2 \end{pmatrix} \]
So \(2 + A\), \(A + 2\), and \(A / 2\) are well-defined. The same element-wise operations also work between two matrices with identical dimensions.
\[ \begin{align*} A - A &= \begin{pmatrix} 6 & 3 & 0.5 \\ 3 & 1 & 0 \\ 0 & 10 & 0 \\ 1 & 0 & 2 \end{pmatrix} - \begin{pmatrix} 6 & 3 & 0.5 \\ 3 & 1 & 0 \\ 0 & 10 & 0 \\ 1 & 0 & 2 \end{pmatrix} \\[2ex] &= \begin{pmatrix} 6 - 6 & 3 - 3 & 0.5 - 0.5 \\ 3 - 3 & 1 - 1 & 0 - 0 \\ 0 - 0 & 10 - 10 & 0 - 0 \\ 1 - 1 & 0 - 0 & 2 - 2 \end{pmatrix} \\[2ex] &= \begin{pmatrix} 0 & 0 & 0 \\ 0 & 0 & 0 \\ 0 & 0 & 0 \\ 0 & 0 & 0 \end{pmatrix} \end{align*} \]
Defined in that manner, the same symbols \(+\), \(-\), \(\times\), and \(/\) are used to perform element-wise operations while retaining the same properties as arithmetic operations between two numbers. The extra conditions (one operand is a scalar or both matrices have identical dimensions) also seem intuitive. Adding Andry + Baiboly does not make sense because they are two objects of different types; even if Andry + Baiboly = something, that something would not be of type Andry or type Baiboly.
Formal
Consider a number \(x\), a matrix \(A = \{a_{i,j}\}\) and \(B = \{b_{i,j}\}\) of dimensions \(m\) and \(n\).
If one of the expresion is a scalar2
- Addition with a scalar \(x + A = \{x + a_{i,j}\}\) for all i and j
- Multiplication with a scalar \(x \times A = \{x \times a_{i,j}\}\) for all i and j
- Subtraction with a scalar \(x - A = \{x - a_{i,j}\}\) for all i and j
- Dividing with a scalar \(\frac{x}{A} = \{\frac{x}{a_{i,j}} \}\) for all i and j
It should be obvious that \(A + x\), \(A - x\), \(A \times x\) and \(A / x\) are defined in the same manner
If two matrices have the identical dimension then
Addition between them \(A + B = \{a_{i,j} + b_{i,j} \}\) for all i and j
Subtraction between them \(A - B = \{a_{i,j} - b_{i,j} \}\) for all i and j
For a multiplication, a special notation is used \(A \circ B = \{a_{i,j} \times b_{i,j}\}\) and is called a Hadamard product.
The equivalent division, called Hadamard quotient, also exists and is noted: \(A \circ B^{-1} = \{\frac{a_{i,j}}{b_{i,j}}\}\)
Examples
Notice that multiplying the matrix by 2 above is equivalent to purchasing twice.
With the example above, one can perform \(A + 1\), but it might not have a practical interpretation:
\[ A + 1 = \begin{pmatrix} 6 + 1 & 3 + 1 & 0.5 + 1\\ 3 + 1 & 1 + 1 & 0 + 1 \\ 0 + 1 & 10 + 1 & 0 + 1\\ 1 + 1 & 0 + 1 & 2 + 1 \end{pmatrix} \]
- Assume matrix \(A\) needs to be converted into kg . If 1 banana is 0.2 kg3, one toko is 0.5 kg, and one kapoka is 0.3 kg, constructing a matrix \(B\) of the same dimensions yields:
\[ B = \begin{pmatrix} 0.2 & 0.5 & 0.3 \\ 0.2 & 0.5 & 0.3 \\ 0.2 & 0.5 & 0.3 \\ 0.2 & 0.5 & 0.3 \end{pmatrix} \]
The conversion is done with a Hadamard product:
\[ A \circ B = \begin{pmatrix} 6 & 3 & 0.5 \\ 3 & 1 & 0 \\ 0 & 10 & 0 \\ 1 & 0 & 2 \end{pmatrix} \circ \begin{pmatrix} 0.2 & 0.5 & 0.3 \\ 0.2 & 0.5 & 0.3 \\ 0.2 & 0.5 & 0.3 \\ 0.2 & 0.5 & 0.3 \end{pmatrix} = \begin{pmatrix} 6 \times 0.2 & 3 \times 0.5 & 0.5 \times 0.3 \\ 3 \times 0.2 & 1 \times 0.5 & 0 \times 0.3 \\ 0 \times 0.2 & 10 \times 0.5 & 0 \times 0.3 \\ 1 \times 0.2 & 0 \times 0.5 & 2 \times 0.3 \end{pmatrix}\]
Tips and warning
- Division by zero is also undefined in element-wise division.
- This serves as a good exercise in how concepts are well-defined in mathematics.
- Because a matrix represents data, these operations exist, and the goal is to determine how they are implemented in R. For instance, the \(\circ\) symbol in the Hadamard product is not a standard keyboard character, so a different symbol is used in code.
- Other matrix products exist (e.g., the Kronecker product); these are left aside for now, but will be defined later if needed.
- Issues may arise during implementation, as applying these concepts in code is not always straightforward .
- Element-wise power can also be defined in the same way.