{"id":20290,"date":"2021-09-09T19:57:45","date_gmt":"2021-09-09T14:27:45","guid":{"rendered":"https:\/\/python-programs.com\/?p=20290"},"modified":"2021-11-22T18:36:23","modified_gmt":"2021-11-22T13:06:23","slug":"python-program-to-find-the-normal-and-trace-of-a-matrix","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-the-normal-and-trace-of-a-matrix\/","title":{"rendered":"Python Program to find the Normal and Trace of a Matrix"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Print Odd Numbers in Given Range Using Recursion<\/a><\/p>\n Given a matrix and the task is to find the normal and trace of the given Matrix in Python.<\/p>\n What is a matrix:<\/strong><\/p>\n A matrix is a rectangular sequence of numbers divided into columns and rows. A matrix element or entry is a number that appears in a matrix.<\/p>\n Example:<\/strong><\/p>\n <\/p>\n Above is the matrix which contains 5 rows and 4 columns and having elements from 1 to 20.<\/p>\n In this order, the dimensions of a matrix indicate the number of rows and columns.<\/p>\n Here as there are 5 rows and 4 columns it is called a 5*4 matrix.<\/p>\n Normal of a matrix:<\/strong><\/p>\n The sum of squares is normal for all the matrix entries.<\/p>\n Trace of a matrix:<\/strong><\/p>\n Trace is the sum of the Matrix’s diagonal parts.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to find the normal and trace of the given Matrix in Python:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Dive into numerous Python Programming Language Examples<\/a> for practice and get the best out of the tutorial and learn python one step at a time.<\/p>\n In the previous article, we have discussed Python Program to Print Odd Numbers in Given Range Using Recursion Given a matrix and the task is to find the normal and trace of the given Matrix in Python. What is a matrix: A matrix is a rectangular sequence of numbers divided into columns and rows. A …<\/p>\nGiven Matix :\r\n2 6 4 \r\n8 5 3 \r\n1 6 8<\/pre>\n
The trace value of the given matrix is : 15\r\nThe normal value of the given matrix is : 6.557438524302<\/pre>\n
Given Matix : \r\n1 2 3 4\r\n7 5 8 6\r\n9 4 2 5\r\n1 1 1 1<\/pre>\n
The trace value of the given matrix is : 9\r\nThe normal value of the given matrix is : 7.745966692414834<\/pre>\n
Program to find the Normal and Trace of a Matrix in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Import the math module using the import keyword.\r\nimport math\r\n# Give the matrix as static input and store it in a variable.\r\nmtrx = [[2, 6, 4], [8, 5, 3], [1, 6, 8]]\r\n# Calculate the number of rows of the given matrix by\r\n# calculating the length of the nested list using the len() function\r\n# and store it in a variable mtrxrows.\r\nmtrxrows = len(mtrx)\r\n# Calculate the number of columns of the given matrix by\r\n# calculating the length of the first list in the nested list\r\n# using the len() function and store it in a variable mtrxcols.\r\nmtrxcols = len(mtrx[0])\r\n# Take a variable say mtrxtrace which stores the trace of the given matrix\r\n# and initialize its value to 0.\r\nmtrxtrace = 0\r\n# Take a variable say mtrxsum which stores the sum of all elements of the matrix\r\n# and initialize its value to 0.\r\nmtrxsum = 0\r\n# Loop till the given number of rows using the For loop.\r\nfor n in range(mtrxrows):\r\n # Inside the For loop, Iterate till the given number of columns using another\r\n # Nested For loop(Inner For loop).\r\n for m in range(mtrxcols):\r\n # Check if the parent loop iterator value is equal to the inner loop iterator value\r\n # using the if conditional statement\r\n # (This is whether the element is diagonal element or not)\r\n if(n == m):\r\n # If it is true then add this value to mtrxtrace.\r\n mtrxtrace = mtrxtrace+mtrx[n][m]\r\n\r\n # After the if conditional statement,\r\n # Add the gvnmatrix[n][m] to the above-initialized mtrxsum\r\n # and store it in the same variable mtrxsum.\r\n mtrxsum = mtrxsum+mtrx[n][m]\r\n# After the end of two loops calculate the square root of the mtrxsum\u00a0value\r\n# using the sqrt() function\r\n# and store this result in mtrxnormal variable.\r\nmtrxnormal = math.sqrt(mtrxsum)\r\n# Print the value of mtrxtrace which gives the value of the trace of the given matrix.\r\nprint('The trace value of the given matrix is :', mtrxtrace)\r\n# Print the value of mtrxnormal which gives the value of the normal of the given matrix.\r\nprint('The normal value of the given matrix is :', mtrxnormal)\r\n<\/pre>\n
The trace value of the given matrix is : 15\r\nThe normal value of the given matrix is : 6.557438524302<\/pre>\n
Method #2: Using For loop (User Input)<\/h3>\n
\n
# Import the math module using the import keyword.\r\nimport math\r\n# Give the number of rows of the matrix as user input using the int(input()) function\r\n# and store it in a variable.\r\nmtrxrows = int(input('Enter some random number of rows of the matrix = '))\r\n# Give the number of columns of the matrix as user input using the int(input()) function\r\n# and store it in another variable.\r\nmtrxcols = int(input('Enter some random number of columns of the matrix = '))\r\n# Take a list and initialize it with an empty value using [] or list() to say gvnmatrix.\r\nmtrx = []\r\n# Loop till the given number of rows using the For loop\r\nfor n in range(mtrxrows):\r\n # Inside the For loop, Give all the row elements of the given Matrix as a list using\r\n # the list(),map(),int(),split() functions and store it in a variable.\r\n l = list(map(int, input(\r\n 'Enter {'+str(mtrxcols)+'} elements of row {'+str(n+1)+'} separated by spaces = ').split()))\r\n # Add the above row elements list to gvnmatrix using the append() function.\r\n mtrx.append(l)\r\n# Take a variable say mtrxtrace which stores the trace of the given matrix\r\n# and initialize its value to 0.\r\nmtrxtrace = 0\r\n# Take a variable say mtrxsum which stores the sum of all elements of the matrix\r\n# and initialize its value to 0.\r\nmtrxsum = 0\r\n# Loop till the given number of rows using the For loop.\r\nfor n in range(mtrxrows):\r\n # Inside the For loop, Iterate till the given number of columns using another\r\n # Nested For loop(Inner For loop).\r\n for m in range(mtrxcols):\r\n # Check if the parent loop iterator value is equal to the inner loop iterator value\r\n # using the if conditional statement\r\n # (This is whether the element is diagonal element or not)\r\n if(n == m):\r\n # If it is true then add this value to mtrxtrace.\r\n mtrxtrace = mtrxtrace+mtrx[n][m]\r\n\r\n # After the if conditional statement,\r\n # Add the gvnmatrix[n][m] to the above-initialized mtrxsum\r\n # and store it in the same variable mtrxsum.\r\n mtrxsum = mtrxsum+mtrx[n][m]\r\n# After the end of two loops calculate the square root of the mtrxsum\u00a0value\r\n# using the sqrt() function\r\n# and store this result in mtrxnormal variable.\r\nmtrxnormal = math.sqrt(mtrxsum)\r\n# Print the value of mtrxtrace which gives the value of the trace of the given matrix.\r\nprint('The trace value of the given matrix is :', mtrxtrace)\r\n# Print the value of mtrxnormal which gives the value of the normal of the given matrix.\r\nprint('The normal value of the given matrix is :', mtrxnormal)\r\n<\/pre>\n
Enter some random number of rows of the matrix = 4\r\nEnter some random number of columns of the matrix = 4\r\nEnter {4} elements of row {1} separated by spaces = 1 2 3 4\r\nEnter {4} elements of row {2} separated by spaces = 7 5 8 6\r\nEnter {4} elements of row {3} separated by spaces = 9 4 2 5\r\nEnter {4} elements of row {4} separated by spaces = 1 1 1 1\r\nThe trace value of the given matrix is : 9\r\nThe normal value of the given matrix is : 7.745966692414834<\/pre>\n
\n