Home > Article > Backend Development > Python program to calculate the sum of the left diagonal of a matrix
Python is a popular, general-purpose programming language used in a wide range of industries, from desktop applications to web development and machine learning.
Its simple syntax makes it ideal for beginners starting to code. In this article, we will learn how to calculate "the sum of the left diagonal elements of a matrix" using Python.
In mathematics, we use a rectangular array or matrix to describe a mathematical object or the properties of a mathematical object. It is a rectangular array or table containing numbers, symbols, or expressions arranged in rows and columns.
2 3 4 5 1 2 3 6 7 5 7 4
So, this is a matrix with 3 rows and 4 columns. And expressed as a 3*4 matrix.
There are two diagonals in the matrix, namely the main diagonal and the sub-diagonal.
The main diagonal is the diagonal line from the upper left corner to the lower right corner, and the secondary diagonal is the diagonal line from the lower left corner to the upper right corner.
From the examples given -
2 3 a00 a01 1 2 a10 a11
Here a00 and a11 are the main diagonal, and a10 and a01 are the main diagonal secondary matrices.
Now that we have reviewed the basics and have a thorough understanding of matrices and diagonals, let's delve deeper into the topic and finish the coding aspect.
To calculate the sum, we take a two-dimensional matrix. Consider a 4*4 matrix with the following elements -
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 primary matrix, there is a condition before completing the task. Let us understand the conditions of two diagonals.
In order to take out the sum of elements present in the main diagonal of a matrix, it should satisfy the row-column condition, which specifies that for the sum of elements, it should have elements as row = column.
Now for the sub-diagonal, for elements a03, a12, a21, a30, the row and column condition will be the number of rows - the number of columns - 1.
In this method, we will use two loops to achieve this, the loop for rows and columns, and the inner loop to check the condition we provide.
Gives a MAX value.
Functions that define matrices.
Use a for loop to iterate over numbers.
Provide conditions for the left diagonal of the matrix.
Print the value.
The example given below is to calculate the sum of left diagonal elements in a 4 x 4 matrix. The for loop goes through each row and column of the matrix, and if they are equal (i.e. on the left diagonal), the element is added 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)
In this method, we simply define a function and use a for loop to create a range for rows and columns. Add a condition for elements present in the left diagonal.
Time complexity− O(N*N), because we use nested loops to check N*N times.
Since we are not consuming any extra space, the complexity of the auxiliary space is O(1).
Sum of left diagonal of the matrix: 179
In this method, a single loop can be used to calculate the sum of the primary and secondary diagonals.
Gives a MAX value.
Functions that define matrices.
Use a for loop to iterate over numbers.
Provide conditions for the left diagonal of the matrix.
Print the value.
The following example defines a function called sumofleftdiagonal that accepts two parameters: matrix and m.
The first parameter Matrix is a two-dimensional array, and the second parameter m represents the size of the two-dimensional array.
There is a variable named left_diagonal in this function, which is used to store the sum of all elements on the left diagonal of the matrix
The for loop then iterates through each element in the range 0 to m (size) and adds the values into left_diagonal.
Finally, the output statement prints "Sum of Left Diagonal is:", followed by the content stored in left_diagonal. Example given with MAX set to 50 and T being another 4x4 array
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)
The time complexity is O(N) because it requires a loop to iterate N elements. Since no additional space is consumed, the auxiliary space complexity is O(1).
Sum of Left Diagonal is: 226
In this article, we briefly discussed two simple ways to calculate the sum of the left diagonals of a matrix using Python programs. The first method uses two loops to accomplish the task given to us, while the second method provides us with an efficient way to accomplish the same task in a shorter path.
The above is the detailed content of Python program to calculate the sum of the left diagonal of a matrix. For more information, please follow other related articles on the PHP Chinese website!