Computing sums of diagonals of a matrix using Python



In this article, we will learn a python program to efficiently compute sums of diagonals of a matrix.

Methos Used

The following are the various methods to accomplish this task ?

  • Using Nested For Loops

  • Using Only Single Loop

In a matrix we have two diagonals ?

  • Principal diagonal

  • Secondary diagonal

Example

Let us take a 3x3 matrix as shown below ?

A00 A01 A02 A10 A11 A12 A20 A21 A22 

Principal Diagonal Condition ? The row-column condition is row = column. The principal diagonal is formed by A00, A11, and A22 elements in a 3x3 matrix.

Secondary Diagonal Condition ? The row-column condition is row = numberOfRows - column -1. The principal diagonal is formed by A02, A11, and A22 elements in a 3x3 matrix.

Method 1: Using Nested For Loops

Algorithm (Steps)

Following are the Algorithms/steps to be followed to perform the desired task. ?

  • Create a function sumOfDiagonals() to print the sum of diagonals of a matrix by accepting the input matrix, rows as arguments.

  • Initialize a variable with 0 to store the principal diagonal sum.

  • Initialize another variable with 0 to store the secondary diagonal sum.

  • Use the for loop to traverse through the rows of a matrix.

  • Use another nested for loop, to traverse through all the columns of a current row.

  • Check if the row number is equal to the column number(Principal Diagonal Condition) using the if conditional statement and if it is true then the matrix element value to the principal diagonal sum.

  • Similarly, Check if the sum of the row number and column number is the number of rows(Secondary Diagonal Condition) using the if conditional statement and if it is true then the matrix element value to secondary diagonal sum.

  • Print the resultant sum of principaldiagonal elements of an input matrix.

  • Print the resultant sum of secondary diagonal elements of an input matrix.

  • Create a variable to store the input matrix.

  • Call the above-defined sumOfDiagonals() function by passing the input matrix and no of rows(dimensions) as arguments to print the sum of diagonals.

Example

The following program returns the sum of diagonals of an input matrix using the nested for loops ?

# creating a function to print the sum of diagonals # of a matrix by accepting input matrix, rows as arguments def sumOfDiagonals(inputMatrix, rows): # Initializing with 0 to store the principal diagonal sum principal_diag = 0 # Initializing with 0 to store the secondary diagonal sum secondary_diag = 0 # Traversing through the rows of a matrix for p in range(0, rows): # Traversing through the columns of the current row for q in range(0, rows): # Principal diagonal condition if (p == q): principal_diag += inputMatrix[p][q] # Secondary diagonal condition(row -1 because index starts from 0) if ((p + q) == (rows - 1)): secondary_diag += inputMatrix[p][q] # Printing the sum of principal diagonal elements print("Sum of principal diagonal elements:", principal_diag) # Printing the sum of secondary diagonal elements print("Sum of secondary diagonal elements:", secondary_diag) # input matrix(3x3 matrix) inputMatrix = [[5, 1, 3], [9, 6, 8], [4, 2, 7]] rows = 3 print("Given Matrix is:") # traversing through the rows of a matrix for p in range(rows): # Traversing through the columns of a current row for q in range(rows): # printing the corresponding element at the current row and column of the matrix print(inputMatrix[p][q], end=" ") # Printing a new line print() # calling sumOfDiagonals() function by passing input matrix # and no of rows(dimensions) to it sumOfDiagonals(inputMatrix, rows) 

Output

On execution, the above program will generate the following output ?

Given Matrix is: 5 1 3 9 6 8 4 2 7 Sum of principal diagonal elements: 18 Sum of secondary diagonal elements: 13 

Time Complexity ? O(N*N), Since we used nested loops to traverse N*N times.

Auxiliary Space ? O(1). Since we are not using any additional space.

Method 2: Using Only Single Loop

Example

The following program returns the sum of diagonals of an input matrix using the only one for loop(Single Loop) ?

# Creating a function to print the sum of diagonals # of a matrix by accepting input matrix, rows as arguments def sumOfDiagonals(inputMatrix, rows): # Initializing with 0 to store the principal diagonal sum principal_diag = 0 # Initializing with 0 to store the secondary diagonal sum secondary_diag = 0 # Traversing the rows of a matrix for p in range(0, rows): # Adding the principal Diagonal element of the current row principal_diag += inputMatrix[p][p] # Adding the secondary Diagonal element of the current row secondary_diag += inputMatrix[p][rows - p - 1] # printing the sum of principal diagonal elements print("Sum of principal diagonal elements:", principal_diag) # printing the sum of secondary diagonal elements print("Sum of secondary diagonal elements:", secondary_diag) # input matrix(3x3 matrix) inputMatrix = [[5, 1, 3], [9, 6, 8], [4, 2, 7]] rows = 3 print("The Given Matrix is:") # traversing through the rows of a matrix for p in range(rows): # Traversing through the columns of a current row for q in range(rows): # printing the corresponding element at the current row and column of the matrix print(inputMatrix[p][q], end=" ") # Printing a new line print() # calling sumOfDiagonals() function by passing input matrix # and no of rows(dimensions) to it sumOfDiagonals(inputMatrix, rows) 

Output

On execution, the above program will generate the following output ?

Given Matrix is: 5 1 3 9 6 8 4 2 7 Sum of principal diagonal elements: 18 Sum of secondary diagonal elements: 13 

Time Complexity ? O(N). Since we used a loop to traverse N times.

Auxiliary Space ? O(1). Since we are not using any additional space.

Conclusion

In this article, we learned about matrix diagonals and two different methods for computing the sums of matrix diagonals (primary and secondary). We learned an efficient method for computing this that requires only a single traversal of the matrix (O(N) Time Complexity).

Updated on: 2023-01-23T14:10:23+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements