0% found this document useful (0 votes)
2 views4 pages

Python Assignment6.Ipynb - Colab

The document provides a series of Python code snippets that demonstrate various matrix operations using NumPy, including creating matrices, performing addition, subtraction, multiplication, finding determinants, inverses, and solving systems of linear equations. It also highlights the conditions under which certain operations can be performed, such as the necessity of compatible dimensions for multiplication and the existence of an inverse based on the determinant. The final outputs of each operation are shown, illustrating the results of these matrix manipulations.
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)
2 views4 pages

Python Assignment6.Ipynb - Colab

The document provides a series of Python code snippets that demonstrate various matrix operations using NumPy, including creating matrices, performing addition, subtraction, multiplication, finding determinants, inverses, and solving systems of linear equations. It also highlights the conditions under which certain operations can be performed, such as the necessity of compatible dimensions for multiplication and the existence of an inverse based on the determinant. The final outputs of each operation are shown, illustrating the results of these matrix manipulations.
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/ 4

1 .

Create a 3x3 matrix A with elements from 1 to 9

import numpy as np
A = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print("A = \n", A)

A =
[[1 2 3]
[4 5 6]
[7 8 9]]

2 . Create a 3x3 identity matrix I

import numpy as np
I = np.eye(3)
print("I = \n", I)

I =
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]

3 . Add matrix A and I

import numpy as np
A = np.array([[11, 2, 3], [0, 1, 4], [3, 8, 9]])
I = np.eye(3)
print("A + I = \n", A + I)

A + I =
[[12. 2. 3.]
[ 0. 2. 4.]
[ 3. 8. 10.]]

4 . Subtract matrix I from A

import numpy as np
A = np.array([[4, 2, 3], [4, 5, 6], [4, 2, 9]])
I = np.eye(3)
print("A - I = \n", A - I)

A - I =
[[3. 2. 3.]
[4. 4. 6.]
[4. 2. 8.]]

5 . Multiply matrix A and I.

import numpy as np
c = np.dot(A,I)
c = A*I
print("Matrix c (A*I)\n",c)

Matrix c (A*I)
[[4. 0. 0.]
[0. 5. 0.]
[0. 0. 9.]]

6 . Find the determinent of matrix A

import numpy as np
print("Determinant of A = ", np.linalg.det(A))

Determinant of A = 72.0

7 . Find the inverse of a matrix A (if it exist)

if det of matrix A is zero, so inverse of A does not exist.

import numpy as np
A = np.array([[1, 9, 6], [4, 1, 6], [9, 8, 9]])
if np.linalg.det(A) != 0:
inv_A = np.linalg.inv(A)
print("Inverse of A = \n", inv_A)
else:
print("Inverse of A does not exist because its determinant is zero.")

Inverse of A =
[[-0.14942529 -0.12643678 0.18390805]
[ 0.06896552 -0.17241379 0.06896552]
[ 0.08812261 0.27969349 -0.13409962]]

8 . Create a 2x2 matrix B with elements [2, 4; 6, 8].

import numpy as np
B = np.array([[2, 4], [6, 8]])
print("B = \n", B)

B =
[[2 4]
[6 8]]

9 . Multiply matrix A by matrix B (if possible).

It is not possible, because matrix A has 3 column and matrix B has 2 row

import numpy as np
A = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
B = np.array([[23, 14], [6, 86]])
try:
C = np.dot(A, B)
print("C = \n", C)
except ValueError:
print("Matrix multiplication not possible.")

Matrix multiplication not possible.

10 . Solve the system of linear equations Ax = b where b = [1,2,3]

import numpy as np
A = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
b = np.array([1, 2, 3])
try:
x = np.linalg.solve(A, b)
print("Solution x:", x)
except np.linalg.LinAlgError:
print("No solution exists.")

No solution exists.

11 . Create a 4x4 matrix C with random integers between 1 and 10.

import numpy as np
C = np.array([[1,1,2 ,2 ], [1,1 ,1 ,5 ], [1,1 ,1 ,6], [2,1 ,8 ,1]])
print("C = \n", C)

C =
[[1 1 2 2]
[1 1 1 5]
[1 1 1 6]
[2 1 8 1]]

12 . Find the determinant of matrix C.

import numpy as np
det_C = np.linalg.det(C)
print("Determinant of C = ", det_C)

Determinant of C = 1.0

13 . Find the inverse of matrix C (if it exist).

import numpy as np
inv_C = np.linalg.inv(C)
print("Inverse of C = \n", inv_C)

Inverse of C =
[[ -7. 23. -17. 1.]
[ 6. -13. 9. -1.]
[ 1. -4. 3. 0.]
[ 0. -1. 1. 0.]]

14 . Multiply matrix C by its inverse and verify if the result is an identity matrix.
import numpy as np
C = np.array([[1, 1, 2, 2], [1, 1, 1, 5], [1, 1, 1, 6], [2, 1, 8, 1]])
inv_C = np.linalg.inv(C)
C_inv_C = np.dot(C, inv_C)
print("C * inv_C = \n", C_inv_C)

C * inv_C =
[[1. 0. 0. 0.]
[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]

15 . Solve the system of linear equations Cx = d where d = [4,5,6,7].

import numpy as np
C = np.array([[1, 1, 2, 2], [1, 1, 1, 5], [1, 1, 1, 6], [2, 1, 8, 1]])
d = np.array([4, 5, 6, 7])
x = np.linalg.solve(C, d)
print("Solution x:", x)

Solution x: [-8. 6. 2. 1.]

You might also like