# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

#importing numpy package
import numpy as np

A = np.array([[3,4],[2,1]])
B = np.array([[1,5],[3,7]])
print('A:\n',A)
print('B:\n',B)

#Addition of matrix
A + B

np.add(A,B)

#Subtraction of matrix
A - B

np.subtract(A,B)

# Scalar addition
print(A)
A + 2

np.add(A,2)

A - 2

np.subtract(A,2)

# Scalar Multiplication
A * 2

np.multiply(A, 2)

#This is not matrix multiplication
print('A:\n',A)
print('B:\n',B)
np.multiply(A,B)

A * B #This is not matrix multiplication

#Matrix Multiplication
np.matmul(A,B) #This is matrix matrix multiplication

# ### Real time example on Matrix Multiplication
# The local shop sells 3 types of fruits. <br/>
# Apple cost Rs.3 each<br/>
# Cherry cost Rs.4 each<br/>
# Blueberry cost Rs.2 each<br/><br/>
#
# ![image.png](attachment:image.png)

# Matrix Multiplication example
C = np.array([[3,4,2]])
D = np.array([[13,9,7,15],[8,7,4,6],[6,4,0,3]])
print('C:\n',C, 'Shape: ',C.shape)
print('D:\n',D, 'Shape: ',D.shape)
np.matmul(C,D)

#Matrix Multiplication is not commutative EF != FE
E = np.array([[1,1],[100,100]])
F = np.array([[-1,1],[1,-1]])
np.matmul(E,F)

np.matmul(F,E)

#Matrix transpose
print('A:\n',A)
print('A Transpose:\n',A.T)

np.transpose(A)

np.transpose(np.transpose(A)) # Two times transpose gives the actual matrix

# ### Transposition rule
# ##### (AB)T = BT x AT
# Here T stands for Transpose

print('A:\n',A)
print('B:\n',B)
NewMatrix = np.matmul(A,B)
np.transpose(NewMatrix)

np.matmul(np.transpose(B), np.transpose(A)) #This satisfies the property

np.matmul(np.transpose(A), np.transpose(B)) #As per rule this is incorrect

# ### Special Matrices

#Symmetric
sMatrix = np.array([[1,1,-1],[1,2,0],[-1,0,5]])
sMatrix

sMatrix.T #Matrix transpose gives us the same matrix as it is a symmetric matrix. aij = aji

# Skew Symmetric Matrix
ssMatrix = np.array([[0,1,-2],[-1,0,3],[2,-3,0]])
ssMatrix

ssMatrix.T #Matrix transpose gives us the same negative matrix as it is skew symmetric matrix. aij = -aji

#Diagonal Matrix
dMatrix = np.array([[12,0,0],[0,18,0],[0,0,26]])
dMatrix

#Convert a vector to diagonal matrix
a = np.array([12,18,26])
np.diag(a)

#Finding Determinant of a matrix
mat = np.array([[3,1],[1,2]])
mat

np.linalg.det(mat)

mat2 = np.array([[2,-3,1],[2,0,-1],[1,4,5]])
sol = np.linalg.det(mat2)
sol

np.around(sol) #Round the array elements

#Identity Matrix
imatrix = np.identity(3)
imatrix

mat2

np.matmul(mat2,imatrix)

np.matmul(mat2,imatrix).astype(int)

# #### Inverse of a matrix

A

A_inv = np.linalg.inv(A)

A_inv

# A X A_inv = I
np.matmul(A,A_inv)

# #### Solving Linear System of equations
# x + y + z = 3 <br/>
# 2x + 3y + 7z = 0 <br/>
# x + 3y - 2z = 17 <br/>

A = np.array([[1,1,1],[2,3,7],[1,3,-2]])
b = np.array([3,0,17])
np.linalg.solve(A,b) # Pass the augmented matrix

# Rank of a matrix
np.linalg.matrix_rank(A)

# #### EigenValues and EigenVectors

A

w, v = np.linalg.eig(A) # w - eigenvalue, v - eigenvectors

w

v

StarcoderdataJupyterScriptsDedupFiltered