Algoritma Analizinde Matris Çarpımı
In [1]:
from random import randint
def create_A_vector(size):
my_vector=[]
for i in range(size):
my_vector.append(randint(0,9))
return my_vector
In [2]:
def create_a_matrix(m,n):
my_matrix=[]
for i in range(m):
my_matrix.append(create_A_vector(n))
return my_matrix
In [3]:
def vector_inner_product(v1,v2):
if(len(v1)!=len(v2)):
print("vectörler aynı boyutta olmalı")
return
result=0
for i in range (len (v1)):
result =result+v1[i]*v2[i]
return result
In [4]:
def matrix_multiplication(a,b):
m=len(a)
n=len(a[0])
#n is also rows of the b
p=len(b[0])
my_matrix=create_a_matrix(m,p)
for i in range(m):
for j in range(p):
vector_1=a[i]
vector_2=[i[j] for i in b]# sütunu alma işlemi
my_matrix[i][j]=vector_inner_product(vector_1,vector_2)
return (my_matrix,m*n*p)
In [5]:
#VECTOR ornegi
size=10
my_vector_1=create_A_vector(size)
my_vector_2=create_A_vector(size)
print(my_vector_1,my_vector_2)
In [6]:
#ORNEK
vector_product=vector_inner_product(my_vector_1,my_vector_2)
vector_product
Out[6]:
In [7]:
#Ornek
ornek_matris=create_a_matrix(2,3)
ornek_matris
Out[7]:
In [9]:
#boyut
len(ornek_matris),len(ornek_matris[0])
Out[9]:
In [10]:
#Ornek
a=create_a_matrix(2,3)
b=create_a_matrix(3,5)
c=matrix_multiplication(a,b)
In [11]:
print(a,"\n\n",b,"\n\n",c,"\n\n")
In [12]:
[i for i in a] #diziyi farklı şekilde yazdırma örneği
Out[12]:
In [13]:
[i[1] for i in a] #1. sutun
Out[13]:
In [14]:
a_1=create_a_matrix(1,10)
a_2=create_a_matrix(10,1000)
a_3=create_a_matrix(1000,1)
a_4=create_a_matrix(1,10)
a_5=create_a_matrix(10,3)
islem_sayisi=0
r_1=matrix_multiplication(a_1,a_2)
islem_sayisi=islem_sayisi+r_1[1]
r_1=matrix_multiplication(r_1[0],a_3)
islem_sayisi=islem_sayisi+r_1[1]
r_1=matrix_multiplication(r_1[0],a_4)
islem_sayisi=islem_sayisi+r_1[1]
r_1=matrix_multiplication(r_1[0],a_5)
islem_sayisi=islem_sayisi+r_1[1]
In [15]:
print(r_1," toplam islem sayisi: ",islem_sayisi)
In [16]:
a_1=create_a_matrix(1,10)
#öne 2 ile 3 ü sonra 1 ileçıkan sonucu çarp
a_2=create_a_matrix(10,1000)
a_3=create_a_matrix(1000,1)
a_4=create_a_matrix(1,10)
a_5=create_a_matrix(10,3)
islem_sayisi2=0
r_1=matrix_multiplication(a_2,a_3)
islem_sayisi2=islem_sayisi2+r_1[1]
r_1=matrix_multiplication(a_1,r_1[0])
islem_sayisi2=islem_sayisi2+r_1[1]
r_2=matrix_multiplication(a_4,a_5)
islem_sayisi2=islem_sayisi2+r_1[1]
r_1=matrix_multiplication(r_1[0],r_2[0])
islem_sayisi2=islem_sayisi2+r_1[1]
In [18]:
print(r_1," toplam islem sayisi: ",islem_sayisi2)
In [19]:
print("İşlem sayisi: ",islem_sayisi-islem_sayisi2," kadar azaldı")
In [ ]:
Yorumlar
Yorum Gönder