tree.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. def pprint_nodes(subtrees):
  2. """
  3. Prettyprints systems of nodes.
  4. Examples
  5. ========
  6. >>> from sympy.printing.tree import pprint_nodes
  7. >>> print(pprint_nodes(["a", "b1\\nb2", "c"]))
  8. +-a
  9. +-b1
  10. | b2
  11. +-c
  12. """
  13. def indent(s, type=1):
  14. x = s.split("\n")
  15. r = "+-%s\n" % x[0]
  16. for a in x[1:]:
  17. if a == "":
  18. continue
  19. if type == 1:
  20. r += "| %s\n" % a
  21. else:
  22. r += " %s\n" % a
  23. return r
  24. if not subtrees:
  25. return ""
  26. f = ""
  27. for a in subtrees[:-1]:
  28. f += indent(a)
  29. f += indent(subtrees[-1], 2)
  30. return f
  31. def print_node(node, assumptions=True):
  32. """
  33. Returns information about the "node".
  34. This includes class name, string representation and assumptions.
  35. Parameters
  36. ==========
  37. assumptions : bool, optional
  38. See the ``assumptions`` keyword in ``tree``
  39. """
  40. s = "%s: %s\n" % (node.__class__.__name__, str(node))
  41. if assumptions:
  42. d = node._assumptions
  43. else:
  44. d = None
  45. if d:
  46. for a in sorted(d):
  47. v = d[a]
  48. if v is None:
  49. continue
  50. s += "%s: %s\n" % (a, v)
  51. return s
  52. def tree(node, assumptions=True):
  53. """
  54. Returns a tree representation of "node" as a string.
  55. It uses print_node() together with pprint_nodes() on node.args recursively.
  56. Parameters
  57. ==========
  58. asssumptions : bool, optional
  59. The flag to decide whether to print out all the assumption data
  60. (such as ``is_integer`, ``is_real``) associated with the
  61. expression or not.
  62. Enabling the flag makes the result verbose, and the printed
  63. result may not be determinisitic because of the randomness used
  64. in backtracing the assumptions.
  65. See Also
  66. ========
  67. print_tree
  68. """
  69. subtrees = []
  70. for arg in node.args:
  71. subtrees.append(tree(arg, assumptions=assumptions))
  72. s = print_node(node, assumptions=assumptions) + pprint_nodes(subtrees)
  73. return s
  74. def print_tree(node, assumptions=True):
  75. """
  76. Prints a tree representation of "node".
  77. Parameters
  78. ==========
  79. asssumptions : bool, optional
  80. The flag to decide whether to print out all the assumption data
  81. (such as ``is_integer`, ``is_real``) associated with the
  82. expression or not.
  83. Enabling the flag makes the result verbose, and the printed
  84. result may not be determinisitic because of the randomness used
  85. in backtracing the assumptions.
  86. Examples
  87. ========
  88. >>> from sympy.printing import print_tree
  89. >>> from sympy import Symbol
  90. >>> x = Symbol('x', odd=True)
  91. >>> y = Symbol('y', even=True)
  92. Printing with full assumptions information:
  93. >>> print_tree(y**x)
  94. Pow: y**x
  95. +-Symbol: y
  96. | algebraic: True
  97. | commutative: True
  98. | complex: True
  99. | even: True
  100. | extended_real: True
  101. | finite: True
  102. | hermitian: True
  103. | imaginary: False
  104. | infinite: False
  105. | integer: True
  106. | irrational: False
  107. | noninteger: False
  108. | odd: False
  109. | rational: True
  110. | real: True
  111. | transcendental: False
  112. +-Symbol: x
  113. algebraic: True
  114. commutative: True
  115. complex: True
  116. even: False
  117. extended_nonzero: True
  118. extended_real: True
  119. finite: True
  120. hermitian: True
  121. imaginary: False
  122. infinite: False
  123. integer: True
  124. irrational: False
  125. noninteger: False
  126. nonzero: True
  127. odd: True
  128. rational: True
  129. real: True
  130. transcendental: False
  131. zero: False
  132. Hiding the assumptions:
  133. >>> print_tree(y**x, assumptions=False)
  134. Pow: y**x
  135. +-Symbol: y
  136. +-Symbol: x
  137. See Also
  138. ========
  139. tree
  140. """
  141. print(tree(node, assumptions=assumptions))