test_conventions.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # -*- coding: utf-8 -*-
  2. from sympy.core.function import (Derivative, Function)
  3. from sympy.core.numbers import oo
  4. from sympy.core.symbol import symbols
  5. from sympy.functions.elementary.exponential import exp
  6. from sympy.functions.elementary.trigonometric import cos
  7. from sympy.integrals.integrals import Integral
  8. from sympy.functions.special.bessel import besselj
  9. from sympy.functions.special.polynomials import legendre
  10. from sympy.functions.combinatorial.numbers import bell
  11. from sympy.printing.conventions import split_super_sub, requires_partial
  12. from sympy.testing.pytest import XFAIL
  13. def test_super_sub():
  14. assert split_super_sub("beta_13_2") == ("beta", [], ["13", "2"])
  15. assert split_super_sub("beta_132_20") == ("beta", [], ["132", "20"])
  16. assert split_super_sub("beta_13") == ("beta", [], ["13"])
  17. assert split_super_sub("x_a_b") == ("x", [], ["a", "b"])
  18. assert split_super_sub("x_1_2_3") == ("x", [], ["1", "2", "3"])
  19. assert split_super_sub("x_a_b1") == ("x", [], ["a", "b1"])
  20. assert split_super_sub("x_a_1") == ("x", [], ["a", "1"])
  21. assert split_super_sub("x_1_a") == ("x", [], ["1", "a"])
  22. assert split_super_sub("x_1^aa") == ("x", ["aa"], ["1"])
  23. assert split_super_sub("x_1__aa") == ("x", ["aa"], ["1"])
  24. assert split_super_sub("x_11^a") == ("x", ["a"], ["11"])
  25. assert split_super_sub("x_11__a") == ("x", ["a"], ["11"])
  26. assert split_super_sub("x_a_b_c_d") == ("x", [], ["a", "b", "c", "d"])
  27. assert split_super_sub("x_a_b^c^d") == ("x", ["c", "d"], ["a", "b"])
  28. assert split_super_sub("x_a_b__c__d") == ("x", ["c", "d"], ["a", "b"])
  29. assert split_super_sub("x_a^b_c^d") == ("x", ["b", "d"], ["a", "c"])
  30. assert split_super_sub("x_a__b_c__d") == ("x", ["b", "d"], ["a", "c"])
  31. assert split_super_sub("x^a^b_c_d") == ("x", ["a", "b"], ["c", "d"])
  32. assert split_super_sub("x__a__b_c_d") == ("x", ["a", "b"], ["c", "d"])
  33. assert split_super_sub("x^a^b^c^d") == ("x", ["a", "b", "c", "d"], [])
  34. assert split_super_sub("x__a__b__c__d") == ("x", ["a", "b", "c", "d"], [])
  35. assert split_super_sub("alpha_11") == ("alpha", [], ["11"])
  36. assert split_super_sub("alpha_11_11") == ("alpha", [], ["11", "11"])
  37. assert split_super_sub("w1") == ("w", [], ["1"])
  38. assert split_super_sub("w𝟙") == ("w", [], ["𝟙"])
  39. assert split_super_sub("w11") == ("w", [], ["11"])
  40. assert split_super_sub("w𝟙𝟙") == ("w", [], ["𝟙𝟙"])
  41. assert split_super_sub("w𝟙2𝟙") == ("w", [], ["𝟙2𝟙"])
  42. assert split_super_sub("w1^a") == ("w", ["a"], ["1"])
  43. assert split_super_sub("ω1") == ("ω", [], ["1"])
  44. assert split_super_sub("ω11") == ("ω", [], ["11"])
  45. assert split_super_sub("ω1^a") == ("ω", ["a"], ["1"])
  46. assert split_super_sub("ω𝟙^α") == ("ω", ["α"], ["𝟙"])
  47. assert split_super_sub("ω𝟙2^3α") == ("ω", ["3α"], ["𝟙2"])
  48. assert split_super_sub("") == ("", [], [])
  49. def test_requires_partial():
  50. x, y, z, t, nu = symbols('x y z t nu')
  51. n = symbols('n', integer=True)
  52. f = x * y
  53. assert requires_partial(Derivative(f, x)) is True
  54. assert requires_partial(Derivative(f, y)) is True
  55. ## integrating out one of the variables
  56. assert requires_partial(Derivative(Integral(exp(-x * y), (x, 0, oo)), y, evaluate=False)) is False
  57. ## bessel function with smooth parameter
  58. f = besselj(nu, x)
  59. assert requires_partial(Derivative(f, x)) is True
  60. assert requires_partial(Derivative(f, nu)) is True
  61. ## bessel function with integer parameter
  62. f = besselj(n, x)
  63. assert requires_partial(Derivative(f, x)) is False
  64. # this is not really valid (differentiating with respect to an integer)
  65. # but there's no reason to use the partial derivative symbol there. make
  66. # sure we don't throw an exception here, though
  67. assert requires_partial(Derivative(f, n)) is False
  68. ## bell polynomial
  69. f = bell(n, x)
  70. assert requires_partial(Derivative(f, x)) is False
  71. # again, invalid
  72. assert requires_partial(Derivative(f, n)) is False
  73. ## legendre polynomial
  74. f = legendre(0, x)
  75. assert requires_partial(Derivative(f, x)) is False
  76. f = legendre(n, x)
  77. assert requires_partial(Derivative(f, x)) is False
  78. # again, invalid
  79. assert requires_partial(Derivative(f, n)) is False
  80. f = x ** n
  81. assert requires_partial(Derivative(f, x)) is False
  82. assert requires_partial(Derivative(Integral((x*y) ** n * exp(-x * y), (x, 0, oo)), y, evaluate=False)) is False
  83. # parametric equation
  84. f = (exp(t), cos(t))
  85. g = sum(f)
  86. assert requires_partial(Derivative(g, t)) is False
  87. f = symbols('f', cls=Function)
  88. assert requires_partial(Derivative(f(x), x)) is False
  89. assert requires_partial(Derivative(f(x), y)) is False
  90. assert requires_partial(Derivative(f(x, y), x)) is True
  91. assert requires_partial(Derivative(f(x, y), y)) is True
  92. assert requires_partial(Derivative(f(x, y), z)) is True
  93. assert requires_partial(Derivative(f(x, y), x, y)) is True
  94. @XFAIL
  95. def test_requires_partial_unspecified_variables():
  96. x, y = symbols('x y')
  97. # function of unspecified variables
  98. f = symbols('f', cls=Function)
  99. assert requires_partial(Derivative(f, x)) is False
  100. assert requires_partial(Derivative(f, x, y)) is True