so first we create a matrix using numpy arange() function and then calculate the principal diagonal

elements sum using __trace()__ function and diagonal element using diagonal() function.

1: __trace():__ trace of an n by n square matrix A is defined to be the sum of the elements on the main diagonal

(the diagonal from the upper left to the lower right) of matrix A.

import numpy as np

a=np.arange(9).reshape(3,3) print("original matrix is :\n",a)

m=np.trace(a)

print("trace of matrix :", m)

__Example:__ for 3-D matrix example code is here for 3-D matrix example code is here .

__ code:__

# create a class

class diagnoal_sum:

# create function

def sum_diagonal( self,a, r,c):

# check whether a given a row and column is valid or not.

if c==0 and r==0:

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

# we use diagonal() function used to return specified diagonal elements.

print("diagonal elements are :",a.diagonal())

# we use trace() function used to Sum along diagonals.

result = np.trace(a)

print("sum of diagonals Elements :",result)

# 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("Original matrix :\n", a)

#call function to execute .

digsum.sum_diagonal(a,r,c)

if __name__ == "__main__":

main()

__output:__

m=np.trace(a)

print("trace of matrix :", m)

output:

original matrix is :

[[ 0 1 2]

[ 3 4 5]

[ 6 7 8 ]]

[ 3 4 5]

[ 6 7 8 ]]

trace of matrix : 12

2: __diagonal():__ diagonal function in numpy returns upper left o right diagonal elements.

If a is 2-D and not a matrix, a 1-D array of the same type as a containing the diagonal is

returned.

If a is a matrix, a 1-D array containing the diagonal is returned in order to maintain backward

compatibility.

If the dimension of a is greater than two, then an array of diagonals is returned, packed from

left-most dimension to right-most (e.g., if a is 3-D, then the diagonals are packed along rows).

left-most dimension to right-most (e.g., if a is 3-D, then the diagonals are packed along rows).

import numpy as np import numpy as np

a=np.arange(9).reshape(3,3) a=np.arange(9).reshape(2,3,3) print("original matrix is :\n",a) print("original matrix is :\n",a)

m=a.diagonal() m=a.diagonal()

print("diagonal elements \n:", m) print("diagonal elements \n:", m)

output: output:

original matrix is : original matrix is :

[[[ 0 1 2] [[[ 0 1 2]

[ 3 4 5] [ 3 4 5]

[ 6 7 8]] [ 6 7 8]]

diagonal elements : [0,4,8]

[[ 9 10 11]

[12 13 14]

[15 16 17]]]

diagonal elements

[[ 9 10 11]

[12 13 14]

[15 16 17]]]

diagonal elements

[[ 0 12]

[ 1 13]

[ 2 14]]

[ 1 13]

[ 2 14]]

#import numpy library

import numpy as np# create a class

class diagnoal_sum:

# create function

def sum_diagonal( self,a, r,c):

# check whether a given a row and column is valid or not.

if c==0 and r==0:

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

# we use diagonal() function used to return specified diagonal elements.

print("diagonal elements are :",a.diagonal())

# we use trace() function used to Sum along diagonals.

result = np.trace(a)

print("sum of diagonals Elements :",result)

# 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("Original matrix :\n", a)

#call function to execute .

digsum.sum_diagonal(a,r,c)

if __name__ == "__main__":

main()

case1: when row and column are equal .

enter row and column :3 3

Original matrix :

[[0 1 2]

[3 4 5]

[6 7 8]]

diagonal elements are : [0 4 8]

sum of diagonals Elements : 12

Original matrix :

[[0 1 2]

[3 4 5]

[6 7 8]]

diagonal elements are : [0 4 8]

sum of diagonals Elements : 12

case2: when row and column are not equal

enter row and column :3 2

Original matrix :

[[0 1]

[2 3]

[4 5]]

diagonal elements are : [0 3]

sum of diagonals Elements : 3

Original matrix :

[[0 1]

[2 3]

[4 5]]

diagonal elements are : [0 3]

sum of diagonals Elements : 3

case3: when row and column are both 0.

enter row and column :0 0

Original matrix :

[]

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

Original matrix :

[]

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

keep coding happy coding :)

## Comments