Hi Sebastian! On Tue, Jul 1, 2008 at 1:30 PM, b45ch1 <[EMAIL PROTECTED]> wrote: > > Hey, > I'd like to use Sympy to compute gradients and Hessians of real valued > functions. > > I got this far: > #--------------------------------------------------------------------- > #!/usr/bin/env python > > from numpy import * > from sympy import * > > N = 3 > x = [Symbol('x%d'%n) for n in range(N)] > > # define function f: R^N -> R > f = 1 > for n in range(N): > f *= x[n] > print 'function=',f > > # compute gradient > g = array([ diff(f,x[n]) for n in range(N)]) > print 'gradient=',g > > # compute Hessian > H = array([[diff(g[m],x[n]) for m in range(N)] for n in range(N)]) > > print 'Hessian=\n',H > > > # evaluating the Hessian at x = [0,1,2,3,...,N-1] > symdict = dict() > for n in range(N): > symdict[x[n]] = n > H1 = [[H[n,m].subs_dict(symdict).evalf() for n in range(N)] for m in > range(N)]
In the latest sympy, use just .subs() instead of subs_dict(). > > print H1 > #--------------------------------------------------------------------- For the record, here is the output of the above script: function= x0*x1*x2 gradient= [x1*x2 x0*x2 x0*x1] Hessian= [[0 x2 x1] [x2 0 x0] [x1 x0 0]] [[0, 2, 1], [2, 0, 0], [1, 0, 0]] > > > Now this is very awkward, and I'd like it to be reasonably fast since > I want to use the gradient and the Hessian for numerical computations. Evaluating the the Hessian is akward? Or computing it. > > Is there a better way? Yes. > I thought that there might be something like: convert an expression to > a Python function, since there is also the conversion to Latex and to > mathml. > I.e. similar to > #--------------------------------------------------------------------- > from sympy import * > x,y,z = symbols('xy') > f = x*y + y*z > g = f.to_python_function() > print g([1.,2.,3.]) > #--------------------------------------------------------------------- In fact, this is already implemented: In [1]: f = x*y + y*z In [2]: g = lambdify((x, y, z), f) In [3]: print g(1, 2, 3) Start ipython or isympy and do: In [4]: lambdify? to read the docstring. You can convert the expression to a python function, or even to a function using numpy, see the docstring. However, you don't just need an expression, but an array of expressions. How do you think this should be handled? I.e. what the interface should be? An array of python (lambda) functions? Or rather one python (lambda) function returning an array? How should this lambda funtion look like? Maybe like this: def g(x1, x2, x3): return array([x1, -x3, x2, ...]) This could be implemented in the lambdify function. Would you like to give it a try? We'll help you with any questions you might have to prepare a patch. Thanks, Ondrej --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sympy" group. To post to this group, send email to sympy@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/sympy?hl=en -~----------~----~----~----~------~----~------~--~---