License: CC-BY-NC-SA 4.0
Author: Murilo M. Marinho (murilo
Pre-requisites¶
The user of this notebook is expected to have prior knowledge in
- Basic Python [Tutorial]
- Numpy
- Jupyter Notebook Basics [Tutorial]
I found an issue¶
Thank you! Please report it at https://
Latex Macros¶
\providecommand{\myvec}[1]{{\mathbf{\boldsymbol{{#1}}}}}
\providecommand{\mymatrix}[1]{{\mathbf{\boldsymbol{{#1}}}}}
A quick Python refresher¶
a = 10
b = 5
Output variables¶
Variables can be output using print
. For example, for
print(a)
10
Output text and variables using f-strings¶
To output and within a string, we can use print
and f-strings as follows
print(f'The value of a = {a} and b = {b}.')
The value of a = 10 and b = 5.
c = a + b
print(f'c={c}')
c=15
c = a - b
print(f'c={c}')
c=5
c = a * b
print(f'c={c}')
c=50
c = a / b
print(f'c={c}')
c=2.0
c = a ** b
print(f'c={c}')
c=100000
Math functions¶
For the following functions, we will need Python’s built-in math
module.
from math import sqrt, exp, log, pi, sin, cos, tan
c = sqrt(a)
n-th root¶
The nth root,
does not seem to have a shorthanded version in Python, but can computed through simple properties such as
For example, suppose that
Then,
n=3
and we can calculate the n-th root like so
# n-th root using fractional exponent. Might be easier but most languages do not support a similar syntax
c = a ** (1/n)
print(f'c={c}')
c=2.154434690031884
or like so
c = exp(log(a)/n)
print(f'c={c}')
c=2.154434690031884
phi = pi/4.0
s_phi = sin(phi)
c_phi = cos(phi)
t_phi = tan(phi)
print(f'phi={phi}')
print(f's_phi={s_phi}')
print(f'c_phi={c_phi}')
print(f't_phi={t_phi}')
phi=0.7853981633974483
s_phi=0.7071067811865475
c_phi=0.7071067811865476
t_phi=0.9999999999999999
Linear Algebra with Numpy¶
Installing the library¶
Just in case numpy
is not already installed, we can install it with the following command. Nothing will happen if the library is already installed.
%%capture
%pip install numpy
%pip install numpy --break-system-packages
Importing the library¶
import numpy as np
Instantiating vectors¶
A row vector can be instanteated from a list of list. For instance, for
we have
# Note the double [[]] to instanteate a vector with explicit row shape.
v = np.array([[1, 2]])
print(f'v={v}')
v=[[1 2]]
# Note that each row is defined by a single element within a [], while the whole vector is within an external []
u = np.array([[1],
[2]])
print(f'u={u}')
u=[[1]
[2]]
c = np.vdot(u,u)
print(f'c={c}')
c=5
Cross product¶
Cross product is defined for vectors in .
For example, for
and
we can obtain the cross product
u3 = np.array([[1, 2, 3]])
v3 = np.array([[4, 5, 6]])
c = np.cross(u3,v3)
print(f'c={c}')
c=[[-3 6 -3]]
Euclidean norm¶
c = np.linalg.norm(u)
print(f'c={c}')
c=2.23606797749979
Instantiating matrices¶
For instance, suppose that we want to instanteate two real square matrices
A = np.array([[1, 2],
[3, 4]])
B = np.array([[5, 6],
[7, 8]])
print(f'A={A},\n\nB={B}')
A=[[1 2]
[3 4]],
B=[[5 6]
[7 8]]
C = A.T
print(f'C={C}')
C=[[1 3]
[2 4]]
C = A + B
print(f'C={C}')
C=[[ 6 8]
[10 12]]
C = A - B
print(f'C={C}')
C=[[-4 -4]
[-4 -4]]
Matrix multiplication¶
For instance,
is implemented with
C = A @ B # Alternatively C = np.matmul(A,B), but that is too verbose
print(f'C={C}')
C=[[19 22]
[43 50]]
c = u @ v
d = v @ u
print(f'c={c},\n\nd={d}')
c=[[1 2]
[2 4]],
d=[[5]]
and, of course, matrices and vectors
c = A @ u
print(f'c={c},\n')
c=[[ 5]
[11]],
Diagonal matrices¶
Diagonal matrices get increasingly sparse with size, so it is important to have shorthanded commands for creating them. For instance, suppose that we have the following diagonal matrix
this can be instanteated in numpy
with
D = np.diag([1, 2, 3])
print(f'D={D}.')
D=[[1 0 0]
[0 2 0]
[0 0 3]].
Identity matrix¶
Among frequently used diagonal matrices, the identity matrix appears frequently. For instance,
can be instanteated in numpy
with
I_3 = np.eye(3)
print(f'I_3={I_3}.')
I_3=[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]].
Zero matrix¶
Another frequently used matrix is the zero matrix. For instance,
O_3 = np.zeros((3,3))
print(f'O_3={O_3}.')
O_3=[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]].