From 0c088515973d154ff857926d648f0598ecce33cb Mon Sep 17 00:00:00 2001 From: mbinary Date: Tue, 2 Oct 2018 21:24:06 +0800 Subject: [PATCH] Add computaional method --- .gitignore | 2 - README.md | 73 +++++--- .../dynamic-programming/matrix-multiply.py | 4 - computationalMethod/README.md | 35 ++++ computationalMethod/interplotion.py | 84 +++++++++ computationalMethod/iteration.py | 108 ++++++++++++ computationalMethod/least_square.py | 104 +++++++++++ computationalMethod/linear_equation.py | 118 +++++++++++++ computationalMethod/numerical_differential.py | 13 ++ computationalMethod/numerical_integration.py | 70 ++++++++ .../solve-linear-by-iteration.py | 130 ++++++++++++++ computationalMethod/tongyu_equation.py | 163 ++++++++++++++++++ computationalMethod/vector_norm.py | 134 ++++++++++++++ dataStructure/bTree.py | 12 ++ dataStructure/redBlackTree.py | 12 ++ {notes => docs}/README.md | 0 {notes => docs}/algorithm-general.md | 0 {notes => docs}/b-tree.md | 0 {notes => docs}/fib-heap.md | 55 ++++-- {notes => docs}/graph.md | 20 ++- {notes => docs}/hashTable.md | 0 {notes => docs}/red-black-tree.md | 0 {notes => docs}/sort.md | 0 {notes => docs}/src/coin1.jpg | Bin {notes => docs}/src/coin2.jpg | Bin {notes => docs}/src/coin3.jpg | Bin {notes => docs}/src/coin4.jpg | Bin {notes => docs}/src/recursive-tree.jpg | Bin {notes => docs}/src/symbol.jpg | Bin {notes => docs}/src/test.jpg | Bin {notes => docs}/tree.md | 0 .../Vec2d.hs | 0 .../__pycache__/LCS.cpython-37.pyc | Bin .../lcs.hs | 0 .../lcs.py | 12 ++ dynamicProgramming/matrix-multiply.py | 16 ++ .../splitStripe.hs | 0 .../splitStripe.py | 12 ++ .../testVec2d.hs | 0 {algorithm => math}/cantor.cc | 0 math/isPrime.py | 44 +++++ math/num_weight.py | 50 ++++++ {algorithm => search}/8Astar.py | 0 .../__pycache__/quickSort.cpython-35.pyc | Bin .../__pycache__/select.cpython-37.pyc | Bin {algorithm/sort => sort}/binaryTree.py | 0 {algorithm/sort => sort}/heapSort.py | 0 {algorithm/sort => sort}/quickSort.py | 0 {algorithm/sort => sort}/radixSort.py | 0 {algorithm/sort => sort}/select.py | 0 {algorithm/sort => sort}/shellSort.py | 0 {algorithm => string}/manacher.py | 0 {algorithm => string}/markov.py | 0 {algorithm => string}/sunday.py | 0 54 files changed, 1221 insertions(+), 50 deletions(-) delete mode 100644 algorithm/dynamic-programming/matrix-multiply.py create mode 100644 computationalMethod/README.md create mode 100644 computationalMethod/interplotion.py create mode 100644 computationalMethod/iteration.py create mode 100644 computationalMethod/least_square.py create mode 100644 computationalMethod/linear_equation.py create mode 100644 computationalMethod/numerical_differential.py create mode 100644 computationalMethod/numerical_integration.py create mode 100644 computationalMethod/solve-linear-by-iteration.py create mode 100644 computationalMethod/tongyu_equation.py create mode 100644 computationalMethod/vector_norm.py rename {notes => docs}/README.md (100%) rename {notes => docs}/algorithm-general.md (100%) rename {notes => docs}/b-tree.md (100%) rename {notes => docs}/fib-heap.md (85%) rename {notes => docs}/graph.md (94%) rename {notes => docs}/hashTable.md (100%) rename {notes => docs}/red-black-tree.md (100%) rename {notes => docs}/sort.md (100%) rename {notes => docs}/src/coin1.jpg (100%) rename {notes => docs}/src/coin2.jpg (100%) rename {notes => docs}/src/coin3.jpg (100%) rename {notes => docs}/src/coin4.jpg (100%) rename {notes => docs}/src/recursive-tree.jpg (100%) rename {notes => docs}/src/symbol.jpg (100%) rename {notes => docs}/src/test.jpg (100%) rename {notes => docs}/tree.md (100%) rename {algorithm/dynamic-programming => dynamicProgramming}/Vec2d.hs (100%) rename {algorithm/dynamic-programming => dynamicProgramming}/__pycache__/LCS.cpython-37.pyc (100%) rename {algorithm/dynamic-programming => dynamicProgramming}/lcs.hs (100%) rename {algorithm/dynamic-programming => dynamicProgramming}/lcs.py (73%) create mode 100644 dynamicProgramming/matrix-multiply.py rename {algorithm/dynamic-programming => dynamicProgramming}/splitStripe.hs (100%) rename {algorithm/dynamic-programming => dynamicProgramming}/splitStripe.py (76%) rename {algorithm/dynamic-programming => dynamicProgramming}/testVec2d.hs (100%) rename {algorithm => math}/cantor.cc (100%) create mode 100644 math/isPrime.py create mode 100644 math/num_weight.py rename {algorithm => search}/8Astar.py (100%) rename {algorithm/sort => sort}/__pycache__/quickSort.cpython-35.pyc (100%) rename {algorithm/sort => sort}/__pycache__/select.cpython-37.pyc (100%) rename {algorithm/sort => sort}/binaryTree.py (100%) rename {algorithm/sort => sort}/heapSort.py (100%) rename {algorithm/sort => sort}/quickSort.py (100%) rename {algorithm/sort => sort}/radixSort.py (100%) rename {algorithm/sort => sort}/select.py (100%) rename {algorithm/sort => sort}/shellSort.py (100%) rename {algorithm => string}/manacher.py (100%) rename {algorithm => string}/markov.py (100%) rename {algorithm => string}/sunday.py (100%) diff --git a/.gitignore b/.gitignore index aebb447..c064458 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,5 @@ .* *.o *.exe -tree_link.py __pycache__/** !.gitignore -todo diff --git a/README.md b/README.md index bc79bfc..f9f21d0 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # Algorithm and Data Structures >Notes and codes for learning algorithm and data structures :smiley: -Some pictures and idead are from `<> +Some pictures and ideas are from `<> I use python 3.6+ and c++ to implements them. Since I used f-Strings in python, you may use python 3.6+ to run the following python scripts. @@ -14,36 +14,67 @@ So,if you wannt to view the notes which contain latex math formulas and are in m # Index * [.](.) - * [notes](./notes) - * [alg-general.md](./notes/alg-general.md) - * [hashTable.md](./notes/hashTable.md) - * [red-black-tree.md](./notes/red-black-tree.md) - * [sort.md](./notes/sort.md) - * [tree.md](./notes/tree.md) - * [b-tree.md](./notes/b-tree.md) - * [graph](./notes/graph.md) - * [fibonacci-heap](./notes/fib-heap.md) - * [algorithm](./algorithm) - * [8Astar.py](./algorithm/8Astar.py) - * [cantor.cc](./algorithm/cantor.cc) - * [manacher.py](./algorithm/manacher.py) - * [markov.py](./algorithm/markov.py) - * [sort](./algorithm/sort) - * [sunday.py](./algorithm/sunday.py) + * [computationalMethod](./computationalMethod) + * [interplotion.py](./computationalMethod/interplotion.py) + * [iteration.py](./computationalMethod/iteration.py) + * [least_square.py](./computationalMethod/least_square.py) + * [linear_equation.py](./computationalMethod/linear_equation.py) + * [numerical_differential.py](./computationalMethod/numerical_differential.py) + * [numerical_integration.py](./computationalMethod/numerical_integration.py) + * [README.md](./computationalMethod/README.md) + * [solve-linear-by-iteration.py](./computationalMethod/solve-linear-by-iteration.py) + * [tongyu_equation.py](./computationalMethod/tongyu_equation.py) + * [vector_norm.py](./computationalMethod/vector_norm.py) * [dataStructure](./dataStructure) - * [redBlackTree.py](./dataStructure/redBlackTree.py) - * [bTree.py](./dataStructure/bTree.py) - * [hashTable.py](./dataStructure/hashTable.py) - * [splayTree.py](./dataStructure/splayTree.py) * [allOone](./dataStructure/allOone) * [binaryHeap.py](./dataStructure/binaryHeap.py) * [binaryTree.py](./dataStructure/binaryTree.py) + * [bTree.py](./dataStructure/bTree.py) * [graph](./dataStructure/graph) + * [hashTable.py](./dataStructure/hashTable.py) * [huffman](./dataStructure/huffman) * [leftHeap.py](./dataStructure/leftHeap.py) * [loserTree.py](./dataStructure/loserTree.py) * [map.cc](./dataStructure/map.cc) * [polynomial.cpp](./dataStructure/polynomial.cpp) * [polynomial.py](./dataStructure/polynomial.py) + * [redBlackTree.py](./dataStructure/redBlackTree.py) + * [splayTree.py](./dataStructure/splayTree.py) * [trie.py](./dataStructure/trie.py) * [winnerTree.py](./dataStructure/winnerTree.py) + * [docs](./docs) + * [algorithm-general.md](./docs/algorithm-general.md) + * [b-tree.md](./docs/b-tree.md) + * [fib-heap.md](./docs/fib-heap.md) + * [graph.md](./docs/graph.md) + * [hashTable.md](./docs/hashTable.md) + * [README.md](./docs/README.md) + * [red-black-tree.md](./docs/red-black-tree.md) + * [sort.md](./docs/sort.md) + * [tree.md](./docs/tree.md) + * [dynamicProgramming](./dynamicProgramming) + * [lcs.hs](./dynamicProgramming/lcs.hs) + * [lcs.py](./dynamicProgramming/lcs.py) + * [matrix-multiply.py](./dynamicProgramming/matrix-multiply.py) + * [splitStripe.hs](./dynamicProgramming/splitStripe.hs) + * [splitStripe.py](./dynamicProgramming/splitStripe.py) + * [testVec2d.hs](./dynamicProgramming/testVec2d.hs) + * [Vec2d.hs](./dynamicProgramming/Vec2d.hs) + * [math](./math) + * [cantor.cc](./math/cantor.cc) + * [isPrime.py](./math/isPrime.py) + * [num_weight.py](./math/num_weight.py) + * [README.md](./README.md) + * [search](./search) + * [8Astar.py](./search/8Astar.py) + * [sort](./sort) + * [binaryTree.py](./sort/binaryTree.py) + * [heapSort.py](./sort/heapSort.py) + * [quickSort.py](./sort/quickSort.py) + * [radixSort.py](./sort/radixSort.py) + * [select.py](./sort/select.py) + * [shellSort.py](./sort/shellSort.py) + * [string](./string) + * [manacher.py](./string/manacher.py) + * [markov.py](./string/markov.py) + * [sunday.py](./string/sunday.py) diff --git a/algorithm/dynamic-programming/matrix-multiply.py b/algorithm/dynamic-programming/matrix-multiply.py deleted file mode 100644 index 80ddcd4..0000000 --- a/algorithm/dynamic-programming/matrix-multiply.py +++ /dev/null @@ -1,4 +0,0 @@ -def adjustOrd(sizes): - ''' adjust the chain-multiply of matrix, sizes=[row1,row2,..,rown,coln]''' - n = len(sizes) - if n<3: return diff --git a/computationalMethod/README.md b/computationalMethod/README.md new file mode 100644 index 0000000..9ae967d --- /dev/null +++ b/computationalMethod/README.md @@ -0,0 +1,35 @@ +# 计算方法 +>一些计算方法的算法,比如插值,拟合 近似计算,解方程组等 +有些功能numpy, sympy可能已经就有,但是为了学习各种计算方法,我就重新写了一遍,主要使用的是numpy的数组,矩阵,sympy的符号运算 + +# 需要 +* python3 +* python modules + - sympy + - numpy + +# 目录说明 +## interplotion +插值, 有Lagrange插值, Newton插值 +## iteration +迭代, 二分迭代, 不动点迭代,差商迭代, 弦截法迭代 +## least_square +最小二乘拟合, 解矛盾方程组 +## linear_equation +解线性方程组,用到 +* doolittle分解 +* crout分解 +* ldlt分解 +* 列主元消元法 +## vector-norm +计算向量,矩阵的各种范数 +## tongyu_equation +解同余方程 + + +## LICENCE +[MIT](LICENCE.txt) + +## 联系我 +* mail: +* QQ : 414313516 diff --git a/computationalMethod/interplotion.py b/computationalMethod/interplotion.py new file mode 100644 index 0000000..99033a9 --- /dev/null +++ b/computationalMethod/interplotion.py @@ -0,0 +1,84 @@ +''' mbinary +######################################################################### +# File : interplotion.py +# Author: mbinary +# Mail: zhuheqin1@gmail.com +# Blog: https://mbinary.coding.me +# Github: https://github.com/mbinary +# Created Time: 2018-10-02 21:14 +# Description: +######################################################################### +''' + +######################################################################### +# File : interplotion.py +# Author: mbinary +# Mail: zhuheqin1@gmail.com +# Blog: https://mbinary.github.io +# Github: https://github.com/mbinary +# Created Time: 2018-05-18 09:29 +# Description: 插值计算,有牛顿插值,拉格朗日插值,以及通过插值得到的多项式估计新的函数值 +######################################################################### + + +import sympy +from collections import namedtuple +from functools import reduce +from operator import mul + +X = sympy.Symbol ('x') +point = namedtuple('point',['x','y']) + +class interplotion: + def __init__(self,points): + self.points = [point(x,y) for x,y in points] + self.xs= [i for i,j in points] + self.poly,self.rem = self.newton(self.points,0,len(self.points)-1) + + def newton(self,li,a,b): + '''li:[(x,f(x))...]''' + + qs = [li[0].y] + + def quoDiff(begin,end): + if begin == end:return li[begin].y + q = (quoDiff(begin+1,end)-quoDiff(begin,end-1))/(li[end].x-li[begin].x) + if begin == a:qs.append(q) + return q + + quoDiff(a,b) + poly ,base = 0, 1 + for i,q in enumerate(qs): + poly += q*base + base*= X-li[i].x + return poly, base*qs[-1] + def lagrange(self,points=None): + xs = None + if points is None: + xs = self.xs + points = self.points + else: xs =[x for x,y in points] + product = reduce(mul,[X-x for x in xs],1) + poly = 0 + for x,y in points: + tmp = product/(X-x) + coef = y/(tmp.subs(X,x)) + poly+= coef *tmp + return poly + + def predict(self,val,poly = None): + if poly is None:poly = self.poly + return poly.subs(X,val) # note the func subs + + +if __name__ == '__main__': + f = interplotion([(81,9),(100,10),(121,11)]) + p = f.lagrange() + print(p.subs(X,105)) + print(p) + + intor = interplotion([(0,11),(0.02,9),(0.04,7),(0.06,10)]) + p = intor.lagrange() + print(p) + res = intor.predict(0.08) + print(res) diff --git a/computationalMethod/iteration.py b/computationalMethod/iteration.py new file mode 100644 index 0000000..6ac66a6 --- /dev/null +++ b/computationalMethod/iteration.py @@ -0,0 +1,108 @@ +''' mbinary +######################################################################### +# File : iteration.py +# Author: mbinary +# Mail: zhuheqin1@gmail.com +# Blog: https://mbinary.coding.me +# Github: https://github.com/mbinary +# Created Time: 2018-10-02 21:14 +# Description: +######################################################################### +''' + +import sympy +import numpy as np +from math import sqrt + + +def newton(y:sympy.core,x0:float,epsilon:float=0.00001,maxtime:int=50) ->(list,list): + ''' + newton 's iteration method for finding a zeropoint of a func + y is the func, x0 is the init x val: int float epsilon is the accurrency + ''' + if epsilon <0:epsilon = -epsilon + ct =0 + t = y.free_symbols + varsymbol = 'x' if len(t)==0 else t.pop() + x0= float(x0) + y_diff = y.diff() + li = [x0] + vals = [] + while 1: + val = y.subs(varsymbol,x0) + vals.append(val) + x = x0- val/y_diff.subs(varsymbol,x0) + li.append(x) + ct+=1 + if ct>maxtime: + print("after iteration for {} times, I still havn't reach the accurrency.\ + Maybe this function havsn't zeropoint\n".format(ct)) + return li ,val + if abs(x-x0)(list,list): + ''' + 弦截法, 使用newton 差商计算,每次只需计算一次f(x) + secant method for finding a zeropoint of a func + y is the func , x0 is the init x val, epsilon is the accurrency + ''' + if epsilon <0:epsilon = -epsilon + ct =0 + x0,x1 = float(x0),float(x1) + li = [x0,x1] + t = y.free_symbols + varsymbol = 'x' if len(t)==0 else t.pop() + last = y.subs(varsymbol,x0) + vals = [last] + while 1: + cur = y.subs(varsymbol,x1) + vals.append(cur) + x = x1-cur*(x1-x0)/(cur-last) + x0 ,x1= x1,x + last = cur + li.append(x) + ct+=1 + if ct>maxtime: + print("after iteration for {} times, I still havn't reach the accurrency.\ + Maybe this function havsn't zeropoint\n".format(ct)) + return li,vals + if abs(x0-x1)dict: + '''solve nonlinear equations:''' + li = list(init_dic.keys()) + delta = {i:0 for i in li} + ct = 0 + while 1: + ys = np.array([f.subs(init_dic) for f in funcs],dtype = 'float') + mat = np.matrix([[i.diff(x).subs(init_dic) for x in li] for i in funcs ],dtype = 'float') + delt = np.linalg.solve(mat,-ys) + for i,j in enumerate(delt): + init_dic[li[i]] +=j + delta[li[i]] = j + if ct>maxtime: + print("after iteration for {} times, I still havn't reach the accurrency.\ + Maybe this function havsn't zeropoint\n".format(ct)) + return init_dic + if sqrt(sum(i**2 for i in delta.values())) File Name: least-square.py + > Author: mbinary + > Mail: zhuheqin1@gmail.com + > Created Time: Sat 07 Apr 2018 09:55:25 PM DST + > Blog: https://mbinary.github.io + > Description: 最小二乘法解线性方程组, 解矛盾方程组 + ************************************************************************''' + + +import re +import numpy as np + + + +def solveConflitEqualtion(A,y): + '''solve equation like this: Av = y, + A:m*n v:n*1 y:m*1 + return vector v + ''' + A = np.matrix(A) + y = np.array(y) + ata = A.T*A + print('AtA') + print(ata) + return np.linalg.solve(ata,A.T*y) # note that is numpy.linalg.solve + +def solveLinear(point,index): + y = [[i[1]] for i in point] + x = [[i[0]] for i in point] + A = [] + for i in x: + A.append([i[0]**j for j in index]) + res = solveConflitEqualtion(A,y) + print('the solution is : \n',res) + print('namely: ') + items = ['{:.4f}x^{}'.format(res[i,0],j) for i, j in enumerate(index)] + print('phi(x) = ',' + '.join(items)) + +def handleInput(s=None,y=None): + # numPt = re.compile (r'\d*\.{0,1}\d+') + if not s: s = input('input matrix A:m*n //m>=n\n') + s = s.replace(' ','') + li = re.findall(r'(\[(\d+)(,(\d+))+\])',s) + li = [parseLst(i[0]) for i in li] + if not y:y = input('input a vector y:n*1\n') + y = parseLst(y) + print('Equation: Av = y:') + + print('y is as follows: ') + print(y) + print('A is as follows: ') + for i in li: + for j in i: + print('{}'.format(j).rjust(5),end='') + print('') + + print('result v is as follows: ') + res = solveConflitEqualtion(li,y) + print(res) +def parseLst(s): + s = s.strip('[]') + li = s.split(',') + li = [float(j) for j in li] + return li + + +if __name__ == '__main__': + ''' + li = '[[23,2],[2,5],[2,6]]' + y = '[1,3]' + while True: + handleInput(li,y) + s = input('input y to continue, n for exit') + if s!='y':break + ''' + point = [(-3,14.3),(-2,8.3),(-1,4.7),(2,-8.3),(4,-22.7)] + lst = [0,3] + solveLinear(point,lst) + + point= [(-3,14.3),(-2,8.3),(-1,4.7),(2,8.3),(4,22.7)] + lst = [0,2] + solveLinear(point,lst) + + A = [[1,2],[2,1],[1,1]] + y = [[5],[6],[4]] + res = solveConflitEqualtion(A,y) + + print(res) + A = [[1,-2],[1,5],[2,1],[1,1]] + y = [[1],[13.1],[7.9],[5.1]] + print(solveConflitEqualtion(A,y)) diff --git a/computationalMethod/linear_equation.py b/computationalMethod/linear_equation.py new file mode 100644 index 0000000..7ad0770 --- /dev/null +++ b/computationalMethod/linear_equation.py @@ -0,0 +1,118 @@ +''' mbinary +######################################################################### +# File : linear_equation.py +# Author: mbinary +# Mail: zhuheqin1@gmail.com +# Blog: https://mbinary.coding.me +# Github: https://github.com/mbinary +# Created Time: 2018-10-02 21:14 +# Description: +######################################################################### +''' + +#coding: utf-8 +'''************************************************************************ + > File Name: doolittle.py + > Author: mbinary + > Mail: zhuheqin1@gmail.com + > Blog: https://mbinary.github.io + > Created Time: 2018-04-20 08:32 + ************************************************************************''' + +import numpy as np +def getLU(A): + '''doolittle : A = LU, + L is in down-triangle form, + U is in up-triangle form + ''' + m,n = A.shape + if m!=n:raise Exception("this matrix is not inversable") + + L = np.zeros([m,m]) + U = np.zeros([m,m]) + L = np.matrix(L) + U = np. matrix(U) + U[0] = A[0] + L[:,0] = A[:,0] / A[0,0] + for i in range(1,m): + for j in range(i,m): + U[i,j]= A[i,j] - sum(L[i,k]*U[k,j] for k in range(i)) + L[j,i] = (A[j,i] - sum(L[j,k]*U[k,i] for k in range(i)))/U[i,i] + print(L) + print(U) + return L,U + + +def gauss_prior_elimination(A): + '''using guass elimination,get up_trianglge form of A''' + m,n = A.shape + if m!=n:raise Exception("[Error]: matrix is not inversable") + B = np.matrix(A,dtype=float) # necessary,otherwise when the dtype of A is int, then it will be wrong + for i in range(m-1): + col = abs(B[i:,i]) # note using abs value, return a matrix in (m-i)x1 form + mx = col.max() + if mx==0: raise Exception("[Error]: matrix is not inversable") + pos = i+col.argmax() + if pos != i : B[[pos,i],:] = B[[i,pos],:] # note how to swap cols/rows + B[i,:] = 1/mx*B[i,:] + for j in range(i+1,m): + #print(B) + B[j,:] -= B[j,i] * B[i,:] + print(B) + return B + +def solveDown(A,b): + '''A is a matrix in down-triangle form''' + sol = np.zeros(b.shape) + for i in range(b.shape[0]): + sol[i,0] = (b[i,0]-sum(A[i,j]*sol[j,0] for j in range(i)))/A[i,i] + return sol + +def solveUp(A,b): + '''A is a matrix in up-triangle form''' + sol = np.zeros(b.shape) + n = b.shape[0] + for i in range(n-1,-1,-1): + sol[i,0] = (b[i,0]-sum(A[i,j]*sol[j,0] for j in range(n-1,i,-1)))/A[i,i] + return sol +def doolittle(A,b): + L,U = getLU(A) + y = solveDown(L,b) + x = solveUp(U,y) + print(y) + print(x) + return x +def ldlt(A,b): + L,U = getLU(A) + D = np.diag(np.diag(U)) + print(D,"D") + z = np.linalg.solve(L,b) + print(z,"z") + y = np.linalg.solve(D,z) + print(y,"y") + x = np.linalg.solve(L.T,y) + print(x,"x") + return x +if __name__ == '__main__': + A = np.matrix([[10,5,0,0], + [2,2,1,0], + [0,10,0,5], + [0,0,2,1]]) + b = np.matrix([[5],[3],[27],[6]]) + gauss_prior_elimination(A) + + '''ldlt + A = np.matrix([[-6,3,2], + [3,5,1], + [2,1,6]]) + b = np.matrix([[-4],[11],[-8]]) + ldlt(A,b) + ''' +''' + A = np.matrix([[2,1,1], + [1,3,2], + [1,2,2]]) + b = np.matrix([[4],[6],[5]]) + doolittle(A,b) +''' + diff --git a/computationalMethod/numerical_differential.py b/computationalMethod/numerical_differential.py new file mode 100644 index 0000000..3b6b4b1 --- /dev/null +++ b/computationalMethod/numerical_differential.py @@ -0,0 +1,13 @@ +''' mbinary +######################################################################### +# File : numerical_differential.py +# Author: mbinary +# Mail: zhuheqin1@gmail.com +# Blog: https://mbinary.coding.me +# Github: https://github.com/mbinary +# Created Time: 2018-10-02 21:14 +# Description: +######################################################################### +''' + + diff --git a/computationalMethod/numerical_integration.py b/computationalMethod/numerical_integration.py new file mode 100644 index 0000000..d022f6f --- /dev/null +++ b/computationalMethod/numerical_integration.py @@ -0,0 +1,70 @@ +''' mbinary +######################################################################### +# File : numerical_integration.py +# Author: mbinary +# Mail: zhuheqin1@gmail.com +# Blog: https://mbinary.coding.me +# Github: https://github.com/mbinary +# Created Time: 2018-10-02 21:14 +# Description: +######################################################################### +''' + + +######################################################################### +# File : numerical integration.py +# Author: mbinary +# Mail: zhuheqin1@gmail.com +# Blog: https://mbinary.github.io +# Github: https://github.com/mbinary +# Created Time: 2018-05-11 08:58 +# Description: +# numerical intergration: using Newton-Cotes integration, and Simpson +# 数值积分, 使用 牛顿-科特斯积分, 辛普森 +######################################################################### + + + +import numpy as np +def trapezoidal(a,b,h,fs): + '''梯形积分公式''' + xs = [i for i in np.arange(a,b+h,h)] + print(xs) + ret = h*(sum(fs)-fs[0]/2 - fs[-1]/2) + print(ret) + return ret + + +def simpson(a,b,h,fs): + '''辛普森积分公式''' + xs = [i for i in np.arange(a,b+h,h)] + print(xs) + ret = h/3*(4* sum(fs[1::2])+ 2*sum(fs[2:-1:2]) + fs[0]+fs[-1]) + print(ret) + return ret + + +def romberg(a,b,f,epcilon): + '''romberg(龙贝格) 数值积分''' + h = b-a + lst1=[h*(f(a)+f(b))/2] + print(lst1) + delta = epcilon + k=1 + while delta >= epcilon: + h/=2 + k+=1 + lst2=[] + lst2.append((lst1[0]+h*2*sum(f(a+(2*i-1)*h) for i in range(1,2**(k-2)+1)))/2) + for j in range(0,k-1): + lst2.append(lst2[j]+(lst2[j]-lst1[j])/(4**(j+1)-1)) + delta = abs(lst2[-1]-lst1[-1]) + lst1=lst2 + print(lst1) + +if __name__=='__main__': + a,b,h = 0.6,1.8,0.2 + fs=[5.7,4.6,3.5,3.7,4.9,5.2,5.5] + trapezoidal(a,b,h,fs) + simpson(a,b,h,fs) + romberg(1,2,lambda x:sin(x**4),1e-4) diff --git a/computationalMethod/solve-linear-by-iteration.py b/computationalMethod/solve-linear-by-iteration.py new file mode 100644 index 0000000..8fd662b --- /dev/null +++ b/computationalMethod/solve-linear-by-iteration.py @@ -0,0 +1,130 @@ +''' mbinary +######################################################################### +# File : solve-linear-by-iteration.py +# Author: mbinary +# Mail: zhuheqin1@gmail.com +# Blog: https://mbinary.coding.me +# Github: https://github.com/mbinary +# Created Time: 2018-10-02 21:14 +# Description: +######################################################################### +''' + +''' +######################################################################### +# File : solve-linear-by-iteration.py +# Author: mbinary +# Mail: zhuheqin1@gmail.com +# Blog: https://mbinary.github.io +# Github: https://github.com/mbinary +# Created Time: 2018-05-04 07:42 +# Description: +######################################################################### +''' +import numpy as np +from operator import le,lt + +def jacob(A,b,x,accuracy=None,times=6): + ''' Ax=b, arg x is the init val, times is the time of iterating''' + A,b,x = np.matrix(A),np.matrix(b),np.matrix(x) + n,m = A.shape + if n!=m:raise Exception("Not square matrix: {A}".format(A=A)) + if b.shape !=( n,1) : raise Exception('Error: {b} must be {n} x1 in dimension'.format(b = b,n=n)) + D = np.diag(np.diag(A)) + DI = np.zeros([n,n]) + for i in range(n):DI[i,i]= 1/D[i,i] + R = np.eye(n) - DI * A + g = DI * b + print('R =\n{}'.format(R)) + print('g =\n{}'.format(g)) + last = -x + if accuracy != None: + ct=0 + while 1: + ct+=1 + tmp = x-last + last = x + mx = max ( abs(i) for i in tmp) + if mx {n:indg n}''' + return {j:i for i in range(m-1) \ + for j in range(m) if (g**i-j)%m==0} + +def gs(m,num=100): + '''return list of m's primary roots below num''' + phi = euler(m) + mp = primeFactoize(phi) + checkLst = [phi//i for i in mp] + return [i for i in range(2,num) \ + if reduce(and_,[(i**n-1)%m !=0 for n in checkLst])] + +def minG(m): + phi = euler(m) + mp = primeFactoize(phi) + checkLst = [phi//i for i in mp] + i=2 + while 1: + if reduce(and_,[(i**n-1)%m !=0 for n in checkLst]):return i + i+=1 + + +class solve: + def __init__(self,equ=None): + self.linearPat= re.compile(r'\s*(\d+)\s*--\s*(\d+)[\s\(]*mod\s*(\d+)') + self.sol = [] + #self.m = m + #self.ind_mp = ind(m,minG(m)) + def noSol(self): + print('equation {equ} has no solution'.format(equ=self.equ)) + def error(self): + print("Error! The divisor m must be postive integer") + + + def solveLinear(self,a,b,m): + '''ax--b(mod m): solve linear equation with one unknown + return ([x1,x2,...],m) + ''' + a,b,m = self.check(a,b,m) + g,x,y=xgcd(a,m) + if a*b%g!=0: + self.noSol() + return None + sol=x*b//g + m0 = m//g + return ([(sol+i*m0)%m for i in range(g)],m) + def check(self,a,b,m): + if m<=0: + self.error() + return None + if a<0:a,b=-a,-b ## important + if b<0:b+= -b//m * m + return a,b,m + + + #def solvePoly(self,m,mp): + ''' mod m, mp:{index:coef} is a dict of the polynomials' coefficient and index''' + ''' g = minG(m) + ind_mp = ind(m,g) + li = [] + for i in mp: + solve + ''' + + def solveHigh(self,a,n,b,m): + ''' ax^n -- b (mod m) ind_mp is a dict of m's {n: indg n}''' + ind_mp = ind(m,minG(m)) + tmp = ind_mp[b] - ind_mp[a] + if tmp < 0:tmp+=m + print(n,tmp) + sol = self.solveLinear(n,tmp,euler(m)) + re_mp = {j:i for i ,j in ind_mp.items()} + print(sol) + return [re_mp[i] for i in sol[0]],m + + def solveGroup(tups): + '''tups is a list of tongyu equation groups, like + [(a1,b1,m1),(a2,b2,m2)...] + and, m1,m2... are all primes + ''' + mp = {} + for a,b,m in tups: + if m in mp: + if mp[m][0]*b!=mp[m][1]*a: + self.noSol() + return + else:mp[m] = (a,b) + product= reduce(lambda i,j:i*j, mp.keys(), 1) + sol = 0 + for i in mp: + x = self.solveLinear(product//i*mp[i][0],1,i) + sol+= x*product//i*mp[i][1] + sol%=m + return ([sol],m) + def __call__(self): + s=input('输入同余方程,用--代表同于号,形如3--5(mod 7)代表3x模7同余于5') + li= self.linearPat.findall(s) + li = [(int(a),int(b),int(m)) for a,b,m in li] + print(self.solveLinear(li[0])) + + +if __name__ == '__main__': + solver = solve() + res = solver.solveLinear(3,6,9) + print(res) + print(solver.solveHigh(1,8,3,11)) diff --git a/computationalMethod/vector_norm.py b/computationalMethod/vector_norm.py new file mode 100644 index 0000000..c75f1d5 --- /dev/null +++ b/computationalMethod/vector_norm.py @@ -0,0 +1,134 @@ +''' mbinary +######################################################################### +# File : vector_norm.py +# Author: mbinary +# Mail: zhuheqin1@gmail.com +# Blog: https://mbinary.coding.me +# Github: https://github.com/mbinary +# Created Time: 2018-10-02 21:14 +# Description: +######################################################################### +''' + +from random import randint,random +import numpy as np +from operator import neg,and_ +from functools import reduce + + +class obj(): + def __init__(self,data): + self.data=np.array(data) + def __add__(self,x): + data = x.data if self.__class__ == x.__class__ else x + return self.__class__(self.data + data) + def __radd__(self,x): + data = x.data if self.__class__ == x.__class__ else x + return self.__class__(data +self.data) + def __iadd__(self,x): + data = x.data if self.__class__ == x.__class__ else x + self.data += data + def __mul__(self,x): + data = x.data if self.__class__ == x.__class__ else x + return self.__class__(self.data * data) + def __imul__(self,x): + data = x.data if self.__class__ == x.__class__ else x + self.data *= data + def __rmul__(self,x): + data = x.data if self.__class__ == x.__class__ else x + return self.__class__(data * self.data) + def __neg__(self): + return neg(self) + def __abs__(self): + return abs(self.data) + ''' + @property + def data(self): + return self.data + @data.setter + def data(self,s): + self.data = s + ''' + def norm(self,n=0): + '''the default is +oo norm''' + absolute = abs(self.data) + if n < 1 :return max(absolute) + return (sum(absolute**n))**(1/n) + def hasNorm(self): + '''check norm's three necessary conditions: + 1. not neg + 2. homogenious (qici) + 3. triangle inequlity + + there is much probably wrong + ''' + bl = reduce(and_,[self.norm(i)>=0 for i in range(3)]) + if bl: + n = randint(2,100) + bl = reduce(and_,[n*(self.norm(i))==(n*self).norm(i) for i in range(3)]) + if bl: + another = self*randint(2,10)-randint(1,100) + return reduce(and_,[(another+self).norm(i)<=another.norm(i)+self.norm(i) for i in range(3)]) + return False + +class vector(obj): + def __init__(self,arr): + ''' arr: iterable''' + self.data =np.array(arr) + def innerProduct(self,x): + return sum(self.data*x) + def outerProduct(self,x): + pass + + +class matrix(obj): + def __init__(self,s): + '''s is a list of lists''' + self.data=np.mat(s) + self.T = None + self. I = None + ''' + @property + def T(self): + if self.T==None:self.T = self.data.T + return self.T + @T.setter + def T(self,s): + self.T = s + @property + def I(self): + if self.I == None: self.I = self.data.I + return self.I + + @I.setter + def I(self,s): + self.I = s + ''' + def E(self,n=None): + if n is None: n = self.data.shape[0] + return np.eye(n) + + def norm(self,n=0): + absolute = abs(self.data) + if n < 1: + # max of one row sum + return max([sum(i) for i in absolute]) + if n==1:return self.norm1() + elif n==2:return self.norm2() + def norm1(self): + ''' max of sum of cols''' + absolute = abs(self.data) + return max(absolute.sum(axis=0)) + def norm2(self): + ''' max of sum of rows''' + absolute = abs(self.data) + return max(absolute.sum(axis=1)) + def norm_f(self): + return sum((self.data**2).sum(axis=1))**0.5 + +if __name__ =='__main__': + v1 = vector([1,-2,3,4]) + v2 = vector([0,2,0,5]) + m1 = matrix([v1,v2,v2,v1]) + print([v1.norm(i) for i in range(3)]) + diff --git a/dataStructure/bTree.py b/dataStructure/bTree.py index 1b9a888..927b168 100644 --- a/dataStructure/bTree.py +++ b/dataStructure/bTree.py @@ -1,3 +1,15 @@ +''' mbinary +######################################################################### +# File : bTree.py +# Author: mbinary +# Mail: zhuheqin1@gmail.com +# Blog: https://mbinary.coding.me +# Github: https://github.com/mbinary +# Created Time: 2018-08-29 12:49 +# Description: +######################################################################### +''' + class node: def __init__(self,keys=None,isLeaf = True,children=None): if keys is None:keys=[] diff --git a/dataStructure/redBlackTree.py b/dataStructure/redBlackTree.py index 7caff91..73ce868 100644 --- a/dataStructure/redBlackTree.py +++ b/dataStructure/redBlackTree.py @@ -1,3 +1,15 @@ +''' mbinary +######################################################################### +# File : redBlackTree.py +# Author: mbinary +# Mail: zhuheqin1@gmail.com +# Blog: https://mbinary.coding.me +# Github: https://github.com/mbinary +# Created Time: 2018-07-14 16:15 +# Description: +######################################################################### +''' + ''' ######################################################################### # File : redBlackTree.py diff --git a/notes/README.md b/docs/README.md similarity index 100% rename from notes/README.md rename to docs/README.md diff --git a/notes/algorithm-general.md b/docs/algorithm-general.md similarity index 100% rename from notes/algorithm-general.md rename to docs/algorithm-general.md diff --git a/notes/b-tree.md b/docs/b-tree.md similarity index 100% rename from notes/b-tree.md rename to docs/b-tree.md diff --git a/notes/fib-heap.md b/docs/fib-heap.md similarity index 85% rename from notes/fib-heap.md rename to docs/fib-heap.md index 57e0029..dc337b2 100644 --- a/notes/fib-heap.md +++ b/docs/fib-heap.md @@ -1,3 +1,29 @@ +--- +title: 『数据结构』Fibonacci-heap +date: 2018-09-06 19:09 +categories: 数据结构与算法 +tags: [数据结构,斐波那契堆] +keywords: 数据结构,斐波那契堆 +mathjax: true +description: +--- + + + +- [1. 结构](#1-结构) +- [2. 势函数](#2-势函数) +- [3. 最大度数](#3-最大度数) +- [4. 操作](#4-操作) + - [4.1. 创建一个斐波那契堆](#41-创建一个斐波那契堆) + - [4.2. 插入一个结点](#42-插入一个结点) + - [4.3. 寻找最小结点](#43-寻找最小结点) + - [4.4. 合并两个斐波那契堆](#44-合并两个斐波那契堆) + - [4.5. 抽取最小值](#45-抽取最小值) + - [4.6. 关键字减值](#46-关键字减值) + - [4.7. 删除结点](#47-删除结点) +- [5. 最大度数的证明](#5-最大度数的证明) + + ![](https://upload-images.jianshu.io/upload_images/7130568-22531846a72b0d83.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) @@ -12,16 +38,15 @@ # 2. 势函数 +下面用势函数来分析摊还代价, 如果你不明白, 可以看[摊还分析](https://www.jianshu.com/p/052fbe9d92a4) + $\Phi(H) = t(H) + 2m(h)$ t 是根链表中树的数目,m(H) 表示被标记的结点数 最初没有结点 # 3. 最大度数 -$D(n)\leqslant \lfloor lgn \rfloor$ -![](https://upload-images.jianshu.io/upload_images/7130568-c9e0cd3be4e98c4b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) - - +结点的最大度数(即孩子数)$D(n)\leqslant \lfloor lgn \rfloor$, 证明放在最后 # 4. 操作 @@ -133,19 +158,13 @@ def cascading-cut(H,y): ## 4.7. 删除结点 +```python decrease(H,nd, MIN) - +extract-min(H) +``` -- [1. 结构](#1-结构) -- [2. 势函数](#2-势函数) -- [3. 最大度数](#3-最大度数) -- [4. 操作](#4-操作) - - [4.1. 创建一个斐波那契堆](#41-创建一个斐波那契堆) - - [4.2. 插入一个结点](#42-插入一个结点) - - [4.3. 寻找最小结点](#43-寻找最小结点) - - [4.4. 合并两个斐波那契堆](#44-合并两个斐波那契堆) - - [4.5. 抽取最小值](#45-抽取最小值) - - [4.6. 关键字减值](#46-关键字减值) - - [4.7. 删除结点](#47-删除结点) - - \ No newline at end of file + +# 5. 最大度数的证明 +这也是`斐波那契`这个名字的由来, +$D(n)\leqslant \lfloor lgn \rfloor$ +![](https://upload-images.jianshu.io/upload_images/7130568-c9e0cd3be4e98c4b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) diff --git a/notes/graph.md b/docs/graph.md similarity index 94% rename from notes/graph.md rename to docs/graph.md index da38f93..7741d19 100644 --- a/notes/graph.md +++ b/docs/graph.md @@ -1,3 +1,13 @@ +--- +title: 图算法 +date: 2018-09-06 19:10 +categories: 数据结构与算法 +tags: [图,算法] +keywords: 图,算法 +mathjax: true +description: +--- + - [1. 图](#1-图) @@ -161,7 +171,7 @@ for edge as u,v in sorted(G.E): return edges ``` 如果并查集的实现采用了 按秩合并与路径压缩技巧, 则 find 与 union 的时间接近常数 -所以时间复杂度在于排序边, 即 $O(ElgE)$, 而 $E ## 3.2. Prim 算法 用了 BFS, 类似 Dijkstra 算法 @@ -190,7 +200,7 @@ while not que.isempty(): # 4. 单源最短路 -求一个结点到其他结点的最短路径, 可以用 Bellman-ford算法, 或者 Dijkstra算法. +求一个结点到其他结点的最短路径, 可以用 Bellman-Ford算法, 或者 Dijkstra算法. 定义两个结点u,v间的最短路 $$ \delta(u,v) = \begin{cases} @@ -205,7 +215,7 @@ $$ ## 4.1. 负权重的边 -Dijkstra 算法不能处理, 只能用 Bellman-Ford 算法, +Dijkstra 算法不能处理负权边, 只能用 Bellman-Ford 算法, 而且如果有负值圈, 则没有最短路, bellman-ford算法也可以检测出来 ## 4.2. 初始化 @@ -288,7 +298,7 @@ $$ $$ \delta(i,j) = l_{ij}^{(|V|-1)} = l_{ij}^{(|V|)} =l_{ij}^{(|V| + 1)}= ... $$ -所以客户处自底向上计算, 如下 +所以可以自底向上计算, 如下 输入权值矩阵 $W(w_{ij})), L^{(m-1)}$,输出$ L^{(m)}$, 其中 $L^{(1)} = W$, ```python n = L.rows @@ -461,4 +471,4 @@ def ford-fulkerson(G,s,t): # 7. 参考资料 [^1]: 算法导论 -[^2]: 图论, 王树禾 \ No newline at end of file +[^2]: 图论, 王树禾 diff --git a/notes/hashTable.md b/docs/hashTable.md similarity index 100% rename from notes/hashTable.md rename to docs/hashTable.md diff --git a/notes/red-black-tree.md b/docs/red-black-tree.md similarity index 100% rename from notes/red-black-tree.md rename to docs/red-black-tree.md diff --git a/notes/sort.md b/docs/sort.md similarity index 100% rename from notes/sort.md rename to docs/sort.md diff --git a/notes/src/coin1.jpg b/docs/src/coin1.jpg similarity index 100% rename from notes/src/coin1.jpg rename to docs/src/coin1.jpg diff --git a/notes/src/coin2.jpg b/docs/src/coin2.jpg similarity index 100% rename from notes/src/coin2.jpg rename to docs/src/coin2.jpg diff --git a/notes/src/coin3.jpg b/docs/src/coin3.jpg similarity index 100% rename from notes/src/coin3.jpg rename to docs/src/coin3.jpg diff --git a/notes/src/coin4.jpg b/docs/src/coin4.jpg similarity index 100% rename from notes/src/coin4.jpg rename to docs/src/coin4.jpg diff --git a/notes/src/recursive-tree.jpg b/docs/src/recursive-tree.jpg similarity index 100% rename from notes/src/recursive-tree.jpg rename to docs/src/recursive-tree.jpg diff --git a/notes/src/symbol.jpg b/docs/src/symbol.jpg similarity index 100% rename from notes/src/symbol.jpg rename to docs/src/symbol.jpg diff --git a/notes/src/test.jpg b/docs/src/test.jpg similarity index 100% rename from notes/src/test.jpg rename to docs/src/test.jpg diff --git a/notes/tree.md b/docs/tree.md similarity index 100% rename from notes/tree.md rename to docs/tree.md diff --git a/algorithm/dynamic-programming/Vec2d.hs b/dynamicProgramming/Vec2d.hs similarity index 100% rename from algorithm/dynamic-programming/Vec2d.hs rename to dynamicProgramming/Vec2d.hs diff --git a/algorithm/dynamic-programming/__pycache__/LCS.cpython-37.pyc b/dynamicProgramming/__pycache__/LCS.cpython-37.pyc similarity index 100% rename from algorithm/dynamic-programming/__pycache__/LCS.cpython-37.pyc rename to dynamicProgramming/__pycache__/LCS.cpython-37.pyc diff --git a/algorithm/dynamic-programming/lcs.hs b/dynamicProgramming/lcs.hs similarity index 100% rename from algorithm/dynamic-programming/lcs.hs rename to dynamicProgramming/lcs.hs diff --git a/algorithm/dynamic-programming/lcs.py b/dynamicProgramming/lcs.py similarity index 73% rename from algorithm/dynamic-programming/lcs.py rename to dynamicProgramming/lcs.py index 6767557..8cb70a2 100644 --- a/algorithm/dynamic-programming/lcs.py +++ b/dynamicProgramming/lcs.py @@ -1,3 +1,15 @@ +''' mbinary +######################################################################### +# File : lcs.py +# Author: mbinary +# Mail: zhuheqin1@gmail.com +# Blog: https://mbinary.coding.me +# Github: https://github.com/mbinary +# Created Time: 2018-08-25 12:00 +# Description: +######################################################################### +''' + def lcs(a,b): '''time: O(mn); space: O(mn)''' m,n= len(a),len(b) diff --git a/dynamicProgramming/matrix-multiply.py b/dynamicProgramming/matrix-multiply.py new file mode 100644 index 0000000..3f54977 --- /dev/null +++ b/dynamicProgramming/matrix-multiply.py @@ -0,0 +1,16 @@ +''' mbinary +######################################################################### +# File : matrix-multiply.py +# Author: mbinary +# Mail: zhuheqin1@gmail.com +# Blog: https://mbinary.coding.me +# Github: https://github.com/mbinary +# Created Time: 2018-08-24 21:24 +# Description: +######################################################################### +''' + +def adjustOrd(sizes): + ''' adjust the chain-multiply of matrix, sizes=[row1,row2,..,rown,coln]''' + n = len(sizes) + if n<3: return diff --git a/algorithm/dynamic-programming/splitStripe.hs b/dynamicProgramming/splitStripe.hs similarity index 100% rename from algorithm/dynamic-programming/splitStripe.hs rename to dynamicProgramming/splitStripe.hs diff --git a/algorithm/dynamic-programming/splitStripe.py b/dynamicProgramming/splitStripe.py similarity index 76% rename from algorithm/dynamic-programming/splitStripe.py rename to dynamicProgramming/splitStripe.py index 1608384..b216f76 100644 --- a/algorithm/dynamic-programming/splitStripe.py +++ b/dynamicProgramming/splitStripe.py @@ -1,3 +1,15 @@ +''' mbinary +######################################################################### +# File : splitStripe.py +# Author: mbinary +# Mail: zhuheqin1@gmail.com +# Blog: https://mbinary.coding.me +# Github: https://github.com/mbinary +# Created Time: 2018-08-24 17:07 +# Description: +######################################################################### +''' + ''' There is stripe which length is n, priceMap contains a map for different length of stripe and its price diff --git a/algorithm/dynamic-programming/testVec2d.hs b/dynamicProgramming/testVec2d.hs similarity index 100% rename from algorithm/dynamic-programming/testVec2d.hs rename to dynamicProgramming/testVec2d.hs diff --git a/algorithm/cantor.cc b/math/cantor.cc similarity index 100% rename from algorithm/cantor.cc rename to math/cantor.cc diff --git a/math/isPrime.py b/math/isPrime.py new file mode 100644 index 0000000..f03125c --- /dev/null +++ b/math/isPrime.py @@ -0,0 +1,44 @@ +''' mbinary +######################################################################### +# File : isPrime.py +# Author: mbinary +# Mail: zhuheqin1@gmail.com +# Blog: https://mbinary.coding.me +# Github: https://github.com/mbinary +# Created Time: 2018-05-19 21:34 +# Description: +######################################################################### +''' + +# created by mbinary @2018-3-4 +# description: judge a num if it's a prime. It will be more efficient when judging many times + + +primes = [2,3,5,7,11,13] +def isPrime(x): + global primes + if x>primes[-1]: + return genPrime(x) + return twoDivideFind(x,primes) + +def genPrime(x): + global primes + while x>primes[-1]: + left = primes[-1] + right = (left+1)**2 + lst = [] + for i in range(left,right): + for j in primes: + if i%j==0:break + else:lst.append(i) + primes+=lst + else:return twoDivideFind(x,lst) + +def twoDivideFind(x,primes): + a,b = 0, len(primes) + while a<=b: + mid = (a+b)//2 + if primes[mid]x: b= mid-1 + else:return True + return False diff --git a/math/num_weight.py b/math/num_weight.py new file mode 100644 index 0000000..71fd221 --- /dev/null +++ b/math/num_weight.py @@ -0,0 +1,50 @@ +''' mbinary +######################################################################### +# File : num_weight.py +# Author: mbinary +# Mail: zhuheqin1@gmail.com +# Blog: https://mbinary.coding.me +# Github: https://github.com/mbinary +# Created Time: 2018-05-19 21:36 +# Description: +######################################################################### +''' + +def covert(s,basefrom=10,baseto=2): + return d2n(n2d(s,basefrom),baseto) +def n2d(s,base=16): + ''' num of base_n(n<36) to decimal''' + dic = {chr(i+ord('0')):i for i in range(10)} + s=s.upper() + if base>10: + dic.update({chr(i+ord('A')):i+10 for i in range(26)}) + #if base in [16,8,2] : + # p=max(map(s.find,'OBX')) + # s=s[p+1:] #remove prefix of hex or bin or oct + rst=0 + for i in s: + rst=dic[i]+rst*base + return rst + +def d2n(n,base=16): + ''' num of base_n(n<36) to decimal''' + dic = {i:chr(i+ord('0')) for i in range(10)} + if base>10: + dic.update({i+10:chr(i+ord('A')) for i in range(26)}) + rst=[] + while n!=0: + i=int(n/base) + rst.append(dic[n-i*base]) + n=i + return ''.join(rst[::-1]) + + + +''' +>>> n2d(str(d2n(4001))) +4001 +>>> d2n(n2d(str(4001)),2) +'100000000000001' +>>> covert('4001',16,2) +'100000000000001' +''' diff --git a/algorithm/8Astar.py b/search/8Astar.py similarity index 100% rename from algorithm/8Astar.py rename to search/8Astar.py diff --git a/algorithm/sort/__pycache__/quickSort.cpython-35.pyc b/sort/__pycache__/quickSort.cpython-35.pyc similarity index 100% rename from algorithm/sort/__pycache__/quickSort.cpython-35.pyc rename to sort/__pycache__/quickSort.cpython-35.pyc diff --git a/algorithm/sort/__pycache__/select.cpython-37.pyc b/sort/__pycache__/select.cpython-37.pyc similarity index 100% rename from algorithm/sort/__pycache__/select.cpython-37.pyc rename to sort/__pycache__/select.cpython-37.pyc diff --git a/algorithm/sort/binaryTree.py b/sort/binaryTree.py similarity index 100% rename from algorithm/sort/binaryTree.py rename to sort/binaryTree.py diff --git a/algorithm/sort/heapSort.py b/sort/heapSort.py similarity index 100% rename from algorithm/sort/heapSort.py rename to sort/heapSort.py diff --git a/algorithm/sort/quickSort.py b/sort/quickSort.py similarity index 100% rename from algorithm/sort/quickSort.py rename to sort/quickSort.py diff --git a/algorithm/sort/radixSort.py b/sort/radixSort.py similarity index 100% rename from algorithm/sort/radixSort.py rename to sort/radixSort.py diff --git a/algorithm/sort/select.py b/sort/select.py similarity index 100% rename from algorithm/sort/select.py rename to sort/select.py diff --git a/algorithm/sort/shellSort.py b/sort/shellSort.py similarity index 100% rename from algorithm/sort/shellSort.py rename to sort/shellSort.py diff --git a/algorithm/manacher.py b/string/manacher.py similarity index 100% rename from algorithm/manacher.py rename to string/manacher.py diff --git a/algorithm/markov.py b/string/markov.py similarity index 100% rename from algorithm/markov.py rename to string/markov.py diff --git a/algorithm/sunday.py b/string/sunday.py similarity index 100% rename from algorithm/sunday.py rename to string/sunday.py