如何使用额外的方法在 Sage 中创建新的 Graph 类?
我正在 Sage 中编写一些代码来使用费曼图进行一些计算,费曼图只是带有边缘标签的有限的、无向的多重图。我需要实现边缘收缩等方法,而 sage.graphs.graph.Graph 类中奇怪地缺少这些方法。但我也想继承所有现有的图形方法,例如 is_tree
。
这是模块 Feynman.sage 的顶部,应该附加新类。
from sage.graphs.graph import Graph
class FeynmanGraph(Graph):
"""An unoriented multi-graph with labeled edges"""
def __init__(self, E=[]):
self._edges = len(E)
def __repr__(self):
return 'A Feynman graph with ' + str(self._edges) + ' edges.'
我没有做正确的事情。虽然类的实例化产生了正确的方法目录,但其中许多方法不起作用,因为
'FeynmanGraph' object has no attribute '_backend'
我认为这与 Sage 只是其他一些图论包的 Pythonic 包装器有关。
请指教。
I am writing some code in Sage to do some calculations with Feynman graphs, which are just finite, un-oriented multigraphs with edge-labels. I need to implement methods such as edge-contraction, which are curiously missing from the class sage.graphs.graph.Graph
. But I also want to inherit all the existing graph methods, like e.g. is_tree
.
Here's the top of the module Feynman.sage that ought to attach the new class.
from sage.graphs.graph import Graph
class FeynmanGraph(Graph):
"""An unoriented multi-graph with labeled edges"""
def __init__(self, E=[]):
self._edges = len(E)
def __repr__(self):
return 'A Feynman graph with ' + str(self._edges) + ' edges.'
I'm not doing something right. Although an instantiation of the class yields the correct directory of methods, many of them don't work because
'FeynmanGraph' object has no attribute '_backend'
I think this has something to do with the way that Sage is just a Pythonic wrapper for some other graph theory package.
Please advise.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能根本没有正确继承事物。尝试将其插入到
__init__()
的开头:You might simply not be inheriting things correctly. Try inserting this at the beginning of
__init__()
: