# Hermite(), inc(), nonneg(), avg(), table(), stats() # # R. Perry, July 2018 from sympy import zeros # create G from A,b and call lllhermite() # def Hermite( Dio, A, b): G = zeros(A.shape[1]+1, A.shape[0]+1) G[:-1, :-1] = A.T G[-1, :-1] = b.reshape(1, b.shape[0]) G[-1, -1] = 1 H, P, rank = Dio.lllhermite(G) return G, H, P, rank # inc: add 1 to c; changes c and z in caller # returns (count,valid) where count is number of z values tested # and valid is 1 if no z values are negative, 0 on overflow, otherwise -1 # c[0]...c[r-1] represent digits in the range 0 to n # pruning: skip values with sum(c) > n or which make negative z worse # def inc(c, r, n, z, y, R, ylen): count = 0 t = sum(c) for i in range(r): valid = 1 c[i] += 1 t += 1 if c[i] > n or t > n: t -= c[i]; c[i] = 0; continue z[0,:] = y + c*R count += 1 prune = 0 for j in range(ylen): if z[j] < 0: valid = -1 if R[i,j] < 0: prune = 1 if prune: t -= c[i]; c[i] = 0; continue return (count,valid) return (count,0) # nonneg: return 1 if all vector elements are non-negative, otherwise 0 # def nonneg( z, n): for i in range(n): if z[i] < 0: return 0 return 1 # avg: calculate average solution # def avg( xTlist): m = len(xTlist) n = len(xTlist[0]) a=[0 for i in range(n)] for i in range(m): for j in range(n): a[j] += xTlist[i][j] for j in range(n): a[j] /= m return a # table: print matrix as HTML table # def table( a): (m,n) = a.shape print('') for i in range(m): print(' ') space='' for j in range(n): if j == n-1: space='' print(' ' + space) print(' ') print('
 ' + str(a[i,j]) + '
') # stats: compute mean and variance from data # x[i] is number of occurances of vals[i] # def stats( x, vals): n = sum(x) m = sum(x[i]*vals[i] for i in range(len(x)))/n v = 0 if n < 2 else sum(x[i]*(vals[i]-m)**2 for i in range(len(x)))/(n-1) return m, v