Baris pertama dapat dipilih sebagai X[0]. Dan, elemen pada baris pertama, kolom pertama dapat dipilih sebagai X[0][0]
Perkalian dua matriks X dan Y hanya terdefinisi jika jumlah kolom di X sama dengan jumlah baris Y
Jika X adalah matriks n x m dan Y adalah matriks m x l maka, XY terdefinisi dan memiliki dimensi n x l (tetapi YX tidak terdefinisi). Berikut adalah beberapa cara untuk mengimplementasikan perkalian matriks dengan Python
Kode sumber. Perkalian Matriks menggunakan Nested Loop
# Program to multiply two matrices using nested loops # 3x3 matrix X = [[12,7,3], [4 ,5,6], [7 ,8,9]] # 3x4 matrix Y = [[5,8,1,2], [6,7,3,0], [4,5,9,1]] # result is 3x4 result = [[0,0,0,0], [0,0,0,0], [0,0,0,0]] # iterate through rows of X for i in range(len(X)): # iterate through columns of Y for j in range(len(Y[0])): # iterate through rows of Y for k in range(len(Y)): result[i][j] += X[i][k] * Y[k][j] for r in result: print(r)Keluaran
[114, 160, 60, 27] [74, 97, 73, 14] [119, 157, 112, 23] _Dalam program ini, kami telah menggunakan [114, 160, 60, 27] [74, 97, 73, 14] [119, 157, 112, 23] 0 loop bersarang untuk mengulangi setiap baris dan setiap kolom. Kami mengumpulkan jumlah produk dalam hasilnya
Teknik ini sederhana tetapi mahal secara komputasi karena kami meningkatkan urutan matriks
Untuk operasi matriks yang lebih besar, kami merekomendasikan paket perangkat lunak yang dioptimalkan seperti NumPy yang beberapa (dalam urutan 1000) kali lebih cepat daripada kode di atas
Kode sumber. Perkalian Matriks Menggunakan Nested List Comprehension
# Program to multiply two matrices using list comprehension # 3x3 matrix X = [[12,7,3], [4 ,5,6], [7 ,8,9]] # 3x4 matrix Y = [[5,8,1,2], [6,7,3,0], [4,5,9,1]] # result is 3x4 result = [[sum(a*b for a,b in zip(X_row,Y_col)) for Y_col in zip(*Y)] for X_row in X] for r in result: print(r)Output dari program ini sama seperti di atas. Untuk memahami kode diatas terlebih dahulu kita harus mengetahui tentang dan menggunakan operator *
Kami telah menggunakan pemahaman daftar bersarang untuk mengulangi setiap elemen dalam matriks. Kode terlihat rumit dan tidak terbaca pada awalnya. Tapi begitu Anda memahami daftar pemahaman, Anda mungkin tidak akan kembali ke loop bersarang
Dalam tutorial ini, kita akan membahas cara menerima masukan matriks dari pengguna dengan Python. Masukan dari klien dapat diperoleh dengan dua cara. Mari kita lihat dua di antaranya
Istilah "matriks" mengacu pada distribusi data persegi panjang dan mungkin angka. Dengan kata lain, ini adalah susunan data atau angka persegi panjang/persegi. Dalam matriks, entri horizontal disebut sebagai 'baris', sedangkan elemen vertikal disebut sebagai 'kolom. Ordo matriks didefinisikan sebagai "r x c" jika memiliki baris "r" dan kolom "c". Setiap entri dalam matriks bisa berupa bilangan bulat, nilai floating point, atau bahkan bilangan kompleks
Contoh
Cara mengambil Matrix sebagai Input
Kami dapat menerima matriks input pengguna dengan Python dalam berbagai cara. Berikut ini adalah beberapa metode matriks input pengguna Python
Metode - 1
Kita dapat menggunakan "for loop" di dalam for loop untuk mengatur baris dan kolom matriks dengan ukuran yang diberikan oleh pengguna
Memasukkan
Keluaran
Give the number of rows:2 Give the number of columns:3 Please give the entries row-wise: 3 5 2 7 4 6 3 5 2 7 4 6 _
Untuk menulis dua for loop dalam satu baris
Memasukkan
Keluaran
Give the number of rows:2 Give the number of columns:3 4 2 6 3 6 3 [[4, 2, 6], [3, 6, 3]]
Metode - 2
Numpy dan fungsi map() sedang digunakan
NumPy, paket Python terkemuka, adalah salah satu yang paling banyak digunakan. Perpustakaan ini harus dimiliki untuk setiap proyek komputasi ilmiah. Ini juga berguna untuk array multidimensi, dan karena sebuah matriks memang merupakan array persegi panjang, kami akan menggunakannya untuk masukan pengguna
Memasukkan
Keluaran
Give the number of rows:2 Give the number of columns:3 Please write the elements of the matrix in a single line and seperated by a space: 3 4 5 3 7 5 [[3 4 5] [3 7 5]] _
Metode - 3
Menggunakan nilai yang dipisahkan ruang, satu baris dalam satu waktu. Dan kemudian menggunakan fungsi map() dan int untuk mengonversi masing-masing. Lihat kodenya
Matriks tidak lain adalah susunan persegi panjang dari data atau angka. Dengan kata lain, itu adalah array persegi panjang dari data atau angka. Entri horizontal dalam matriks disebut sebagai 'baris' sedangkan entri vertikal disebut sebagai 'kolom'. Jika suatu matriks memiliki r banyaknya baris dan c banyaknya kolom maka ordo matriks tersebut adalah r x c. Setiap entri dalam matriks dapat berupa bilangan bulat, atau nilai mengambang, atau bahkan bilangan kompleks
Contoh
// 3 x 4 matrix 1 2 3 4 M = 4 5 6 7 6 7 8 9 // 2 x 3 matrix in Python A = ( [ 2, 5, 7 ], [ 4, 7, 9 ] ) // 3 x 4 matrix in Python where entries are floating numbers B = ( [ 1.0, 3.5, 5.4, 7.9 ], [ 9.0, 2.5, 4.2, 3.6 ], [ 1.5, 3.2, 1.6, 6.5 ] )Di Python, kita dapat mengambil matriks masukan pengguna dengan berbagai cara. Beberapa metode untuk matriks input pengguna dengan Python ditunjukkan di bawah ini
Kode #1
# A basic code for matrix input from user
________47__48_______ int(input(Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 1Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 2
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 3= int(input(Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 9Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 2
Enter the number of rows:2 Enter the number of columns:2 Enter the entries in a single line separated by space: 1 2 3 1 [[1 2] [3 1]]2
Enter the number of rows:2 Enter the number of columns:2 Enter the entries in a single line separated by space: 1 2 3 1 [[1 2] [3 1]]3= Enter the number of rows:2 Enter the number of columns:2 Enter the entries in a single line separated by space: 1 2 3 1 [[1 2] [3 1]]5
________13______6(Enter the number of rows:2 Enter the number of columns:2 Enter the entries in a single line separated by space: 1 2 3 1 [[1 2] [3 1]]8Enter the number of rows:2 Enter the number of columns:2 Enter the entries in a single line separated by space: 1 2 3 1 [[1 2] [3 1]]9
# A basic code for matrix input from user1
# A basic code for matrix input from user2 # A basic code for matrix input from user3# A basic code for matrix input from user4 # A basic code for matrix input from user5# A basic code for matrix input from user6# A basic code for matrix input from user7
# A basic code for matrix input from user8# A basic code for matrix input from user9________48__13_______5
# A basic code for matrix input from user8# A basic code for matrix input from user2 4# A basic code for matrix input from user4 # A basic code for matrix input from user5 7 8
9R0int(________51__47_______4
# A basic code for matrix input from user8R6
R8
# A basic code for matrix input from user2 # A basic code for matrix input from user3# A basic code for matrix input from user4 # A basic code for matrix input from user5________48______3
# A basic code for matrix input from user8# A basic code for matrix input from user2 4# A basic code for matrix input from user4 # A basic code for matrix input from user5=9
int0Enter the number of rows:2 Enter the number of columns:2 Enter the entries in a single line separated by space: 1 2 3 1 [[1 2] [3 1]]6int2= int4Enter the number of rows:2 Enter the number of columns:2 Enter the entries in a single line separated by space: 1 2 3 1 [[1 2] [3 1]]9
# A basic code for matrix input from user8Enter the number of rows:2 Enter the number of columns:2 Enter the entries in a single line separated by space: 1 2 3 1 [[1 2] [3 1]]6int8
Keluaran
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 _Satu baris
int_9
(0= (2int(input(6# A basic code for matrix input from user2 (_______________________________________________________________________________________.
Kode #2. Menggunakan fungsi input7 dan input8
Di Python, ada perpustakaan populer bernama NumPy. Perpustakaan ini adalah perpustakaan dasar untuk perhitungan ilmiah apa pun. Ini juga digunakan untuk array multidimensi dan seperti yang kita ketahui matriks adalah array persegi panjang, kita akan menggunakan pustaka ini untuk matriks input pengguna
input9 Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 00
________47__48_______ int(input(Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 1Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 2
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 3= int(input(Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 9Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 2
________13______6(Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 22Enter the number of rows:2 Enter the number of columns:2 Enter the entries in a single line separated by space: 1 2 3 1 [[1 2] [3 1]]9
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 _25
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 _26
Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 27= Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 29(Enter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 31(intEnter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 34inputEnter the number of rows:2 Enter the number of columns:3 Enter the entries rowwise: 1 2 3 4 5 6 1 2 3 4 5 6 36