[Tutor] Calculating a math formula and finding errors
Jens Kubieziel
maillist@kuwest.de
Mon Jan 13 05:40:08 2003
On Fri, Jan 10, 2003 at 04:03:49PM +0100, Michael Janssen wrote:
> On Thu, 9 Jan 2003, Jens Kubieziel wrote:
>
> {n-1 \choose i} =2 \left\lfloor \frac{n+2}{2} \right\rfloor
"choose" is much better here. Thanks for this.
> > def MBrechts(n):
> > b=0
> > for i in range(0,n):
> > for j in range(0,n-i):
> > betrag = n + 1 - (2*i) - (2*j)
> > if betrag < 0:
> > betrag = betrag * -1
> > prod = betrag * perm(n-i,j)
>
> MBrechts is the left side, isn't it? It must be perm(n-1, i) to represent
You're right, my fault.
> latex: ${n-1 \choose i}$ (which is the binominalcoeffizient of n-1 and i)
> But this won't make the output equal.
Did you look at my note? The original formula has an "i" there, but it
doesn't make much sense to mee. I assume that it must be a "j".
> the "sigma-ranges" might (must? I don't know much about math notation) be
> range(0,n+1) and range(0,n-i+1) (both zeros aren't neccessary in python
> but provided for the readers convenience). (From the rest of the code I
Yep, right.
But when I add this, I get a runtime error:
Traceback (most recent call last):
File "Projekte/Python/MB-Wurzel.py", line 30, in ?
print "n = %d, Formel rechts = %d, Formel links = %d" % (erg,MBrechts(erg),MBlinks(erg))
File "Projekte/Python/MB-Wurzel.py", line 22, in MBrechts
prod = betrag * perm(n-i,j)
File "Projekte/Python/MB-Wurzel.py", line 9, in facul
return z*facul(z-1)
RuntimeError: maximum recursion depth exceeded
So I guess I have to change my "facul"-code.
> guess, you're familar with "range" so this typo needn't be explained)
No, it doesn't. I corrected this within my "perm", but forgot it in
"MBrechts".
> In case this doesn't already solve your problem, could you repost a
> patched version (perhaps with english var-names to give the rest of
> tutor-list a better chance :) and clean from rechts/links mess) so that
> we can restart from a coherent version?
#v+
#! /usr/bin/python
import math
def factorial(z):
if z == 1 or z == 0:
return 1
else:
return z*factorial(z-1)
def binomial(x,y):
w = 1
for i in range(y+1,x+1):
w = w * i
return w/factorial(x-y)
def MBleft(n):
b=0
for i in range(0,n+1):
for j in range(0,n-i+1):
betrag = abs(n + 1 - 2*(i+j))
prod = betrag * binomial(n-i,j)
b = b + prod
return b
def MBright(n):
return 2 * math.floor((n+2)/2) * binomial(n+1,math.floor((n+1)/2)) - n - 1
for erg in range(1,6):
print "n = %d, Formel rechts = %d, Formel links = %d" % (erg,MBrechts(erg),MBlinks(erg))
#v-
I have to think about the factorial function. Because it seems that the
recursion doesn't work with the correction in MBleft.
--
Jens Kubieziel mailto:jens@kubieziel.de
The study of non-linear physics is like the study of non-elephant biology.