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)]

print H1
#---------------------------------------------------------------------


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.

Is there a better way?

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.])
#---------------------------------------------------------------------


best regards,
Sebastian


--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to