Sympy

The geek forum. PHP, Perl, HTML, hardware questions etc.. it's all in here. Got a techie question? We'll sort you out. Ask your questions or post a link to your own site here!

Sympy

Postby Dante » Thu May 14, 2009 6:40 pm

Alright, so this is a rather in depth question... I know, but this is for those of you who work with the Python Programming Language. My problem is that I'm trying to find a way to pass a function to a method using sympy and mpmath modules. Example

from sympy import *
from mpmath import *

x = Symbol('x')
y = x**2 + x + 10

integral = quad(lambda x: y, [0,10])
print integral

This should print out the integral of x**2 + x + 10 from zero to ten, or anything else y is set to allowing dynamic numerical integral using the Gauss-Legendre method. It would really make my current Thesis problem a lot easier.

Unfortunately, (despite being able to pass everything else in the book). Python is happy with this,
integral = quad(lambda x: x**2 + x + 10, [0,10])
but it doesn't know what to do with this,
integral = quad(lambda x: y, [0,10])

It is as though it is not equating x**2 + x + 10 and y despite my setting them equal. There has to be a way to send a function via this method, otherwise the program is completely worthless (if all it does is take the integral of a pre-defined integral before compile time, then I might as well do the integral by hand). The problem is, I can't find it! Can I maybe get some help from someone else who might know how to fix this? (I'll keep searching too, if I come across the answer, I'll post it here).

Thank you,
Pascal
FKA Pascal
User avatar
Dante
 
Posts: 1323
Joined: Thu Mar 04, 2004 8:24 pm
Location: Where-ever it is, it sure is hot!

Postby Dante » Thu May 14, 2009 7:31 pm

OK! So I have a solution (hopefully this works for everything else as well). I was talking to my friend Ross, a computational physics colleague I knew from ASU and he gave me the following info for how to solve this. Notice that there is a somewhat mysterious lambda x: someFunction written into the Gaussian-Quadrature integral. Well, to solve this problem you simply need to replace this whole thing by the equation you seek to solve, ie

z = lambda x: someFunction

This allows you to specify the some function before runtime and then pass the function z along in the following format,

quad(z, [x0,xf])

NOW IT WORKS! WOOHOO!!!

Thanks everyone for taking a look at this for me!

-Pascal
FKA Pascal
User avatar
Dante
 
Posts: 1323
Joined: Thu Mar 04, 2004 8:24 pm
Location: Where-ever it is, it sure is hot!

Postby blkmage » Thu May 14, 2009 8:04 pm

Now that you've explained it, it makes complete sense. I guess I don't encounter lambdas enough, since I totally would never have caught that.
User avatar
blkmage
 
Posts: 4529
Joined: Mon May 03, 2004 5:40 pm

Postby Dante » Thu May 14, 2009 11:32 pm

I know, I looked at the code in the quad integration method and thought to myself, "That looks weird, what are they doing there?".
FKA Pascal
User avatar
Dante
 
Posts: 1323
Joined: Thu Mar 04, 2004 8:24 pm
Location: Where-ever it is, it sure is hot!


Return to Computing and Links

Who is online

Users browsing this forum: No registered users and 64 guests