Konu: Algoritma Analizi dersinde yapılan örnekler, verilen ödevler ve uygulama örnekleri...
HAFTA - 1 (23 Şubat 2018 Cuma)
import time millis_1 = int(round(time.time())) print(millis_1) time.sleep(5) millis_2 = int(round(time.time())) print(millis_2) print(millis_2 - millis_1) #1519384535 #1519384540 #5
def fibRec(n): if(n<2): return n else: return fibRec(n-1)+fibRec(n-2)
fibRec(0),fibRec(1),fibRec(2),fibRec(3),fibRec(4) #(0, 1, 1, 2, 3)
n=40 time_1 = int(round(time.time())) fibRec(n) time_2 = int(round(time.time())) print(n, "için geçen süre:", time_2-time_1,"saniye") #40 için geçen süre: 58 saniye
def fib(n): a, b = 0, 1 while n>0: a, b = b, a+b n -= 1 return a
print(fib(0), fib(1), fib(2), fib(3), fib(4)) #(0, 1, 1, 2, 3)
n=1000000 time_3 = int(round(time.time())) fib(n) time_4 = int(round(time.time())) print(n, "için geçen süre:", time_4-time_3,"saniye") #1000000 için geçen süre: 9 saniye
import matplotlib.pyplot as plt for n in range(41): time_1 = int(round(time.time())) fib(n) #loop time_2 = int(round(time.time())) print(n, "için geçen süre (loop):", time_2-time_1, "saniye") time_A = time_2-time_1 time_3 = int(round(time.time())) fibRec(n) #np time_4 = int(round(time.time())) print(n, "için geçen süre (np):", time_4-time_3, "saniye") time_B = time_4-time_3 plt.plot([n,time_A]) plt.plot([n,time_B]) plt.xlabel('Blue: loop values\nOrange: np values') plt.ylabel('Saniye') plt.show() #40 için geçen süre (loop): 0 saniye #40 için geçen süre (np): 61 saniye
HAFTA - 2 (2 Mart 2018 Cuma)
from random import randrange
def createRandomList(n): myarray = [] for i in range(n): foo = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50] random_index = randrange(0,len(foo)) myarray.append(foo[random_index]) return myarray
def bubbleSort(alist): step = 0 for passnum in range(len(alist)-1,0,-1): for i in range(passnum): if alist[i]>alist[i+1]: step += 1 temp = alist[i] alist[i] = alist[i+1] alist[i+1] = temp return step
def insertionSort(alist): step = 0 for index in range(1,len(alist)): currentvalue = alist[index] position = index while position>0 and alist[position-1]>currentvalue: step += 1 alist[position]=alist[position-1] position = position-1 alist[position]=currentvalue return step
newArray = createRandomList(20) print("Bubble Sort\n-----------") print("Eski Liste: " , newArray) step = bubbleSort(newArray) print("Yeni Liste: " , newArray) print("Adım Sayısı: " , step, "\n") newArray = createRandomList(20) print("Insertion Sort\n--------------") print("Eski Liste: " , newArray) step = insertionSort(newArray) print("Yeni Liste: " , newArray) print("Adım Sayısı: " , step)
#Bubble Sort #----------- #Eski Liste: [4, 25, 31, 45, 23, 19, 1, 49, 28, 24, 41, 14, 41, 0, 18, 11, 43, 37, 8, 33] #Yeni Liste: [0, 1, 4, 8, 11, 14, 18, 19, 23, 24, 25, 28, 31, 33, 37, 41, 41, 43, 45, 49] #Adım Sayısı: 97 #Insertion Sort #-------------- #Eski Liste: [48, 41, 14, 3, 47, 16, 36, 36, 26, 26, 2, 20, 36, 35, 8, 48, 47, 49, 46, 49] #Yeni Liste: [2, 3, 8, 14, 16, 20, 26, 26, 35, 36, 36, 36, 41, 46, 47, 47, 48, 48, 49, 49] #Adım Sayısı: 71
HAFTA - 3 (16 Mart 2018 Cuma)
import sys my_L_1 = [] size_1 = sys.getsizeof(my_L_1) print("Başlangıç Boyutu: " , size_1) for i in range(10): my_L_1.append(str(i+1) + ".test") if(size_1 != sys.getsizeof(my_L_1)): print("Dizi yer değiştirdi" , end=" ~~ ") size_1 = sys.getsizeof(my_L_1) print("Size: " , size_1) print(my_L_1)
#Başlangıç Boyutu: 64 #Dizi yer değiştirdi ~~ Size: 96 #Dizi yer değiştirdi ~~ Size: 128 #Dizi yer değiştirdi ~~ Size: 192 #['1.test', '2.test', '3.test', '4.test', '5.test', '6.test', '7.test', '8.test', '9.test', '10.test']
HAFTA - 4/1 (23 Mart 2018 Cuma)
#include < stdio.h > #include < stdlib.h > typedef struct list { int sayi; struct list *sonraki; } liste; liste *ilk = NULL, *son = NULL, *yeni, *silGecici, *gecici; void menu(); void ekle(); void sil(int ); void listele(); void arama(int ); void ekranTemizle(); void bekle(); int main() { while (1) { menu(); } return 0; } void ekle(int veri) { yeni = (liste *) malloc(sizeof (liste)); yeni->sayi = veri; yeni->sonraki = NULL; if (ilk == NULL) { ilk = yeni; son = ilk; } else { son->sonraki = yeni; son = son->sonraki; } } void sil(int veri) { int kontrol = 0; if (ilk->sayi == veri) { silGecici = ilk; ilk = ilk->sonraki; free(silGecici); kontrol = 1; } else { gecici = ilk; while (gecici->sonraki != NULL) { if (gecici->sonraki->sayi == veri) { kontrol = 1; silGecici = gecici->sonraki; if (silGecici == son) son = gecici; gecici->sonraki = silGecici->sonraki; free(silGecici); break; } gecici = gecici->sonraki; } } if (kontrol == 0) printf("Silmek Istediginiz Eleman Listede Yoktur...\n"); else printf("%d sayisi listeden silinmiştir...\n", veri); } void listele() { gecici = ilk; while (gecici != NULL) { printf("%d\n", gecici->sayi); gecici = gecici->sonraki; } } void arama(int aranan) { int kontrol = 0; gecici = ilk; while (gecici != NULL) { if (aranan == gecici->sayi) { kontrol = 1; } gecici = gecici->sonraki; } if (kontrol == 1) printf("\n%d Sayisi Bulunmustur..\n", aranan); else printf("\nAranan Sayi Bulunamamistir...\n"); } void menu() { int secim, sayi; printf("1-Ekleme\n"); printf("2-Listeleme\n"); printf("3-Arama\n"); printf("4-Silme\n"); printf("5-Cikis\n"); printf("Secim: "); scanf("%d", &secim); switch (secim) { case 1: ekranTemizle(); printf("Sayi Giriniz: "); scanf("%d", &sayi); ekle(sayi); printf("%d sayisi listeye eklendi\n",sayi); bekle(); break; case 2: ekranTemizle(); if (ilk == NULL) { printf("Liste Bos.\n"); bekle(); break; } listele(); bekle(); break; case 3: ekranTemizle(); if (ilk == NULL) { printf("Liste Bos.\n"); bekle(); break; } printf("Sayi Giriniz: "); scanf("%d", &sayi); arama(sayi); bekle(); break; case 4: ekranTemizle(); if (ilk == NULL) { printf("Liste Bos.\n"); bekle(); break; } printf("Sayi Giriniz: "); scanf("%d", &sayi); sil(sayi); bekle(); break; case 5: ekranTemizle(); printf("Program Bitti!\n"); exit(0); break; default: ekranTemizle(); printf("Hatali Secim\n"); bekle(); } } void ekranTemizle() { system("cls"); } void bekle() { char temp; printf("Devam etmek icin Enter'a basiniz!\n"); temp = getchar(); temp = getchar(); ekranTemizle(); }
HAFTA - 4/2 (30 Mart 2018 Cuma)
class DynamicArray: def __init__(self): self._n = 0 self._capacity = 1 self._A = self._make_array(self._capacity) def __len__(self): return self._n def __getitem__(self, k): if not 0 <= k < self._n: raise IndexError('invalid index') return self._A[k] def append(self, obj): if self._n == self._capacity: self._resize(2 * self._capacity) self._A[self._n] = obj self._n +=1 def _resize(self, c): B = self._make_array(c) for k in range(self._n): B[k] = self._A[k] self._A = B self._capacity = c def _make_array(self, c): return (c * ctypes.py_object)()
HAFTA - 5 (30 Mart 2018 Cuma)
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 def vector_inner_product(v1,v2): if(len(v1) != len(v2)): print("vektörler eşit değil") return result = 0 for i in range(len(v1)): result = result+v1[i]*v2[i] return result def create_A_matrix(m,n): my_matrix=[] for i in range(m): my_matrix.append(create_A_vector(n)) return my_matrix def matrix_multiplication(a,b): m=len(a) n=len(a[0]) p=len(b[0]) my_matrix_sonuc = 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] my_matrix_sonuc[i][j]=vector_inner_product(vector_1,vector_2) return my_matrix_sonuc,m*n*p my_vector_1=create_A_vector(10) my_vector_2=create_A_vector(10) vector_product=vector_inner_product(my_vector_1,my_vector_2) my_matrix_1=create_A_matrix(3,5) my_matrix_2=create_A_matrix(5,10) c=matrix_multiplication(my_matrix_1,my_matrix_2) c #([[139, 117, 108, 128, 120, 67, 74, 82, 117, 84], # [132, 99, 102, 104, 115, 55, 82, 75, 104, 88], # [22, 24, 30, 26, 27, 20, 13, 25, 16, 13]], # 150) 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,5) a_5=create_A_matrix(5,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] print(r_1,"toplam islem sayisi:",islem_sayisi) #([[114523605, 110281990, 79742362]], 15) toplam islem sayisi: 11020
HAFTA - 6 - Ödev (3 Nisan 2018 Salı)
#include < stdio.h > #include < stdlib.h > #include < time.h > #include < conio.h > static int sum = 0; struct Node{ int data; struct Node *left,*right; }; struct Node *addNode(struct Node *root,int data){ if(root==NULL){ struct Node *tmp = (struct Node*)malloc(sizeof(struct Node)); tmp->data = data; tmp->left = NULL; tmp->right = NULL; return tmp; } if(root->data < data){ root->right = addNode(root->right,data); }else{ root->left = addNode(root->left,data); } return root; } void Print(struct Node *root){ if(root){ printf("%d ",root->data); Print(root->left); Print(root->right); } } int minimum(struct Node *root){ if(root->left == NULL){ return root->data; } return minimum(root->left); } int maximum(struct Node *root){ if(root->right == NULL){ return root->data; } return maximum(root->right); } int nodeNumber(struct Node *root){ if(root==NULL) return 0; return 1+nodeNumber(root->left)+nodeNumber(root->right); } int depth(struct Node * root){ if(root==NULL) return 0; else{ int left_height=0, right_height=0; left_height=depth(root->left); right_height=depth(root->right); if(right_height>left_height){ return right_height+1; } else{ return left_height+1; } } } int summaryDepth(struct Node * root){ if(root==NULL) return 0; int depthValue = depth(root)-1; int i, summary=0; for(i=depthValue; i>=0; i--){ summary += i; } return summary; } void summaryDepth2(struct Node *root, int depthValue){ if(root==NULL) return; if(root->right != NULL || root->left != NULL){ depthValue++; if(root->left != NULL){ sum += depthValue; summaryDepth2(root->left,depthValue); } if(root->right != NULL){ sum += depthValue; summaryDepth2(root->right,depthValue); } } } int search(struct Node *root, int val){ while (root != NULL) { if (val == root->data) { return 1; } if (val < root->data) { root = root->left; } else { root = root->right; } } return 0; } int main(){ struct Node *root=NULL; int randomNumber; srand(time(NULL)); int i, number; printf("\n\tEnter Node Value: "); scanf("%d", &number); for(i=0; i < number; i++){ randomNumber=1+rand()%100; root = addNode(root,randomNumber); } printf("\n\t"); Print(root); printf("\n\n\tMinimum \t: %d",minimum(root)); printf("\n\tMaximum \t: %d",maximum(root)); printf("\n\tNode Number \t: %d",nodeNumber(root)); printf("\n\tDepth \t\t: %d",depth(root)-1); printf("\n\tSum Depth-Line \t: %d",summaryDepth(root)); summaryDepth2(root,0); printf("\n\tSum Depth-All \t: %d\n",sum); getch(); //printf("\nSearch (25) = %d",search(root,25)); return 0; }
HAFTA - 6 (6 Nisan 2018 Cuma)
# coding: utf-8 # In[89]: def scale(data,factor): n=len(data) for i in range(n): data[i]*=factor my_list_1=["1","2",3,4,5] scale(my_list_1,5) my_list_1 # In[57]: from random import randint def create_n_random_numbers_to_be_inserted_tree(size): my_vector=[] for i in range(size): my_vector.append(randint(-500,500)) print(my_vector) return my_vector def BinaryTree(r=0): return [0, [], []] def insertData(tree_root,data): if(tree_root[0]==None): tree_root=[0,[],[]] return if(tree_root is not None and tree_root!=[]): if(tree_root[0]> data and tree_root[1]==[]): tree_root[1]=[data,[],[]] elif(tree_root[0]>data and tree_root[1]!=[]): insertData(tree_root[1],data) elif(tree_root[0] < data and tree_root[2]==[]): tree_root[2]=[data,[],[]] elif(tree_root[0] < data and tree_root[2]!=[]): insertData(tree_root[2],data) else: print(" duplicate .... ") def prety_print(tree, depth=0): ret = "" # Print right branch if tree[2] != []: ret += prety_print(tree[2],depth + 1) # Print own value ret += "\n" + (" "*depth) + str(tree[0]) # Print left branch if tree[1] != []: ret += prety_print(tree[1],depth + 1) return ret def get_minimum_of_the_tree(tree_pass_parameter): tree_root=tree_pass_parameter if (tree_root is not None and tree_root!=[]): if(tree_root[1]==[]): return tree_root[0] else: return get_minimum_of_the_tree(tree_root[1]) def get_maximum_of_the_tree(tree_pass_parameter): tree_root=tree_pass_parameter if (tree_root is not None and tree_root!=[]): if(tree_root[2]==[]): #print("şu an max da : ",tree_root[2]) return tree_root[0] else: #print("şu an ilerliyor : ",tree_root[2]) return get_maximum_of_the_tree(tree_root[2]) def traverse_the_tree(tree,depth=0): global sum_of_the_depth_for_all_nodes # sum_of_the_depth_for_all_nodes=0 tree_root=tree if (tree_root is not None and tree_root!=[]): if(tree_root[1]==[]): pass #print(tree_root[0],",",end="") else: depth=depth+1 traverse_the_tree(tree_root[1],depth) if (tree_root is not None and tree_root!=[]): print(tree_root[0],",",end="") sum_of_the_depth_for_all_nodes=sum_of_the_depth_for_all_nodes+depth if (tree_root is not None and tree_root!=[]): if(tree_root[2]==[]): pass #print(tree_root[0],",",end="") else: traverse_the_tree(tree_root[2],depth) #return sum_of_the_depth_for_all_nodes def find_data_on_tree(tree,data,position="0"): tree_root=tree #print("tree : ",tree) #print("data :",data) #print() if (tree_root is not None and tree_root!=[]): if(tree_root[0]==data): print("found on",position) print(tree_root[0],",",end="") return position elif (tree_root[1]!=[] and data < tree_root[0]): find_data_on_tree(tree_root[1],data,position+"1") elif (tree_root[2]!=[] and data > tree_root[0]): find_data_on_tree(tree_root[2],data,position+"2") else: print("not found ") return 0 # In[71]: r=BinaryTree() agacta_kac_sayi_var=10 my_numbers=create_n_random_numbers_to_be_inserted_tree(agacta_kac_sayi_var) for i in my_numbers: insertData(r,i) ## print(prety_print(r)) max=get_maximum_of_the_tree(r) min=get_minimum_of_the_tree(r) max,min print(prety_print(r)) sum_of_the_depth_for_all_nodes=0 traverse_the_tree(r) sum_of_the_depth_for_all_nodes # In[65]: r # In[66]: find_data_on_tree(r,-38) # In[67]: r=None my_list=[] r=BinaryTree(0) my_numbers=create_n_random_numbers_to_be_inserted_tree(15) for i in my_numbers: insertData(r,i) ## print(prety_print(r)) max=get_maximum_of_the_tree(r) min=get_minimum_of_the_tree(r) min,max traverse_the_tree(r) #------------------------------------------------------- # coding: utf-8 # In[3]: # Dynamic Programming Python implementation of Matrix # Chain Multiplication. See the Cormen book for details # of the following algorithm import sys # Matrix Ai has dimension p[i-1] x p[i] for i = 1..n def MatrixChainOrder(p, n): # For simplicity of the program, one extra row and one # extra column are allocated in m[][]. 0th row and 0th # column of m[][] are not used m = [[0 for x in range(n)] for x in range(n)] # m[i,j] = Minimum number of scalar multiplications needed # to compute the matrix A[i]A[i+1]...A[j] = A[i..j] where # dimension of A[i] is p[i-1] x p[i] # cost is zero when multiplying one matrix. for i in range(1, n): m[i][i] = 0 # L is chain length. for L in range(2, n): for i in range(1, n-L+1): j = i+L-1 m[i][j] = sys.maxsize # previously sys.maxint in original code from https://www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-multiplication/ for k in range(i, j): # q = cost/scalar multiplications q = m[i][k] + m[k+1][j] + p[i-1]*p[k]*p[j] if q < m[i][j]: m[i][j] = q return m[1][n-1] # In[4]: # Driver program to test above function arr = [1, 2, 3 ,4] size = len(arr) print("Minimum number of multiplications is " + str(MatrixChainOrder(arr, size))) # This Code is contributed by Bhavya Jain # In[57]: class My_Matrix(): matrix=None def __init__(self,m=5,n=5,value=-1): from random import randint matrix_list_of_list=[] low=0 high=100 for i in range(m): row_list=[] for j in range(n): if value==-1: row_list.append(randint(low,high)) else: row_list.append(value) matrix_list_of_list.append(row_list) self.matrix=matrix_list_of_list def prety_print(self): m=len(self.matrix) n=len(self.matrix[0]) print() print(" ************************************************** " ) print("dimension is :",m,n) print(" ",end="") for i in range(len(self.matrix[0])): print(i," ",end="") print(end="\n") for i in range(len(self.matrix)): print("row ",i," : ",self.matrix[i]) print(" ----------------------------------------------------------" ) def get_row_i(self,i): return self.matrix[i] def get_column_i(self,i): col_i=[[x[i]] for x in m_1.matrix] return col_i def matrix_addition(self,matrix_2): m=len(self.matrix) n=len(self.matrix[0]) for i in range(m): for j in range(n): self.matrix[i][j]=self.matrix[i][j]+matrix_2.matrix[i][j] def matrix_subraction(self,matrix_2): m=len(self.matrix) n=len(self.matrix[0]) for i in range(m): for j in range(n): self.matrix[i][j]=self.matrix[i][j]-matrix_2.matrix[i][j] def matrix_scalar_multiplication(self,alpha): m=len(self.matrix) n=len(self.matrix[0]) for i in range(m): for j in range(n): self.matrix[i][j]=self.matrix[i][j]*alpha def test_matrix(): m_1=My_Matrix(m=3,n=4,value=1) m_1.prety_print() m_2=My_Matrix(m=3,n=4) m_2.prety_print() test_matrix() # In[ ]: def matrix_vector_multiplication(matrix->My_Matrix,vector->My_Vector): matrix. b=[] b. for # In[60]: m_1=My_Matrix() m_1.prety_print() m_1.matrix_scalar_multiplication(5) m_1.prety_print() # In[62]: m_1=My_Matrix() m_1.prety_print() m_2=My_Matrix() m_2.prety_print() m_1.matrix_addition(m_2) m_1.prety_print() m_1.matrix_subraction(m_2) m_1.prety_print() # In[55]: m_1.get_row_i(1) # In[56]: m_1.get_column_i(1) # In[104]: class My_Vector(): vector=None size=None def __init__(self,n=5,value=0,low=0,high=10): from random import randint self.vector=[] if(value is "random" or value is "r"): for i in range(n): self.vector.append([randint(low,high)]) self.size=(n,1) else: for i in range(n): self.vector.append([value]) self.size=(n,1) def scalar_multiplication(self,alpha=1): m=self.size[0] for i in range(m): self.vector[i][0]=self.vector[i][0]*alpha # self.vector[i][0]=self.vector[i][0]*alpha,error if array 5x1, nx1 then access array[i] instead of array[i][0] # düzeldi ... def vectorel_addition(self,vector_2): if(self.size!=vector_2.size): print("they should be same size ") return m=self.size[0] for i in range(m): # print(self.size) # print(vector_2.size) self.vector[i][0]=self.vector[i][0]+vector_2.vector[i][0] def vector_inner_product(self,vector_2): result=0 if(self.size!=vector_2.size): print("not same dim in vectors") return else: for i in range(self.size[0]): result=result+self.vector[i][0]*vector_2.vector[i][0] return result def get_m_n(self): print("dimension : ",self.size) return self.size def pprint(self): if(self.vector is None): print("not yet initialize") else: for i in range(len(self.vector)): print(self.vector[i]) def test_vector(): v_1=My_Vector(2,2) v_1.get_m_n() v_1.pprint() v_2=My_Vector(2,'r') v_2.get_m_n() v_2.pprint() v_1.vectorel_addition(v_2) v_1.get_m_n() v_1.pprint() a=v_1.vector_inner_product(v_2) print("inner product : ",a) test_vector() # In[99]: v_2.vector # In[97]: from random import randint def create_vector(size): my_vector=[] for i in range(size): my_vector.append(randint(0,50)) return my_vector def vector_inner_product(v1,v2): result=0 if(len(v1)!=len(v2)): print("not same dim in vectors") return else: for i in range(len(v1)): result=result+v1[i]*v2[i] return result def create_matrix(m,n): my_matrix=[] for i in range(m): my_matrix.append(create_vector(n)) return my_matrix def matrix_multiplication(a,b): m=len(a) n_1=len(a[0]) n_2=len(b) p=len(b[0]) my_matrix=create_matrix(m,p) for i in range(m): for j in range(p): row_a=a[i] col_b=[row[j] for row in b] my_matrix[i][j]=vector_inner_product(row_a,col_b) return my_matrix def matrix_pprint(m): print("------------------------") for i in range(len(m)): print(m[i]) print("------------------") def matrix_pprint_loop(m): i=0 #print("---------------------------------------------------------") for item in m: print(" item : ",i) i=i+1 matrix_pprint(item) #print("--------------------------------------------------------") v_1=create_vector(4) m_1=create_matrix(2,7) v_1,m_1 # In[46]: m_2=create_matrix(2,2) m_2 # In[47]: c_1=[row[1] for row in m_2] c_1 # In[61]: m_1=create_matrix(2,3) m_2=create_matrix(3,2) m_3=matrix_multiplication(m_1,m_2) matrix_pprint(m_1) matrix_pprint(m_2) matrix_pprint(m_3) # In[35]: matrix_pprint(m_2) # In[36]: matrix_pprint(m_3) # In[16]: # row_i_th=matrix[i] m_1[1] # In[17]: # col_i_th=[row[i] for row in matrix] col_3_th=[row[3] for row in m_1] col_3_th # In[5]: v1=create_vector(5) v2=create_vector(5) x=vector_inner_product(v1,v2) v1,v2,x # In[64]: m_1=create_matrix(4,4) matrix_pprint(m_1) # In[72]: m=4 n=4 a=zip(range(m),range(n)) a for item in a: print(item) # In[74]: indices_for_lower_part=[(i,j) for i in range(m) for j in range(n) if i > j] matrix_pprint(indices_for_lower_part) # In[88]: import copy def matrix_transform_it_to_lower_triangular(matrix_1): matrix=copy.deepcopy(matrix_1) m=len(matrix) n=len(matrix) indices_for_lower_part=[(i,j) for i in range(m) for j in range(n) if i > j] for i,j in indices_for_lower_part: matrix[i][j]=0 return matrix def matrix_transform_it_to_upper_triangular(matrix_1): matrix=copy.deepcopy(matrix_1) m=len(matrix) n=len(matrix) indices_for_lower_part=[(i,j) for i in range(m) for j in range(n) if i < j] for i,j in indices_for_lower_part: matrix[i][j]=0 return matrix # In[90]: m_1=create_matrix(4,4) matrix_pprint(m_1) m_2=matrix_transform_it_to_lower_triangular(m_1) # or m_1=matrix_transform_it_to_lower_triangular(m_1) matrix_pprint(m_2) matrix_pprint(m_1) m_3=matrix_transform_it_to_upper_triangular(m_1) # or m_1=matrix_transform_it_to_lower_triangular(m_1) matrix_pprint(m_3) matrix_pprint(m_1) # In[98]: m_1=create_matrix(3,1) m_2=create_matrix(1,3) m_3=matrix_multiplication(m_1,m_2) matrix_pprint_loop([m_1,m_2,m_3]) # In[99]: m_1=create_matrix(1,3) m_2=create_matrix(3,1) m_3=matrix_multiplication(m_1,m_2) matrix_pprint_loop([m_1,m_2,m_3]) # In[87]: v_1.vector[0][0] # In[86]: v_2.vector