test_tableform.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. from sympy.core.singleton import S
  2. from sympy.printing.tableform import TableForm
  3. from sympy.printing.latex import latex
  4. from sympy.abc import x
  5. from sympy.functions.elementary.miscellaneous import sqrt
  6. from sympy.functions.elementary.trigonometric import sin
  7. from sympy.testing.pytest import raises
  8. from textwrap import dedent
  9. def test_TableForm():
  10. s = str(TableForm([["a", "b"], ["c", "d"], ["e", 0]],
  11. headings="automatic"))
  12. assert s == (
  13. ' | 1 2\n'
  14. '-------\n'
  15. '1 | a b\n'
  16. '2 | c d\n'
  17. '3 | e '
  18. )
  19. s = str(TableForm([["a", "b"], ["c", "d"], ["e", 0]],
  20. headings="automatic", wipe_zeros=False))
  21. assert s == dedent('''\
  22. | 1 2
  23. -------
  24. 1 | a b
  25. 2 | c d
  26. 3 | e 0''')
  27. s = str(TableForm([[x**2, "b"], ["c", x**2], ["e", "f"]],
  28. headings=("automatic", None)))
  29. assert s == (
  30. '1 | x**2 b \n'
  31. '2 | c x**2\n'
  32. '3 | e f '
  33. )
  34. s = str(TableForm([["a", "b"], ["c", "d"], ["e", "f"]],
  35. headings=(None, "automatic")))
  36. assert s == dedent('''\
  37. 1 2
  38. ---
  39. a b
  40. c d
  41. e f''')
  42. s = str(TableForm([[5, 7], [4, 2], [10, 3]],
  43. headings=[["Group A", "Group B", "Group C"], ["y1", "y2"]]))
  44. assert s == (
  45. ' | y1 y2\n'
  46. '---------------\n'
  47. 'Group A | 5 7 \n'
  48. 'Group B | 4 2 \n'
  49. 'Group C | 10 3 '
  50. )
  51. raises(
  52. ValueError,
  53. lambda:
  54. TableForm(
  55. [[5, 7], [4, 2], [10, 3]],
  56. headings=[["Group A", "Group B", "Group C"], ["y1", "y2"]],
  57. alignments="middle")
  58. )
  59. s = str(TableForm([[5, 7], [4, 2], [10, 3]],
  60. headings=[["Group A", "Group B", "Group C"], ["y1", "y2"]],
  61. alignments="right"))
  62. assert s == dedent('''\
  63. | y1 y2
  64. ---------------
  65. Group A | 5 7
  66. Group B | 4 2
  67. Group C | 10 3''')
  68. # other alignment permutations
  69. d = [[1, 100], [100, 1]]
  70. s = TableForm(d, headings=(('xxx', 'x'), None), alignments='l')
  71. assert str(s) == (
  72. 'xxx | 1 100\n'
  73. ' x | 100 1 '
  74. )
  75. s = TableForm(d, headings=(('xxx', 'x'), None), alignments='lr')
  76. assert str(s) == dedent('''\
  77. xxx | 1 100
  78. x | 100 1''')
  79. s = TableForm(d, headings=(('xxx', 'x'), None), alignments='clr')
  80. assert str(s) == dedent('''\
  81. xxx | 1 100
  82. x | 100 1''')
  83. s = TableForm(d, headings=(('xxx', 'x'), None))
  84. assert str(s) == (
  85. 'xxx | 1 100\n'
  86. ' x | 100 1 '
  87. )
  88. raises(ValueError, lambda: TableForm(d, alignments='clr'))
  89. #pad
  90. s = str(TableForm([[None, "-", 2], [1]], pad='?'))
  91. assert s == dedent('''\
  92. ? - 2
  93. 1 ? ?''')
  94. def test_TableForm_latex():
  95. s = latex(TableForm([[0, x**3], ["c", S.One/4], [sqrt(x), sin(x**2)]],
  96. wipe_zeros=True, headings=("automatic", "automatic")))
  97. assert s == (
  98. '\\begin{tabular}{r l l}\n'
  99. ' & 1 & 2 \\\\\n'
  100. '\\hline\n'
  101. '1 & & $x^{3}$ \\\\\n'
  102. '2 & $c$ & $\\frac{1}{4}$ \\\\\n'
  103. '3 & $\\sqrt{x}$ & $\\sin{\\left(x^{2} \\right)}$ \\\\\n'
  104. '\\end{tabular}'
  105. )
  106. s = latex(TableForm([[0, x**3], ["c", S.One/4], [sqrt(x), sin(x**2)]],
  107. wipe_zeros=True, headings=("automatic", "automatic"), alignments='l'))
  108. assert s == (
  109. '\\begin{tabular}{r l l}\n'
  110. ' & 1 & 2 \\\\\n'
  111. '\\hline\n'
  112. '1 & & $x^{3}$ \\\\\n'
  113. '2 & $c$ & $\\frac{1}{4}$ \\\\\n'
  114. '3 & $\\sqrt{x}$ & $\\sin{\\left(x^{2} \\right)}$ \\\\\n'
  115. '\\end{tabular}'
  116. )
  117. s = latex(TableForm([[0, x**3], ["c", S.One/4], [sqrt(x), sin(x**2)]],
  118. wipe_zeros=True, headings=("automatic", "automatic"), alignments='l'*3))
  119. assert s == (
  120. '\\begin{tabular}{l l l}\n'
  121. ' & 1 & 2 \\\\\n'
  122. '\\hline\n'
  123. '1 & & $x^{3}$ \\\\\n'
  124. '2 & $c$ & $\\frac{1}{4}$ \\\\\n'
  125. '3 & $\\sqrt{x}$ & $\\sin{\\left(x^{2} \\right)}$ \\\\\n'
  126. '\\end{tabular}'
  127. )
  128. s = latex(TableForm([["a", x**3], ["c", S.One/4], [sqrt(x), sin(x**2)]],
  129. headings=("automatic", "automatic")))
  130. assert s == (
  131. '\\begin{tabular}{r l l}\n'
  132. ' & 1 & 2 \\\\\n'
  133. '\\hline\n'
  134. '1 & $a$ & $x^{3}$ \\\\\n'
  135. '2 & $c$ & $\\frac{1}{4}$ \\\\\n'
  136. '3 & $\\sqrt{x}$ & $\\sin{\\left(x^{2} \\right)}$ \\\\\n'
  137. '\\end{tabular}'
  138. )
  139. s = latex(TableForm([["a", x**3], ["c", S.One/4], [sqrt(x), sin(x**2)]],
  140. formats=['(%s)', None], headings=("automatic", "automatic")))
  141. assert s == (
  142. '\\begin{tabular}{r l l}\n'
  143. ' & 1 & 2 \\\\\n'
  144. '\\hline\n'
  145. '1 & (a) & $x^{3}$ \\\\\n'
  146. '2 & (c) & $\\frac{1}{4}$ \\\\\n'
  147. '3 & (sqrt(x)) & $\\sin{\\left(x^{2} \\right)}$ \\\\\n'
  148. '\\end{tabular}'
  149. )
  150. def neg_in_paren(x, i, j):
  151. if i % 2:
  152. return ('(%s)' if x < 0 else '%s') % x
  153. else:
  154. pass # use default print
  155. s = latex(TableForm([[-1, 2], [-3, 4]],
  156. formats=[neg_in_paren]*2, headings=("automatic", "automatic")))
  157. assert s == (
  158. '\\begin{tabular}{r l l}\n'
  159. ' & 1 & 2 \\\\\n'
  160. '\\hline\n'
  161. '1 & -1 & 2 \\\\\n'
  162. '2 & (-3) & 4 \\\\\n'
  163. '\\end{tabular}'
  164. )
  165. s = latex(TableForm([["a", x**3], ["c", S.One/4], [sqrt(x), sin(x**2)]]))
  166. assert s == (
  167. '\\begin{tabular}{l l}\n'
  168. '$a$ & $x^{3}$ \\\\\n'
  169. '$c$ & $\\frac{1}{4}$ \\\\\n'
  170. '$\\sqrt{x}$ & $\\sin{\\left(x^{2} \\right)}$ \\\\\n'
  171. '\\end{tabular}'
  172. )