Cannot turn numpy array into a graph in SageMath

Question:

After downloading testnb.sws following the instructions
on the website
https://sourceforge.net/p/networksym/code/ci/master/tree/
I tried to run it in the legacy “Sage Notebook”
(not the Jupyter notebook), as follows:

  • open the Sage Notebook
  • click “Upload”
  • click “Browse”
  • select testnb.sws
  • click “Upload worksheet”
  • click “Evaluate”

Evaluating the code cell in this worksheet results in the
following error:

ValueError: This input cannot be turned into a graph

It seems like in Sage, np.array() is not valid.

However, when I use

Aij32 = ([[0,1,0],[1,0,1],[0,1,0]])

instead of

Aij32 = np.array([[0,1,0],[1,0,1],[0,1,0]])

it shows

AttributeError: 'list' object has no attribute 'copy'

How to overcome this problem?

Asked By: sleeve chen

||

Answers:

Turn a numpy array into a graph

If a is a numpy array representing an adjacency matrix
for a graph, then instead of

Graph(a)

one can use

Graph(matrix(a))

to build the corresponding graph.

Fix for the worksheet referred to in the question

In the worksheet testnb.sws referred to in the question,
replacing this block

# get clusters
print "the orbits are:"
print data32.get_orbits()

by the following block

def get_orbits(a):
    r"""
    Return the orbits as a list of lists.
    """
    if a._orbits is None:
        a._group, a._orbits = sg.Graph(
                matrix(a.get_adjacency_matrix())
            ).automorphism_group(orbits=True)
    return sg.copy(a._orbits)

# get clusters
print(f"the orbits are:n{get_orbits(data32)}")

makes everything work nicely.

Answered By: Samuel Lelièvre
Categories: questions Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.