[Tutor] Advise...

Jacob S. keridee at jayco.net
Wed Jan 26 05:54:02 CET 2005


Hi all.

    Long time no see. (About five days, right?)
Anyway, I know the first thing that some of you are going to say is using 
eval(). I don't want a whole
guilt trip on security risks and all that. I do not want to share the code 
with anyone else while it's on my
computer, and I sure don't have anyone near me that knows python. I would be 
surprised if more than 50
people in Portland, IN knew anything about python. So security is not a 
problem. I guess what I'm
looking for is someone who knows the Reimann Sum better than I do and can 
tell me whether I can do
something to make it more efficient. It's horribly slow with ten thousand 
steps-- I don't know the notation
very well, but it loops the loop O(step*(maximum-minimum)) times, which is 
horribly sucky.
    In case anyone doesn't know, Reimann's sum is the computer's version of 
the definite integral, the area
under a curve in a particular domain.

Basic input and output.
If a curve is a straight line, say y = x, the area under the line on an 
interval can be found by geometric means.
However, if you use a parabola, or any other function, say y = 3*x**2,


What is the function? 3*x*x
What is the minimum? 2
What is the maximum? 5
117.000435

Which, considering that it is supposed to be exactly 117, It's darn good. 
Unfortunately, it also takes about
10 seconds to do all that.
Any suggestions? Any advice? TIA
Jacob Schmidt


############################
from __future__ import division
import psyco
psyco.full()
fofx = raw_input("What is the function? ")
minimum = raw_input("What is the minimum? ")
maximum = raw_input("What is the maximum? ")
minimum = float(minimum)
maximum = float(maximum)
total = 0
step = 100000
x = minimum
while minimum <= x <= maximum:
    area = eval(fofx)*1/step
    total = total+area
    x = x+1/step
print total
############################# 



More information about the Tutor mailing list