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

1

Uploaded by

jondhaleom7
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)
16 views3 pages

1

Uploaded by

jondhaleom7
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

EXP No:01 Matrix

//code :

def create_matrix(rows, cols):


matrix = []
for i in range(rows):
row = []
for j in range(cols):
element = int(input(f"Enter element at row {i+1}, col {j+1}: "))
row.append(element)
matrix.append(row)
return matrix

def add_matrices(matrix1, matrix2):


if len(matrix1) != len(matrix2) or len(matrix1[0]) != len(matrix2[0]):
print("Matrices must have the same dimensions for addition.")
return None
result = [[0 for _ in range(len(matrix1[0]))] for _ in range(len(matrix1))]
for i in range(len(matrix1)):
for j in range(len(matrix1[0])):
result[i][j] = matrix1[i][j] + matrix2[i][j]
return result

def subtract_matrices(matrix1, matrix2):


if len(matrix1) != len(matrix2) or len(matrix1[0]) != len(matrix2[0]):
print("Matrices must have the same dimensions for subtraction.")
return None
result = [[0 for _ in range(len(matrix1[0]))] for _ in range(len(matrix1))]
for i in range(len(matrix1)):
for j in range(len(matrix1[0])):
result[i][j] = matrix1[i][j] - matrix2[i][j]
return result

def multiply_matrices(matrix1, matrix2):


if len(matrix1[0]) != len(matrix2):
print("Incompatible dimensions for matrix multiplication.")
return None
result = [[0 for _ in range(len(matrix2[0]))] for _ in range(len(matrix1))]
for i in range(len(matrix1)):
for j in range(len(matrix2[0])):
for k in range(len(matrix2)):
result[i][j] += matrix1[i][k] * matrix2[k][j]
return result

def transpose_matrix(matrix):
result = [[0 for _ in range(len(matrix))] for _ in range(len(matrix[0]))]
for i in range(len(matrix)):
for j in range(len(matrix[0])):
result[j][i] = matrix[i][j]
return result

def is_upper_triangular(matrix):
for i in range(1, len(matrix)):
for j in range(i):
if matrix[i][j] != 0:
return False
return True

def sum_diagonal_elements(matrix):
sum = 0
for i in range(len(matrix)):
sum += matrix[i][i]
return sum

def print_matrix(matrix):
for row in matrix:
for element in row:
print(f"{element:4}", end="")
print()

if __name__ == "__main__":
rows1 = int(input("Enter the number of rows for matrix 1: "))
cols1 = int(input("Enter the number of columns for matrix 1: "))
matrix1 = create_matrix(rows1, cols1)

rows2 = int(input("Enter the number of rows for matrix 2: "))


cols2 = int(input("Enter the number of columns for matrix 2: "))
matrix2 = create_matrix(rows2, cols2)

print("\nMatrix 1:")
print_matrix(matrix1)

print("\nMatrix 2:")
print_matrix(matrix2)

print("\nAddition of matrices:")
result_add = add_matrices(matrix1, matrix2)
if result_add:
print_matrix(result_add)

print("\nSubtraction of matrices:")
result_sub = subtract_matrices(matrix1, matrix2)
if result_sub:
print_matrix(result_sub)

print("\nMultiplication of matrices:")
result_mul = multiply_matrices(matrix1, matrix2)
if result_mul:
print_matrix(result_mul)

print("\nTranspose of matrix 1:")


result_transpose = transpose_matrix(matrix1)
print_matrix(result_transpose)

print("\nIs matrix 1 upper triangular?", is_upper_triangular(matrix1))

print("\nSum of diagonal elements of matrix 1:", sum_diagonal_elements(matrix1))


//OUTPUT:>

You might also like