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.Sat, 07 Dec 2013 03:10:39 -0600Using copy() for matrices still returns errorhttp://ask.sagemath.org/question/10812/using-copy-for-matrices-still-returns-error/I want to take the zero matrix and assign entries to make a new matrix. I understand that the zero matrix is immutable and I need to make a copy, but copy() isn't working. Here is what I put in:
A = copy(matrix(3,3,0))
A[0][0]=1
and the error says vector is immutable; please change a copy instead (use copy())
What am I doing wrong?
Thank you!Fri, 06 Dec 2013 18:46:44 -0600http://ask.sagemath.org/question/10812/using-copy-for-matrices-still-returns-error/Comment by kcrisman for <p>I want to take the zero matrix and assign entries to make a new matrix. I understand that the zero matrix is immutable and I need to make a copy, but copy() isn't working. Here is what I put in:</p>
<p>A = copy(matrix(3,3,0))
A[0][0]=1</p>
<p>and the error says vector is immutable; please change a copy instead (use copy())</p>
<p>What am I doing wrong? </p>
<p>Thank you!</p>
http://ask.sagemath.org/question/10812/using-copy-for-matrices-still-returns-error/?comment=16555#post-id-16555I'll just add to ppurka's answer that `copy` *is* working, in case anyone was confused by that not being mentioned explicitly.Sat, 07 Dec 2013 03:10:39 -0600http://ask.sagemath.org/question/10812/using-copy-for-matrices-still-returns-error/?comment=16555#post-id-16555Answer by ppurka for <p>I want to take the zero matrix and assign entries to make a new matrix. I understand that the zero matrix is immutable and I need to make a copy, but copy() isn't working. Here is what I put in:</p>
<p>A = copy(matrix(3,3,0))
A[0][0]=1</p>
<p>and the error says vector is immutable; please change a copy instead (use copy())</p>
<p>What am I doing wrong? </p>
<p>Thank you!</p>
http://ask.sagemath.org/question/10812/using-copy-for-matrices-still-returns-error/?answer=15770#post-id-15770Look at the documentation `A.__getitem__?` for the explanation. In short, `A[0]` returns the first row of the matrix which is an immutable vector. And `A[0][0]` returns the first entry of that vector.
If you want the first entry of the matrix directly, then use `A[0,0]`. In this case, you don't need to work with a copy and you can change the original matrix directly since the matrices are not immutable by default.
sage: A = matrix(3,3,0); A
[0 0 0]
[0 0 0]
[0 0 0]
sage: A.is_immutable()
False
sage: A[0,0] = 1; A
[1 0 0]
[0 0 0]
[0 0 0]
sage: A[0]
(1, 0, 0)
sage: A[0].is_immutable()
True
Fri, 06 Dec 2013 19:55:17 -0600http://ask.sagemath.org/question/10812/using-copy-for-matrices-still-returns-error/?answer=15770#post-id-15770