1.  About numpy 

1.1 What is numpy ?

Numpy is an open source library associated with the Python language, created specially for scientific computing, notably matrix computing, while providing multiple functions allowing the direct creation and manipulation of matrices, vectors, etc... via data. Official documentation: https://numpy.org/doc/

1.2 How to install numpy ?

Installing the numpy library is a trivial operation, just launch the cmd command prompt and type:

pip install numpy

2. Matrix or table with numpy 

2.1 Syntax

import numpy as np
a = np.array([ligne1, ligne2 , ligne3])

Exemple: matrix with 2 rows and 3 columns 2x3

import numpy as np
a = np.array([[2,7,3] , [8,5,4]])
print(a)
#The output is as below::
[[2 7 3]
[8 5 4]]

2.2 Product term by term

import numpy as np
a = np.array([[5, 3],[2, 3]])
b = np.array([[1, 3],[7, 3]])
print(a*b)
#The output is as below:
[[ 5 9]
[14 9]]

2.3 Matrix product

import numpy as np
a = np.array([[5, 3] ,
[2, 3]])
b = np.array([[1, 3],
[1, 3]])
product = np.dot(a,b)
print(product)
#The output is as below:
[[ 8 24]
[ 5 15]]

2.4 Transpose of a matrix

import numpy as np
a = np.array([[5, 3] , [2, 3]])
print(a.T)
# The output is:
[[5 2]
[3 3]]

2.5 Unit matrix

import numpy as np
a = np.eye(3)
print(a)
# The output is:
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]

2.6 Create a matrix by subdividing an interval 

 numpy.linspace() returns a one-dimensional array from a start value to an end value following a given step.

Exemple

import numpy as np
m = np.linspace(0, 20, 5)
print("The matrix obtained using step 5 is: ",m)
# The output is:
The matrix obtained using step 5 is : [ 0. 5. 10. 15. 20.]

2.7 Action of a mathematical function on an array

NumPy has a large number of mathematical functions that can be applied directly to an array. In this case, the function is applied to each of the elements of the array.

Example

import numpy as np  
x = np.linspace(0, 3,4)
y = np.exp(x)
print(y)
# The output is: [ 1. 2.71828183 7.3890561 20.08553692]

3.  The sublibrary linear algebra

3.1  Inverse of a matrix with linalg numpy

To invert a matrix with numpy, we use the method: NumPy linear algebra. For more details on this function, see the official documentation: https://docs.scipy.org/doc/numpy/reference/routines.linalg.html

import numpy as np 
a = np.array([[2, 1],
[5, 3]])
b = np.linalg.inv(a)
print("The matrix inverse of a is : " , b)
# The output is:
The matrix inverse of a is :
[[ 3. -1.]
[-5. 2.]]

3.2 The eigenvalues of matrix with linalg numpy

To calculate the eigenvalues ​​of a matrix with numpy, we use the eig() method:

Example:

import numpy as np  
a = np.array([[1, 2],
[3, 2]])
eg = np.linalg.eigvals(a)
print("The eigenvalues of matrix a are : " , eg)
# The output is: The eigenvalues of matrix a are : [-1. 4.]

Remark

You can use several other notions of linear algebra with the linalg numpy library, such as characteristic polynomial, minimal polome, rank of a matrix ... See the official documentation: https://docs.scipy.org/doc/numpy/reference/routines.linalg.html

Younes Derfoufi
my-courses.net

Leave a Reply