@@ -423,7 +423,7 @@ class _FullGraphRewrite(Exception):
423423
424424def _normalize_edges (edges ):
425425 for edge in edges :
426- if edge .has_form ("Property" , 2 ):
426+ if edge .has_form ("Pymathics` Property" , 2 ):
427427 expr , prop = edge .elements
428428 yield Expression (edge .get_head (), list (_normalize_edges ([expr ]))[0 ], prop )
429429 elif edge .get_head_name () == "System`Rule" :
@@ -726,7 +726,8 @@ def _graph_from_list(rules, options, new_vertices=None):
726726def _create_graph (
727727 new_edges , new_edge_properties , options , from_graph = None , new_vertices = None
728728):
729-
729+ vertices_dict = {}
730+ # Classification of vertex and edges
730731 known_vertices = set ()
731732 vertices = []
732733 vertex_properties = []
@@ -745,9 +746,6 @@ def add_vertex(x, attr_dict=None):
745746 directed_edges = []
746747 undirected_edges = []
747748
748- if new_vertices is not None :
749- vertices = [add_vertex (v ) for v in new_vertices ]
750-
751749 if from_graph is not None :
752750 old_vertices = dict (from_graph .nodes .data ())
753751 vertices += old_vertices
@@ -767,6 +765,31 @@ def add_vertex(x, attr_dict=None):
767765
768766 multigraph = [False ]
769767
768+
769+ if new_vertices is not None :
770+ for v in new_vertices :
771+ add_vertex (v )
772+
773+ def add_vertex (x , attr_dict = None ):
774+ if attr_dict is None :
775+ attr_dict = {}
776+ if x .has_form ("Pymathics`Property" , 2 ):
777+ expr , prop = x .elements
778+ attr_dict .update (_parse_property (prop , attr_dict ))
779+ return add_vertex (expr , attr_dict )
780+ elif x not in known_vertices :
781+ known_vertices .add (x )
782+ vertices .append (x )
783+ vertex_properties .append (attr_dict )
784+ vertices_dict [x ] = attr_dict
785+ else :
786+ vertices_dict [x ].update (attr_dict )
787+ return x
788+
789+ if new_vertices is not None :
790+ for v in new_vertices :
791+ add_vertex (v )
792+
770793 known_edges = set (edges )
771794 # It is simpler to just recompute this than change the above to work
772795 # incrementally
@@ -788,12 +811,28 @@ def track_edges(*edges):
788811 SymbolDirectedEdge if use_directed_edges else SymbolUndirectedEdge
789812 )
790813
791- def parse_edge (r , attr_dict ):
814+ def parse_edge (r , attr_dict = None ):
815+ if attr_dict is None :
816+ attr_dict = {}
817+
792818 if isinstance (r , Atom ):
793819 raise _GraphParseError (
794820 msg = f"{ r } is an atom, and hence does not define an edge."
795821 )
796822
823+ if r .has_form ("Pymathics`Property" , None ):
824+ expr , prop = r .elements
825+ attr_dict .update (_parse_property (prop , attr_dict ))
826+ return parse_edge (expr , attr_dict )
827+
828+
829+ if r .head not in (SymbolRule , SymbolDirectedEdge , SymbolUndirectedEdge ):
830+ raise _GraphParseError (
831+ msg = f"{ r } is not an edge description."
832+ )
833+
834+
835+
797836 name = r .get_head_name ()
798837 elements = r .elements
799838
@@ -1138,7 +1177,6 @@ class DegreeCentrality(_Centrality):
11381177
11391178 def _from_dict (self , graph , centrality ):
11401179 s = len (graph .G ) - 1 # undo networkx's normalization
1141- print ("_from_dict" , (graph , type (graph )))
11421180 return ListExpression (
11431181 * [Integer (s * centrality .get (v , 0 )) for v in graph .vertices ],
11441182 )
@@ -1274,7 +1312,6 @@ def eval(self, graph, expression, evaluation, options):
12741312 if graph :
12751313
12761314 def rules ():
1277- print ("Looking for Edge rules" )
12781315 for edge in graph .edges :
12791316 u , v = edge
12801317 yield Expression (SymbolRule , u , v )
@@ -1566,7 +1603,7 @@ class KatzCentrality(_ComponentwiseCentrality):
15661603 """
15671604
15681605 rules = {
1569- "KatzCentrality[g_, alpha_]" : "KatzCentrality[g, alpha, 1]" ,
1606+ "Pymathics` KatzCentrality[g_, Pymathics` alpha_]" : "Pymathics` KatzCentrality[g, Pymathics` alpha, 1]" ,
15701607 }
15711608
15721609 def _centrality (self , g , weight , alpha , beta ):
@@ -1575,11 +1612,16 @@ def _centrality(self, g, weight, alpha, beta):
15751612 )
15761613
15771614 def eval (self , graph , alpha , beta , expression , evaluation , options ):
1578- "%(name)s [graph_, alpha_, beta_, OptionsPattern[%(name)s]]"
1615+ "Pymathics`KatzCentrality [graph_, alpha_, beta_, OptionsPattern[%(name)s]]"
15791616 graph = self ._build_graph (graph , evaluation , options , expression )
1617+ print ("Katz graph" , graph )
15801618 if graph :
1581- py_alpha = alpha .to_mpmath ()
1582- py_beta = beta .to_mpmath ()
1619+ print ([alpha , beta , type (alpha ), type (beta )])
1620+ try :
1621+ py_alpha = alpha .to_mpmath ()
1622+ py_beta = beta .to_mpmath ()
1623+ except NotImplementedError :
1624+ return
15831625 if py_alpha is None or py_beta is None :
15841626 return
15851627 return self ._compute (
@@ -1639,6 +1681,7 @@ def eval(self, graph, expression, evaluation, options):
16391681 graph = self ._build_graph (graph , evaluation , options , expression , quiet = True )
16401682 if graph :
16411683 return from_python (graph .is_mixed_graph ())
1684+ return SymbolFalse
16421685
16431686
16441687class MultigraphQ (_NetworkXBuiltin ):
@@ -1757,14 +1800,21 @@ class PropertyValue(Builtin):
17571800
17581801 def eval (self , graph , item , name , evaluation ):
17591802 "PropertyValue[{graph_Graph, item_}, name_Symbol]"
1803+ name_str = name .get_name ()
17601804 if isinstance (graph , Graph ):
1761- try :
1762- # Fixme: this does not work...
1763- value = graph .G .get_property (item , name .get_name ())
1764- except :
1765- return SymbolFailed
1766- if value is None :
1805+ if (
1806+ item .has_form ("Rule" , 2 )
1807+ or item .has_form ("DirectedEdge" , 2 )
1808+ or item .has_form ("UndirectedEdge" , 2 )
1809+ ):
1810+ item_g = graph .G .edges ().get (item .elements )
1811+ else :
1812+ item_g = graph .G .nodes ().get (item )
1813+
1814+ if item_g is None :
17671815 return SymbolFailed
1816+
1817+ value = item_g .get (name_str , SymbolFailed )
17681818 return value
17691819
17701820
0 commit comments