2023MT13122_Maths_Assignment_1
2023MT13122_Maths_Assignment_1
Name: RAJASEKAR PV
ID: 2023MT13122
The code is solved using the Eclipse IDE Editor using the NumPy Library.
row = 0
break
if pivot != 0:
for r in range(rows):
if r != row:
row += 1
return matrix
rref_matrix = rref(matrix.copy())
print("Intermediate Steps:")
print(matrix)
print(rref_matrix)
Output Screen Shot:
Code for Gauss Elimination:
import numpy as np
def gauss_jordan_inverse(matrix):
"""
Args:
Returns:
"""
n = matrix.shape[0]
# Forward Elimination
for i in range(n):
# Find pivot
if augmented_matrix[i, i] == 0:
if augmented_matrix[j, i] != 0:
augmented_matrix[[i, j]] = augmented_matrix[[j, i]] #swap
rows
break
return None
# Make pivot 1
pivot = augmented_matrix[i, i]
for j in range(n):
if i != j:
factor = augmented_matrix[j, i]
# Extract inverse
return inverse
[2, 2, 0, -2],
[1, 1, -2, 0],
gauss_jordan_inverse(matrix)
1. Initialization:
2. Forward Elimination:
4. Inverse Extraction:
o The code extracts the right half of the augmented matrix, which
now represents the inverse of the original matrix.
o The inverse is printed.
Console Output:
[[ 0. 1. -1. 1. 1. 0. 0. 0.]
[ 2. 2. 0. -2. 0. 1. 0. 0.]
[ 1. 1. -2. 0. 0. 0. 1. 0.]
[ 0. 1. 2. 0. 0. 0. 0. 1.]]
[[ 2. 2. 0. -2. 0. 1. 0. 0.]
[ 0. 1. -1. 1. 1. 0. 0. 0.]
[ 1. 1. -2. 0. 0. 0. 1. 0.]
[ 0. 1. 2. 0. 0. 0. 0. 1.]]
[[ 1. 1. 0. -1. 0. 0.5 0. 0. ]
[ 0. 1. -1. 1. 1. 0. 0. 0. ]
[ 1. 1. -2. 0. 0. 0. 1. 0. ]
[ 0. 1. 2. 0. 0. 0. 0. 1. ]]
[[ 1. 1. 0. -1. 0. 0.5 0. 0. ]
[ 0. 1. -1. 1. 1. 0. 0. 0. ]
[ 1. 1. -2. 0. 0. 0. 1. 0. ]
[ 0. 1. 2. 0. 0. 0. 0. 1. ]]
[[ 1. 1. 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[ 0. 0. 1. 0. -1. -0.5 1. 1. ]
[ 0. 0. 0. 1. -2. -1.5 3. 2. ]]
Inverse Matrix:
[[-4. -2. 5. 3. ]
[ 2. 1. -2. -1. ]
[-1. -0.5 1. 1. ]
[-2. -1.5 3. 2. ]]
Console Output:
Steps:
Coding:
import numpy as np
def check_basis_r3(vectors):
"""
Args:
"""
return False
matrix = np.column_stack(vectors)
print("Matrix formed from the vectors:\n", matrix)
determinant = np.linalg.det(matrix)
if determinant != 0:
return True
else:
return False
check_basis_r3(vectors)
Output:
[[ 0 3 1]
[ 1 0 1]
[-2 1 4]]
Console Output:
Coding:
import numpy as np
def find_matrix_rank(matrix):
"""
Finds the rank of a matrix using NumPy and prints each step.
Args:
"""
for i in range(rows):
# Find pivot
pivot_row = i
pivot_row += 1
if pivot_row == rows:
continue # Skip to next column
if pivot_row != i:
# Swap rows
pivot = reduced_matrix[i, i]
for j in range(rows):
if i != j:
factor = reduced_matrix[j, i]
rank = 0
for i in range(rows):
rank += 1
return rank
[3, 6, 2, 5, 1],
find_matrix_rank(matrix_a)
Output:
Original Matrix:
[[ 1 2 0 1 1]
[ 2 4 1 3 0]
[ 3 6 2 5 1]
[-4 -8 1 -3 1]]
[[ 1. 2. 0. 1. 1.]
[ 2. 4. 1. 3. 0.]
[ 3. 6. 2. 5. 1.]
[[ 1. 2. 0. 1. 1.]
[ 0. 0. 1. 1. -2.]
[ 3. 6. 2. 5. 1.]
[[ 1. 2. 0. 1. 1.]
[ 0. 0. 1. 1. -2.]
[ 0. 0. 2. 2. -2.]
[ 0. 0. 1. 1. -2.]
[ 0. 0. 2. 2. -2.]
[ 0. 0. 1. 1. 5.]]
[[ 1. 2. 0. 1. 1.]
[ 0. 0. 1. 1. -2.]
[ 0. 0. 1. 1. -1.]
[ 0. 0. 1. 1. 5.]]
[[ 1. 2. 0. 1. 1.]
[ 0. 0. 1. 1. -2.]
[ 0. 0. 1. 1. -1.]
[ 0. 0. 1. 1. 5.]]
[[ 1. 2. 0. 1. 1.]
[ 0. 0. 0. 0. -1.]
[ 0. 0. 1. 1. -1.]
[ 0. 0. 1. 1. 5.]]
[[ 1. 2. 0. 1. 1.]
[ 0. 0. 0. 0. -1.]
[ 0. 0. 1. 1. -1.]
[ 0. 0. 0. 0. 6.]]
[[ 1. 2. 0. 1. 1.]
[ 0. 0. 0. 0. -1.]
[ 0. 0. 1. 1. -1.]
[ 0. 0. 0. 0. 6.]]
compute_svd function:
Compute SVD:
Reconstruction (Verification):
import numpy as np
def compute_svd(matrix):
"""
Args:
"""
# Compute SVD
U, S, V_T = np.linalg.svd(matrix)
Sigma = np.zeros(matrix.shape)
np.fill_diagonal(Sigma, S)
[1, 2, 2, -1]])
compute_svd(matrix_a)
Output:
Original Matrix:
[[ 3 1 1 0]
[ 1 2 2 -1]]
[[-0.73186305 -0.68145174]
[-0.68145174 0.73186305]]
[4.18543121 1.86605616]