[Edu-sig] Re: More on intro to Python (today's 3 hr training)

2005-03-29 Thread André Roberge
Kirby Urner wrote: [snip] def derivative(f): """ Factory function: accepts a function, returns a closure """ def df(x, h=1e-8): return (f(x + h) - f(x))/h return df Sorry for being picky, but while your derivative factory function follows the mathematical definiti

RE: [Edu-sig] Re: More on intro to Python (today's 3 hr training)

2005-03-29 Thread Kirby Urner
> Sorry for being picky, but while your derivative factory > function follows the mathematical definition, it is not the > "best" way to do it numerically. The preferred way is: > > def derivative(f): > """ > Factory function: accepts a function, returns a closure > """ >

[Edu-sig] RE: Integration correction

2005-03-29 Thread Henrik Eriksson
Kirby got the trapezoidal integration rule wrong. This is the corrected version. def integrate(f,a,b,n=1000): sum = 0 h = (b-a)/float(n) for i in range(1,n): sum += f(a+i*h) return h*(0.5*f(a)+sum+0.5*f(b)) The interval must be divided into n equal parts, so an arbitrar

[Edu-sig] Scandroid 1.0

2005-03-29 Thread ajsiegel
>From Charles O. Hartman, Professor of English and Poet in Residence at Conneticut College: http://cherry.conncoll.edu/cohar/COH%20programs%20page.htm """ The Scandroid is a program that scans iambic pentameters. You can load the text file of a poem, or type lines in by hand. As you "Step" thro

RE: [Edu-sig] RE: Integration correction

2005-03-29 Thread Kirby Urner
> Kirby got the trapezoidal integration rule wrong. Right, I was just doing a simple average, not any trapezoid. My rectangles took the mean between f(x-h) and f(x+h), nothing more. Not the best approximation, I agree, but simple to think about, and some text books show it. > This is the correc

RE: [Edu-sig] Re: More on intro to Python (today's 3 hr training)

2005-03-29 Thread Kirby Urner
> So, the "smart" algorithm should be (untested here left as an > exercise :-) > > sum = ( f(a) + f(b) ) / 2. > x = a + h > B = b-h/2 # takes care of round-off error at end point. > > .while x <= B: > .sum += f(x) > .x += h > . > . sum *= h > . return sum > > > André Your

RE: [Edu-sig] RE: Integration correction

2005-03-29 Thread Kirby Urner
> Kirby got the trapezoidal integration rule wrong. > This is the corrected version. > > def integrate(f,a,b,n=1000): > sum = 0 > h = (b-a)/float(n) > for i in range(1,n): >sum += f(a+i*h) > return h*(0.5*f(a)+sum+0.5*f(b)) > Here's the same thing using a generator e

RE: [Edu-sig] RE: Integration correction

2005-03-29 Thread Kirby Urner
> But you never use the trapezoidal rule in real life! > Simpson gives much smaller error for the same amount > of computation. In fact, it is exact for Kirby's example! > > Henrik Here's my implementation of Simpson's, except it divides the interval into 2n segments. >>> def simpson(f,a,b,n):

RE: [Edu-sig] RE: Integration correction

2005-03-29 Thread Kirby Urner
> Here's my implementation of Simpson's, except it divides the interval into > 2n segments. > > >>> def simpson(f,a,b,n): >h = float(b-a)/(2*n) >sum1 = sum(f(a + 2*k *h) for k in range(1,n)) >sum2 = sum(f(a + (2*k-1)*h) for k in range(1,n+1)) >return (h/3)*(f(a)

Re: RE: [Edu-sig] RE: Integration correction

2005-03-29 Thread ajsiegel
From: Kirby Urner <[EMAIL PROTECTED]> > @simpson > def g(x): return x*x > > >>> g(0, 3) > 9.0036 My resistance to decorators is not unrelated to the fact that I don't seem capable of getting my mind around them. I do find it quite disconcerting that the arguments "g" is expecti

RE: RE: [Edu-sig] RE: Integration correction

2005-03-29 Thread Kirby Urner
> From: Kirby Urner <[EMAIL PROTECTED]> > > @simpson > > def g(x): return x*x > > > > >>> g(0, 3) > > 9.0036 > > My resistance to decorators is not unrelated to the fact that I don't seem > capable of getting my mind around them. > Hi Art -- Actually, I'm with you. The above use i

[Edu-sig] Re: Integration correction

2005-03-29 Thread Scott David Daniels
[EMAIL PROTECTED] wrote: From: Kirby Urner <[EMAIL PROTECTED]> @simpson def g(x): return x*x g(0, 3) 9.0036 My resistance to decorators is not unrelated to the fact that I don't > seem capable of getting my mind around them. > > I do find it quite disconcerting that the arguments "g"

Re: [Edu-sig] RE: Integration correction

2005-03-29 Thread Guido van Rossum
> And with a decorator: > > def simpson(f): >def defint(a,b,n=1000): > [...] >return defint > > @simpson > def g(x): return x*x > > >>> g(0, 3) > 9.0036 No matter how cool decorators are, I don't think this is a good example of how to use them. The function g(x) is a

[Edu-sig] Re: Integration correction

2005-03-29 Thread André Roberge
Kirby Urner wrote: From: Kirby Urner <[EMAIL PROTECTED]> [snip ... Kirby, Art and many others including myself discuss the possible misuse of decorators in the context of calculating > derivatives and integrals numerically end snip] Now, if g(x) really *did* go on for 30-40 lines, OK, then

[Edu-sig] Re: More on intro to Python (today's 3 hr training)

2005-03-29 Thread André Roberge
Kirby Urner wrote: [snip - deleted stuff about numerical integration] So my general question is, as usual: wouldn't this be an interesting and intelligent way to teach math? You get concepts, and you get programming skills, and you start to think about analyzing and fine tuning algorithms. Plus y

[Edu-sig] Re: Integration correction

2005-03-29 Thread Scott David Daniels
Kirby Urner wrote: Again, I think you're probably right, that this particular example is perverse. Edu-sig is a scratch pad for bad ideas too. :-D Sorry, Kirby, I see we all seemed to jump on top of you here. --Scott David Daniels [EMAIL PROTECTED] ___