Python
 Computer >> コンピューター >  >> プログラミング >> Python

2つの行列のPythonプログラム乗算。


与えられた2つのユーザー入力マトリックス。私たちのタスクは、2つの行列の加算を表示することです。これらの問題では、ネストされたリストを包括的に使用します。

アルゴリズム

Step1: input two matrix.
Step 2: nested for loops to iterate through each row and each column.
Step 3: take one resultant matrix which is initially contains all 0. Then we multiply each row elements of first matrix with each elements of second matrix, then add all multiplied value. That is the value of resultant matrix.

サンプルコード

# Program to multiply two matrices
A=[]
n=int(input("Enter N for N x N matrix: "))         
print("Enter the element ::>")
for i in range(n): 
   row=[]                                      #temporary list to store the row
   for j in range(n): 
      row.append(int(input()))           #add the input to row list
      A.append(row)                      #add the row to the list
print(A)
# [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
#Display the 2D array
print("Display Array In Matrix Form")
for i in range(n):
   for j in range(n):
      print(A[i][j], end=" ")
   print()                                        #new line
B=[]
n=int(input("Enter N for N x N matrix : "))           #3 here
#use list for storing 2D array
#get the user input and store it in list (here IN : 1 to 9)
print("Enter the element ::>")
for i in range (n): 
   row=[]                                      #temporary list to store the row
   for j in range(n): 
      row.append(int(input()))           #add the input to row list
      B.append(row)                       #add the row to the list
print(B)
# [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
#Display the 2D array
print("Display Array In Matrix Form")
for i in range(n):
   for j in range(n):
      print(B[i][j], end=" ")
   print()                                           
result = [[0,0,0], [0,0,0], [0,0,0]] 
for i in range(len(A)): 
   for j in range(len(B[0])): 
      for k in range(len(B)): 
         result[i][j] += A[i][k] * B[k][j] 
print("The Resultant Matrix Is ::>")
for r in result: 
   print(r) 

出力

Enter N for N x N matrix: 3
Enter the element ::>
2
1
4
2
1
2
3
4
3
[[2, 1, 4], [2, 1, 2], [3, 4, 3]]
Display Array In Matrix Form
2 1 4 
2 1 2 
3 4 3 
Enter N for N x N matrix : 3
Enter the element ::>
1
2
3
4
5
6
7
8
9
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Display Array In Matrix Form
1 2 3 
4 5 6 
7 8 9 
The Resultant Matrix Is ::>
[34, 41, 48]
[20, 25, 30]
[40, 50, 60]

  1. 行列の転置を見つけるPythonプログラム

    この記事では、特定の問題ステートメントを解決するための解決策とアプローチについて学習します。 問題の説明 行列が与えられた場合、転置を同じ行列に格納して表示する必要があります。 行列の転置は、行を列に、列を行に変更することで得られます。つまり、A行列の転置はA[i][j]をA[j][i]に変更することで得られます。 以下に示す実装を見てみましょう- 例 N = 4 def transpose(A):    for i in range(N):       for j in range(i+1, N):     &nbs

  2. 2つの数値を追加するPythonプログラム

    この記事では、特定の問題ステートメントを解決するための解決策とアプローチについて学習します。 問題の説明 2つの大きな数が与えられ、それらを追加して出力を表示する必要があります。 ブルートフォースアプローチでは、オペランド間に「+」演算子を使用するか、2つの数値を反復可能に格納して、Python標準ライブラリで使用可能な組み込みのsum関数を使用できます。 このアプローチでは、計算が10進数で直接行われるため、時間計算量が増加します。 次に、10進数のビットを処理する別のアプローチについて説明します。 ここでは、合計とキャリーを計算する加算器の概念を使用します。 それでは、実装を見