21bei042 Prac05
21bei042 Prac05
Name-Meena Rangrej
Roll no -21BEI042
EXPERIMENT – 05
1. As you know, a magic square is a matrix all of whose row sums, column sums and
the sums of the two diagonals are the same. (One diagonal of a matrix goes from the
top left to the bottom right, the other diagonal goes from top right to bottom left.)
Show by direct computation that if the matrix A is given by
The matrix A has 5 row sums (one for each row), 5 column sums (one for each column) and
two diagonal sums. These 12 sums should all be exactly the same, and you could verify
that they are the same by printing them and “seeing” that they are the same. It is easy to
miss small differences among so many numbers, though. Instead, verify that A is a magic
square by constructing the 5 column sums and computing the maximum and minimum
values of the column sums. Do the same for the 5 row sums, and compute the two diagonal
sums. Check that these six values are the same. If the maximum and minimum values are
the same, the flyswatter principle says that all values are the same.
CODE:
import numpy as np
def is_magic_square(mat):
n = len(mat)
sumd1 = sum(mat[i][i] for i in range(n))
sumd2 = sum(mat[i][n-1-i] for i in range(n))
if sumd1 != sumd2:
return False
for i in range(n):
row_sum = sum(mat[i][j] for j in range(n))
print("sum of the row",row_sum)
col_sum = sum(mat[j][i] for j in range(n))
print("sum of column",col_sum)
if row_sum != col_sum or col_sum != sumd1:
return False
return True
# Given matrix A
A = np.array([[17, 24, 1, 8, 15],
[23, 5, 7, 14, 16],
[4, 6, 13, 20, 22],
[10, 12, 19, 21, 3],
[11, 18, 25, 2, 9]])
if is_magic_square(A):
print("given matrix is Magic Square")
else:
print("Not a magic Square")
OUTPUT:
import numpy as np
def numpy_calculator():
print("Simple Scientific Calculator")
print("Supported Operations:")
print("1. Addition (+)")
print("2. Subtraction (-)")
print("3. Multiplication (*)")
print("4. Division (/)")
print("5. Exponentiation (^)")
print("6. Sine (sin)")
print("7. Cosine (cos)")
print("8. Tangent (tan)")
operation_str=expression.split(' ')
if len(operation_str)>=2:
if '+' in operation_str:
num1 = int(operation_str[0])
num2 = int(operation_str[2])
result = np.add(num1, num2)
elif '-' in operation_str:
num1 = int(operation_str[0])
num2 = int(operation_str[2])
result = np.subtract(num1, num2)
elif '*' in operation_str:
num1 = int(operation_str[0])
num2 = int(operation_str[2])
result = np.multiply(num1, num2)
elif '/' in operation_str:
num1 = int(operation_str[0])
num2 = int(operation_str[2])
if num2 == 0:
return print("Error: Cannot divide by zero.")
result = np.divide(num1, num2)
elif '^' in operation_str:
num1 = int(operation_str[0])
num2 = int(operation_str[2])
result = np.power(num1, num2)
elif 'sin' in operation_str[0].lower():
num1 = int(operation_str[1])
result = np.sin(np.radians(num1))
elif 'cos' in operation_str[0].lower():
num1 = int(operation_str[1])
result = np.cos(np.radians(num1))
elif 'tan' in operation_str[0].lower():
num1 = int(operation_str[1])
result = np.tan(np.radians(num1))
else:
print("Invalid operation. Please enter a valid operation.")
return
print("Result:", result)
else:
print("Invalid expression. Please enter a valid expression.")
return
OUTPUT:
OUTPUT:
CONCLUSION:
In this Practical, we have learnt about the implementation of numpy in 2 problem statement
whether it is a magic square or not , and the second one created a scientific calculator using
numpy API.