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 Dec 2014 20:19:04 +0100defining function for graph (help needed)https://ask.sagemath.org/question/9859/defining-function-for-graph-help-needed/I need help defining a function that does the following (I'm very new to Python/Sage).
I need a function called ihara(G) where G is any graph input by the user.
I need the function ihara(G) to return the Z resulting from all the calculations below:
D = G.to_directed()
L = D.line_graph()
L.delete_edges([((x,y,None), (y,x,None)) for x,y in G.edges( labels=None)])
L.delete_edges([((x,y,None), (y,x,None)) for y,x in G.edges( labels=None)])
IM = identity_matrix(QQ,D.size())
T = L.adjacency.matrix()
var('u')
X = IM-u*T
Z = X.det()
Could anybody help me with this? or at least get me started in the right direction...Wed, 27 Feb 2013 14:14:51 +0100https://ask.sagemath.org/question/9859/defining-function-for-graph-help-needed/Comment by FrédéricC for <p>I need help defining a function that does the following (I'm very new to Python/Sage).</p>
<p>I need a function called ihara(G) where G is any graph input by the user.</p>
<p>I need the function ihara(G) to return the Z resulting from all the calculations below:</p>
<pre><code>D = G.to_directed()
L = D.line_graph()
L.delete_edges([((x,y,None), (y,x,None)) for x,y in G.edges( labels=None)])
L.delete_edges([((x,y,None), (y,x,None)) for y,x in G.edges( labels=None)])
IM = identity_matrix(QQ,D.size())
T = L.adjacency.matrix()
var('u')
X = IM-u*T
Z = X.det()
</code></pre>
<p>Could anybody help me with this? or at least get me started in the right direction...</p>
https://ask.sagemath.org/question/9859/defining-function-for-graph-help-needed/?comment=25208#post-id-25208Is what you want the same as
sage: g=graphs.CycleGraph(5)
sage: g.ihara_zeta_function_inverse()
?Wed, 10 Dec 2014 20:19:04 +0100https://ask.sagemath.org/question/9859/defining-function-for-graph-help-needed/?comment=25208#post-id-25208Answer by fidbc for <p>I need help defining a function that does the following (I'm very new to Python/Sage).</p>
<p>I need a function called ihara(G) where G is any graph input by the user.</p>
<p>I need the function ihara(G) to return the Z resulting from all the calculations below:</p>
<pre><code>D = G.to_directed()
L = D.line_graph()
L.delete_edges([((x,y,None), (y,x,None)) for x,y in G.edges( labels=None)])
L.delete_edges([((x,y,None), (y,x,None)) for y,x in G.edges( labels=None)])
IM = identity_matrix(QQ,D.size())
T = L.adjacency.matrix()
var('u')
X = IM-u*T
Z = X.det()
</code></pre>
<p>Could anybody help me with this? or at least get me started in the right direction...</p>
https://ask.sagemath.org/question/9859/defining-function-for-graph-help-needed/?answer=14607#post-id-14607To declare a function you can just follow this scheme
def function_name(<arguments>):
"Optional documentation string."
<Python/Sage instructions, could be multiple lines of code>
return <variable/expression>
In your case, I guess you'd like to receive `G` as an argument, so it could be of the form
def ihara( G ):
<your source code here>
return Z
For more details on functions you can consult [this](http://en.wikibooks.org/wiki/Non-Programmer%27s_Tutorial_for_Python_2.6/Defining_Functions) webpage.Wed, 27 Feb 2013 15:41:17 +0100https://ask.sagemath.org/question/9859/defining-function-for-graph-help-needed/?answer=14607#post-id-14607