Skip to content

Commit 5db97c2

Browse files
committed
Add CompleteKaryTree. Directed->DirectedEdges
This matches WL better
1 parent 90d2783 commit 5db97c2

File tree

2 files changed

+62
-22
lines changed

2 files changed

+62
-22
lines changed

pymathics/graph/graph_generators.py

Lines changed: 61 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def graph_helper(
2121
*args,
2222
**kwargs
2323
) -> Optional[Callable]:
24-
should_digraph = can_digraph and options["System`Directed"].to_python()
24+
should_digraph = can_digraph and options["System`DirectedEdges"].to_python()
2525
try:
2626
G = (
2727
graph_generator_func(*args, create_using=nx.DiGraph, **kwargs)
@@ -214,6 +214,44 @@ def apply_multipartite(self, n, evaluation, options):
214214
nx.complete_multipartite_graph(*[i.get_int_value() for i in n.leaves])
215215
)
216216

217+
class CompleteKaryTree(_NetworkXBuiltin):
218+
"""<dl>
219+
<dt>'CompleteKaryTree[$n$, $k$]'
220+
<dd>Creates a complete $k$-ary tree of $n$ levels.
221+
</dl>
222+
223+
In the returned tree, with $n$ nodes, the from root $R$ to any
224+
leaf be $k.
225+
226+
>> CompleteKaryTree[2, 3]
227+
= -Graph-
228+
229+
>> CompleteKaryTree[3]
230+
= -Graph-
231+
232+
"""
233+
234+
options = DEFAULT_TREE_OPTIONS
235+
236+
def apply(self, k, n, expression, evaluation, options):
237+
"%(name)s[n_Integer, k_Integer, OptionsPattern[%(name)s]]"
238+
239+
n_int = n.get_int_value()
240+
k_int = k.get_int_value()
241+
242+
new_n_int = int(((k_int ** n_int) - 1) / (k_int - 1))
243+
return f_r_t_apply(self, k, Integer(new_n_int), expression, evaluation, options)
244+
245+
246+
# FIXME: can be done with rules?
247+
def apply_2(self, n, expression, evaluation, options):
248+
"%(name)s[n_Integer, OptionsPattern[%(name)s]]"
249+
250+
n_int = n.get_int_value()
251+
252+
new_n_int = int(2 ** n_int) - 1
253+
return f_r_t_apply(self, Integer(2), Integer(new_n_int), expression, evaluation, options)
254+
217255

218256
class CycleGraph(_NetworkXBuiltin):
219257
"""<dl>
@@ -230,6 +268,27 @@ def apply(self, n, expression, evaluation, options):
230268
return hkn_harary_apply(self, Integer(2), n, expression, evaluation, options)
231269

232270

271+
def f_r_t_apply(self, r, n, expression, evaluation, options):
272+
py_r = r.get_int_value()
273+
274+
if py_r < 0:
275+
evaluation.message(self.get_name(), "ilsmp", expression)
276+
return
277+
278+
py_n = n.get_int_value()
279+
if py_n < 0:
280+
evaluation.message(self.get_name(), "ilsmp", expression)
281+
return
282+
283+
args = (py_r, py_n)
284+
g = graph_helper(nx.full_rary_tree, options, True, "tree", 0, *args)
285+
if not g:
286+
return None
287+
288+
g.G.r = r
289+
g.G.n = n
290+
return g
291+
233292
class FullRAryTree(_NetworkXBuiltin):
234293
"""<dl>
235294
<dt>'FullRAryTree[$r$, $n$]'
@@ -252,28 +311,9 @@ class FullRAryTree(_NetworkXBuiltin):
252311
}
253312

254313
options = DEFAULT_TREE_OPTIONS
255-
256314
def apply(self, r, n, expression, evaluation, options):
257315
"%(name)s[r_Integer, n_Integer, OptionsPattern[%(name)s]]"
258-
py_r = r.get_int_value()
259-
260-
if py_r < 0:
261-
evaluation.message(self.get_name(), "ilsmp", expression)
262-
return
263-
264-
py_n = n.get_int_value()
265-
if py_n < 0:
266-
evaluation.message(self.get_name(), "ilsmp", expression)
267-
return
268-
269-
args = (py_r, py_n)
270-
g = graph_helper(nx.full_rary_tree, options, True, "tree", 0, *args)
271-
if not g:
272-
return None
273-
274-
g.G.r = r
275-
g.G.n = n
276-
return g
316+
return f_r_t_apply(self, r, n, expression, evaluation, options)
277317

278318

279319
class GraphAtlas(_NetworkXBuiltin):

pymathics/graph/tree.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
DEFAULT_TREE_OPTIONS = {
66
**DEFAULT_GRAPH_OPTIONS,
7-
**{"Directed": "False", "GraphLayout": '"tree"'},
7+
**{"DirectedEdges": "False", "GraphLayout": '"tree"'},
88
}
99

1010
from mathics.builtin.base import Builtin, AtomBuiltin

0 commit comments

Comments
 (0)