|
10 | 10 | from mathics.builtin.graphics import GraphicsBox |
11 | 11 | from mathics.builtin.randomnumbers import RandomEnv |
12 | 12 | from mathics.core.expression import ( |
13 | | - Expression, |
14 | | - Symbol, |
15 | 13 | Atom, |
| 14 | + Expression, |
16 | 15 | Integer, |
17 | 16 | Real, |
| 17 | + String, |
| 18 | + Symbol, |
18 | 19 | from_python, |
19 | 20 | ) |
20 | 21 | from mathics.builtin.patterns import Matcher |
|
31 | 32 | "PlotLabel": "Null", |
32 | 33 | } |
33 | 34 |
|
34 | | -DEFAULT_TREE_OPTIONS = {**DEFAULT_GRAPH_OPTIONS, **{"Directed" : "False"}} |
35 | | - |
| 35 | +DEFAULT_TREE_OPTIONS = {**DEFAULT_GRAPH_OPTIONS, |
| 36 | + **{"Directed" : "False", |
| 37 | + "GraphLayout": '"tree"'}} |
36 | 38 |
|
37 | 39 | try: |
38 | 40 | import networkx as nx |
@@ -445,6 +447,12 @@ def update_weights(self, evaluation): |
445 | 447 | return weights |
446 | 448 |
|
447 | 449 |
|
| 450 | +class TreeGraph(Graph): |
| 451 | + def __init__(self, G, **kwargs): |
| 452 | + super(Graph, self).__init__() |
| 453 | + self.G = G |
| 454 | + |
| 455 | + |
448 | 456 | def _is_connected(G): |
449 | 457 | if len(G) == 0: # empty graph? |
450 | 458 | return True |
@@ -508,9 +516,6 @@ def _create_graph(new_edges, new_edge_properties, options, from_graph=None): |
508 | 516 |
|
509 | 517 | multigraph = [False] |
510 | 518 |
|
511 | | - if "System`VertexStyle" in options: |
512 | | - vertex_options = options["System`VertexStyle"].to_python() |
513 | | - |
514 | 519 | known_vertices = set(vertices) |
515 | 520 | known_edges = set(edges) |
516 | 521 |
|
@@ -640,7 +645,10 @@ def full_new_edge_properties(new_edge_style): |
640 | 645 | n_undirected=len(undirected_edges), |
641 | 646 | ) |
642 | 647 |
|
643 | | - return Graph(G) |
| 648 | + G.vertex_labeling = options["System`VertexLabeling"] |
| 649 | + g = Graph(G) |
| 650 | + G.title = g.title = options["System`PlotLabel"] |
| 651 | + return g |
644 | 652 |
|
645 | 653 |
|
646 | 654 | class Property(Builtin): |
@@ -739,6 +747,22 @@ def apply(self, graph, evaluation, options): |
739 | 747 | return _graph_from_list(graph.leaves, options) |
740 | 748 |
|
741 | 749 |
|
| 750 | +class TreeGraphAtom(AtomBuiltin): |
| 751 | + """ |
| 752 | + >> TreeGraph[{1->2, 2->3, 3->1}] |
| 753 | + = -Graph- |
| 754 | +
|
| 755 | + """ |
| 756 | + |
| 757 | + options = DEFAULT_TREE_OPTIONS |
| 758 | + |
| 759 | + def apply(self, graph, evaluation, options): |
| 760 | + "TreeGraph[graph_List, OptionsPattern[%(name)s]]" |
| 761 | + g = _graph_from_list(graph.leaves, options) |
| 762 | + g.G.graph_layout = String("tree") |
| 763 | + # Compute/check/set for root? |
| 764 | + return g |
| 765 | + |
742 | 766 | class PathGraph(_NetworkXBuiltin): |
743 | 767 | """ |
744 | 768 | >> PathGraph[{1, 2, 3}] |
|
0 commit comments