You are on page 1of 6

Python for Data Science & Artificial Intelligence Lecture 4

Reminder
matrices, operations on matrices.

Matrix
In mathematics, a matrix (plural matrices) is a rectangular array of numbers, arranged in rows and columns.

 a11 a12  a1m 


a a  a2 m 
A =  21 22 = [aij ]1i  n;1 j  m
   
 
an1 an 2  anm 

This is an nxm matrix.

The matrix A is “n-by-m” matrix (nxm) if a matrix has n-rows and m-columns. This is the matrix size.
An element of a matrix could be denoted by aij an element with two subscripts.
The first subscript is the row number: 1 ≤ i ≤ n.
The second subscript is the column number: 1 ≤ j ≤ n.

Examples
2 3 7
𝐵= [ ] B is 2x3 matrix.
4 1 5
8 1
𝐶 = [4 3] C is 3x2 matrix
2 5

D = [2 9 4] D is 1x3 matrix

2
𝐸 = [4] E is 3x1 matrix
6

Vector
Vector – column vector is an nx1 matrix.
Example vector E.

Vector – row vector is an 1xm matrix,


Example vector D.

Matrix addition

If two matrices A, B have the same size we can define matrix A+B their sum:

A = [aij]1≤i≤n;1≤j≤m ; B = [bij]1≤i≤n;1≤j≤m then A + B exist and A + B = [(aij + bij)] 1≤i≤n;1≤j≤m .

 a11 a12  a1m  b11 b12  b1m   a11 + b11 a12 + b12  a1m + b1m 
a a  a2 m  b21 b22  b2 m  a21 + b21 a22 + b22  a2 m + b2 m 
A + B =  21 22 + =
              
     
an1 an 2  anm  bn1 bn 2  bnm   an1 + bn1 an 2 + bn 2  anm + bnm 

Example

P. Zaremba 2022 1/6 Lect_04_Matrix.docx


Python for Data Science & Artificial Intelligence Lecture 4

2 3 1 6 3 9
[4 1] + [ 3 5] = [ 7 6]
7 5 2 2 9 7

Comment
If we can add matrices A, B then:
A+B=B+A

Example
7 2
1 2 5
[ ] + [3 3] = ? ? ?
3 4 7
1 4

That sum do not have sense, do not exist!!

Scalar multiplication

The product of a scalar k and matrix A written kA or Ak:

kA = Ak
is the matrix:

 ka11 ka12  ka1m 


ka ka22  ka2 m 
kA =  21 = [kaij ]1i  n;1 j  m
    
 
kan1 kan 2  kanm 

Zero matrix

0 0  0
0 0  0
0=   = [0]1i  n;1 j  m
   
 
0 0  0

Properties of matrix under addition and scalar multiplication:

• A+B=B+A
• A+0=A
• -A = (-1)A
• A + (-A) = 0
• k(A + B) = kA +kB
• (k + p)A = kA + pA
• (kp)A = k(pA)

Transposed matrix

P. Zaremba 2022 2/6 Lect_04_Matrix.docx


Python for Data Science & Artificial Intelligence Lecture 4

 a11 a12  a1m   a11 a12  a1n 


a a  a2 m  a a22  a2 n 
A =  21 22 = [aij ]1i  n;1 j  m ; AT =  21 = [a ji ]1 j  m;1i  n
       
   
an1 an 2  anm  am1 am 2  amn 

Example of transposition:

1 3
1 2 5𝑇
[ ] = [2 4]
3 4 7
5 7

If A is nxm matrix then AT is mxn matrix.

Matrix multiplication

If A is nxm matrix and B is mxk matrix then we define matrix C their product as:

A = [aij]1≤i≤n;1≤j≤m ; B = [bjk]1≤j≤m;1≤k≤w then

AB = C

C = [cik]1≤i≤n;1≤k≤w

where
𝑚

𝑐𝑖𝑘 = ∑ 𝑎𝑖𝑗 𝑏𝑗𝑘


𝑗=1

Multiplication of matrices A and B is possible only if the number of columns in matrix A is equal to the number
of rows in matrix B.

Notice
If the number of columns in matrix A is not equal to the number of rows in matrix B, then the product AB is not
defined.

Matrix multiplication is not commutative.

2 1 5 3 2 = 32 30
3 4 7 6 1 61 45
4 5

3 2 2 1 5 = 12 11 29
6 1 3 4 7 15 10 37
4 5 23 24 55

P. Zaremba 2022 3/6 Lect_04_Matrix.docx


Python for Data Science & Artificial Intelligence Lecture 4

2 1 5 3 = 32
3 4 7 6 61
4

3 2 1 5 = not exist!
6 3 4 7
4

Properties of matrix multiplication

• (AB)C = A(BC)
• A(B+C) = AB + AC
• k(AB) = (kA)B = A(kB)

Square matrix

If the number of rows and columns are equal, we say matrix is square matrix.
Matrix nxn is said to be square matrix of order n or n-square matrix.

Diagonal
The main diagonal or simply diagonal of n-square matrix A= [aij] consists of the elements a11, a22, … , ann.

Unit matrix
The square matrix with 1’s along the main diagonal 0’s elsewhere is called unit matrix and is denoted by I.

Notice
Unit matrix plays the same role in matrix multiplication as 1 in number multiplication:

IA = AI = A

A, I – n-square matrices.

Determinants
Determinant of n-square matrix A is the number denoted |A| or det(A) recursively defined as follows:

For 1x1 matrix


A = [a] ; det(A) = a.

For nxn matrix n>1

Minor Mij of n-square matrix A is the determinant of the (n-1) square submatrix of A formed from A by deleting
i-th row and j-th column from matrix A.
𝑛 𝑛
(𝑖0 +𝑗)
det(𝐴) = ∑(−1) 𝑎𝑖0𝑗 𝑀𝑖0 𝑗 = ∑(−1)(𝑖+𝑗0 ) 𝑎𝑖𝑗0 𝑀𝑖𝑗0
𝑗=1 𝑖=1

P. Zaremba 2022 4/6 Lect_04_Matrix.docx


Python for Data Science & Artificial Intelligence Lecture 4

For n = 2 we have

𝑎 𝑐
𝐴=[ ] ; det(A) = ad – bc .
𝑏 𝑑

For n = 3 we have
𝑎 𝑑 𝑔
𝐴 = [𝑏 𝑒 ℎ]
𝑐 𝑓 𝑖

a d g a d
b e h b e
c f i c f

det(A) = aei + dhc + gbf – ceg – fha – ibd

Example

2 3
A= 5 7

det(A) = 2∙7 - 5∙3 = -1

Example
2 3 6
A= 5 7 5
2 3 4

det(A) = 2∙7∙4 + 3∙5∙2 + 6∙5∙3 - 2∙7∙6 - 3∙5∙2 - 4∙5∙3 = 2

Invertible matrices
A square matrix A is said to be invertible if there exists a matrix B such that

AB = BA = I

If such matrix B exist is unique is called the inverse of matrix A and is denoted A-1.

Theorem
If det(A) ≠ 0 for square matrix A than A-1 exists.

Inverse matrix
A is n-square matrix such that det(A) ≠ 0. Than

1 1 𝑇
𝐴−1 = [(−1)(𝑖+𝑗) 𝑀𝑗𝑖 ]𝑖𝑗 = [(−1)(𝑖+𝑗) 𝑀𝑖𝑗 ]𝑖𝑗
det (𝐴) det (𝐴)

P. Zaremba 2022 5/6 Lect_04_Matrix.docx


Python for Data Science & Artificial Intelligence Lecture 4

Example – Inverse matrix 2x2

4 2
A= 5 3

det(A) = 4*3 -5*2 = 2

4 5 3/2 -2/2
AT = 2 3 A-1 = -5/2 4/2

We can check:

AA-1 = A-1A = I

Example – Inverse matrix 3x3

0 1 2
A= 1 2 1
2 1 0

det(A) = 0∙2∙0 + 1∙1∙2 + 2∙1∙1 – 2∙2∙2 – 1∙1∙0 – 0∙1∙1 = 0 + 2 +2 – 8 – 0 – 0 = –4

det(A) = –4 .

0 1 2 0,25 -0,5 0,75


AT= 1 2 1 A-1= -0,5 1 -0,5
2 1 0 0,75 -0,5 0,25

We can check:

AA-1 = A-1A = I

P. Zaremba 2022 6/6 Lect_04_Matrix.docx

You might also like