0% found this document useful (0 votes)
4 views8 pages

Python Assignment 2

The document provides a comprehensive guide on various matrix operations using NumPy in Python, including creating matrices, calculating sums, eigenvalues, and modifying elements. It also covers special matrices like identity and diagonal matrices, random matrix operations, and array manipulations. Additionally, it checks for solutions to a system of equations using matrix rank and solving techniques.

Uploaded by

hajerakhatunth
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views8 pages

Python Assignment 2

The document provides a comprehensive guide on various matrix operations using NumPy in Python, including creating matrices, calculating sums, eigenvalues, and modifying elements. It also covers special matrices like identity and diagonal matrices, random matrix operations, and array manipulations. Additionally, it checks for solutions to a system of equations using matrix rank and solving techniques.

Uploaded by

hajerakhatunth
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

# 1.

Matrix Operations
import numpy as np

# (a) Create a square matrix A of order 4


A = [Link](1, 17).reshape(4, 4)
print("Matrix A:\n", A)
''' Output :
Matrix A:
[[ 1 2 3 4]
[ 5 6 7 8]
[ 9 10 11 12]
[13 14 15 16]]'''

# (b) Number of rows and columns


rows, cols = [Link]
print("Rows:", rows, "Columns:", cols)

# (c) Row sums and column sums


print("Row sums:", [Link](axis=1))
print("Column sums:", [Link](axis=0))
'''Output :
Rows: 4 Columns: 4
Row sums: [10 26 42 58]
Column sums: [28 32 36 40]
'''
# (d) Trace of A
print("Trace:", [Link](A))
'''Output : Trace: 34'''
# (e) Minimum and maximum element
print("Min:", [Link](), "Max:", [Link]())
# Output : Min: 1 Max: 16
# (f) Eigenvalues and eigenvectors
eigvals, eigvecs = [Link](A)
print("Eigenvalues:", eigvals)
print("Eigenvectors:\n", eigvecs)
'''Output :
Eigenvalues: [ 3.62093727e+01 -2.20937271e+00 -2.62410379e-15
-9.51420961e-17]
Eigenvectors:
[[-0.15115432 -0.72704996 -0.47688035 -0.07702174]
[-0.34923733 -0.28320876 0.83665151 -0.30149603]
[-0.54732033 0.16063243 -0.24266197 0.83405728]
[-0.74540333 0.60447363 -0.11710919 -0.45553951]]'''
# (g) Replace (2,3) element with 100 and 2nd row with ones
A[1, 2] = 100
A[1, :] = [Link](cols)
print("Modified A:\n", A)
''' output :
Modified A:
[[ 1 2 3 4]
[ 1 1 1 1]
[ 9 10 11 12]
[13 14 15 16]]
'''
# (h) Add 2 to each element → B
B = A + 2
print("Matrix B:\n", B)
print("A + B:\n", A + B)
print("A * B:\n", A @ B) # matrix product
''' Output :
Matrix B:
[[ 3 4 5 6]
[ 3 3 3 3]
[11 12 13 14]
[15 16 17 18]]
A + B:
[[ 4 6 8 10]
[ 4 4 4 4]
[20 22 24 26]
[28 30 32 34]]
A * B:
[ 4 4 4 4]
[20 22 24 26]
[28 30 32 34]]
[ 4 4 4 4]
[20 22 24 26]
[ 4 4 4 4]
[ 4 4 4 4]
[20 22 24 26]
[ 4 4 4 4]
[20 22 24 26]
[ 4 4 4 4]
[ 4 4 4 4]
[ 4 4 4 4]
[ 4 4 4 4]
[20 22 24 26]
[28 30 32 34]]
A * B:
[[102 110 118 126]
[ 32 35 38 41]
[358 390 422 454]
[486 530 574 618]]
'''
# (i) Create C by eliminating 3rd, 4th rows & cols
C = A[:2, :2]
print("Matrix C:\n", C)
det_C = [Link](C)
print("Determinant of C:", det_C)
if det_C != 0:
print("C is nonsingular. Inverse:\n", [Link](C))
else:
print("C is singular.")
'''Output :
Matrix C:
[[1 2]
[1 1]]
Determinant of C: -1.0
C is nonsingular. Inverse:
[[-1. 2.]
[ 1. -1.]]
'''
# (j) Reshape A to 2x8
A_reshaped = [Link](2, 8)
print("Reshaped A:\n", A_reshaped)
'''Output :
Reshaped A:
[[ 1 2 3 4 1 1 1 1]
[ 9 10 11 12 13 14 15 16]]
'''
# 2 Special Matrices

# (a) Identity matrix of order 5


I = [Link](5)
print("Identity:\n", I)
'''Output :
Identity:
[[1. 0. 0. 0. 0.]
[0. 1. 0. 0. 0.]
[0. 0. 1. 0. 0.]
[0. 0. 0. 1. 0.]
[0. 0. 0. 0. 1.]]
'''
# (b) Upper triangular matrix of order 4 with 3s
U = [Link]([Link]((4,4)) * 3)
print("Upper triangular:\n", U)
'''Output :
Upper triangular:
[[3. 3. 3. 3.]
[0. 3. 3. 3.]
[0. 0. 3. 3.]
[0. 0. 0. 3.]]'''
# (c) Diagonal matrix
diag_elements = [2,5,6,3,5,1,4,5,6,8]
D = [Link](diag_elements)
print("Diagonal matrix:\n", D)
'''Output :
Diagonal matrix:
[[2 0 0 0 0 0 0 0 0 0]
[0 5 0 0 0 0 0 0 0 0]
[0 0 6 0 0 0 0 0 0 0]
[0 0 0 3 0 0 0 0 0 0]
[0 0 0 0 5 0 0 0 0 0]
[0 0 0 0 0 1 0 0 0 0]
[0 0 0 0 0 0 4 0 0 0]
[0 0 0 0 0 0 0 5 0 0]
[0 0 0 0 0 0 0 0 6 0]
[0 0 0 0 0 0 0 0 0 8]]
'''
# (d) 4x5 matrix of ones
M = [Link]((4,5))
print("Matrix of ones:\n", M)

'''Output :
Matrix of ones:
[[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]
[1. 1. 1. 1. 1.]]
'''
# 3 Random Matrix Operation
# (a) 5x5 matrix with random integers 1–100
R = [Link](1, 101, (5,5))
print("Random matrix:\n", R)
'''Output :
Random matrix:
[[57 75 48 83 17]
[44 77 20 7 6]
[74 41 59 93 49]
[83 89 3 14 56]
[99 78 80 35 35]]
'''
# (b) Average of all elements
mean_val = [Link]()
print("Mean:", mean_val)
# Output : Mean: 52.88
# (c) Subtract mean from all elements
R_centered = R - mean_val
print("Centered matrix:\n", R_centered)
'''Output :
Centered matrix:
[[ 4.12 22.12 -4.88 30.12 -35.88]
[ -8.88 24.12 -32.88 -45.88 -46.88]
[ 21.12 -11.88 6.12 40.12 -3.88]
[ 30.12 36.12 -49.88 -38.88 3.12]
[ 46.12 25.12 27.12 -17.88 -17.88]]
'''
# (d) Replace negatives with 0
R_nonneg = [Link](R_centered < 0, 0, R_centered)
print("Non-negative matrix:\n", R_nonneg)
'''Output :
Non-negative matrix:
[[ 4.12 22.12 0. 30.12 0. ]
[ 0. 24.12 0. 0. 0. ]
[21.12 0. 6.12 40.12 0. ]
[30.12 36.12 0. 0. 3.12]
[46.12 25.12 27.12 0. 0. ]]
'''
# 4 Array Manipulation
# (a) Array from 0.5 to 50 step 0.5
arr = [Link](0.5, 50.5, 0.5)
print("Array:\n", arr)
'''Output :
Array:
[ 0.5 1. 1.5 2. 2.5 3. 3.5 4. 4.5 5. 5.5 6. 6.5 7.
7.5 8. 8.5 9. 9.5 10. 10.5 11. 11.5 12. 12.5 13. 13.5 14.
14.5 15. 15.5 16. 16.5 17. 17.5 18. 18.5 19. 19.5 20. 20.5 21.
21.5 22. 22.5 23. 23.5 24. 24.5 25. 25.5 26. 26.5 27. 27.5 28.
28.5 29. 29.5 30. 30.5 31. 31.5 32. 32.5 33. 33.5 34. 34.5 35.
35.5 36. 36.5 37. 37.5 38. 38.5 39. 39.5 40. 40.5 41. 41.5 42.
42.5 43. 43.5 44. 44.5 45. 45.5 46. 46.5 47. 47.5 48. 48.5 49.
49.5 50. ]'''
# (b) Arrange into square matrix
n = int([Link](len(arr)))
sq_matrix = arr[:n*n].reshape(n, n)
print("Square matrix:\n", sq_matrix)
'''Output :
Square matrix:
[[ 0.5 1. 1.5 2. 2.5 3. 3.5 4. 4.5 5. ]
[ 5.5 6. 6.5 7. 7.5 8. 8.5 9. 9.5 10. ]
[10.5 11. 11.5 12. 12.5 13. 13.5 14. 14.5 15. ]
[15.5 16. 16.5 17. 17.5 18. 18.5 19. 19.5 20. ]
[20.5 21. 21.5 22. 22.5 23. 23.5 24. 24.5 25. ]
[25.5 26. 26.5 27. 27.5 28. 28.5 29. 29.5 30. ]
[30.5 31. 31.5 32. 32.5 33. 33.5 34. 34.5 35. ]
[35.5 36. 36.5 37. 37.5 38. 38.5 39. 39.5 40. ]
[40.5 41. 41.5 42. 42.5 43. 43.5 44. 44.5 45. ]
[45.5 46. 46.5 47. 47.5 48. 48.5 49. 49.5 50. ]]
'''
# (c) Any 4x4 sub-matrix (top-left corner)
sub1 = sq_matrix[:4, :4]
print("4x4 sub-matrix:\n", sub1)
'''Output :
4x4 sub-matrix:
[[ 0.5 1. 1.5 2. ]
[ 5.5 6. 6.5 7. ]
[10.5 11. 11.5 12. ]
[15.5 16. 16.5 17. ]]
'''
# (d) Sub-matrix with even rows & odd columns
sub2 = sq_matrix[1::2, ::2] # even rows (index 1,3,..), odd cols (index
0,2,..)
print("Even-row, odd-col sub-matrix:\n", sub2)
'''output :
Even-row, odd-col sub-matrix:
[[ 5.5 6.5 7.5 8.5 9.5]
[15.5 16.5 17.5 18.5 19.5]
[25.5 26.5 27.5 28.5 29.5]
[35.5 36.5 37.5 38.5 39.5]
[45.5 46.5 47.5 48.5 49.5]]

'''
# 5 Check for soln.
# Coefficient matrix
A_sys = [Link]([[3, 2, 1],[2, 9, 11],[1, 1, 0]])

# Right-hand side vector


b_sys = [Link]([10, 2, 1])

# Check ranks
rank_A = [Link].matrix_rank(A_sys)
rank_aug = [Link].matrix_rank(np.column_stack((A_sys, b_sys)))

if rank_A == rank_aug:
sol = [Link](A_sys, b_sys)
print("System has a solution:")
print("x1 =", sol[0], "x2 =", sol[1], "x3 =", sol[2])
else:
print("No solution exists.")
'''
Output :
System has a solution:
x1 = 5.277777777777778 ; x2 = -4.277777777777777 ; x3 = 2.7222222222222214

'''

Name : Hajera Khatun


Roll No. : MAT244021

You might also like