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.Tue, 12 Oct 2021 19:31:54 +0200function changing input valuehttps://ask.sagemath.org/question/59336/function-changing-input-value/The following code is supposed to implement a basic percolation process on a matrix of 0's and 1's. For some reason, it's changing the value of the matrix I input, and I can't figure out why. I'm sure it's a basic error on my part, but if someone can set me straight, I'd appreciate it!
Here's the function I defined:
<pre><code>
n = 4;
def percolate(B):
update = B;
for i in [0..n-1]:
for j in [0..n-1]:
nbr_sum = 0;
if (i==0):
nbr_sum += B[i+1,j];
elif (i==n-1):
nbr_sum += B[i-1,j];
else:
nbr_sum += B[i-1,j]+B[i+1,j];
if (j==0):
nbr_sum += B[i,j+1];
elif (j==n-1):
nbr_sum += B[i,j-1];
else:
nbr_sum += B[i,j-1]+B[i,j+1];
if (nbr_sum > 1):
update[i,j] = 1;
return update;
</code></pre>
When I execute
<pre><code>
Bid = matrix.identity(n);
percolate(Bid)
</code></pre>
I get the expected output
<pre><code>
[1 1 0 0]
[1 1 1 0]
[0 1 1 1]
[0 0 1 1]
</code></pre>
but the value of <code>Bid</code> also changes to the same value (as opposed to continuing to be the n x n identity matrix, as desired).Kyle OrmsbyTue, 12 Oct 2021 19:31:54 +0200https://ask.sagemath.org/question/59336/sage recursive functions programminghttps://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.tsangThu, 10 Jan 2013 20:23:15 +0100https://ask.sagemath.org/question/9699/