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

Week 4

The document contains Python code for three different matrix operations: creating a random matrix, performing addition, subtraction, and multiplication of two matrices, and solving a system of linear equations using matrix inversion. It utilizes the NumPy library for matrix manipulations and provides user prompts for input and operation selection. Each section includes checks for matrix dimensions to ensure valid operations.
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 views3 pages

Week 4

The document contains Python code for three different matrix operations: creating a random matrix, performing addition, subtraction, and multiplication of two matrices, and solving a system of linear equations using matrix inversion. It utilizes the NumPy library for matrix manipulations and provides user prompts for input and operation selection. Each section includes checks for matrix dimensions to ensure valid operations.
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
You are on page 1/ 3

1 # Create a matrix of order m x n with random integers in the range 1 to 99999

2
3 import numpy as np
4
5 def create_random_matrix(m, n):
6 matrix = np.random.randint(1, 100000, size=(m, n))
7 return matrix
8
9 # Example usage:
10 m = int(input("Enter number of rows: "))
11 n = int(input("Enter number of columns: "))
12
13 random_matrix = create_random_matrix(m, n)
14 print("Random Matrix of order", m, "x", n)
15 print(random_matrix)
16
1 #Write a program that adds, subtracts and multiplies two matrices.
2 #Provide an interface such that,based on the prompt,
3 #the function (addition, subtraction, multiplication) should be performed
4
5 # importing numpy as np
6 import numpy as np
7
8
9 # creating first matrix
10 A = np.array([[1, 2], [3, 4]])
11
12 print("Size of A: ",A.shape)
13
14 # creating second matrix
15 B = np.array([[4, 5], [6, 7]])
16
17 print("Size of B: ",B.shape)
18
19 print("Printing elements of first matrix")
20 print(A)
21 print("Printing elements of second matrix")
22 print(B)
23
24 # Provide options for operations
25 print("\nChoose an operation:")
26 print("1. Addition")
27 print("2. Subtraction")
28 print("3. Multiplication")
29
30 choice = input("Enter your choice (1/2/3): ")
31
32 if choice == '1':
33 # adding two matrix
34 print("Addition of two matrix")
35 if A.shape == B.shape:
36 print(np.add(A, B))
37 else:
38 print("Both Matrices should be of same dimensions")
39 elif choice == '2':
40 # subtracting two matrix
41 print("Subtraction of two matrix")
42 if A.shape == B.shape:
43 print(np.subtract(A, B))
44 else:
45 print("Both Matrices should be of same dimensions")
46 elif choice == '3':
47 # multiplying two matrix
48 print("Multiplication of two matrix")
49 if A.shape[1] == B.shape[0]:
50 print(np.dot(A, B))
51 else:
52 print("The number of columns in the first matrix must equal the number of rows
in the second matrix for multiplication.")
53 else:
54 print("Invalid choice. Please select 1, 2, or 3.")
55
1 # Write a program to solve a system of n linear equations in n variables using matrix
inverse.
2
3 #import required modules
4 import numpy as np
5
6 #display the system of linear equations
7 print("The system of equations are:")
8 print('x + 2y = 4')
9 print ('AND')
10 print('3x − 5y = 1')
11
12 #form the matrices from the equations
13 A = np.array ([[1,2],[3,-5]])
14 B = np.array([[4],[1]])
15
16 if A.shape[0] != A.shape[1]:
17 print("Matrix A must be square (n x n).")
18 if A.shape[1] != B.shape[0]:
19 print("Matrix A and vector b dimensions must match.")
20
21 A_inv = np.linalg.inv(A)
22
23 x = np.dot(A_inv, B)
24
25 print("Solution: ",x)
26

You might also like