Python program to calculate the sum of the right diagonal elements of a matrix

WBOY
Release: 2023-08-19 11:29:14
forward
1425 people have browsed it

Python program to calculate the sum of the right diagonal elements of a matrix

A popular general-purpose programming language is Python. It is used in a variety of industries, including desktop applications, web development, and machine learning. Fortunately, Python has a simple and easy-to-understand syntax that is suitable for beginners. In this article, we will use Python to calculate the sum of the right diagonal of a matrix.

What is a matrix?

In mathematics, we use a rectangular array or matrix to describe a mathematical object or its properties. It is a rectangular array or table containing numbers, symbols, or expressions arranged in rows. and column arrangement.

For example −

2 3 4 5
1 2 3 6
7 5 7 4
Copy after login

So, this is a matrix with 3 rows and 4 columns, expressed as a 3*4 matrix.

Now, there are two diagonals in the matrix, the main diagonal and the sub-diagonal. The main diagonal is the diagonal line from the upper left corner to the lower right corner, and the secondary diagonal is the diagonal line from the lower left corner to the upper right corner.

We can see from the above example that a00 and a11 are both main diagonals (left diagonals), while a10 and a01 are secondary diagonals (right diagonals), as shown below

2 3   a<sub>00</sub> a<sub>01</sub>
1 2   a<sub>10</sub> a<sub>11</sub>
Copy after login

Sum of right diagonal of matrix

Since we have revised the basic concepts and have a complete understanding of matrices and diagonals, let us now delve deeper into the topic and complete the coding part of the concept.

To calculate the sum, we can use a 2D matrix. Consider a 4*4 matrix whose elements are

  • Here, a00, a11, a22 and a33 are the main diagonal elements of the matrix. The subdiagonal consists of elements a30, a21, a12 and a03.

  • Before completing this task, there is an important condition to consider: in order to take the sum of the elements on the main diagonal, it must satisfy the so-called row and column conditions, that is, each element in each row must have a Equal column numbers.

Similarly, for calculating the sum of elements on the sub-diagonal (a03, a12, a21 and a30), the row and column conditions will be equal to the number of rows minus the number of columns minus 1.

2 4 6 8    a00 a01 a02 a03
3 5 7 9    a10 a11 a12 a13
1 4 6 7    a20 a21 a22 a23
3 5 1 4    a30 a31 a32 a33 
Copy after login

Use For Loop

In this method we will use two loops, one for rows and columns and another to check the condition we provide.

algorithm

  • Give a value, which is the maximum value.

  • Define a function for the matrix.

  • Use a for loop to iterate over numbers

  • Provide conditions for the right diagonal of the matrix.

  • Print this value.

Example

This example defines a constant MAX with a value of 50, and then creates a function called SUM_RIGHT_MATRIX that accepts a matrix and an integer as parameters.

This function adds all the numbers on the right diagonal of the given matrix (i.e. from the upper right corner to the lower left corner) and prints out the sum.

MAX = 50
def SUM_RIGHT_MATRIX (matrix, m):
   rightD = 0;
   for i in range (0, m):
      for j in range (0, m):
         if ((i + j) == (m - 1)):
            rightD += matrix[i][j]
   print ("Sum of right diagonal is:", rightD)
T = [[ 13, 21, 33, 45 ],
   [ 52, 16, 27, 28 ],
   [ 17, 28, 31, 43 ],
   [ 54, 26, 87, 28 ]]
SUM_RIGHT_MATRIX (T, 4)
Copy after login

Output

After executing the above program, we get "The sum of the right diagonals is: 155". This means the sum of all the numbers on the right diagonal is 155.

Sum of right diagonal is: 154
Copy after login

Use a single loop

Using this method, the sum of the main diagonal and the sub-diagonal is calculated through a loop.

algorithm

  • Give a value, which is the maximum value.

  • Define a function for the matrix.

  • Use a for loop to iterate over numbers.

  • Provide conditions for the right diagonal of the matrix.

  • Print this value.

Example

The following example defines a function named sumofrightdiagonal that accepts two parameters: a matrix and m.

  • It loops through the matrix and adds each number on the right diagonal of the matrix, storing them in a variable called right_diagonal.

  • Finally, it prints "Sum of Right Diagonal is:", followed by the value stored in right_diagonal. The example also includes an input example T (a 4x4 matrix) where m equals 4, so when Sumofrightdiagonal is called with these values ​​as arguments, it will calculate and print out the sum of all elements on the right diagonal of T.

MAX = 50
def sumofrightdiagonal (matrix, m):
   right_diagonal = 0
   for i in range (0, m):
      right_diagonal += matrix [i] [m - i - 1]
   print ("Sum of Right Diagonal is:", right_diagonal)
T = [[ 11, 12, 33, 24 ],
   [ 54, 69, 72, 84 ],
   [ 14, 22, 63, 34 ],
   [ 53, 64, 79, 83 ]]
sumofrightdiagonal (T, 4)
Copy after login

Output

Sum of Right Diagonal: 171
Copy after login

in conclusion

In this article, we briefly discussed two simple ways to calculate the right diagonal sum of a matrix using Python programs. The first method uses two loops to accomplish the task we provided, while the second method provides a more efficient way to accomplish the same task, but with a shorter path.

The above is the detailed content of Python program to calculate the sum of the right diagonal elements of a matrix. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!