# Python Program to calculate the sum of left diagonal of the matrix

Python is a popular, general-purpose programming language that can be used in a wide range of industries, from desktop applications to web development and machine learning.

Its straightforward syntax makes it ideal for beginners to get started with coding. In this article, we'll see how Python can be used to calculate “The sum of the left diagonal elements in a matrix”.

## 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.

## Example

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


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

• There are two diagonals in the matrix that is 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 example given −

2 3        a00 a01
1 2        a10 a11


Here both a00, a11 are the principal diagonals and a10, a01 is the secondary matrix.

## Sum of Left Diagonal of a Matrix

Now that we have reviewed the fundamentals and have a thorough understanding of matrices and diagonals, let's dive deeper into the topic and finish up with the coding aspect.

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

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

• Here, a00, a11, a22, a33 is the primary or the principal matrix, there is a condition before completing the task. Let’s learn the conditions for both the diagonals.

• For taking out the sum of the elements present in the primary diagonal of a matrix it should fulfil the condition which is the row-column condition which specifies that for the sum of elements it should have the elements with elements of rows = column.

• Now for the secondary diagonal, for the elements a03, a12, a21, a30 the rowcolumn condition will be number of rows-column -1.

## Using For Loops

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 left diagonal of the matrix.

• Print the value.

### Example

The example given below is calculating the sum of the left diagonal elements in a 4 x 4 matrix. The for loops go through each row and column of the matrix, and if they are equal (i.e. it's on the left diagonal) then add that element to a variable called "leftmatrix".

max = 50
def sumleftmatrix(matrix, m):
leftmatrix = 0
for i in range(0, m):
for j in range(0, m):
if (i == j):
leftmatrix += matrix[i][j]
print("Sum of left diagonal of the matrix:", leftmatrix)
A = [[ 10, 22, 13, 84 ],
[ 52, 63, 97, 82 ],
[ 11, 32, 23, 14 ],
[ 55, 63, 72, 83 ]]
sumleftmatrix(A, 4)


### Output

In this method we have simply defined a function and created a range using the for loop for the rows and columns. The condition for adding the elements present in the left diagonal.

Time Complexity − O(N*N), as we examine N*N times using nested loops.

As we are not consuming any additional space, the auxiliary space is O(1).

Sum of left diagonal of the matrix: 179


## Using a Single Loop

The sum of the major and secondary diagonals can be 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 left diagonal of the matrix.

• Print the value.

### Example

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

• The first parameter, matrix, is a 2D array of numbers while the second parameter, m, represents the size of the 2D array.

• Within this function there is a variable called left_diagonal that stores the sum of all elements on the left diagonal of the matrix

• A for loop then runs through each element in range 0 to m (the size) and adds up these values into left_diagonal.

• Finally an output statement prints out "Sum of Left Diagonal is:" followed by what was stored in left_diagonal. An example case with MAX set to 50 and T being another 4x4 array is given

MAX = 50
def sumofleftdiagonal (matrix, m):
left_diagonal = 0
for i in range(0, m):
left_diagonal += matrix[i][i]
print("Sum of Left Diagonal is:", left_diagonal)
T = [[ 11, 12, 33, 24 ],
[ 54, 69, 72, 84 ],
[ 14, 22, 63, 34 ],
[ 53, 64, 79, 83 ]]
sumofleftdiagonal (T, 4)


### Output

The time complexity is O(N), as it requires a loop to iterate through N elements. Since no additional space is being consumed, the auxiliary space complexity is O(1).

Sum of Left Diagonal is: 226


## Conclusion

In this article, we have briefly discussed about the two simple methods using python program to calculate the sum of left 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

210 Views