Program to Find Normal and Trace of a Matrix in Python using For loop
ADVERTISEMENTS
Program to find normal and trace of a matrix in python using for loop. In this article, you will learn how to make program to find normal and trace of a matrix in python using for loop.
What is the Normal of a square matrix?
The square root of the sum of the squares of each element of the matrix.
What is the Trace of a square matrix?
The sum of the diagonal elements of the square matrix.
Source Code
# Program to Find Normal and Trace of a Matrix in Python using For loop
import math
# to call math.sqrt function
i, j, n, sum, sum1, a, normal, x = 0, 0, 0, 0, 0, 0, 0, []
print ("Enter the number of rows[columns] of the matrix::")
n = int (input ())
print ("\n----Enter the ", n * n, " elements of the first matrix----\n")
for i in range (n):
x.append([])
for j in range (n):
x[i].append(int (input ()))
a = x[i][j] * x[i][j]
sum1 += a
normal = math.sqrt(sum1)
print ("\n\nThe normal of the given matrix is: ", normal)
for i in range (n):
sum = sum + x[i][i]
print ("\n\nThe Trace of the given matrix is: ", sum)
Output
Enter the number of rows[columns] of the matrix::
3
----Enter the 9 elements of the first matrix----
43
54
4
3
54
23
54
23
5
The normal of the given matrix is: 108.18964830333815
The Trace of the given matrix is: 102