Hello,

On Sat, Jun 12, 2010 at 5:27 PM, Byungchul Cha <[email protected]> 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

This does not create a copy of a.  When you do "a = 3", this creates
new object for the integer 3 and then makes "a" point to that object.
When you do "b = a", it makes b point to the object that you
originally created.  When you do "b = 2", it makes a new object for
the integer 2 and makes "b" point to that.  Notice that you are never
changing (mutating) any of the objects.

>
> So far, it looks good to me. But, when I do a similar thing with
> matrices, it doesn't look to be the same.
> ...
> Shouldn't the value of v remain the same? Why does the change in u
> (or, a row of u) affect v?

Here, the line "sage: u[2] = [0,0,0]" change the object that u in
referencing.  Since u and v point to that same object (as above), you
see the changes when you look at v.  You can use copy() to actually
make a copy of v:

sage: sage: v = matrix(ZZ, 3, range(9))
sage: v = matrix(ZZ, 3, range(9))
sage: u = copy(v)
sage: u[2] = [0,0,0]
sage: u
[0 1 2]
[3 4 5]
[0 0 0]
sage: v
[0 1 2]
[3 4 5]
[6 7 8]

--Mike

-- 
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

Reply via email to