def test_makelist(self): ''' Test make array 1 dimention. ''' res1 = ut.makelist(10) self.assertEqual(len(res1), 10) res2 = ut.makelist(100,0) self.assertEqual(len(res2), 100)
def testprint(): ''' Test print for checking cgi and loading module. ''' testdata = ut.makelist(10) return str(testdata)
def dijkstra(route, start, end): ''' Dijkstra route search function with priority queue. Return tuple of most smallest route list and cost. Argument start is start vertex index. Argument end is end vertex index. ''' #TODO:This is not work normality.Must be repaired. que = qu.PriorityQueue() vtxlen = route.vertexlen distance = ut.makelist(length=vtxlen, initvalue=float("inf")) distance[start] = 0 que.put((0, start)) #shorest distance, vertex number smallroute = [start] totalcost = 0 while que.empty() == False: smldist = que.get() vertexid = smldist[1] if distance[vertexid] < smldist[0]: continue for vtxidx in route.edges: edges = route.edges[vtxidx] for edgidx in range(len(edges)): edge = edges[edgidx] cost = distance[vertexid]+edge.cost if distance[edge.to] > cost: distance[edge.to] = cost que.put((distance[edge.to], edge.to)) smallroute.append(edge.to) totalcost += edge.cost return (smallroute, totalcost)
def __init__(self, size=0, values=[]): ''' Init heap with heap size and values. ''' if len(values) > 0: self.values = values else: self.values = ut.makelist(size) self.pos = 0
def fib(number): ''' fibonach function ''' memo = ut.makelist(number+1,0) def rec_fib(n): if n <= 1: return n if memo[n] != 0 : return memo[n] memo[n] = rec_fib(n-1)+rec_fib(n-2) return memo[n] return rec_fib(number)
def without_priority_queue_dijkstra(route, start, end): ''' Dijkstra route search function without priority queue. ''' #TODO:This is not work normality.Must be repaired. vtxlen = route.vertexlen d = ut.makelist(length=vtxlen, initvalue=float("inf")) used = ut.makelist(length=vtxlen, initvalue=False) d[start] = 0 prev = ut.makelist(length=vtxlen, initvalue=-1) while True: v = -1 for u in range(vtxlen): if (used[u] == False) and (v == -1 or d[u] < d[v]): v = u if v == -1: break used[v] = True for u in range(vtxlen): cost = d[v]+route.edges[v][u].cost if d[u] > cost: d[u] = cost prev[u] = v path = [] t = end while t != -1: t = prev[t] path.append(t) return (path[::-1], d[end])
def multireg(x1s, x2s, ys): ''' Multiple regression analysis. ''' ones = ut.makelist(len(x1s), 1) vecs1 = [lr.Vector([x1,x2,one]) for x1,x2,one in zip(x1s,x2s,ones)] mat1 = lr.Matrix(vecs1) vx1 = lr.Vector(x1s) vx2 = lr.Vector(x2s) vone = lr.Vector(ones) vecs2 = [vx1,vx2,vone] mat2 = lr.Matrix(vecs2) vy = lr.Vector(ys) mat3 = lr.Matrix([vy]) res = (mat1*mat2)**-1*mat1*mat3 return res.rows[0].cols
def prime(n): ''' Caluculate prime number length under "n" by Eratosthenes sieve. n: under n prime number detect and return. ''' prime = [] is_prime = ut.makelist(n+1, initvalue=True) is_prime[0] = False is_prime[1] = False i = 2 #Most minimum prime number. while i <= n: if is_prime[i] == True: prime.append(i) j = 2*i while j <= n: is_prime[j] = False j += i i += 1 return prime
def __eigen_2dim(self): ''' Calculate eigen value for 2 dimension matrix. ''' formula = ut.makelist(length=3, initvalue=0) formula[0] = 1 #x^2 coef rows = self.rows cols1 = rows[0].cols cols2 = rows[1].cols a = cols1[0] d = cols2[1] formula[1] = a*-1+(-1)*d formula[2] = self.det() egs = nt.quadeq(formula) if len(egs) <= 1: raise ValueError("Multiple root cannot deal.") res = {} for eg in egs: resA = a-eg resC = cols2[0] #TODO: reduct function is fault. # if resA != 1 and resC != 1: # if resA>resC: # resA,resC = nt.reduct(resA, resC) # elif resC<resA: # resC,resA = nt.reduct(resC, resA) # else: # resA = resC = 1 res[eg] = Vector([-resC,resA]) return res
def __init__(self, forknum=5): self.forks = ut.makelist(length=forknum, initvalue=False)