ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Sun, 01 Jan 2017 12:05:56 -0600Deepcopy of a Matrix SageMathhttp://ask.sagemath.org/question/36134/deepcopy-of-a-matrix-sagemath/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).Sun, 01 Jan 2017 05:38:54 -0600http://ask.sagemath.org/question/36134/deepcopy-of-a-matrix-sagemath/Answer by tmonteil for <p>I am creating a deepcopy of a Matrix in SageMath.</p>
<pre><code>import copy
A = Matrix([[1,2],[3,4]]).augment(Matrix.identity(2), subdivide=True)
B = copy.deepcopy(A)
print A
print B
</code></pre>
<p>Gives me:</p>
<pre><code>[1 2|1 0]
[3 4|0 1]
[1 2 1 0]
[3 4 0 1]
</code></pre>
<p>What is the correct way to deepcopy a matrix with the subdivision? Do I have to use:</p>
<pre><code>B.subdivide(*A.subdivisions())
</code></pre>
<p>SageMath version 7.2, Release Date: 2016-05-15</p>
<p>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).</p>
http://ask.sagemath.org/question/36134/deepcopy-of-a-matrix-sagemath/?answer=36137#post-id-36137The 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.
Sun, 01 Jan 2017 09:27:53 -0600http://ask.sagemath.org/question/36134/deepcopy-of-a-matrix-sagemath/?answer=36137#post-id-36137Comment by kyticka for <p>The reason is that a specific <code>__copy__</code> method was written for matrices, but not a <code>__deepcopy__</code> one, hence <code>copy.deepcopy</code> falls back to a generic method which does not care about the subdivision. As you can see, the copy works well:</p>
<pre><code>sage: C = copy.copy(A)
sage: C
[1 2|1 0]
[3 4|0 1]
</code></pre>
<p>and you can check in its source code that a special care of subdivision is made:</p>
<pre><code>sage: A.__copy__??
</code></pre>
<p>If you need this feature, let me suggest to add a <code>__deepcopy__</code> method in Sage source code.</p>
http://ask.sagemath.org/question/36134/deepcopy-of-a-matrix-sagemath/?comment=36140#post-id-36140Link to this answer. provided at stackoverflow.Sun, 01 Jan 2017 12:05:56 -0600http://ask.sagemath.org/question/36134/deepcopy-of-a-matrix-sagemath/?comment=36140#post-id-36140Comment by kyticka for <p>The reason is that a specific <code>__copy__</code> method was written for matrices, but not a <code>__deepcopy__</code> one, hence <code>copy.deepcopy</code> falls back to a generic method which does not care about the subdivision. As you can see, the copy works well:</p>
<pre><code>sage: C = copy.copy(A)
sage: C
[1 2|1 0]
[3 4|0 1]
</code></pre>
<p>and you can check in its source code that a special care of subdivision is made:</p>
<pre><code>sage: A.__copy__??
</code></pre>
<p>If you need this feature, let me suggest to add a <code>__deepcopy__</code> method in Sage source code.</p>
http://ask.sagemath.org/question/36134/deepcopy-of-a-matrix-sagemath/?comment=36139#post-id-36139I 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.Sun, 01 Jan 2017 11:51:31 -0600http://ask.sagemath.org/question/36134/deepcopy-of-a-matrix-sagemath/?comment=36139#post-id-36139