三重対角行列圧縮格納アニメーション演示 アニメーションでコードを可視化しよう

图码-数据结构可视化动画版

三对角矩阵是仅在主对角线、其下方和上方的对角线上具有非零元素的矩阵。 所有其他元素均为零。 因此,维数小于或等于 3 的三对角矩阵似乎毫无意义。

示例1:

[a11, a22, 0, 0, 0, 0]   

[a21, a22, a23, 0 , 0 , 0 ]

[0、a32、a33、a34、0、0]

[0、0、a43、a44、a55、0]

[0、0、0、a54、a55、a56]

[0、0、0、0、a65、a66]

示例2:

[1, 1, 0, 0, 0, 0]   

[1, 1, 1, 0, 0, 0]

[0, 1, 1, 1, 0, 0]

[0, 0, 1, 1, 1, 0]

[0, 0, 0, 1, 1, 1]

[0, 0, 0, 0, 1, 1]

方法 

  • 将矩阵的大小作为输入
  • 检查是否大于3
    • 如果没有,退出
    • 如果是,请进一步进行
  • 将矩阵的元素作为输入
  • 现在,除了主对角线以及主对角线下方和上方的对角线之外,所有地方都置零。

程序:

Python3

# if you enter number n it will automatically 
# be considered as a square matrix of size n by n
size_of_a_matrix = int(input("Enter size the matrix that you want : "))
   
if size_of_a_matrix <= 3:
      
    # since size should be greater than 3
    print("Please enter the size that is greater than 3")
    exit()
  
diagonal = []
numbers1 = [[0 for j in range(0, size_of_a_matrix)]
            for i in range(0, size_of_a_matrix)]
   
# created a loop to enter numbers
for a in range(size_of_a_matrix):
    numbers1 = int(input(f"Enter the numbers for the main diagonal for position[{a}][{a}] : "))
       
    # appending the values to the list
    diagonal.append(numbers1)
  
diagonalAbove = []
print("*********")
   
# created a loop to enter numbers
for k in range(size_of_a_matrix-1):
    numbers2 = int(input(f"Enter the numbers for diagonal above the main diagonal for position[{k}][{k+1}]: "))
      
    # appending the values to the list
    diagonalAbove.append(numbers2)
  
diagonalBelow = []
print("*********")
   
# created a loop to enter numbers
for z in range(size_of_a_matrix-1):
    numbers3 = int(input(f"Enter the numbers for diagonal below the main diagonal for position[{z+1}][{z}]: "))
      
    # appending the values to the list
    diagonalBelow.append(numbers3)
print("*********")
   
   
def tridiagonal(size_of_a_matrix, diagonal, diagonalAbove, diagonalBelow):
   
    matrix = [[0 for j in range(size_of_a_matrix)]
              for i in range(size_of_a_matrix)]
      
    for k in range(size_of_a_matrix-1):
        matrix[k][k] = diagonal[k]
        matrix[k][k+1] = diagonalAbove[k]
        matrix[k+1][k] = diagonalBelow[k]
      
    matrix[size_of_a_matrix-1][size_of_a_matrix - 1] = diagonal[size_of_a_matrix-1]
   
    # so that the values will print row by row
    for row in matrix:
        print(row)
  
    return "this is the tridiagonal matrix"
  
# printing final values
print(tridiagonal(size_of_a_matrix, diagonal, diagonalAbove, diagonalBelow))


              

输出:

我们程序的输出


試験合格、キャリアアップ、あるいは純粋な興味を問わず、このデータ構造とアルゴリズムの可視化サイトは貴重なリソースとなるでしょう。

このサイトにアクセスして、学習の旅を始めましょう!

これらは一般的なものです:【C言語記述】『データ構造とアルゴリズム』データ構造JAVA実装 データ構造とアルゴリズム基礎(青島大学-王卓)データ構造とアルゴリズム王道データ構造c言語実装 速成データ構造期末試験前救急 データ構造ビデオC言語版チュートリアル データ構造厳蔚敏 データ構造郝斌 データ構造大学院試験 JAVAデータ構造アルゴリズムと基礎 データ構造王道 2022データ構造学習 データ構造小甲魚 王卓 データ構造学習 データ構造浙江大学 データ構造復習 データ構造馬士兵 データ構造ゼロ基礎チュートリアル データ構造とアルゴリズム データ構造入門 大学院試験データ構造問題解説 データ構造期末復習 コンピュータ2級