Linear Algebra Python
Linear Algebra Python
Linear Algebra Python
Linear Algebra
In [1]: import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
In [2]: v = [3,4]
u = [1,2,3]
In [3]: v ,u
In [4]: type(v)
Out[4]: list
In [5]: w = np.array([9,5,7])
In [6]: type(w)
Out[6]: numpy.ndarray
In [7]: w.shape[0]
Out[7]: 3
In [8]: w.shape
Out[8]: (3,)
In [10]: a[0]
Out[10]: 7
In [11]: a[1:]
In [12]: a[1:4]
In [13]: a[-1]
Out[13]: 2
In [14]: a[-3]
Out[14]: 9
In [15]: a[-6]
Out[15]: 7
In [16]: a[-3:-1]
Plotting a Vector
What is vector : https://www.youtube.com/watch?
v=fNk_zzaMoSs&list=PLZHQObOWTQDPD3MizzM2xVFitgF8hE_ab&index=1
(https://www.youtube.com/watch?
v=fNk_zzaMoSs&list=PLZHQObOWTQDPD3MizzM2xVFitgF8hE_ab&index=1)
In [17]: v = [3,4]
u = [1,2,3]
In [18]: plt.plot (v)
Plot 2D Vector
In [20]: plt.plot([0,v[0]] , [0,v[1]])
plt.plot([8,-8] , [0,0] , 'k--')
plt.plot([0,0] , [8,-8] , 'k--')
plt.grid()
plt.axis((-8, 8, -8, 8))
plt.show()
Vector Addition
In [22]: v1 = np.array([1,2])
v2 = np.array([3,4])
v3 = v1+v2
v3 = np.add(v1,v2)
print('V3 =' ,v3)
plt.plot([0,v1[0]] , [0,v1[1]] , 'r' , label = 'v1')
plt.plot([0,v2[0]] , [0,v2[1]], 'b' , label = 'v2')
plt.plot([0,v3[0]] , [0,v3[1]] , 'g' , label = 'v3')
plt.plot([8,-8] , [0,0] , 'k--')
plt.plot([0,0] , [8,-8] , 'k--')
plt.grid()
plt.axis((-8, 8, -8, 8))
plt.legend()
plt.show()
V3 = [4 6]
In [23]: plt.plot([0,v1[0]] , [0,v1[1]] , 'r' , label = 'v1')
plt.plot([0,v2[0]]+v1[0] , [0,v2[1]]+v1[1], 'b' , label = 'v2')
plt.plot([0,v3[0]] , [0,v3[1]] , 'g' , label = 'v3')
plt.plot([8,-8] , [0,0] , 'k--')
plt.plot([0,0] , [8,-8] , 'k--')
plt.grid()
plt.axis((-8, 8, -8, 8))
plt.legend()
plt.show()
Scalar Multiplication
In [24]: u1 = np.array([3,4])
a = .5
u2 = u1*a
plt.plot([0,u1[0]] , [0,u1[1]] , 'r' , label = 'v1')
plt.plot([0,u2[0]] , [0,u2[1]], 'b--' , label = 'v2')
plt.plot([8,-8] , [0,0] , 'k--')
plt.plot([0,0] , [8,-8] , 'k--')
plt.grid()
plt.axis((-8, 8, -8, 8))
plt.legend()
plt.show()
In [25]: u1 = np.array([3,4])
a = -.3
u2 = u1*a
plt.plot([0,u1[0]] , [0,u1[1]] , 'r' , label = 'v1')
plt.plot([0,u2[0]] , [0,u2[1]], 'b' , label = 'v2')
plt.plot([8,-8] , [0,0] , 'k--')
plt.plot([0,0] , [8,-8] , 'k--')
plt.grid()
plt.axis((-8, 8, -8, 8))
plt.legend()
plt.show()
Multiplication of vectors
In [26]: a1 = [5 , 6 ,8]
a2 = [4, 7 , 9]
print(np.multiply(a1,a2))
[20 42 72]
Dot Product
Dot Product :
https://www.youtube.com/watch?v=WNuIhXo39_k (https://www.youtube.com/watch?
v=WNuIhXo39_k)
https://www.youtube.com/watch?v=LyGKycYT2v0 (https://www.youtube.com/watch?
v=LyGKycYT2v0)
In [27]: a1 = np.array([1,2,3])
a2 = np.array([4,5,6])
dotp = a1@a2
print(" Dot product - ",dotp)
dotp = np.dot(a1,a2)
print(" Dot product usign np.dot",dotp)
dotp = np.inner(a1,a2)
print(" Dot product usign np.inner", dotp)
dotp = sum(np.multiply(a1,a2))
print(" Dot product usign np.multiply & sum",dotp)
dotp = np.matmul(a1,a2)
print(" Dot product usign np.matmul",dotp)
dotp = 0
for i in range(len(a1)):
dotp = dotp + a1[i]*a2[i]
print(" Dot product usign for loop" , dotp)
Dot product - 32
Dot product usign np.dot 32
Dot product usign np.inner 32
Dot product usign np.multiply & sum 32
Dot product usign np.matmul 32
Dot product usign for loop 32
Length of Vector
In [28]: v3 = np.array([1,2,3,4,5,6])
length = np.sqrt(np.dot(v3,v3))
length
Out[28]: 9.539392014169456
In [29]: v3 = np.array([1,2,3,4,5,6])
length = np.sqrt(sum(np.multiply(v3,v3)))
length
Out[29]: 9.539392014169456
In [30]: v3 = np.array([1,2,3,4,5,6])
length = np.sqrt(np.matmul(v3,v3))
length
Out[30]: 9.539392014169456
Normalized Vector
How to normalize a vector : https://www.youtube.com/watch?v=7fn03DIW3Ak
(https://www.youtube.com/watch?v=7fn03DIW3Ak)
In [31]: v1 = [2,3]
length_v1 = np.sqrt(np.dot(v1,v1))
norm_v1 = v1/length_v1
length_v1 , norm_v1
In [32]: v1 = [2,3]
norm_v1 = v1/np.linalg.norm(v1)
norm_v1
In [36]: # https://www.youtube.com/watch?v=FCmH4MqbFGs
v1 = np.array([1,2,3])
v2 = np.array([4,5,6])
np.inner(v1,v2)
print("\n Inner Product ==> \n", np.inner(v1,v2))
print("\n Outer Product ==> \n", np.outer(v1,v2))
In [37]: v1 = np.array([1,2,3])
v2 = np.array([4,5,6])
print("\nVector Cross Product ==> \n", np.cross(v1,v2))
Matrix Operations
Matrix Creation
In [38]: A = np.array([[1,2,3,4] , [5,6,7,8] , [10 , 11 , 12 ,13] , [14,15,16,17]])
In [39]: A
In [40]: type(A)
Out[40]: numpy.ndarray
In [41]: A.dtype
Out[41]: dtype('int64')
In [43]: type(B)
Out[43]: numpy.ndarray
In [44]: B.dtype
Out[44]: dtype('float64')
In [45]: A.shape
Out[45]: (4, 4)
In [46]: A[0,]
In [47]: A[:,0]
In [48]: A[0,0]
Out[48]: 1
In [49]: A[0][0]
Out[49]: 1
Matrix Types :
https://www.youtube.com/watch?v=alc9i7V2e9Q&list=PLmdFyQYShrjcoVkhCCIwxNj9N4rW1-
T5I&index=5 (https://www.youtube.com/watch?
v=alc9i7V2e9Q&list=PLmdFyQYShrjcoVkhCCIwxNj9N4rW1-T5I&index=5)
https://www.youtube.com/watch?
v=nfG4NwLhH14&list=PLmdFyQYShrjcoVkhCCIwxNj9N4rW1-T5I&index=6
(https://www.youtube.com/watch?
v=nfG4NwLhH14&list=PLmdFyQYShrjcoVkhCCIwxNj9N4rW1-T5I&index=6)
Zero Matrix
Zero Matrix - https://www.web-
formulas.com/Math_Formulas/Linear_Algebra_Definition_of_Zero_Matrix.aspx (https://www.web-
formulas.com/Math_Formulas/Linear_Algebra_Definition_of_Zero_Matrix.aspx)
In [51]: np.zeros(9).reshape(3,3)
In [52]: np.zeros((3,3))
Matrix of Ones
Matrix of Ones - https://en.wikipedia.org/wiki/Matrix_of_ones
(https://en.wikipedia.org/wiki/Matrix_of_ones)
In [53]: np.ones(9).reshape(3,3)
In [54]: np.ones((3,3))
Identity Matrix
Identity Matrix : https://en.wikipedia.org/wiki/Identity_matrix
(https://en.wikipedia.org/wiki/Identity_matrix)
In [56]: I = np.eye(9)
I
Out[56]: array([[1., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 1., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 1., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 1., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 1., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 1., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 1., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 1., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 1.]])
Diagonal Matrix
Diagonal Matrix : https://en.wikipedia.org/wiki/Diagonal_matrix
(https://en.wikipedia.org/wiki/Diagonal_matrix)
In [57]: D = np.diag([1,2,3,4,5,6,7,8])
D
Concatenate Matrices
Matrix Concatenation :
https://docs.scipy.org/doc/numpy/reference/generated/numpy.concatenate.html
(https://docs.scipy.org/doc/numpy/reference/generated/numpy.concatenate.html)
In [59]: A = np.array([[1,2] , [3,4] ,[5,6]])
B = np.array([[1,1] , [1,1]])
C = np.concatenate((A,B))
C , C.shape , type(C) , C.dtype
In [60]: np.full((5,5) , 8)
In [61]: M
In [62]: M.flatten()
Out[62]: array([2, 7, 5, 3, 0, 2, 9, 8, 2, 2, 8, 4, 7, 4, 8, 2, 2, 4, 4, 6, 8, 1,
8, 0, 5])
Matrix Addition
Matrix Addition : https://www.youtube.com/watch?v=ZCmVpGv6_1g
(https://www.youtube.com/watch?v=ZCmVpGv6_1g)
In [63]: #********************************************************#
M = np.array([[1,2,3],[4,-3,6],[7,8,0]])
N = np.array([[1,1,1],[2,2,2],[3,3,3]])
print("\n First Matrix (M) ==> \n", M)
print("\n Second Matrix (N) ==> \n", N)
C = M+N
print("\n Matrix Addition (M+N) ==> \n", C)
# OR
C = np.add(M,N,dtype = np.float64)
print("\n Matrix Addition using np.add ==> \n", C)
#********************************************************#
Matrix subtraction
Matrix subtraction : https://www.youtube.com/watch?
v=7jb_AO_hRc8&list=PLmdFyQYShrjcoVkhCCIwxNj9N4rW1-T5I&index=8
(https://www.youtube.com/watch?v=7jb_AO_hRc8&list=PLmdFyQYShrjcoVkhCCIwxNj9N4rW1-
T5I&index=8)
In [64]: #********************************************************#
M = np.array([[1,2,3],[4,-3,6],[7,8,0]])
N = np.array([[1,1,1],[2,2,2],[3,3,3]])
print("\n First Matrix (M) ==> \n", M)
print("\n Second Matrix (N) ==> \n", N)
C = M-N
print("\n Matrix Subtraction (M-N) ==> \n", C)
# OR
C = np.subtract(M,N,dtype = np.float64)
print("\n Matrix Subtraction using np.subtract ==> \n", C)
#********************************************************#
Transpose of a matrix
Transpose of a matrix : https://www.youtube.com/watch?
v=g_Rz94DXvNo&list=PLmdFyQYShrjcoVkhCCIwxNj9N4rW1-T5I&index=13
(https://www.youtube.com/watch?v=g_Rz94DXvNo&list=PLmdFyQYShrjcoVkhCCIwxNj9N4rW1-
T5I&index=13)
In [66]: M = np.array([[1,2,3],[4,-3,6],[7,8,0]])
print("\n Matrix (M) ==> \n", M)
print("\nTranspose of M ==> \n", np.transpose(M))
# OR
print("\nTranspose of M ==> \n", M.T)
Transpose of M ==>
[[ 1 4 7]
[ 2 -3 8]
[ 3 6 0]]
Transpose of M ==>
[[ 1 4 7]
[ 2 -3 8]
[ 3 6 0]]
Determinant of a matrix
Determinant of a matrix :
https://www.youtube.com/watch?v=21LWuY8i6Hw&t=88s (https://www.youtube.com/watch?
v=21LWuY8i6Hw&t=88s)
https://www.youtube.com/watch?
v=Ip3X9LOh2dk&list=PLZHQObOWTQDPD3MizzM2xVFitgF8hE_ab&index=6
(https://www.youtube.com/watch?
v=Ip3X9LOh2dk&list=PLZHQObOWTQDPD3MizzM2xVFitgF8hE_ab&index=6)
In [67]: M = np.array([[1,2,3],[4,-3,6],[7,8,0]])
print("\n Matrix (M) ==> \n", M)
print("\nDeterminant of M ==> ", np.linalg.det(M))
Rank of M ==> 3
Trace of matrix
In [69]: M = np.array([[1,2,3],[4,-3,6],[7,8,0]])
print("\n Matrix (M) ==> \n", M)
print("\nTrace of M ==> ", np.trace(M))
Trace of M ==> -2
Inverse of matrix A
Inverse of matrix : https://www.youtube.com/watch?v=pKZyszzmyeQ
(https://www.youtube.com/watch?v=pKZyszzmyeQ)
In [70]: M = np.array([[1,2,3],[4,-3,6],[7,8,0]])
print("\n Matrix (M) ==> \n", M)
print("\nInverse of M ==> \n", np.linalg.inv(M))
Inverse of M ==>
[[-0.24615385 0.12307692 0.10769231]
[ 0.21538462 -0.10769231 0.03076923]
[ 0.27179487 0.03076923 -0.05641026]]
https://www.youtube.com/watch?v=vzt9c7iWPxs&t=207s (https://www.youtube.com/watch?
v=vzt9c7iWPxs&t=207s)
https://www.youtube.com/watch?
v=XkY2DOUCWMU&list=PLZHQObOWTQDPD3MizzM2xVFitgF8hE_ab&index=4
(https://www.youtube.com/watch?
v=XkY2DOUCWMU&list=PLZHQObOWTQDPD3MizzM2xVFitgF8hE_ab&index=4)
In [72]: M = np.array([[1,2,3],[4,-3,6],[7,8,0]])
N = np.array([[1,1,1],[2,2,2],[3,3,3]])
print("\n First Matrix (M) ==> \n", M)
print("\n Second Matrix (N) ==> \n", N)
print("\n Matrix Dot Product ==> \n", M@N)
# OR
print("\n Matrix Dot Product using np.matmul ==> \n", np.matmul(M,N))
# OR
print("\n Matrix Dot Product using np.dot ==> \n", np.dot(M,N))
Column-Wise Addition
In [75]: N = np.array([[1,1,1],[2,2,2],[3,3,3]])
print("\n Matrix (N) ==> \n", N)
print ("Column-Wise summation ==> ")
print (np.sum(N,axis=0))
Row-Wise Addition
In [76]: N = np.array([[1,1,1],[2,2,2],[3,3,3]])
print("\n Matrix (N) ==> \n", N)
print ("Row-Wise summation ==>")
print (np.sum(N,axis=1))
In [78]: M2 = np.array([[10,10,10],[10,10,10]])
M2
In [79]: np.kron(M1,M2)
Out[79]: array([[10, 10, 10, 20, 20, 20, 30, 30, 30],
[10, 10, 10, 20, 20, 20, 30, 30, 30],
[40, 40, 40, 50, 50, 50, 60, 60, 60],
[40, 40, 40, 50, 50, 50, 60, 60, 60]])
Matrix Powers
In [82]: M1 = np.array([[1,2],[4,5]])
M1
Tensor
What is Tensor :
https://www.youtube.com/watch?v=f5liqUk0ZTw (https://www.youtube.com/watch?
v=f5liqUk0ZTw)
https://www.youtube.com/watch?v=bpG3gqDM80w&t=634s
(https://www.youtube.com/watch?v=bpG3gqDM80w&t=634s)
https://www.youtube.com/watch?v=uaQeXi4E7gA (https://www.youtube.com/watch?
v=uaQeXi4E7gA)
In [85]: # Create Tensor
T1 = np.array([
[[1,2,3], [4,5,6], [7,8,9]],
[[10,20,30], [40,50,60], [70,80,90]],
[[100,200,300], [400,500,600], [700,800,900]],
])
T1
In [86]: T2 = np.array([
[[3,3,3] , [4,4,4] , [5,5,5]],
[[1,1,1] , [1,1,1] , [1,1,1]],
[[2,2,2] , [2,2,2] , [2,2,2]]
])
T2
[[1, 1, 1],
[1, 1, 1],
[1, 1, 1]],
[[2, 2, 2],
[2, 2, 2],
[2, 2, 2]]])
Tensor Addition
In [87]: A = T1+T2
A
In [88]: np.add(T1,T2)
Tensor Subtraction
In [89]: S = T1-T2
S
[[ 9, 19, 29],
[ 39, 49, 59],
[ 69, 79, 89]],
[[ 9, 19, 29],
[ 39, 49, 59],
[ 69, 79, 89]],
In [92]: np.multiply(T1,T2)
In [94]: np.divide(T1,T2)
[[1, 1, 1],
[1, 1, 1],
[1, 1, 1]],
[[2, 2, 2],
[2, 2, 2],
[2, 2, 2]]])
In [97]: np.tensordot(T1,T2)
Solving Equations
𝐴𝑋 = 𝐵
Solving Equations :
https://www.youtube.com/watch?v=NNmiOoWt86M (https://www.youtube.com/watch?
v=NNmiOoWt86M)
https://www.youtube.com/watch?v=a2z7sZ4MSqo (https://www.youtube.com/watch?
v=a2z7sZ4MSqo)
In [99]: B = np.random.random((3,1))
B
Out[99]: array([[0.17629069],
[0.28765383],
[0.68182386]])
In [100]: # Ist Method
X = np.dot(np.linalg.inv(A) , B)
X
Out[100]: array([[-1.27364894e+15],
[ 2.54729789e+15],
[-1.27364894e+15]])
Out[101]: array([[-1.27364894e+15],
[ 2.54729789e+15],
[-1.27364894e+15]])
Out[102]: array([[-1.27364894e+15],
[ 2.54729789e+15],
[-1.27364894e+15]])
Out[103]: array([[-1.27364894e+15],
[ 2.54729789e+15],
[-1.27364894e+15]])
https://www.youtube.com/watch?v=PFDu9oVAE-g&t=36s (https://www.youtube.com/watch?
v=PFDu9oVAE-g&t=36s)
https://www.youtube.com/watch?v=cdZnhQjJu4I&t=557s (https://www.youtube.com/watch?
v=cdZnhQjJu4I&t=557s)
In [104]: M = np.array([[4, 2], [7, 3]])
eigval, eigvec = np.linalg.eig(M)
print ("Matrix ==> \n" , M, "\n")
print(f"Eigen Value :- \n {eigval} \n")
print(f"Eigen Value :- \n {eigvec} \n")
Matrix ==>
[[4 2]
[7 3]]
Eigen Value :-
[ 7.27491722 -0.27491722]
Eigen Value :-
[[ 0.52119606 -0.42376194]
[ 0.85343697 0.9057736 ]]
https://www.youtube.com/watch?v=mBcLRGuAFUk (https://www.youtube.com/watch?
v=mBcLRGuAFUk)
https://www.youtube.com/watch?v=rYz83XPxiZo (https://www.youtube.com/watch?
v=rYz83XPxiZo)
In [105]: M = np.array([[1, 2], [3, 4], [5, 6]])
U, s, V = np.linalg.svd(L)
print(f"\n{U}\n")
print(f"\n{s}\n")
print(f"\n{V}\n")
QR Decomposition
QR Decomposition :
https://www.youtube.com/watch?v=J41Ypt6Mftc (https://www.youtube.com/watch?
v=J41Ypt6Mftc)
Q :-
[[-0.16903085 0.89708523]
[-0.50709255 0.27602622]
[-0.84515425 -0.34503278]]
R :-
[[-5.91607978 -7.43735744]
[ 0. 0.82807867]]
Moore-Penrose Pseudoinverse
Moore-Penrose Pseudoinverse :
https://www.youtube.com/watch?v=vXk-o3PVUdU (https://www.youtube.com/watch?v=vXk-
o3PVUdU)
[[-0.94444444 0.44444444]
[-0.11111111 0.11111111]
[ 0.72222222 -0.22222222]]
End