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 use Ubuntu Hardy, right? :) You may fetch a newer package from
Intrepid, for example from here:
https://launchpad.net/ubuntu/intrepid/i386/python-sympy/0.5.15-1
or simply download the tarball from our site, you don't need to
install anything, just unpack it and that's it.
Yes, the lambda arguments order was changed to follow the same order
as in Python.
>
>
>>
>> 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 for your trust :). I could do that.
> But I won't have time for it until summer break (which is in three
> weeks for me).
No problem. I created an issue for this, see below, and if someone
finds time to fix it, it will be done, otherwise it will be waiting
for you. :)
> I guess I'd like it to have like this:
> x,y,z = symbols('xyz')
>
> v = [x,y,z]
> f = [ x*y, y*z]
> g = lambdify(f, v)
> w = g([1,2,3]) # w = [2,6], i.e. list
>
>
> v = [x,y,z]
> f = array([ x*y, y*z])
> g = lambdify(f, v)
> w = g([1,2,3]) # w = array([2,6]), i.e. numpy.array
>
>
> v = (x,y,z)
> f = ( x*y, y*z)
> g = lambdify(f, v)
> w = g(1,2,3) # w = (2,6), i.e. tuple
>
> I.e. the input of lambdify specifies what the lambda function g
> expects as input and also what type it returns.
Thanks for the input, it looks reasonable. I created an issue for this:
http://code.google.com/p/sympy/issues/detail?id=906
Ondrej