Python program to display upper triangular matrix

WBOY
Release: 2023-09-05 20:49:06
forward
1392 people have browsed it

Python program to display upper triangular matrix

A matrix is ​​a two-dimensional array consisting of many numbers arranged in rows and columns. A square matrix (one whose rows and columns have the same number of elements) has two diagonals. One is the main diagonal - located from the upper left corner to the lower right corner of the square. The second is the auxiliary diagonal - located from the upper right corner to the lower left corner.

For a square matrix, if all elements below the main diagonal are zero, it is called upper triangular matrix.

[1, 3, 4]
[0, 5, 6]
[0, 0, 3]
Copy after login

If the given matrix is ​​not a square matrix, the matrix cannot be converted to an upper triangular matrix.

Input and output scenarios

Suppose we have a square matrix. The output matrix will be an upper triangular matrix.

Input matrix:
[1, 3, 5, 7]
[9, 2, 4, 2]
[6, 3, 1, 4]
[5, 8, 7, 6]
 
Upper triangular matrix:
[1, 3, 5, 7]
[0, 2, 4, 2]
[0, 0, 1, 4]
[0, 0, 0, 6]
Copy after login

Let us see the following example to display an upper triangular matrix. We will use python list of lists to create the matrix.

Example

In this example, we will display the upper triangular matrix by replacing the lower triangular elements (4, 8, 1) with zeros.

arr = [[1, 2, 3],
       [4, 5, 6],
       [1, 8, 5]]

print("The original matrix: ")
for row in arr:
   print(row)
print()

print("The upper triangular matrix: ")
if(len(arr) != len(arr[0])):  
   print("Matrix should be a square matrix");  
else: 
   for i in range(3):
      for j in range(3):
         if(i<=j):
            print(arr[i][j],end="  ")
         else:
            print(0,end="  ")
      print()
Copy after login

Output

The original matrix: 
[1, 2, 3]
[4, 5, 6]
[1, 8, 5]

The upper triangular matrix: 
1  2  3  
0  5  6  
0  0  5 
Copy after login

Example

In this example we only show the upper triangular matrix. Instead of converting the lower try element to zero.

arr = [[1, 2, 3],
       [4, 5, 6],
       [1, 8, 5]]

print("The original matrix: ")
for row in arr:
   print(row)
print()

print("The upper triangular matrix: ")
for i in range(3):
   for j in range(3):
      if(i > j):
         print(end="  ")
      else:
         print(arr[i][j],end=" ")
   print(" ")
Copy after login

Output

The original matrix: 
[1, 2, 3]
[4, 5, 6]
[1, 8, 5]

The upper triangular matrix: 
1 2 3  
  5 6  
    5  
Copy after login

Example

In this example, we will update the original matrix by converting the lower triangle elements to zero, and then we will display the upper triangle matrix.

arr = [[1, 2, 3],
       [4, 5, 6],
       [1, 8, 5]]

print("The original matrix: ")
for row in arr:
   print(row)
print()

print("The upper triangular matrix: ")
for i in range(3):
   for j in range(3):
      if(i > j):
         arr[i][j] = 0
         print(arr[i][j],end=" ")
      else:
         print(arr[i][j],end=" ")
   print(" ")
Copy after login

Output

The original matrix: 
[1, 2, 3]
[4, 5, 6]
[1, 8, 5]

The upper triangular matrix: 
1 2 3  
0 5 6  
0 0 5  
Copy after login

The above is the detailed content of Python program to display upper triangular matrix. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!