Ask Your Question
0

Using copy() for matrices still returns error

asked 2013-12-06 18:46:44 -0500

EEChisholm gravatar image

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 flag offensive 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.

kcrisman gravatar imagekcrisman ( 2013-12-07 03:10:39 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-12-06 19:55:17 -0500

ppurka gravatar image

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
edit flag offensive delete link more

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 2013-12-06 18:46:44 -0500

Seen: 227 times

Last updated: Dec 06 '13