Skip to content

Commit ce2bde1

Browse files
committed
Set spirial shape for PathGraph...
and move this to generators
1 parent 696d210 commit ce2bde1

File tree

2 files changed

+63
-31
lines changed

2 files changed

+63
-31
lines changed

pymathics/graph/__main__.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@
3535
# And many others. Is there a list somewhere?
3636
}
3737

38+
WL_COLOR_TO_MATPLOTLIB = {
39+
"Green": "g",
40+
"Blue": "b",
41+
# And many others. Is there a list somewhere?
42+
}
43+
3844
DEFAULT_GRAPH_OPTIONS = {
3945
"DirectedEdges": "False",
4046
"EdgeStyle": "{}",
@@ -72,6 +78,15 @@ def _process_graph_options(g, options: dict) -> None:
7278
)
7379

7480
g.G.node_shape = g.node_shape = WL_MARKER_TO_MATPLOTLIB.get(shape, shape)
81+
82+
color = (
83+
options["System`VertexStyle"].get_string_value()
84+
if "System`VertexStyle" in options
85+
else "Blue"
86+
)
87+
88+
g.G.node_color = g.node_color = WL_COLOR_TO_MATPLOTLIB.get(color, color)
89+
7590
g.G.title = g.title = (
7691
options["System`PlotLabel"].get_string_value()
7792
if "System`PlotLabel" in options
@@ -809,24 +824,6 @@ def apply_1(self, vertices, edges, evaluation, options):
809824
edges.leaves, options=options, new_vertices=vertices.leaves
810825
)
811826

812-
813-
class PathGraph(_NetworkXBuiltin):
814-
"""
815-
>> PathGraph[{1, 2, 3}]
816-
= -Graph-
817-
"""
818-
819-
def apply(self, l, evaluation, options):
820-
"PathGraph[l_List, OptionsPattern[%(name)s]]"
821-
leaves = l.leaves
822-
823-
def edges():
824-
for u, v in zip(leaves, leaves[1:]):
825-
yield Expression("UndirectedEdge", u, v)
826-
827-
return _graph_from_list(edges(), options)
828-
829-
830827
class PathGraphQ(_NetworkXBuiltin):
831828
"""
832829
>> PathGraphQ[Graph[{1 -> 2, 2 -> 3}]]

pymathics/graph/generators.py

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
WL_MARKER_TO_MATPLOTLIB,
1313
_NetworkXBuiltin,
1414
_convert_networkx_graph,
15+
_graph_from_list,
1516
has_directed_option,
1617
_process_graph_options,
1718
nx,
@@ -180,19 +181,20 @@ def apply(self, n, expression, evaluation, options):
180181

181182

182183
def complete_graph_apply(self, n, expression, evaluation, options):
183-
py_n = n.get_int_value()
184+
py_n = n.get_int_value()
184185

185-
if py_n < 1:
186-
evaluation.message(self.get_name(), "ilsmp", expression)
187-
return
186+
if py_n < 1:
187+
evaluation.message(self.get_name(), "ilsmp", expression)
188+
return
188189

189-
args = (py_n,)
190-
g = graph_helper(nx.complete_graph, options, False, "circular", None, *args)
191-
if not g:
192-
return None
190+
args = (py_n,)
191+
g = graph_helper(nx.complete_graph, options, False, "circular", None, *args)
192+
if not g:
193+
return None
194+
195+
g.G.n = n
196+
return g
193197

194-
g.G.n = n
195-
return g
196198

197199
class CompleteGraph(_NetworkXBuiltin):
198200
"""
@@ -224,6 +226,7 @@ def apply_multipartite(self, n, evaluation, options):
224226
nx.complete_multipartite_graph(*[i.get_int_value() for i in n.leaves])
225227
)
226228

229+
227230
class CompleteKaryTree(_NetworkXBuiltin):
228231
"""<dl>
229232
<dt>'CompleteKaryTree[$n$, $k$]'
@@ -252,15 +255,16 @@ def apply(self, k, n, expression, evaluation, options):
252255
new_n_int = int(((k_int ** n_int) - 1) / (k_int - 1))
253256
return f_r_t_apply(self, k, Integer(new_n_int), expression, evaluation, options)
254257

255-
256258
# FIXME: can be done with rules?
257259
def apply_2(self, n, expression, evaluation, options):
258260
"%(name)s[n_Integer, OptionsPattern[%(name)s]]"
259261

260262
n_int = n.get_int_value()
261263

262264
new_n_int = int(2 ** n_int) - 1
263-
return f_r_t_apply(self, Integer(2), Integer(new_n_int), expression, evaluation, options)
265+
return f_r_t_apply(
266+
self, Integer(2), Integer(new_n_int), expression, evaluation, options
267+
)
264268

265269

266270
class CycleGraph(_NetworkXBuiltin):
@@ -279,7 +283,10 @@ def apply(self, n, expression, evaluation, options):
279283
if n_int < 3:
280284
return complete_graph_apply(self, n, expression, evaluation, options)
281285
else:
282-
return hkn_harary_apply(self, Integer(2), n, expression, evaluation, options)
286+
return hkn_harary_apply(
287+
self, Integer(2), n, expression, evaluation, options
288+
)
289+
283290

284291
def f_r_t_apply(self, r, n, expression, evaluation, options):
285292
py_r = r.get_int_value()
@@ -302,6 +309,7 @@ def f_r_t_apply(self, r, n, expression, evaluation, options):
302309
g.G.n = n
303310
return g
304311

312+
305313
class FullRAryTree(_NetworkXBuiltin):
306314
"""<dl>
307315
<dt>'FullRAryTree[$r$, $n$]'
@@ -324,6 +332,7 @@ class FullRAryTree(_NetworkXBuiltin):
324332
}
325333

326334
options = DEFAULT_TREE_OPTIONS
335+
327336
def apply(self, r, n, expression, evaluation, options):
328337
"%(name)s[r_Integer, n_Integer, OptionsPattern[%(name)s]]"
329338
return f_r_t_apply(self, r, n, expression, evaluation, options)
@@ -360,6 +369,7 @@ def apply(self, n, expression, evaluation, options):
360369
g.n = n
361370
return g
362371

372+
363373
def hkn_harary_apply(self, k, n, expression, evaluation, options):
364374
py_k = k.get_int_value()
365375

@@ -481,6 +491,7 @@ class KaryTree(_NetworkXBuiltin):
481491
}
482492

483493
options = DEFAULT_TREE_OPTIONS
494+
484495
def apply(self, n, expression, evaluation, options):
485496
"%(name)s[n_Integer, OptionsPattern[%(name)s]]"
486497
return f_r_t_apply(self, Integer(2), n, expression, evaluation, options)
@@ -490,6 +501,29 @@ def apply_2(self, n, k, expression, evaluation, options):
490501
return f_r_t_apply(self, k, n, expression, evaluation, options)
491502

492503

504+
class PathGraph(_NetworkXBuiltin):
505+
"""
506+
<dl>
507+
<dt>'PathGraph[{$v_1$, $v_2$, ...}]'
508+
<dd>Returns a Graph with a path with vertices $v_i$ and edges between $v-i$ and $v_i+1$ .
509+
</dl>
510+
>> PathGraph[{1, 2, 3}]
511+
= -Graph-
512+
"""
513+
514+
def apply(self, l, evaluation, options):
515+
"PathGraph[l_List, OptionsPattern[%(name)s]]"
516+
leaves = l.leaves
517+
518+
def edges():
519+
for u, v in zip(leaves, leaves[1:]):
520+
yield Expression("UndirectedEdge", u, v)
521+
522+
g = _graph_from_list(edges(), options)
523+
g.G.graph_layout = options["System`GraphLayout"].get_string_value() or "spiral_equidistant"
524+
return g
525+
526+
493527
class RandomGraph(_NetworkXBuiltin):
494528
"""
495529
<dl>
@@ -500,6 +534,7 @@ class RandomGraph(_NetworkXBuiltin):
500534
<dd>Returns list of $k$ RandomGraph[{$n$, $m$}].
501535
</dl>
502536
"""
537+
503538
def _generate(self, n, m, k, evaluation, options):
504539
py_n = n.get_int_value()
505540
py_m = m.get_int_value()

0 commit comments

Comments
 (0)