line.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. from node import Leaf
  2. from traverse import traverse_depth_first
  3. OPERATORS = [
  4. ('+', '-'),
  5. ('*', '/', 'mod'),
  6. ('^', )
  7. ]
  8. MAX_PRED = len(OPERATORS)
  9. def is_operator(node):
  10. """
  11. Check if a given node is an operator (otherwise, it's a function).
  12. """
  13. label = node.title()
  14. return any(map(lambda x: label in x, OPERATORS))
  15. def pred(node):
  16. """
  17. Get the precedence of an operator node.
  18. """
  19. # Check binary and n-ary operators
  20. if not node.is_leaf and len(node) > 1:
  21. op = node.title()
  22. for i, group in enumerate(OPERATORS):
  23. if op in group:
  24. return i
  25. # Unary operator and leaves have highest precedence
  26. return MAX_PRED
  27. def generate_line_old(root):
  28. """
  29. Print an expression tree in a single text line. Where needed, add
  30. parentheses.
  31. >>> from node import Node, Leaf
  32. >>> l0, l1 = Leaf(1), Leaf(2)
  33. >>> plus = Node('+', l0, l1)
  34. >>> print generate_line_old(plus)
  35. 1 + 2
  36. >>> plus2 = Node('+', l0, l1)
  37. >>> times = Node('*', plus, plus2)
  38. >>> print generate_line_old(times)
  39. (1 + 2)(1 + 2)
  40. >>> l2 = Leaf(3)
  41. >>> uminus = Node('-', l2)
  42. >>> times = Node('*', plus, uminus)
  43. >>> print generate_line_old(times)
  44. (1 + 2) * -3
  45. >>> exp = Leaf('x')
  46. >>> inf = Leaf('oo')
  47. >>> minus_inf = Node('-', inf)
  48. >>> integral = Node('int', exp, minus_inf, inf)
  49. >>> print generate_line_old(integral)
  50. int(x, -oo, oo)
  51. """
  52. def traverse(node):
  53. """
  54. The expression tree is traversed using preorder traversal:
  55. 1. Visit the root
  56. 2. Traverse the subtrees in left-to-right order
  57. """
  58. if not node:
  59. return '<empty expression>'
  60. op = node.title()
  61. if not node.nodes:
  62. return op
  63. arity = len(node)
  64. if is_operator(node):
  65. if arity == 1:
  66. # Unary operator
  67. sub = node[0]
  68. sub_exp = traverse(sub)
  69. # Negated sub-expressions with spaces in them should be
  70. # enclosed in parentheses, unless they have a higher precedence
  71. # than subtraction are rewritten to a factor of a subtraction:
  72. # -(1 + 2)
  73. # -(1 - 2)
  74. # -4a
  75. # -(4 * 5)
  76. # 1 - 4 * 5
  77. # 1 + -(4 * 5) -> 1 - 4 * 5
  78. if ' ' in sub_exp and sub.is_Leaf \
  79. and hasattr(node, 'marked_negation') \
  80. and pred(sub) > 0:
  81. sub_exp = '(' + sub_exp + ')'
  82. result = op + sub_exp
  83. else:
  84. # N-ary operator
  85. node_pred = pred(node)
  86. result = ''
  87. sep = ' ' + op + ' '
  88. e = []
  89. # Mark added and subtracted negations for later use when adding
  90. # parentheses
  91. if op in ('+', '-'):
  92. for child in node:
  93. if child.title() == '-' and len(child) == 1:
  94. child.marked_negation = True
  95. for i, child in enumerate(node):
  96. exp = traverse(child)
  97. # Check if there is a precedence conflict
  98. # If so, add parentheses
  99. child_pred = pred(child)
  100. if child_pred < node_pred or \
  101. (i and child_pred == node_pred \
  102. and op != child.title()):
  103. exp = '(' + exp + ')'
  104. e.append(exp)
  105. if op == '*':
  106. # Check if an explicit multiplication sign is nessecary
  107. left, right = node
  108. # Get the previous multiplication element if the arity is
  109. # greater than 2
  110. if left.title() == '*':
  111. left = left[1]
  112. # a * b -> ab
  113. # a * 2 -> a * 2
  114. # a * (b) -> a(b)
  115. # (a) * b -> (a)b
  116. # (a) * (b) -> (a)(b)
  117. # 2 * a -> 2a
  118. left_id = is_id(left)
  119. right_id = is_id(right)
  120. left_paren = e[0][-1] == ')'
  121. right_paren = e[1][0] == '('
  122. left_int = is_int(left)
  123. if (left_id or left_paren or left_int) \
  124. and (right_id or right_paren):
  125. sep = ''
  126. result += sep.join(e)
  127. else:
  128. # Function call
  129. result = op + '(' + ', '.join(map(traverse, node)) + ')'
  130. return result
  131. # An addition with negation can be written as a subtraction, e.g.:
  132. # 1 + -2 -> 1 - 2
  133. return traverse(root).replace('+ -', '- ')
  134. def is_id(node):
  135. return node.is_leaf and not node.title().isdigit()
  136. def is_int(node):
  137. return node.is_leaf and node.title().isdigit()
  138. def generate_line(root):
  139. """
  140. Print an expression tree in a single text line. Where needed, add
  141. parentheses.
  142. >>> from node import Node, Leaf
  143. >>> l0, l1 = Leaf(1), Leaf(2)
  144. >>> plus = Node('+', l0, l1)
  145. >>> print generate_line(plus)
  146. 1 + 2
  147. >>> plus2 = Node('+', l0, l1)
  148. >>> times = Node('*', plus, plus2)
  149. >>> print generate_line(times)
  150. (1 + 2)(1 + 2)
  151. >>> l2 = Leaf(3)
  152. >>> uminus = Node('-', l2)
  153. >>> times = Node('*', plus, uminus)
  154. >>> print generate_line(times)
  155. (1 + 2) * -3
  156. >>> exp = Leaf('x')
  157. >>> inf = Leaf('oo')
  158. >>> minus_inf = Node('-', inf)
  159. >>> integral = Node('int', exp, minus_inf, inf)
  160. >>> print generate_line(integral)
  161. int(x, -oo, oo)
  162. >>> minus = Node('-', Leaf(2), Node('-', Node('*', Leaf(15), Leaf('x'))))
  163. >>> print generate_line(minus)
  164. 2 - -15x
  165. >>> left = Node('/', Leaf(22), Leaf(77))
  166. >>> right = Node('/', Leaf(28), Leaf(77))
  167. >>> minus = Node('-', left, right)
  168. >>> print generate_line(minus)
  169. 22 / 77 - 28 / 77
  170. >>> plus = Node('+', left, Node('-', right))
  171. >>> print generate_line(plus)
  172. 22 / 77 - 28 / 77
  173. """
  174. if not root:
  175. return '<empty expression>'
  176. if root.is_leaf:
  177. return '-' * root.negated + root.title()
  178. content = {}
  179. def construct_unary(node):
  180. result = node.title()
  181. value = node[0]
  182. # -a
  183. # -3*4
  184. # --a
  185. if value.is_leaf \
  186. or not ' ' in content[value] or pred(value) > 0:
  187. return result + content[value]
  188. return '%s(%s)' % (result, content[value])
  189. def construct_nary(node):
  190. op = node.title()
  191. # N-ary operator
  192. node_pred = pred(node)
  193. sep = ' ' + op + ' '
  194. e = []
  195. for i, child in enumerate(node):
  196. exp = content[child]
  197. # Check if there is a precedence conflict
  198. # If so, add parentheses
  199. child_pred = pred(child)
  200. if child_pred < node_pred or \
  201. (i and child_pred == node_pred \
  202. and op != child.title()):
  203. exp = '(' + exp + ')'
  204. e.append(exp)
  205. if op == '*':
  206. # Check if an explicit multiplication sign is nessecary
  207. left, right = node
  208. # Get the previous multiplication element if the arity is
  209. # greater than 2
  210. if left.title() == '*':
  211. left = left[1]
  212. # a * b -> ab
  213. # a * 2 -> a * 2
  214. # a * (b) -> a(b)
  215. # (a) * b -> (a)b
  216. # (a) * (b) -> (a)(b)
  217. # 2 * a -> 2a
  218. left_paren = e[0][-1] == ')'
  219. right_paren = e[1][0] == '('
  220. if (is_id(left) or left_paren or is_int(left)) \
  221. and ((not right.negated and is_id(right)) or right_paren):
  222. sep = ''
  223. exp = sep.join(e)
  224. #if node.negated:
  225. # FIXME: Keep it this way?
  226. if node.negated and op != '*':
  227. exp = '(' + exp + ')'
  228. return exp
  229. def construct_function(node):
  230. buf = []
  231. for child in node:
  232. buf.append(content[child])
  233. return '%s(%s)' % (node.title(), ', '.join(buf))
  234. # Traverse the expression tree and construct the mathematical expression in
  235. # the leafs and nodes in depth first order.
  236. for node in traverse_depth_first(root):
  237. if node.is_leaf:
  238. content[node] = node.title()
  239. else:
  240. arity = len(node)
  241. if is_operator(node):
  242. if arity == 1:
  243. content[node] = construct_unary(node)
  244. else:
  245. content[node] = construct_nary(node)
  246. else:
  247. content[node] = construct_function(node)
  248. # Add negations
  249. content[node] = '-' * node.negated + content[node]
  250. # Merge binary plus and unary minus signs into binary minus.
  251. return content[root].replace('+ -', '- ')