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, 10 Nov 2021 22:48:22 +0100Error computing kernel of matrix over symbolic ringhttps://ask.sagemath.org/question/59645/error-computing-kernel-of-matrix-over-symbolic-ring/I am trying to compute the kernel of the Laplacian of the hypercube graph where all nonzero entries are symbolic variables. The below code works for N=2
N = 3 #dimension of hypercube graph
G = graphs.CubeGraph(N).to_directed()
#Assign edge labels as symbolic variables
for u,v,l in G.edges(sort=False):
this_label = 'e' + str(u) + 'e' + str(v)
G.set_edge_label(u, v, this_label)
var(this_label)
#relabel vertices to facilitate conversion to matrix
G.relabel()
#Create edge dictionary to facilitate conversion to matrix
edge_dict = {(u,v):k for u,v,k in G.edges()}
#Construct Laplacian matrix
temp = matrix(SR, G.order(), G.order(), edge_dict)
L = temp - diagonal_matrix(sum(temp))
#compute Laplacian kernel
ker = transpose(L).kernel();
but gives the below error for N=3 at the last line:
TypeError: ECL says: THROW: The catch RAT-ERR is undefined.
During handling of the above exception, another exception occurred:
...
TypeError: Multiplying row by Symbolic Ring element cannot be done over Symbolic Ring, use change_ring or with_added_multiple_of_row instead.
Any ideas as to the cause of the error? Can SAGE handle computing kernels of symbolic matrices of this size?
Thanks!
Mon, 08 Nov 2021 22:05:03 +0100https://ask.sagemath.org/question/59645/error-computing-kernel-of-matrix-over-symbolic-ring/Answer by Max Alekseyev for <p>I am trying to compute the kernel of the Laplacian of the hypercube graph where all nonzero entries are symbolic variables. The below code works for N=2</p>
<pre><code>N = 3 #dimension of hypercube graph
G = graphs.CubeGraph(N).to_directed()
#Assign edge labels as symbolic variables
for u,v,l in G.edges(sort=False):
this_label = 'e' + str(u) + 'e' + str(v)
G.set_edge_label(u, v, this_label)
var(this_label)
#relabel vertices to facilitate conversion to matrix
G.relabel()
#Create edge dictionary to facilitate conversion to matrix
edge_dict = {(u,v):k for u,v,k in G.edges()}
#Construct Laplacian matrix
temp = matrix(SR, G.order(), G.order(), edge_dict)
L = temp - diagonal_matrix(sum(temp))
#compute Laplacian kernel
ker = transpose(L).kernel();
</code></pre>
<p>but gives the below error for N=3 at the last line:</p>
<pre><code>TypeError: ECL says: THROW: The catch RAT-ERR is undefined.
During handling of the above exception, another exception occurred:
...
TypeError: Multiplying row by Symbolic Ring element cannot be done over Symbolic Ring, use change_ring or with_added_multiple_of_row instead.
</code></pre>
<p>Any ideas as to the cause of the error? Can SAGE handle computing kernels of symbolic matrices of this size?</p>
<p>Thanks!</p>
https://ask.sagemath.org/question/59645/error-computing-kernel-of-matrix-over-symbolic-ring/?answer=59664#post-id-59664First, I cannot reproduce an error in Sage 9.4, but computation of the kernel takes forever.
Second, it's better to avoid calculations in the symbolic ring (as inefficient) whenever possible. In this case, the elements of the kernel can be viewed as rational functions in the label variables and thus as elements of the corresponding fraction field. Correspondingly, modifying the code into:
N = 3 #dimension of hypercube graph
G = graphs.CubeGraph(N).to_directed()
#Assign edge labels as symbolic variables
for u,v,l in G.edges(sort=False):
this_label = 'e' + str(u) + 'e' + str(v)
G.set_edge_label(u, v, this_label)
#relabel vertices to facilitate conversion to matrix
G.relabel()
#Create edge dictionary to facilitate conversion to matrix
edge_dict = {(u,v):k for u,v,k in G.edges()}
F = PolynomialRing(QQ, list(edge_dict.values()) ).fraction_field()
#Construct Laplacian matrix
temp = matrix(F, G.order(), G.order(), edge_dict)
L = temp - diagonal_matrix(sum(temp))
#compute Laplacian kernel
ker = transpose(L).kernel()
makes it run in a matter of seconds.Tue, 09 Nov 2021 21:06:49 +0100https://ask.sagemath.org/question/59645/error-computing-kernel-of-matrix-over-symbolic-ring/?answer=59664#post-id-59664Comment by Max Alekseyev for <p>First, I cannot reproduce an error in Sage 9.4, but computation of the kernel takes forever.</p>
<p>Second, it's better to avoid calculations in the symbolic ring (as inefficient) whenever possible. In this case, the elements of the kernel can be viewed as rational functions in the label variables and thus as elements of the corresponding fraction field. Correspondingly, modifying the code into:</p>
<pre><code>N = 3 #dimension of hypercube graph
G = graphs.CubeGraph(N).to_directed()
#Assign edge labels as symbolic variables
for u,v,l in G.edges(sort=False):
this_label = 'e' + str(u) + 'e' + str(v)
G.set_edge_label(u, v, this_label)
#relabel vertices to facilitate conversion to matrix
G.relabel()
#Create edge dictionary to facilitate conversion to matrix
edge_dict = {(u,v):k for u,v,k in G.edges()}
F = PolynomialRing(QQ, list(edge_dict.values()) ).fraction_field()
#Construct Laplacian matrix
temp = matrix(F, G.order(), G.order(), edge_dict)
L = temp - diagonal_matrix(sum(temp))
#compute Laplacian kernel
ker = transpose(L).kernel()
</code></pre>
<p>makes it run in a matter of seconds.</p>
https://ask.sagemath.org/question/59645/error-computing-kernel-of-matrix-over-symbolic-ring/?comment=59682#post-id-59682I believe fraction field elements are stored in the reduced form, i.e numerator and denominator do not share common factors. simplify_full() would be redundant for them.Wed, 10 Nov 2021 22:48:22 +0100https://ask.sagemath.org/question/59645/error-computing-kernel-of-matrix-over-symbolic-ring/?comment=59682#post-id-59682Comment by jn21 for <p>First, I cannot reproduce an error in Sage 9.4, but computation of the kernel takes forever.</p>
<p>Second, it's better to avoid calculations in the symbolic ring (as inefficient) whenever possible. In this case, the elements of the kernel can be viewed as rational functions in the label variables and thus as elements of the corresponding fraction field. Correspondingly, modifying the code into:</p>
<pre><code>N = 3 #dimension of hypercube graph
G = graphs.CubeGraph(N).to_directed()
#Assign edge labels as symbolic variables
for u,v,l in G.edges(sort=False):
this_label = 'e' + str(u) + 'e' + str(v)
G.set_edge_label(u, v, this_label)
#relabel vertices to facilitate conversion to matrix
G.relabel()
#Create edge dictionary to facilitate conversion to matrix
edge_dict = {(u,v):k for u,v,k in G.edges()}
F = PolynomialRing(QQ, list(edge_dict.values()) ).fraction_field()
#Construct Laplacian matrix
temp = matrix(F, G.order(), G.order(), edge_dict)
L = temp - diagonal_matrix(sum(temp))
#compute Laplacian kernel
ker = transpose(L).kernel()
</code></pre>
<p>makes it run in a matter of seconds.</p>
https://ask.sagemath.org/question/59645/error-computing-kernel-of-matrix-over-symbolic-ring/?comment=59681#post-id-59681Wow, this is fantastic! Thank you! I am using Sage 9.3 and making your changes does indeed solve this quickly for N=3. My original intention was to solve this for N=4, which still fails on my laptop (Sage does not give an error, the Jupyter kernel just fails. I will try to run on a more powerful machine)
Given that the computations are now being done in the fraction field, does this mean that the resulting elements of the kernel (which are of FractionFieldElement type) are automatically expressed in lowest terms? I noticed that the FractionFieldElement class does not have a simplify_full() method.Wed, 10 Nov 2021 22:21:29 +0100https://ask.sagemath.org/question/59645/error-computing-kernel-of-matrix-over-symbolic-ring/?comment=59681#post-id-59681