{"id":20181,"date":"2021-09-06T13:41:57","date_gmt":"2021-09-06T08:11:57","guid":{"rendered":"https:\/\/python-programs.com\/?p=20181"},"modified":"2021-11-22T18:36:26","modified_gmt":"2021-11-22T13:06:26","slug":"python-program-to-find-the-sum-of-an-upper-triangular-matrix","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-the-sum-of-an-upper-triangular-matrix\/","title":{"rendered":"Python Program to Find the Sum of an Upper Triangular Matrix"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Find the Sum of all Elements in a 2D Array<\/a><\/p>\n Given a matrix and the task is to find the sum of an upper triangular matrix 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 Upper Triangular Matrix:<\/strong><\/p>\n An upper triangular matrix is a square matrix in which all of the entries below the major diagonal are zero.<\/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 sum of an upper triangular matrix 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 Find the Sum of all Elements in a 2D Array Given a matrix and the task is to find the sum of an upper triangular matrix of the given matrix in Python. What is a matrix: A matrix is a rectangular sequence of numbers divided …<\/p>\nGiven Matrix :\r\n6 2 1\r\n1 5 0\r\n2 8 3<\/pre>\n
The sum of Upper Triangular matrix of the given matrix is :\r\n11<\/pre>\n
Given Matrix :\r\n4 3\r\n9 8<\/pre>\n
The sum of Upper Triangular matrix of the given matrix is :\r\n9<\/pre>\n
Program to Find the Sum of an Upper Triangular Matrix in Python<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the matrix as static input and store it in a variable.\r\nmtrx = [[6, 2, 1], [1, 5, 0], [2, 8, 3]]\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\nprint(\"The sum of Upper Triangular matrix of the given matrix is :\")\r\n# Take a variable say uppr_sum and initialize its value to 0.\r\nuppr_sum = 0\r\n# To print all the elements of the given matrix.\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 condition n is greater than m using the if conditional statement where n\r\n # is the iterator value of the parent For loop and m is the iterator value of the\r\n # inner For loop.\r\n if n > m:\r\n # If the statement is true, add the gvnmatrix[n][m] to the above-initialized\r\n # uppr_sum\u00a0and store it in the same variable uppr_sum.\r\n uppr_sum += mtrx[n][m]\r\n# Print the variable uppr_sum\u00a0to get the sum of an upper triangular matrix of the\r\n# given matrix.\r\nprint(uppr_sum)\r\n<\/pre>\n
The sum of Upper Triangular matrix of the given matrix is :\r\n11<\/pre>\n
Method #2: Using For loop (User Input)<\/h3>\n
\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\r\n mtrx.append(l)\r\n \r\n# Take a variable say uppr_sum and initialize its value to 0.\r\nuppr_sum = 0\r\nprint(\"The sum of Upper Triangular matrix of the given matrix is :\")\r\n# To print all the elements of the given matrix.\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 condition n is greater than m using the if conditional statement where n\r\n # is the iterator value of the parent For loop and m is the iterator value of the\r\n # inner For loop.\r\n if n > m:\r\n # If the statement is true, add the gvnmatrix[n][m] to the above-initialized\r\n # uppr_sum\u00a0and store it in the same variable uppr_sum.\r\n uppr_sum += mtrx[n][m]\r\n# Print the variable uppr_sum\u00a0to get the sum of an upper triangular matrix of the\r\n# given matrix.\r\nprint(uppr_sum)\r\n\r\n<\/pre>\n
Enter some random number of rows of the matrix = 2\r\nEnter some random number of columns of the matrix = 2\r\nEnter {2} elements of row {1} separated by spaces = 4 3\r\nEnter {2} elements of row {2} separated by spaces = 9 8\r\nThe sum of Upper Triangular matrix of the given matrix is :\r\n9<\/pre>\n
\n