f'(x) evaluated at x_0

59 views
Skip to first unread message

David Ketcheson

unread,
Jan 16, 2013, 7:06:29 AM1/16/13
to sy...@googlegroups.com
Is there any way in sympy to express the derivative of an abstract function evaluated at a point?  I can do

import sympy
x,x0 = sympy.var(('x','x0'))
f=sympy.Function('f') (x)
sympy.diff(f)

to get the derivative w.r.t. x evaluated at x.  If I do

sympy.diff(f).subs(x,x0)

then I get the derivative w.r.t. x0 evaluated at x0.  Can I get the derivative w.r.t. x evaluated at x0?

Aaron Meurer

unread,
Jan 16, 2013, 1:25:57 PM1/16/13
to sy...@googlegroups.com
Try Subs(diff(f, x), x, x0).

Note that mathematically, this should be the same as diff(f,
x).subs(x, x0), because x is a bound (not free) variable in this
expression, it doesn't matter if a different one is used, or if an
expression is used that doesn't use it at all. Note that if you use a
non-variable instead of x0, it will automatically give you the Subs
form (in particular, if you use a number, like diff(f, x).subs(x,
10)). It will also give you the Subs form if you later substitute x0
for a number (in terms of x0, but it doesn't matter because x0 serves
as a dummy variable in this case).

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/sympy/-/cQScvWEPp9MJ.
> To post to this group, send email to sy...@googlegroups.com.
> To unsubscribe from this group, send email to
> sympy+un...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/sympy?hl=en.

David Ketcheson

unread,
Jan 19, 2013, 12:00:39 PM1/19/13
to sy...@googlegroups.com
Thanks, Aaron.  That works, though I had hoped for something prettier than:

Subs(Derivative(f(_x), _x), (_x,), (x0,))

It would be really great if sympy knew how to print this kind of thing nicely (say, in an IPython notebook).

-David

Aaron Meurer

unread,
Jan 22, 2013, 11:50:46 PM1/22/13
to sy...@googlegroups.com
It does. What you have is the string form, which is designed to be
readable, but also copy-pastable. Like almost all SymPy objects, there
are pretty printers for Subs, including ASCII, Unicode, and LaTeX.
For ASCII/Unicode, you get the bar notation:

In [3]: pprint(Subs(diff(f(x), x), x, 0))
⎛d ⎞│
⎜──(f(x))⎟│
⎝dx ⎠│x=0

In LaTeX, you get

In [1]: latex(Subs(f(x), x, 0))
Out[1]: \left. \operatorname{f}{\left (x \right )} \right|_{\substack{ x=0 }}

which is the same thing. If you want this in the IPython notebook,
just use the SymPy printing extension, which will make everything
print with LaTeX using MathJax. Just run

%load_ext sympy.interactive.ipythonprinting

at the top of the notebook (init_printing(use_latex=True) should work too).

Aaron Meurer

David Ketcheson

unread,
Jan 23, 2013, 3:22:22 AM1/23/13
to sy...@googlegroups.com
Actually, that's what I was already trying:

import sympy
%load_ext sympy.interactive.ipythonprinting
x,x0 = sympy.var(('x','x0'))
f=sympy.Function('f') (x)
sympy.Subs(sympy.diff(f, x), x, x0)

That just gives (in an IPython notebook):

Subs(Derivative(f(x), x), (x,), (x0,))

which is strange because the ASCII and latex versions work as you say.  Am I doing something wrong?

-David

Aaron Meurer

unread,
Jan 23, 2013, 2:51:00 PM1/23/13
to sy...@googlegroups.com
I guess there is some bug with it. It works for me if you reload the
extension after loading it, like

import sympy
%load_ext sympy.interactive.ipythonprinting
%reload_ext sympy.interactive.ipythonprinting

Maybe this is the same as http://code.google.com/p/sympy/issues/detail?id=3566.

Aaron Meurer
Reply all
Reply to author
Forward
0 new messages