1

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.

4
  • Hi Sammy, you might have better luck posting your question at ask.sagemath.org. (I only know how to do such things in Mathematica: If you forgive some self-advertising see Feynman Diagrams and Symanzik Polynomials)
    – Simon
    Mar 15, 2011 at 5:15
  • Also, Edge contraction is just a special case of vertex merging, where you only merge 2 vertices and you preserve the multiplicities. So see merge_vertices which is (along with is_tree) in generic_graph.py. There's also a trac about edge_contract, so see the discussion there.
    – Simon
    Mar 17, 2011 at 1:30
  • 1
    Finally, not all sage graph objects are just wrappers of other packages. If you contract a graph from a list of edges, adjacency matrix etc, then you by default create objects that use SparseGraphBackend or DenseGraphBackend.
    – Simon
    Mar 17, 2011 at 1:36
  • Most of the graph theory stuff in Sage is networkx, which is a graph theory package programmed IN PYTHON. networkx.lanl.gov
    – GeoffDS
    Dec 22, 2011 at 3:31

1 Answer 1

3

You might simply not be inheriting things correctly. Try inserting this at the beginning of __init__():

super().__init__()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.