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.Sun, 06 Mar 2022 18:33:25 +0100replicate recursive sumhttps://ask.sagemath.org/question/61392/replicate-recursive-sum/i have a function defined as
https://imgur.com/a/SfF2Edl.png
![image description](https://i.imgur.com/4InWicG.png)
how can i replicate this function in sagemath using the sum function? i know it can be done in pure python with recursion but then whats the point of sagemath and symbolic calculation?
what ive tried so far is:
<code>
f(x) = 1 if x == 0 else sum(1+f(j-1), j, 0, x-1)
</code>
but i get the following error:
<code>
NameError: name 'f' is not defined
</code>
now i know its not possible to use recursion on a variable like this but there must be some tricky way to go about doing that (i hope so and thats why im here)
thanks in advanceFri, 04 Mar 2022 18:44:05 +0100https://ask.sagemath.org/question/61392/replicate-recursive-sum/Comment by Emmanuel Charpentier for <p>i have a function defined as</p>
<p><a href="https://imgur.com/a/SfF2Edl.png">https://imgur.com/a/SfF2Edl.png</a></p>
<p><img src="https://i.imgur.com/4InWicG.png" alt="image description"></p>
<p>how can i replicate this function in sagemath using the sum function? i know it can be done in pure python with recursion but then whats the point of sagemath and symbolic calculation?</p>
<p>what ive tried so far is:</p>
<p><code>
f(x) = 1 if x == 0 else sum(1+f(j-1), j, 0, x-1)
</code></p>
<p>but i get the following error:</p>
<p><code>
NameError: name 'f' is not defined
</code></p>
<p>now i know its not possible to use recursion on a variable like this but there must be some tricky way to go about doing that (i hope so and thats why im here)</p>
<p>thanks in advance</p>
https://ask.sagemath.org/question/61392/replicate-recursive-sum/?comment=61396#post-id-61396Homework ?
If so, try to solve it yourself. One (big (fat)) hint : recurrence...
Another hint : try to write `f` as a pure Python function (probably horribly inefficient...) (or do it by hand on paper (or in your head)), then see what its value is for the few first positive integers. Illumination should ensue quickly, and the point of your (probable) homework is probably to *prove* what these numerical results hint at...
Yet another one : try to think of ways to write efficiently your Python function. There are lots of way to do it, and t's a good exercise...
Bonus question : what happens if x is **not** a non-negative integer ? What should you add to your Python implementation ?
Other bonus question : explain the error message returned by your first attempt.Fri, 04 Mar 2022 20:56:24 +0100https://ask.sagemath.org/question/61392/replicate-recursive-sum/?comment=61396#post-id-61396Comment by mahmood for <p>i have a function defined as</p>
<p><a href="https://imgur.com/a/SfF2Edl.png">https://imgur.com/a/SfF2Edl.png</a></p>
<p><img src="https://i.imgur.com/4InWicG.png" alt="image description"></p>
<p>how can i replicate this function in sagemath using the sum function? i know it can be done in pure python with recursion but then whats the point of sagemath and symbolic calculation?</p>
<p>what ive tried so far is:</p>
<p><code>
f(x) = 1 if x == 0 else sum(1+f(j-1), j, 0, x-1)
</code></p>
<p>but i get the following error:</p>
<p><code>
NameError: name 'f' is not defined
</code></p>
<p>now i know its not possible to use recursion on a variable like this but there must be some tricky way to go about doing that (i hope so and thats why im here)</p>
<p>thanks in advance</p>
https://ask.sagemath.org/question/61392/replicate-recursive-sum/?comment=61395#post-id-61395done replaced with a link, thanksFri, 04 Mar 2022 18:51:36 +0100https://ask.sagemath.org/question/61392/replicate-recursive-sum/?comment=61395#post-id-61395Comment by slelievre for <p>i have a function defined as</p>
<p><a href="https://imgur.com/a/SfF2Edl.png">https://imgur.com/a/SfF2Edl.png</a></p>
<p><img src="https://i.imgur.com/4InWicG.png" alt="image description"></p>
<p>how can i replicate this function in sagemath using the sum function? i know it can be done in pure python with recursion but then whats the point of sagemath and symbolic calculation?</p>
<p>what ive tried so far is:</p>
<p><code>
f(x) = 1 if x == 0 else sum(1+f(j-1), j, 0, x-1)
</code></p>
<p>but i get the following error:</p>
<p><code>
NameError: name 'f' is not defined
</code></p>
<p>now i know its not possible to use recursion on a variable like this but there must be some tricky way to go about doing that (i hope so and thats why im here)</p>
<p>thanks in advance</p>
https://ask.sagemath.org/question/61392/replicate-recursive-sum/?comment=61394#post-id-61394To post links as a new user, insert spaces in them, and someone with enough "karma" can fix them.
Something like: https ://example .comFri, 04 Mar 2022 18:47:01 +0100https://ask.sagemath.org/question/61392/replicate-recursive-sum/?comment=61394#post-id-61394Comment by slelievre for <p>i have a function defined as</p>
<p><a href="https://imgur.com/a/SfF2Edl.png">https://imgur.com/a/SfF2Edl.png</a></p>
<p><img src="https://i.imgur.com/4InWicG.png" alt="image description"></p>
<p>how can i replicate this function in sagemath using the sum function? i know it can be done in pure python with recursion but then whats the point of sagemath and symbolic calculation?</p>
<p>what ive tried so far is:</p>
<p><code>
f(x) = 1 if x == 0 else sum(1+f(j-1), j, 0, x-1)
</code></p>
<p>but i get the following error:</p>
<p><code>
NameError: name 'f' is not defined
</code></p>
<p>now i know its not possible to use recursion on a variable like this but there must be some tricky way to go about doing that (i hope so and thats why im here)</p>
<p>thanks in advance</p>
https://ask.sagemath.org/question/61392/replicate-recursive-sum/?comment=61393#post-id-61393Welcome to Ask Sage! Thank you for your question.Fri, 04 Mar 2022 18:46:06 +0100https://ask.sagemath.org/question/61392/replicate-recursive-sum/?comment=61393#post-id-61393Answer by John Palmieri for <p>i have a function defined as</p>
<p><a href="https://imgur.com/a/SfF2Edl.png">https://imgur.com/a/SfF2Edl.png</a></p>
<p><img src="https://i.imgur.com/4InWicG.png" alt="image description"></p>
<p>how can i replicate this function in sagemath using the sum function? i know it can be done in pure python with recursion but then whats the point of sagemath and symbolic calculation?</p>
<p>what ive tried so far is:</p>
<p><code>
f(x) = 1 if x == 0 else sum(1+f(j-1), j, 0, x-1)
</code></p>
<p>but i get the following error:</p>
<p><code>
NameError: name 'f' is not defined
</code></p>
<p>now i know its not possible to use recursion on a variable like this but there must be some tricky way to go about doing that (i hope so and thats why im here)</p>
<p>thanks in advance</p>
https://ask.sagemath.org/question/61392/replicate-recursive-sum/?answer=61419#post-id-61419I don't know about using Sage's symbolic sum, but this seems to work (idea based on the documentation for Sage's `function`, obtained by looking at `function?` and also at https://doc.sagemath.org/html/en/reference/calculus/sage/calculus/var.html#sage.calculus.var.function):
sage: def ev(self, x):
....: if x == 0:
....: return 1
....: else:
....: return sum(1+self(j) for j in range(x))
....:
sage: f = function("f", nargs=1, eval_func=ev)Sun, 06 Mar 2022 03:30:12 +0100https://ask.sagemath.org/question/61392/replicate-recursive-sum/?answer=61419#post-id-61419Comment by John Palmieri for <p>I don't know about using Sage's symbolic sum, but this seems to work (idea based on the documentation for Sage's <code>function</code>, obtained by looking at <code>function?</code>and also at <a href="https://doc.sagemath.org/html/en/reference/calculus/sage/calculus/var.html#sage.calculus.var.function">https://doc.sagemath.org/html/en/refe...</a>):</p>
<pre><code>sage: def ev(self, x):
....: if x == 0:
....: return 1
....: else:
....: return sum(1+self(j) for j in range(x))
....:
sage: f = function("f", nargs=1, eval_func=ev)
</code></pre>
https://ask.sagemath.org/question/61392/replicate-recursive-sum/?comment=61428#post-id-61428Good fix on the `sum` part. I mistakenly based mine on the code in the question rather than the actual function in the picture.Sun, 06 Mar 2022 18:33:25 +0100https://ask.sagemath.org/question/61392/replicate-recursive-sum/?comment=61428#post-id-61428Comment by Emmanuel Charpentier for <p>I don't know about using Sage's symbolic sum, but this seems to work (idea based on the documentation for Sage's <code>function</code>, obtained by looking at <code>function?</code>and also at <a href="https://doc.sagemath.org/html/en/reference/calculus/sage/calculus/var.html#sage.calculus.var.function">https://doc.sagemath.org/html/en/refe...</a>):</p>
<pre><code>sage: def ev(self, x):
....: if x == 0:
....: return 1
....: else:
....: return sum(1+self(j) for j in range(x))
....:
sage: f = function("f", nargs=1, eval_func=ev)
</code></pre>
https://ask.sagemath.org/question/61392/replicate-recursive-sum/?comment=61421#post-id-61421Possible slight refinements (and a slight fix) of John's solution :
def f_ev(self, x):
"""
A recursively defined symbolic function.
Do not return anything for non-numeric argument : the expression will
be returned unmodified.
"""
if SR(x).is_numeric():
if x in NN and x >= 0:
if x == 0:
return 1
return 1 + sum(self(j) for j in range(x))
return SR(float("nan"))
which allows for :
sage: {u:f(u) for u in list(range(5))+[x, 3/2]}
{0: 1, 1: 2, 2: 4, 3: 8, 4: 16, x: f(x), 3/2: NaN}
Now, you may be interested to attack the questions I raised in my previous comment...Sun, 06 Mar 2022 09:40:21 +0100https://ask.sagemath.org/question/61392/replicate-recursive-sum/?comment=61421#post-id-61421