ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 24 Apr 2017 03:20:31 +0200Recurrence, for-loop and zero.https://ask.sagemath.org/question/37380/recurrence-for-loop-and-zero/Hello guys! Hope you guys are having a nice day.
I have some problem as follows: I setup some recurrence as follows
def f(j,k):
if k==0:
m=j
elif j==0:
m=k
else:
m=j/k*(f(j-1,k)+1/k*(f(j-1,k)))+k/j*(f(j,k-1)+1/j*(f(j,k-1)))
return m
And if we calculate
`f(1,1),
f(2,1),
f(1,2),
f(2,2)`
in sage, then what we get are
4,
35/2,
35/2,
105/2.
However, if we do the for loop as in the below, I found the above results to be zero!!:
for k in range(3):
for j in range(3):
(j,k), expVal(j,k)
And the calculation result from the sage is:
((0, 0), 0)
((1, 0), 1)
((2, 0), 2)
((0, 1), 1)
**((1, 1), 0)**
**((2, 1), 0)**
((0, 2), 2)
**((1, 2), 0)**
**((2, 2), 0)**
And observe the bolded result above... it is weird that the result is zero... Can anyone help me fix this please...
Thank you for any help! Hope you have a nice rest of the day!
Mon, 24 Apr 2017 00:09:35 +0200https://ask.sagemath.org/question/37380/recurrence-for-loop-and-zero/Answer by nbruin for <p>Hello guys! Hope you guys are having a nice day.</p>
<p>I have some problem as follows: I setup some recurrence as follows</p>
<pre><code>def f(j,k):
if k==0:
m=j
elif j==0:
m=k
else:
m=j/k*(f(j-1,k)+1/k*(f(j-1,k)))+k/j*(f(j,k-1)+1/j*(f(j,k-1)))
return m
</code></pre>
<p>And if we calculate </p>
<p><code>f(1,1),
f(2,1),
f(1,2),
f(2,2)</code> </p>
<p>in sage, then what we get are </p>
<p>4,
35/2,
35/2,
105/2.</p>
<p>However, if we do the for loop as in the below, I found the above results to be zero!!:</p>
<pre><code>for k in range(3):
for j in range(3):
(j,k), expVal(j,k)
</code></pre>
<p>And the calculation result from the sage is:</p>
<p>((0, 0), 0)</p>
<p>((1, 0), 1)</p>
<p>((2, 0), 2)</p>
<p>((0, 1), 1)</p>
<p><strong>((1, 1), 0)</strong></p>
<p><strong>((2, 1), 0)</strong></p>
<p>((0, 2), 2)</p>
<p><strong>((1, 2), 0)</strong></p>
<p><strong>((2, 2), 0)</strong></p>
<p>And observe the bolded result above... it is weird that the result is zero... Can anyone help me fix this please...</p>
<p>Thank you for any help! Hope you have a nice rest of the day!</p>
https://ask.sagemath.org/question/37380/recurrence-for-loop-and-zero/?answer=37381#post-id-37381Try
for k in srange(3):
for j in srange(3):
print (j,k),f(j,k)
instead. Standard pitfall unfortunately: in python 2, "integer" division is truncating. Normally sage will provide you with "sage" integers for which division produces rational numbers, but if you use "range" then sage doesn't get the chance.Mon, 24 Apr 2017 02:49:59 +0200https://ask.sagemath.org/question/37380/recurrence-for-loop-and-zero/?answer=37381#post-id-37381Comment by sssageeee for <p>Try</p>
<pre><code>for k in srange(3):
for j in srange(3):
print (j,k),f(j,k)
</code></pre>
<p>instead. Standard pitfall unfortunately: in python 2, "integer" division is truncating. Normally sage will provide you with "sage" integers for which division produces rational numbers, but if you use "range" then sage doesn't get the chance.</p>
https://ask.sagemath.org/question/37380/recurrence-for-loop-and-zero/?comment=37382#post-id-37382Thank you so much nbruin. Hope you have a good day!Mon, 24 Apr 2017 03:20:31 +0200https://ask.sagemath.org/question/37380/recurrence-for-loop-and-zero/?comment=37382#post-id-37382