On Jun 12, 2010, at 17:27 , Byungchul Cha wrote:
Please tell me if this is a bug, or, I'm missing something obvious...
sage: a = 3 # Assign a value to a variable a
sage: b = a # Create a copy of a
You're not really copying a, you're just making 'b' refer to the same
thing that 'a' does, i.e. '3'.
sage: b = 2 # Change the value of b
Now b points to a different integer (2).
sage: b
2
sage: a # The value of a remains unchanged, as expected.
3
So far, it looks good to me. But, when I do a similar thing with
matrices, it doesn't look to be the same.
sage: v = matrix(ZZ, 3, range(9))
sage: u = v
u and v point to the same thing.
sage: u[2] = [0,0,0]
The matrix stored in the variable u has not been reassigned, it' been
mutated.
sage: u
[0 1 2]
[3 4 5]
[0 0 0]
sage: v
[0 1 2]
[3 4 5]
[0 0 0]
Shouldn't the value of v remain the same? Why does the change in u
(or, a row of u) affect v?
On Jun 12, 2010, at 8:25 PM, Justin C. Walker wrote:
On Jun 12, 2010, at 19:07 , William Stein wrote:
On Saturday, June 12, 2010, Justin C. Walker <[email protected]> wrote:
On Jun 12, 2010, at 17:27 , Byungchul Cha wrote:
[snip]
Shouldn't the value of v remain the same? Why does the change in u
(or, a row of u) affect v?
[snip]
For, e.g., integers, "u=v" means that the names u,v both refer to
their own copies of the value in question.
Are you sure??? I think you statement that u is a new copy is
wrong. I bet
u is v
would still return true above.
Picky picky picky. I was hoping to avoid a trip into the twisty
maze of passages in language definition (all of which are subtly
different :-}). But you are correct. "u is v" does return true and
the two actually refer to the same (physical) value. And, if one
variable is modified, this doesn't modify the other, or the value
that both previously referred to.
Actually, even in the case of integers, if you were to modify one, it
would modify the other. Most object are (basically) immutable, so this
doesn't come up. Assignment never copies, it only creates references.
- Robert
--
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org