ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 24 Jan 2018 02:19:57 +0100Copying a Matrixhttps://ask.sagemath.org/question/40754/copying-a-matrix/I'm trying to take the Singular Value Decomposition of the adjacency matrix of a graph A, but all my "reasonable" attempts at doing so have failed, see the code below.
I believe the problem has something to do with the matrix being immutable, so I've been trying to copy A into a different matrix M and then perform SVD on M. he only thing that I've found to work is to essentially build A from scratch, but there must be a better method.
sage: A=graphs.CycleGraph(3).adjacency_matrix()
#Method 1: Try and get SVD directly from A; gives error at last step.
#sage: A.change_ring(RDF)
#sage: A.SVD()
#Method 2: Try and copy A to a different matrix and then do SVD; gives error at last step.
# sage: M=copy(A)
# sage: M.change_ring(RDF)
# sage: M.SVD()
#Method 3: Build A up again from scratch and then do SVD; works, but is tedious.
sage: M=matrix(RDF,3,3)
sage: for i in [0..2]:
for j in [0..2]:
if(A[i,j]==1):
M[i,j]=1
sage: M.SVD()Tue, 23 Jan 2018 19:44:19 +0100https://ask.sagemath.org/question/40754/copying-a-matrix/Answer by dan_fulea for <p>I'm trying to take the Singular Value Decomposition of the adjacency matrix of a graph A, but all my "reasonable" attempts at doing so have failed, see the code below. </p>
<p>I believe the problem has something to do with the matrix being immutable, so I've been trying to copy A into a different matrix M and then perform SVD on M. he only thing that I've found to work is to essentially build A from scratch, but there must be a better method.</p>
<pre><code>sage: A=graphs.CycleGraph(3).adjacency_matrix()
#Method 1: Try and get SVD directly from A; gives error at last step.
#sage: A.change_ring(RDF)
#sage: A.SVD()
#Method 2: Try and copy A to a different matrix and then do SVD; gives error at last step.
# sage: M=copy(A)
# sage: M.change_ring(RDF)
# sage: M.SVD()
#Method 3: Build A up again from scratch and then do SVD; works, but is tedious.
sage: M=matrix(RDF,3,3)
sage: for i in [0..2]:
for j in [0..2]:
if(A[i,j]==1):
M[i,j]=1
sage: M.SVD()
</code></pre>
https://ask.sagemath.org/question/40754/copying-a-matrix/?answer=40755#post-id-40755Note that `A.change_ring(RDF)` does not change `A` "in place", (so as it is the case with the `sort` method, applied on a list,) instead, it delivers an other object living over `RDF`. To see this:
sage: G = graphs.CycleGraph(3)
sage: A = G.adjacency_matrix()
sage: A.base_ring()
Integer Ring
sage: A.change_ring( RDF )
[0.0 1.0 1.0]
[1.0 0.0 1.0]
[1.0 1.0 0.0]
sage: A
[0 1 1]
[1 0 1]
[1 1 0]
sage: A.base_ring()
Integer Ring
Now the solution is simple:
sage: G = graphs.CycleGraph(3)
sage: A = G.adjacency_matrix()
sage: a = A.change_ring( RDF )
sage: a.SVD()
(
[ -0.5773502691896255 2.7755575615628914e-16 0.8164965809277258]
[ -0.5773502691896257 -0.7071067811865475 -0.4082482904638628]
[ -0.5773502691896255 0.7071067811865476 -0.40824829046386313],
[ 2.0 0.0 0.0]
[ 0.0 0.9999999999999999 0.0]
[ 0.0 0.0 0.9999999999999999],
[ -0.577350269189626 -0.0 -0.8164965809277259]
[-0.5773502691896256 0.7071067811865475 0.40824829046386313]
[-0.5773502691896256 -0.7071067811865476 0.40824829046386313]
)
sage:
Tue, 23 Jan 2018 22:24:23 +0100https://ask.sagemath.org/question/40754/copying-a-matrix/?answer=40755#post-id-40755Comment by zorkkoi for <p>Note that <code>A.change_ring(RDF)</code> does not change <code>A</code> "in place", (so as it is the case with the <code>sort</code> method, applied on a list,) instead, it delivers an other object living over <code>RDF</code>. To see this:</p>
<pre><code>sage: G = graphs.CycleGraph(3)
sage: A = G.adjacency_matrix()
sage: A.base_ring()
Integer Ring
sage: A.change_ring( RDF )
[0.0 1.0 1.0]
[1.0 0.0 1.0]
[1.0 1.0 0.0]
sage: A
[0 1 1]
[1 0 1]
[1 1 0]
sage: A.base_ring()
Integer Ring
</code></pre>
<p>Now the solution is simple:</p>
<pre><code>sage: G = graphs.CycleGraph(3)
sage: A = G.adjacency_matrix()
sage: a = A.change_ring( RDF )
sage: a.SVD()
(
[ -0.5773502691896255 2.7755575615628914e-16 0.8164965809277258]
[ -0.5773502691896257 -0.7071067811865475 -0.4082482904638628]
[ -0.5773502691896255 0.7071067811865476 -0.40824829046386313],
[ 2.0 0.0 0.0]
[ 0.0 0.9999999999999999 0.0]
[ 0.0 0.0 0.9999999999999999],
[ -0.577350269189626 -0.0 -0.8164965809277259]
[-0.5773502691896256 0.7071067811865475 0.40824829046386313]
[-0.5773502691896256 -0.7071067811865476 0.40824829046386313]
)
sage:
</code></pre>
https://ask.sagemath.org/question/40754/copying-a-matrix/?comment=40757#post-id-40757Thank you so much for that excellent answer, it was exactly what I was looking for!Wed, 24 Jan 2018 02:19:57 +0100https://ask.sagemath.org/question/40754/copying-a-matrix/?comment=40757#post-id-40757