def Gauss_Newton(eq, data_dict, init_params_dict, eps=1e-4, max_iter=100): J = [] #Jacobian rs = [] #residuals dlen = 0 for key, val in data_dict.items(): dlen = len(val) break params_tuple = tuple(init_params_dict.keys()) nparams = len(params_tuple) for idx in range(dlen): #Substitute x,y,... with their data of measurement r = data_dict[eq.lhs][idx] - eq.rhs for var in data_dict: r = r.subs(var, data_dict[var][idx]) Jrow = [] for idx in range(nparams): Jrow.append(JIT().compile(params_tuple, r.diff(params_tuple[idx]))) J.append(Jrow) rs.append(JIT().compile(params_tuple, r)) init_vals = [] for idx in range(nparams): init_vals.append(init_params_dict[params_tuple[idx]]) for it in range(max_iter): NJ = [] for row in J: NJrow = [] for idx in range(nparams): NJrow.append(row[idx](*init_vals)) NJ.append(NJrow) A = Matrix(NJ) Nrs = [] for r in rs: Nrs.append( r(*init_vals) ) bb = Matrix(Nrs) Ab = (A.T*A).row_join(A.T*bb) sol = solve_linear_system(Ab, *params_tuple) norm = 0 for idx in range(nparams): upd = sol[params_tuple[idx]] init_vals[idx] = init_vals[idx] - upd norm = norm + upd*upd if sqrt(norm) < eps: #print "Number of iteratons: %d" % it break ret = {} for idx in range(nparams): ret[params_tuple[idx]] = init_vals[idx] return ret
from jit_compile import JIT from sympy.abc import x from ctypes import * func = JIT().Compile([x], x + x) print "Scala return value:", func(1) exprs = [x, x**2, x**3] func = JIT().BatchCompile([x], exprs) print "Batch return values (pass by parameter)" #Output buffer outAry = (c_double * len(exprs))() print "Before call:" for i in outAry: print i, N = 100000 for i in range(N): func(2.0, byref(outAry)) print "\nAfter ", N, " call:" for i in outAry: print i,
from sympy.abc import x from ctypes import * print __file__ NExpr = 10 f_exprs = [] for i in range(1,NExpr): expr = reduce(lambda a, b:a+b, [x**(1.0/(j+1)) for j in range(0,i)]) f_exprs.append(expr) #print expr vlen = 100 ts = time.time() g1 = map(lambda a:JIT().VecCompile([x], vlen, [a]), f_exprs) te = time.time() time_compile = (te-ts) outAry = (c_double*vlen)() pX = (c_double*vlen)() NN=10000000 N=NN/vlen xx=0.1 out=0.0 time_compute = 0 for i in range(len(g1)): ts = time.time() for j in range(N): for k in range(vlen):
qudots = qudots.subs(kdd) # mprint(qudots) # qudots.simplify() # mprint(qudots) #JIT compile the symbol functions x1, x2, x3, x4 = symbols('x1 x2 x3 x4') dim = qudots.shape qudots_ary = [] for i in range(dim[0]): qudots_ary.append(qudots[i, 0].subs([(q1, x1), (q2, x2), (u1, x3), (u2, x4)])) ary_len = len(qudots_ary) func = JIT().BatchCompile([l, m, g, x1, x2, x3, x4], qudots_ary) def rhs(y, t, l, m, g): q1 = y[0] q2 = y[1] u1 = y[2] u2 = y[3] dydt = zeros((len(y))) outAry = (c_double * ary_len)() func(l, m, g, q1, q2, u1, u2, byref(outAry)) for i in range(len(outAry)): dydt[i] = outAry[i] #return outAry return dydt
from jit_compile import JIT from sympy import * from sympy.abc import x, y R = 0.127-(x*0.194/(y+0.194)); Rdy = R.diff(y); print Rdy jit = JIT() func = jit.Compile([x, y], Rdy) print func(0.362, 0.556)
rosen = 0 for i in range(1, N): rosen += 100 * (xs[i] - xs[i - 1]**2)**2 + (1 - xs[i - 1])**2 if debug: print rosen #Gradient grad = [] for x in xs: grad.append(rosen.diff(x)) if debug: for e in grad: print e ts = time.time() func_grad = JIT().BatchCompile(xs, grad) te = time.time() #print "llvm rosen grad (N=",N,") compile time: ",(te-ts) vlen = len(xs) outAry = (c_double * vlen)() args = [] for i in range(len(xs)): args.append(0.0) func_grad(*(args + [byref(outAry)])) if debug: for d in outAry: print d,
f_exprs = [] for i in range(1,NExpr): expr = reduce(lambda a, b:a+b, [x**(1.0/(j+1)) for j in range(0,i)]) f_exprs.append(expr) #print expr NN=102400 #Total number of data (# of evaluation) VectorLens = [1, 2, 4, 8, 16, 32, 64, 128] TotalCompileTimes = [] TotalEvalTimes = [] for nData in VectorLens: CompileTimes = [] EvalTimes = [] for nTry in range(8): ts = time.time() g1 = map(lambda a:JIT().VecCompile([x], nData, [a]), f_exprs) te = time.time() time_compile = (te-ts) #Prepare parameters for call the JITed function outAry = (c_double*nData)() pX = (c_double*nData)() for i in range(len(pX)): pX[i] = 0.1 N=NN/nData time_compute = 0 for i in range(len(g1)): ts = time.time() for j in range(N): g1[i](byref(pX), byref(outAry))
def Gauss_Newton(eq, data_dict, init_params_dict, eps=1e-4, max_iter=100): J = [] #Jacobian in a list (rowwise) rs = [] #residuals dlen = 0 #length of measurement data for key, val in data_dict.items(): dlen = len(val) break params_tuple = tuple(init_params_dict.keys()) nparams = len(params_tuple) for idx in range(dlen): #Substitute x,y,... with their data of measurement r = data_dict[eq.lhs][idx] - eq.rhs for var in data_dict: r = r.subs(var, data_dict[var][idx]) for idx in range(nparams): J.append(r.diff(params_tuple[idx])) rs.append(r) #print J #print rs funcJ = JIT().compile2(params_tuple, J) len_J = len(J) funcRs = JIT().compile2(params_tuple, rs) len_rs = len(rs) init_vals = [] for idx in range(nparams): init_vals.append(init_params_dict[params_tuple[idx]]) for it in range(max_iter): outAryJ = (c_double*len_J)() funcJ(*tuple(init_vals + [byref(outAryJ)])) outAryRs = (c_double*len_rs)() funcRs(*tuple(init_vals+[byref(outAryRs)])) NJ = [] #Jacobian matrix, dim: (nRow,nCol)=(dlen, nparams) for row in range(dlen): NJrow = [] for col in range(nparams): NJrow.append(outAryJ[row*nparams+col]) NJ.append(NJrow) A = Matrix(NJ) Nrs = [] for row in range(dlen): Nrs.append(outAryRs[row]) bb = Matrix(Nrs) Ab = (A.T*A).row_join(A.T*bb) sol = solve_linear_system(Ab, *params_tuple) norm = 0 for idx in range(nparams): upd = sol[params_tuple[idx]] init_vals[idx] = init_vals[idx] - upd norm = norm + upd*upd if sqrt(norm) < eps: #print "Number of iteratons: %d" % it break ret = {} for idx in range(nparams): ret[params_tuple[idx]] = init_vals[idx] return ret
from sympy.abc import x #from math import factorial print __file__ NExpr = 10 f_exprs = [] for i in range(0, NExpr): expr = reduce(lambda a, b: a + b, [1.0 / factorial(j) * x**j for j in range(0, i + 1)]) f_exprs.append(expr) print expr ts = time.time() g1 = map(lambda a: JIT().Compile([x], a), f_exprs) te = time.time() print g1 print "LLVM JIT compile time: ", (te - ts) #print "LLVM JIT eval value= " #for g in g1: # print g(0.1) N = 10000000 xx = 0.1 out = 0.0 for i in range(len(g1)): ts = time.time() for j in range(N): g1[i](xx) te = time.time()
import nlopt from numpy import * from jit_compile import JIT import sympy as sm x1,x2,a,b = sm.symbols('x1 x2 a b') obj = sm.sqrt(x2) ctr =(a*x1+b)**3 - x1 jit = JIT() objGrad1 = jit.Compile([x1, x2], obj.diff(x1)) objGrad2 = jit.Compile([x1, x2], obj.diff(x2)) ctrGrad1 = jit.Compile([x1, x2, a, b], ctr.diff(x1)) ctrGrad2 = jit.Compile([x1, x2, a, b], ctr.diff(x2)) def myfunc(x, grad): if grad.size > 0: grad[0] = objGrad1(x[0], x[1]) grad[1] = objGrad2(x[0], x[1]) return sqrt(x[1]) def myconstraint(x, grad, a, b): if grad.size > 0: grad[0] = ctrGrad1(x[0], x[1], a, b) grad[1] = ctrGrad2(x[0], x[1], a, b) return (a*x[0] + b)**3 - x[1] opt = nlopt.opt(nlopt.LD_MMA, 2) opt.set_lower_bounds([-float('inf'), 0]) opt.set_min_objective(myfunc) opt.add_inequality_constraint(lambda x,grad: myconstraint(x,grad,2,0), 1e-8)
qudots = qudots.subs(kdd) # mprint(qudots) # qudots.simplify() # mprint(qudots) #JIT compile the symbol functions x1, x2, x3, x4 = symbols('x1 x2 x3 x4') dim = qudots.shape forcing_vector_func = [] for i in range(dim[0]): ele = qudots[i,0].subs([ (q1, x1), (q2, x2), (u1, x3), (u2, x4)]) func = JIT().Compile([l,m,g,x1,x2,x3,x4], ele) forcing_vector_func.append(func) # for func in forcing_vector_func: # print func(1,2,3,4,5,6,7) def rhs(y, t, l, m, g): # q1 = y[0] # q2 = y[1] # u1 = y[2] # u2 = y[3] dydt = zeros((len(y))) dydt[0] = forcing_vector_func[0](l,m,g,*y) dydt[1] = forcing_vector_func[1](l,m,g,*y) dydt[2] = forcing_vector_func[2](l,m,g,*y) dydt[3] = forcing_vector_func[3](l,m,g,*y)
from jit_compile import JIT from sympy.abc import x, y from ctypes import * vlen = 3 exprs = [x+y, x-y] #Vectorized compile with batch evaluation func = JIT().VecCompile([x, y], vlen, exprs) print "Vector return values (pass by parameter)" #Output buffer outLen = vlen*len(exprs) outAry = (c_double*outLen)() print "Before call:" for i in outAry: print i, pX = (c_double*vlen)(*[1.0,2.0,3.0]) pY = (c_double*vlen)(*[30.0,20.0,10.0]) #func(pointer(pX), pointer(pY), pointer(outAry)) func(byref(pX), byref(pY), byref(outAry)) print "\nAfter call:" for i in outAry: print i,