-
Notifications
You must be signed in to change notification settings - Fork 0
/
leibniz.py
163 lines (147 loc) · 5.28 KB
/
leibniz.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
f = open("output.txt", "w")
new = []
for line in open ('matrix.txt').readlines():
for a in line.splitlines():
a = a.strip()
if not a:
continue
#split by ','
b = a.split(",")
#convert to number
v = [int(i) for i in b]
new.append(v)#append to new list
def bubble_sort(alist):
"""
Pre: take an argument alist
Post: return either + or -
Bestcase: O(N)
Worstcase: O(N^2)
N is the length of list
"""
step = 0
n = len(alist)
for i in range(n-1,0,-1):#count from backward
swapped = False
for j in range(i):
if alist[j] > alist[j+1]:
swap(alist,j,j+1)
step += 1#step +=1 whenever swapped is true
swapped = True
if not swapped:
break#break earlier
#if number of steps is even, add
if step == 0 or step%2 == 0:
return '+'
else:#else minus
return '-'
def swap(a,i,j):
"""
Pre:alist and two position
Post: swapped
"""
a[i],a[j] = a[j], a[i]
def checkDimension(a):
"""
Pre: Take in a matrix
Post: return True or Error
Bestcase: O(N)
Worstcase: O(N)
N is the length of parameter a
"""
for i in range (len(a)):
#check column and row
if len(a[i]) != len(a):
print("Incorrect dimensions")
return "Error"
return True
tmp2=[]
array=[]
#OPTIMIZED-Decision explanation:
####################################################################################################################
#Reference1: Below code refers to heap pseudo-algorithm from wikipedia
# https://en.wikipedia.org/wiki/Heap%27s_algorithm#Details_of_the_algorithm
#Reference2: Research paper on analysis at all the permutations algorithm
# http://homepage.divms.uiowa.edu/~goodman/22m150.dir/2007/Permutation%20Generation%20Methods.pdf
# Where in conclusion, it stated that Heap's algorithm is slightly more effecient and can code faster
#Reference3: Books Electircal Engineering and Intelligent Systems Chapter6.1
# stated"The heap method runs faster and is simpler than other methods as presented"
#Reference4: Where I got a better understanding:
# https://www.youtube.com/watch?v=YMMNgn6qZVA
######################################################################################################################
#Implementation of Heap's algorithm
def det(b,alist):
"""
pre: b and alist is define
post:perform operation of aux_det, return tmp2,array2,array
Bestcase: O(1)
Worstcase: O(1)
"""
array2 = []
aux_det(b,alist,len(alist),array2,tmp2,array)
return tmp2,array2,array
def aux_det(b,a,n,array2,tmp2,array):
"""
pre: b,a,n,array2,tmp2,array are define
post:perform operation
Bestcase: O(N!), N is length of row/column of matrix(eg. 3x3)
Worstcase: O(N!)
"""
if n == 1:
array2.append(a[:])
for j in range(n):
tmp = []
for k in range(len(b)):
tmp.append(b[k][a[k]-1])
#init as 1
mult=1
for i in range(len(tmp)):
mult *= tmp[i]
#append to an array to check its sign later
array.append(mult)
tmp2.append(tmp)
else:
#perform the algorithm stated
for i in range(n):
#produce all of the permutations that end with the element that
#has just been moved to the last position
aux_det(b,a,n-1,array2,tmp2,array)
if(n%2==1):#when n is odd switch the first element and the last one
swap(a,0,n-1)
else:#when n is even, switch ith element and the last one
swap(a,i,n-1)
def main():
"""
Pre: take a matrix and its all possible permutation
Post: either return result or error message
(Part1)Bestcase: O(N^2), N is length of row/column of matrix(eg. 3x3)
Worstcase: O(N^2)
(Part2)Bestcase: O(N!), N is length of row/column of matrix(eg. 3x3)
Worstcase: O(N!)
(Part3)Bestcase: O(N), N is the length of row/column of matrix(eg. 3x3)
Worstcase: O(N)
"""
#process if dimension all right
if checkDimension(new) == True:
final = 0
f.write("N = "+str(len(new))+"\n")
f.write("Input Matrix: \n")
#Part1:Print as required format
for i in range (len(new)):
for j in range (len(new)):
f.write(str(new[i][j]))
f.write("\n")
#Part2:Calculate Determination
plist = [i+1 for i in range(len(new))]
a,b,array = det(new,plist)
#Part3: Sign
for i in range(len(b)):
if ((bubble_sort(b[i])) == '+'):
final += array[i]#sgn(+1)
else:
final -= array[i]#sgn(-1)
f.write("\nDeterminant = "+str(final))
return
else:
return 'Error'
print(main())
f.close()