Ask Your Question
2

Deepcopy of a Matrix SageMath

asked 8 years ago

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:

B.subdivide(*A.subdivisions())

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).

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
1

answered 8 years ago

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.

Preview: (hide)
link

Comments

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 ( 8 years ago )

Link to this answer. provided at stackoverflow.

kyticka gravatar imagekyticka ( 8 years ago )

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

Stats

Asked: 8 years ago

Seen: 997 times

Last updated: Jan 01 '17