Python Program to calculate the sum of right diagonal the matrix

A well-liked general-purpose programming language is Python. It is applied in a variety of industries, including desktop applications, web development, and machine learning. Fortunately, Python features a straightforward, user-friendly syntax for beginners. Here, in our article we will be using python for calculating the sum of right diagonal of the matrix.

What is a Matrix?

In mathematics, we use a rectangular arrangement or a matrix, which is used to describe a mathematical object or an attribute of one, it is a rectangular array or table containing numbers, symbols, or expressions that are arranged in rows and columns.

For Instance −

2 3 4 5
1 2 3 6
7 5 7 4


Hence, this is a matrix with 3 rows and 4 columns, which is represented as 3*4 matrix.

Now, there are two diagonals in the matrix i.e the Primary or Principal diagonal and the Secondary diagonal. Primary or principal diagonal is the diagonal starting from the upper left corner to the lower right corner and the secondary diagonal starting from lower left corner to the upper right corner.

From the above example we see that both a00, a11 are the principal diagonal (left diagonal) and a10, a01 is the secondary matrix (right diagonal), as shown below

2 3   a00 a01
1 2   a10 a11


Sum of Right Diagonal of the Matrix

As we have revised the basics concepts and we have a complete understanding of the matrix and the diagonals let’s dive deep into the topic now and complete the coding part of the concept.

For calculating the sum let’s take a 2D matrix. Consider a 4*4 matrix with elements

• Here, a00, a11, a22 and a33 are the elements of the primary or principal diagonal of the matrix. The secondary or right diagonal consists of elements a30, a21, a12 and a03.

• There is an important condition to consider before completing this task: for taking out the sum of elements present in the primary diagonal, it must fulfill what is known as the row-column condition which states that for every element in each row there must be an equivalent column number.

Similarly, for calculating the sum of elements on the secondary diagonal (a03, a12, a21 and a30), its row-column condition will be equal to number of rows minus column -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


Using For Loop

In this method, we will use two loops for the purpose, that is for the rows and columns and an inner loop for checking the condition we have provided.

Algorithm

• Give a value that is MAX.

• Define a function for the matrix.

• Use for loop for iterating through the numbers

• Provide the condition for the right diagonal of the matrix.

• Print the value.

Example

This example defines a constant MAX with the value 50, and then creates a function named SUM_RIGHT_MATRIX which takes in a matrix and an integer.

The function adds together all of the numbers on the right diagonal of the given matrix (i.e. from top right to bottom left) 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)


Output

On the execution of the above program, we get the "Sum of right diagonal is: 155". This means that the total sum of all numbers in the right diagonal is 155.

Sum of right diagonal is: 154


Using a Single Loop

The sum of the major and secondary diagonals is calculated using a single loop in this approach.

Algorithm

• Give a value that is MAX.

• Define a function for the matrix.

• Use for loop for iterating through the numbers.

• Provide the condition for the right diagonal of the matrix.

• Print the value.

Example

The following example is defining a function called sumofrightdiagonal, which takes in two parameters: a matrix and m.

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

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

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)


Output

Sum of Right Diagonal: 171


Conclusion

In this article, we have briefly discussed about the two simple methods using python program to calculate the sum of right diagonal of the matrix. The first method uses two loops for completing the task provided to us while the second method provides us an efficient approach of completing the same task but with a shorter path.

Updated on: 21-Apr-2023

288 Views