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/