Ask Your Question
1

Using copy() for matrices still returns error

asked 2013-12-07 01:46:44 +0100

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 10:10:39 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2013-12-07 02:55:17 +0100

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-07 01:46:44 +0100

Seen: 1,620 times

Last updated: Dec 07 '13