Ask Your Question

Deepcopy of a Matrix SageMath

asked 2017-01-01 12:38:54 +0200

kyticka gravatar image

I am creating a deepcopy of a Matrix in SageMath.

import copy
A = Matrix([[1,2],[3,4]]).augment(Matrix.identity(2), subdivide=True)
B = copy.deepcopy(A)
print A
print B

Gives me:

[1 2|1 0]
[3 4|0 1]

[1 2 1 0]
[3 4 0 1]

What is the correct way to deepcopy a matrix with the subdivision? Do I have to use:


SageMath version 7.2, Release Date: 2016-05-15

I did not know about ask.sagemath before. This is a repost of stackoverflow .com /questions/41322359/deepcopy-of-a-matrix-sagemath (my karma is not enough to post links).

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2017-01-01 16:27:53 +0200

tmonteil gravatar image

The reason is that a specific __copy__ method was written for matrices, but not a __deepcopy__ one, hence copy.deepcopy falls back to a generic method which does not care about the subdivision. As you can see, the copy works well:

sage: C = copy.copy(A)
sage: C
[1 2|1 0]
[3 4|0 1]

and you can check in its source code that a special care of subdivision is made:

sage: A.__copy__??

If you need this feature, let me suggest to add a __deepcopy__ method in Sage source code.

edit flag offensive delete link more


I am a bit new to both Sage and Python and __deepcopy__ seemed like the function I should use. But there is no advantage in using __deepcopy__ over __copy__ for me.

kyticka gravatar imagekyticka ( 2017-01-01 18:51:31 +0200 )edit

Link to this answer. provided at stackoverflow.

kyticka gravatar imagekyticka ( 2017-01-01 19:05:56 +0200 )edit

Your Answer

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

Add Answer

Question Tools

1 follower


Asked: 2017-01-01 12:38:54 +0200

Seen: 800 times

Last updated: Jan 01 '17