test_textplot.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. from sympy.core.singleton import S
  2. from sympy.core.symbol import Symbol
  3. from sympy.functions.elementary.exponential import log
  4. from sympy.functions.elementary.miscellaneous import sqrt
  5. from sympy.functions.elementary.trigonometric import sin
  6. from sympy.plotting.textplot import textplot_str
  7. from sympy.utilities.exceptions import ignore_warnings
  8. def test_axes_alignment():
  9. x = Symbol('x')
  10. lines = [
  11. ' 1 | ..',
  12. ' | ... ',
  13. ' | .. ',
  14. ' | ... ',
  15. ' | ... ',
  16. ' | .. ',
  17. ' | ... ',
  18. ' | ... ',
  19. ' | .. ',
  20. ' | ... ',
  21. ' 0 |--------------------------...--------------------------',
  22. ' | ... ',
  23. ' | .. ',
  24. ' | ... ',
  25. ' | ... ',
  26. ' | .. ',
  27. ' | ... ',
  28. ' | ... ',
  29. ' | .. ',
  30. ' | ... ',
  31. ' -1 |_______________________________________________________',
  32. ' -1 0 1'
  33. ]
  34. assert lines == list(textplot_str(x, -1, 1))
  35. lines = [
  36. ' 1 | ..',
  37. ' | .... ',
  38. ' | ... ',
  39. ' | ... ',
  40. ' | .... ',
  41. ' | ... ',
  42. ' | ... ',
  43. ' | .... ',
  44. ' 0 |--------------------------...--------------------------',
  45. ' | .... ',
  46. ' | ... ',
  47. ' | ... ',
  48. ' | .... ',
  49. ' | ... ',
  50. ' | ... ',
  51. ' | .... ',
  52. ' -1 |_______________________________________________________',
  53. ' -1 0 1'
  54. ]
  55. assert lines == list(textplot_str(x, -1, 1, H=17))
  56. def test_singularity():
  57. x = Symbol('x')
  58. lines = [
  59. ' 54 | . ',
  60. ' | ',
  61. ' | ',
  62. ' | ',
  63. ' | ',' | ',
  64. ' | ',
  65. ' | ',
  66. ' | ',
  67. ' | ',
  68. ' 27.5 |--.----------------------------------------------------',
  69. ' | ',
  70. ' | ',
  71. ' | ',
  72. ' | . ',
  73. ' | \\ ',
  74. ' | \\ ',
  75. ' | .. ',
  76. ' | ... ',
  77. ' | ............. ',
  78. ' 1 |_______________________________________________________',
  79. ' 0 0.5 1'
  80. ]
  81. assert lines == list(textplot_str(1/x, 0, 1))
  82. lines = [
  83. ' 0 | ......',
  84. ' | ........ ',
  85. ' | ........ ',
  86. ' | ...... ',
  87. ' | ..... ',
  88. ' | .... ',
  89. ' | ... ',
  90. ' | .. ',
  91. ' | ... ',
  92. ' | / ',
  93. ' -2 |-------..----------------------------------------------',
  94. ' | / ',
  95. ' | / ',
  96. ' | / ',
  97. ' | . ',
  98. ' | ',
  99. ' | . ',
  100. ' | ',
  101. ' | ',
  102. ' | ',
  103. ' -4 |_______________________________________________________',
  104. ' 0 0.5 1'
  105. ]
  106. # RuntimeWarning: divide by zero encountered in log
  107. with ignore_warnings(RuntimeWarning):
  108. assert lines == list(textplot_str(log(x), 0, 1))
  109. def test_sinc():
  110. x = Symbol('x')
  111. lines = [
  112. ' 1 | . . ',
  113. ' | . . ',
  114. ' | ',
  115. ' | . . ',
  116. ' | ',
  117. ' | . . ',
  118. ' | ',
  119. ' | ',
  120. ' | . . ',
  121. ' | ',
  122. ' 0.4 |-------------------------------------------------------',
  123. ' | . . ',
  124. ' | ',
  125. ' | . . ',
  126. ' | ',
  127. ' | ..... ..... ',
  128. ' | .. \\ . . / .. ',
  129. ' | / \\ / \\ ',
  130. ' |/ \\ . . / \\',
  131. ' | \\ / \\ / ',
  132. ' -0.2 |_______________________________________________________',
  133. ' -10 0 10'
  134. ]
  135. # RuntimeWarning: invalid value encountered in double_scalars
  136. with ignore_warnings(RuntimeWarning):
  137. assert lines == list(textplot_str(sin(x)/x, -10, 10))
  138. def test_imaginary():
  139. x = Symbol('x')
  140. lines = [
  141. ' 1 | ..',
  142. ' | .. ',
  143. ' | ... ',
  144. ' | .. ',
  145. ' | .. ',
  146. ' | .. ',
  147. ' | .. ',
  148. ' | .. ',
  149. ' | .. ',
  150. ' | / ',
  151. ' 0.5 |----------------------------------/--------------------',
  152. ' | .. ',
  153. ' | / ',
  154. ' | . ',
  155. ' | ',
  156. ' | . ',
  157. ' | . ',
  158. ' | ',
  159. ' | ',
  160. ' | ',
  161. ' 0 |_______________________________________________________',
  162. ' -1 0 1'
  163. ]
  164. # RuntimeWarning: invalid value encountered in sqrt
  165. with ignore_warnings(RuntimeWarning):
  166. assert list(textplot_str(sqrt(x), -1, 1)) == lines
  167. lines = [
  168. ' 1 | ',
  169. ' | ',
  170. ' | ',
  171. ' | ',
  172. ' | ',
  173. ' | ',
  174. ' | ',
  175. ' | ',
  176. ' | ',
  177. ' | ',
  178. ' 0 |-------------------------------------------------------',
  179. ' | ',
  180. ' | ',
  181. ' | ',
  182. ' | ',
  183. ' | ',
  184. ' | ',
  185. ' | ',
  186. ' | ',
  187. ' | ',
  188. ' -1 |_______________________________________________________',
  189. ' -1 0 1'
  190. ]
  191. assert list(textplot_str(S.ImaginaryUnit, -1, 1)) == lines