### Python find sum the diagonal elements of the matrix

Python program to find sum the diagonal elements of the matrix

Description: we have to find the sum of diagonal elements in a matrix .so first we create a matrix
using numpy arange() function and then calculate the principal diagonal (the diagonal from the upper
left to the lower right) elements sum .again calculate the secondary diagonal(the diagonal from the
upper right to the lower left) elements sum.

Example: [[ 0 1 2]
[ 3 4 5]
[ 6 7 8 ]]

principal diagonal elements sum : 0+4+8= 12
principal diagonal elements sum : 2+4+6= 12
Code:

#import numpy library
import numpy as np
# create a class
class diagnoal_sum:
# create function
def sum_diagonal( self,a, r,c):
# check wheather a given a row and column is valid or not.
if r!= c or c==0 or r==0:
return print("Enter valid matrix ,Matrix should be (n x n) format")
# calculate the size of matrix
l=len(a[
0])
# if size is null then return matrix

# initialize variable with 0 value
main=
0
second=
0
for i in range(r):
#count matrix diagonal element which is principal element of matrix a[n][n]
main +=a[i][i]
#count matrix diagonal element which is secondary element of matrix a[n][l-i-1] .example:
# a[2][0],a[1][1] a[0][2] f matrix is 3 x 3.
second+=a[i][l-i
-1]
print(
"main diagonal elements sum :", main)
print(
"second diagonal elements sum :", second)

# define main() function.
def main():
digsum=diagnoal_sum()
#taking input from console.
row,col=input(
"enter row and column :").split()
r=int(row)
c=int(col)
# create matrix using numpy function arange() in sequential order.
a=np.arange(r*c).reshape(r,c)
print(
"given matrix :\n", a)
#call function to execute .
digsum.sum_diagonal(a,r,c)
if __name__ == "__main__":
main()

Output:
case1: enter row and column :2 2
[[0 1]
[2 3]]

main
diagonal elements sum : 3
second
diagonal elements sum : 3

case2:
enter row and column :0 2
given matrix : []
Enter valid matrix, Matrix should be (n x n) format
case3:
enter row and column :3 6
given matrix :
[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]
[12 13 14 15 16 17]]

Enter valid matrix ,Matrix should be (n x n) format

case 4:
enter row and column : 0 0
given matrix :
[]
Enter valid matrix ,Matrix should be (n x n) format

Time complexity is O(n) ans space complexity is O(1)
keep coding happy coding :)

Next : Python numpy program to find sum the diagonal elements of the matrix  here

#### More Articles of Akrati Sharma:

Name Views Likes
Python numpy program to generate random number 834 0
Program to construct BST from level order traversal 643 2
Python numpy program to find sum the diagonal elements of the matrix 8458 1
Python find sum the diagonal elements of the matrix 1606 0
python numpy find the transpose of a matrix 393 0
Python numpy convert a csv file date to a matrix 492 1
Python program to read configuration file 382 1
Python program to convert doc file into pdf file. 997 19
Python program to add new page at the end of a pdf file 310 18
Python Program to find sink odd nodes in binary tree. 436 14
creating project in pythons scrapy library 405 16
Python program to check given array of size n can represent binary search tree of n levels or not. 298 16
Python program to find out greater sum tree elements of binary search tree 306 17
Selectors of scrapy library 396 24
Introduction to python scrapy library 314 16
Python Program to find an element in tuple by value using in and not in 288 18
Find an element in tuple by value using in and not in : 93 25
Python program to count Half Nodes in Binary Tree recursion 443 20
Python Program to find top three elements in binary tree. 262 20
Python program to count half nodes in binary tree without recursion 374 14
Python program to find deepest right leaf node in a binary tree using recursion. 350 18
Python program to convert a given tree to its Sumtree 490 26
Python program to convert a tree into its sum tree 84 21
Python Program to Convert Binary tree into Binary Search Tree 381 17
Python Program to check if each internal node of a BST has exactly one child 334 18
Python Program to find median of binary search tree 637 11
Python Program to find an Element into a Binary tree. 261 18
Python program to replace every element with the least greater element on its right 326 18
Python Program to delete an element at specific index in tuple. 20 13
Python program to insert an element to specific position in tuple 361 20
Python program to insert an element at specific position in tuple. 13 11
Python Program to append an element in tuple at the end. 313 20
Python Program to count subtree that sum up to a given value x 558 20
Python program to print all root to leaf paths with there relative positions 312 16
Python program to check given tree is Sumtree or not 412 16
Python program to find sum of nodes at maximum depth of a binary tree 505 21
Python Program to replace an element at specific index in tuple. 354 24
Python program to find given key x in binary tree 289 14
Python program to find vertical width of binary tree 352 15
Python program to find mirror of a given node in binary tree 369 19
Python program to find largest subtree sum in a tree 260 18
Python Program to calculate the size of tree without recursion 328 19
Python Program to find tilt of binary tree 452 18
Python Program to find maximum node in left subtree in binay tree. 240 16
Python program to convert a List to Dictionary with same Value. 278 19
Python program to count minimum swap require to construct Binary Search Tree from Binary tree 461 18
Program to construct BST from level order traversal 354 24
Python Program to delete an element at specific index in tuple. 572 21
Python Program to print all the elements in Binary Search tree. 1324 14