Ask Your Question

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!

edit retag close merge delete

Comments

I'll just add to ppurka's answer that copy *is* working, in case anyone was confused by that not being mentioned explicitly.

( 2013-12-07 10:10:39 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted

Look 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

more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Stats

Asked: 2013-12-07 01:46:44 +0200

Seen: 831 times

Last updated: Dec 07 '13