Express Dervivative in terms of finite differences.

79 views
Skip to first unread message

Björn Dahlgren

unread,
Apr 7, 2014, 11:34:31 AM4/7/14
to sy...@googlegroups.com
After reading Aaron's recommendation about looking for unanswered sympy questions at stackoverflow I stumbled upon:

http://stackoverflow.com/questions/18701290/derivatives-discretization-with-sympy

I implemented a solution for this and it is available as a WIP PR over here:
https://github.com/sympy/sympy/pull/7382

I would love to hear your feedback on this.

Best regards
/Björn

Alexey U. Gudchenko

unread,
Apr 7, 2014, 1:04:38 PM4/7/14
to sy...@googlegroups.com
Hi,
From the stackoverflow question here is your example:

> Here is an example:
>>> from sympy import symbols, Function
>>> x, h = symbols('x h')
>>> f = Function('f')
>>> print(f(x).diff(x).as_finite_diff())
-f(-h + x)/(2*h) + f(h + x)/(2*h)


Is it means that you use "h" symbol from the global namespace?




--
Alexey Gudchenko


> Best regards
> /Björn
>



Björn Dahlgren

unread,
Apr 7, 2014, 1:25:59 PM4/7/14
to sy...@googlegroups.com
h is Symbol('h') (and given how sympy cache works - yes it is global).
I was unsure whether I should use this or force the user to pass at least one parameter.
Maybe I should let it default to 1 instead of Symbol('h')? I'm starting to lean towards that
the more I think of it..

Björn Dahlgren

unread,
Apr 7, 2014, 1:35:35 PM4/7/14
to sy...@googlegroups.com

Giving this another minute of thought you are right that this is no good. I added a new commit and updated my answer at Stack Overflow.
Thank you for your input!
 

Alexey U. Gudchenko

unread,
Apr 7, 2014, 1:49:15 PM4/7/14
to sy...@googlegroups.com


On 07.04.2014 21:25, Björn Dahlgren wrote:
> h is Symbol('h') (and given how sympy cache works - yes it is global).
> I was unsure whether I should use this or force the user to pass at least
> one parameter.
> Maybe I should let it default to 1 instead of Symbol('h')? I'm starting to
> lean towards that
> the more I think of it..
>

I think one of those both variants (obligatory parameter or just 1) more
eligible then usage of global "h". And since finite differences concerns
the discretization theme then "1" is sufficient, I think. And "1"
convenient then obligatory parameter.

Also please add more examples (may be place tests from
"test_function.py" to the test_finite_diff.py files as it is created)
such as:

Second derivatives: f(x).diff(x, x)

Multivariables: f(x, y).diff(x, y)

And examples a few variants of schemes:
f(x).diff(x) ~ f(x+h)-f(x-h))/(2*h)

or

f(x).diff(x) ~ (f(x-2h) - 27f(x-h) + 27f(x+h) - f(x+2h))/24


Some of them to the doc string of "as_finite_diff" method.


--
Alexey Gudchenko

Aaron Meurer

unread,
Apr 7, 2014, 2:54:33 PM4/7/14
to sy...@googlegroups.com
That's awesome. Stackoverflow questions are great sources of things that should be implemented.

Aaron Meurer


--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at http://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/a59c9945-2132-42b6-a65a-c3f7c595bb3a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alexey U. Gudchenko

unread,
Apr 8, 2014, 10:29:44 AM4/8/14
to sy...@googlegroups.com
Thank you.

--
Alexey Gudchenko
Reply all
Reply to author
Forward
0 new messages