Remove legacy assumptions thus improving import time (#963)
[sympy.git] / sympy / printing / tree.py
blob94c0912dba06f3ac1ccb8f2874eadbf88f7c5cb1
1 from sympy.utilities.iterables import preorder_traversal
3 def pprint_nodes(subtrees):
4 """
5 Prettyprints systems of nodes.
7 Example:
9 >>> print pprint_nodes(["a", "b1\nb2", "c"])
10 +-a
11 +-b1
12 | b2
13 +-c
15 """
16 def indent(s,type=1):
17 x = s.split("\n")
18 r = "+-%s\n"%x[0]
19 for a in x[1:]:
20 if a=="": continue
21 if type==1:
22 r += "| %s\n"%a
23 else:
24 r += " %s\n"%a
25 return r
26 if len(subtrees)==0: return ""
27 f="";
28 for a in subtrees[:-1]:
29 f += indent(a)
30 f += indent(subtrees[-1],2)
31 return f
33 def print_node(node):
34 """
35 Returns an information about the "node".
37 This includes class name, string representation and assumptions.
38 """
39 s = "%s: %s\n" % (node.__class__.__name__, str(node))
40 if len(node._assumptions) > 0:
41 for a in node._assumptions:
42 s += "%s: %s\n" % (a, node._assumptions[a])
43 return s
45 def tree(node):
46 """
47 Returns a tree representation of "node" as a string.
49 It uses print_node() together with pprint_nodes() on node.args recursively.
51 See also: print_tree()
52 """
53 subtrees = []
54 for arg in node.args:
55 subtrees.append(tree(arg))
56 s = print_node(node)+pprint_nodes(subtrees)
57 return s
59 def print_tree(node):
60 """
61 Prints a tree representation of "node".
63 In [1]: print_tree(x**2)
64 Pow: x**2
65 +-Symbol: x
66 | comparable: False
67 | noncommutative: False
68 | commutative: True
69 +-Integer: 2
70 real: True
71 comparable: True
72 commutative: True
73 infinitesimal: False
74 nonzero: True
75 unbounded: False
76 noncommutative: False
77 noninteger: False
78 zero: False
79 complex: True
80 bounded: True
81 rational: True
82 integer: True
83 imaginary: False
84 finite: True
85 irrational: False
88 See also: tree()
89 """
90 print tree(node)