On 17 February 2014 21:18, "André Walker-Loud <walksl...@gmail.com>" <walksl...@gmail.com> wrote: > > What I am trying to avoid is having to write a special case for each order of > polynomial I want. I tried the following > > def poly(x,pars): > val = 0. > for i,ci in enumerate(pars): > val += x**i * ci > return val > def f_lambda(x,pars): > return lambda x,*pars: poly(x,*pars) > x = np.array([1,2,3]) > f = f_lambda(x,[-.5,2.]) > f(x,[-.5,2.]) > array([ 1.5, 3.5, 5.5]) > > f(x,[-.5,3.]) > array([ 2.5, 5.5, 8.5]) > etc. > > This function correctly gives me a polynomial of arbitrary order, but > > f.func_code.co_varnames > ('x', 'pars') > > so I can not pass this to the third party minimizer, as it does not know how > to interpret 'pars'. You can probably see trying to replicate the lambda > function method for an arbitrary polynomial is what naturally led to the > string/exec option.
This particular case is easily solved: def f_lambda(x,pars): return lambda x: poly(x,*pars) You let the closure take care of pars and return a function that takes exactly one argument x. Oscar _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor