ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 14 Jan 2013 11:25:24 -0600sage recursive functions programminghttp://ask.sagemath.org/question/9699/sage-recursive-functions-programming/Hi everyone, can anybody please help me with this please? I am still relatively new to sage programming.
I have two states here a and b, and function f to compute binary strings.
If f is at state a, then f(1) = 0, also return to state a, while f(0)=1, but goes to next state b.
b is the identity state, when f is at b, b(0)=0, and stays at state b, while b(1)=1, but goes back to state a.
for example: suppose start from state a, then we have
f(100) = a(100) = 0a(00) = 01b(0) = 010
similarly, suppose we start from state b, then we have
f(100) = b(100) = 1a(00) = 11b(0) = 110
Therefore, any binary strings can be calculated by the function and output another binary strings.
Can anybody please help me on how to program this in sage please? Thanks a lot.Thu, 10 Jan 2013 13:23:15 -0600http://ask.sagemath.org/question/9699/sage-recursive-functions-programming/Comment by benjaminfjones for <p>Hi everyone, can anybody please help me with this please? I am still relatively new to sage programming. </p>
<p>I have two states here a and b, and function f to compute binary strings. </p>
<p>If f is at state a, then f(1) = 0, also return to state a, while f(0)=1, but goes to next state b.</p>
<p>b is the identity state, when f is at b, b(0)=0, and stays at state b, while b(1)=1, but goes back to state a.</p>
<p>for example: suppose start from state a, then we have
f(100) = a(100) = 0a(00) = 01b(0) = 010
similarly, suppose we start from state b, then we have
f(100) = b(100) = 1a(00) = 11b(0) = 110</p>
<p>Therefore, any binary strings can be calculated by the function and output another binary strings.</p>
<p>Can anybody please help me on how to program this in sage please? Thanks a lot.</p>
http://ask.sagemath.org/question/9699/sage-recursive-functions-programming/?comment=18412#post-id-18412This sounds like homework...Thu, 10 Jan 2013 19:58:21 -0600http://ask.sagemath.org/question/9699/sage-recursive-functions-programming/?comment=18412#post-id-18412Comment by tsang for <p>Hi everyone, can anybody please help me with this please? I am still relatively new to sage programming. </p>
<p>I have two states here a and b, and function f to compute binary strings. </p>
<p>If f is at state a, then f(1) = 0, also return to state a, while f(0)=1, but goes to next state b.</p>
<p>b is the identity state, when f is at b, b(0)=0, and stays at state b, while b(1)=1, but goes back to state a.</p>
<p>for example: suppose start from state a, then we have
f(100) = a(100) = 0a(00) = 01b(0) = 010
similarly, suppose we start from state b, then we have
f(100) = b(100) = 1a(00) = 11b(0) = 110</p>
<p>Therefore, any binary strings can be calculated by the function and output another binary strings.</p>
<p>Can anybody please help me on how to program this in sage please? Thanks a lot.</p>
http://ask.sagemath.org/question/9699/sage-recursive-functions-programming/?comment=18397#post-id-18397Hi, thanks for reply. It is not homework, but part of research program, trying to program automatons, which is self-similar groups. I basically really weak with programming, but need to try to test some group structures.Mon, 14 Jan 2013 11:23:46 -0600http://ask.sagemath.org/question/9699/sage-recursive-functions-programming/?comment=18397#post-id-18397Comment by ppurka for <p>Hi everyone, can anybody please help me with this please? I am still relatively new to sage programming. </p>
<p>I have two states here a and b, and function f to compute binary strings. </p>
<p>If f is at state a, then f(1) = 0, also return to state a, while f(0)=1, but goes to next state b.</p>
<p>b is the identity state, when f is at b, b(0)=0, and stays at state b, while b(1)=1, but goes back to state a.</p>
<p>for example: suppose start from state a, then we have
f(100) = a(100) = 0a(00) = 01b(0) = 010
similarly, suppose we start from state b, then we have
f(100) = b(100) = 1a(00) = 11b(0) = 110</p>
<p>Therefore, any binary strings can be calculated by the function and output another binary strings.</p>
<p>Can anybody please help me on how to program this in sage please? Thanks a lot.</p>
http://ask.sagemath.org/question/9699/sage-recursive-functions-programming/?comment=18413#post-id-18413This is not really a sage specific thing. This is simply implementing a recursive mathematical definition into a python program/function. So, are you asking whether Sage has some library or class which works with states?Thu, 10 Jan 2013 19:18:32 -0600http://ask.sagemath.org/question/9699/sage-recursive-functions-programming/?comment=18413#post-id-18413Answer by Rolandb for <p>Hi everyone, can anybody please help me with this please? I am still relatively new to sage programming. </p>
<p>I have two states here a and b, and function f to compute binary strings. </p>
<p>If f is at state a, then f(1) = 0, also return to state a, while f(0)=1, but goes to next state b.</p>
<p>b is the identity state, when f is at b, b(0)=0, and stays at state b, while b(1)=1, but goes back to state a.</p>
<p>for example: suppose start from state a, then we have
f(100) = a(100) = 0a(00) = 01b(0) = 010
similarly, suppose we start from state b, then we have
f(100) = b(100) = 1a(00) = 11b(0) = 110</p>
<p>Therefore, any binary strings can be calculated by the function and output another binary strings.</p>
<p>Can anybody please help me on how to program this in sage please? Thanks a lot.</p>
http://ask.sagemath.org/question/9699/sage-recursive-functions-programming/?answer=14448#post-id-14448Hi, I have used a Boolean to distinguish the states: 'a'=True and 'b'=False.
@cached_function
def recur(string,initial_state):
if string=='': return ''
else:
start=eval(string[0])
return str((initial_state+start)%2)+recur(string[1:],start)
Does this answer your question? Roland
Fri, 11 Jan 2013 10:08:57 -0600http://ask.sagemath.org/question/9699/sage-recursive-functions-programming/?answer=14448#post-id-14448Comment by tsang for <p>Hi, I have used a Boolean to distinguish the states: 'a'=True and 'b'=False. </p>
<p>@cached_function
def recur(string,initial_state):</p>
<pre><code>if string=='': return ''
else:
start=eval(string[0])
return str((initial_state+start)%2)+recur(string[1:],start)
</code></pre>
<p>Does this answer your question? Roland</p>
http://ask.sagemath.org/question/9699/sage-recursive-functions-programming/?comment=18396#post-id-18396I will try a little bit with this, thanks heaps. If I still cannot make it work, I may try to ask you again, thanks a lot. Mon, 14 Jan 2013 11:25:24 -0600http://ask.sagemath.org/question/9699/sage-recursive-functions-programming/?comment=18396#post-id-18396