Operations on matrices
How to add two matrices ?
Both matrices must have the same dimension i.e. the same number of rows and the same number of columns.
Adding two matrices is simple : just add the corresponding elements and place the sum in the same corresponding position.
Example:
A and B are two matrices of dimension 2 x 2
`A = [[1,5], [6, -4]]`
`B = [[0, -12], [3,7]]`
We can sum then,
`A + B = [[1+0,5-12], [6+3, -4+7]] = [[1, -7], [9,3]]`
How to subtract two matrices ?
In the same way, the two matrices must have the same dimension i.e. the same number of rows and the same number of columns.
To subtract them, just subtract the elements in the same position and place the result in the same corresponding position.
Example:
A and B are two matrices of dimension 3 x 2
`A = [[2,6], [7, -2], [5,11]]`
`B = [[1, -10], [4,7], [-9,13]]`
then,
`A - B = [[2-1,6- (-10)], [7-4, -2-7], [5- (-9) ,11-13]] = [[1,16], [3, -9], [14, -2]]`
How to multiply two matrices ?
Given two matrices A and B, the mutiplication of the two matrices A.B is possible only if the number of columns of A is equal to the number of rows of B. Thus, one can multiply a matrix 2 x 3 by a matrix 3 x 4 but not by a matrix 2 x 2. We can generalize as follows,
The matrix product A.B is defined only for matrices with the following dimensions :
A dimension m x n
B dimension n x p
The product of the two matrices P = A.B is a matrix of dimension m x p.
Pay attention : the order of A and B in the product matters, this is A.B and not B.A which is not defined if p is different from m (matrix multiplication is not commutative).
How to calculate the matrices product ?
Assume A is a 2 x 3 matrix and B a 3 x 2 matrix. According to the above definitions (m=2, n=3 and p=2), multiplication is possible and the matrices product P = A.B is of dimension 2 x 2
`A = [[1,5,2], [3,4,7]]`
`B = [[0, -1], [8,6], [-2,10]]`
`P = A*B = [[\color {red} {1},\color {red} {5},\color {red} {2}], [3,4,7]] * [[\color {red} {0}, -1], [\color {red} {8}, 6], [\color {red} {-2} ,10]] = [[\color {red} {c_11}, c_12], [c_21, c_22]]`
- To calculate the coefficient `c_11`, we "multiply" the 1st row by the 1st column. So we have,
`c_11 = [1,5,2] * [[0], [8], [-2]] = 1*0 +5*8 +2* (-2) = 36`
- To calculate the coefficient `c_12`, we "multiply" the 1st row by the 2nd column. So we have,
`c_12 = [1,5,2] * [[-1], [6], [10]] = 1* (-1) +5*6 +2* (10) = 49`
- To calculate the coefficient `c_21`, we "multiply" the 2nd row by the 1st column. So we have,
`c_21 = [3,4,7] * [[0], [8], [-2]] = 3*0 +4*8 +7* (-2) = 18`
- To calculate the coefficient `c_22`, we "multiply" the 2nd row by the 2nd column. So we have,
`c_22 = [3,4,7] * [[-1], [6], [10]] = 3* (-1) +4*6 +7* (10) = 91`
We write the final result,
`P = A*B = [[36,49], [18,91]]`
We generalize this method as follows,
Assume that A and B are two matrices of respective dimensions m x n and n x p, then the product P = A.B is a matrix of dimension m x p. We denote `c_ (ij)`the element of matrix P which is in the first row and jth column.
The coefficient `c_ (ij)`is calculated by 'multiplying' line i of matrix A by column j of matrix B.
How to divide two matrices ?
Assumed that A and B are two matrices such as :
- A is a matrix of dimension m x n
- B is an invertible square matrix of size n (See Inverse Matrix)
Under these conditions, we can do a division of A by B. The matrix division is:
`D = A.B^ (-1)`
This leads to a multiplication of two matrices which is explained above. Let's take an example.
Example: How to divide A by B ?
`A = [[1,2], [5,7]]`
`B = [[-1,2], [10,7]]`
Let's check the conditions of divisibility explained above :
- Is B a square matrix ? yes, because the number of columns is the same as the number of rows (=2).
- Is B invertible? yes because its determinant is different from 0 (det[B] = -1*7-2*10 = -27).
The conditions of divisibility are checked, so we can calculate `D = A. B^ (-1)`
We inverse B matrix, we get
`B^ (-1) = [[-7/27,2/27], [10/27,1/27]]`
`D = A*B^ (-1) = [[1,2], [5,7]] * [[-7/27,2/27], [10/27,1/27]]`
We get the final result,
`D = [[13,4], [35,17]]`