abc.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. """
  2. This module exports all latin and greek letters as Symbols, so you can
  3. conveniently do
  4. >>> from sympy.abc import x, y
  5. instead of the slightly more clunky-looking
  6. >>> from sympy import symbols
  7. >>> x, y = symbols('x y')
  8. Caveats
  9. =======
  10. 1. As of the time of writing this, the names ``O``, ``S``, ``I``, ``N``,
  11. ``E``, and ``Q`` are colliding with names defined in SymPy. If you import them
  12. from both ``sympy.abc`` and ``sympy``, the second import will "win".
  13. This is an issue only for * imports, which should only be used for short-lived
  14. code such as interactive sessions and throwaway scripts that do not survive
  15. until the next SymPy upgrade, where ``sympy`` may contain a different set of
  16. names.
  17. 2. This module does not define symbol names on demand, i.e.
  18. ``from sympy.abc import foo`` will be reported as an error because
  19. ``sympy.abc`` does not contain the name ``foo``. To get a symbol named ``foo``,
  20. you still need to use ``Symbol('foo')`` or ``symbols('foo')``.
  21. You can freely mix usage of ``sympy.abc`` and ``Symbol``/``symbols``, though
  22. sticking with one and only one way to get the symbols does tend to make the code
  23. more readable.
  24. The module also defines some special names to help detect which names clash
  25. with the default SymPy namespace.
  26. ``_clash1`` defines all the single letter variables that clash with
  27. SymPy objects; ``_clash2`` defines the multi-letter clashing symbols;
  28. and ``_clash`` is the union of both. These can be passed for ``locals``
  29. during sympification if one desires Symbols rather than the non-Symbol
  30. objects for those names.
  31. Examples
  32. ========
  33. >>> from sympy import S
  34. >>> from sympy.abc import _clash1, _clash2, _clash
  35. >>> S("Q & C", locals=_clash1)
  36. C & Q
  37. >>> S('pi(x)', locals=_clash2)
  38. pi(x)
  39. >>> S('pi(C, Q)', locals=_clash)
  40. pi(C, Q)
  41. """
  42. from typing import Any, Dict as tDict
  43. import string
  44. from .core import Symbol, symbols
  45. from .core.alphabets import greeks
  46. from sympy.parsing.sympy_parser import null
  47. ##### Symbol definitions #####
  48. # Implementation note: The easiest way to avoid typos in the symbols()
  49. # parameter is to copy it from the left-hand side of the assignment.
  50. a, b, c, d, e, f, g, h, i, j = symbols('a, b, c, d, e, f, g, h, i, j')
  51. k, l, m, n, o, p, q, r, s, t = symbols('k, l, m, n, o, p, q, r, s, t')
  52. u, v, w, x, y, z = symbols('u, v, w, x, y, z')
  53. A, B, C, D, E, F, G, H, I, J = symbols('A, B, C, D, E, F, G, H, I, J')
  54. K, L, M, N, O, P, Q, R, S, T = symbols('K, L, M, N, O, P, Q, R, S, T')
  55. U, V, W, X, Y, Z = symbols('U, V, W, X, Y, Z')
  56. alpha, beta, gamma, delta = symbols('alpha, beta, gamma, delta')
  57. epsilon, zeta, eta, theta = symbols('epsilon, zeta, eta, theta')
  58. iota, kappa, lamda, mu = symbols('iota, kappa, lamda, mu')
  59. nu, xi, omicron, pi = symbols('nu, xi, omicron, pi')
  60. rho, sigma, tau, upsilon = symbols('rho, sigma, tau, upsilon')
  61. phi, chi, psi, omega = symbols('phi, chi, psi, omega')
  62. ##### Clashing-symbols diagnostics #####
  63. # We want to know which names in SymPy collide with those in here.
  64. # This is mostly for diagnosing SymPy's namespace during SymPy development.
  65. _latin = list(string.ascii_letters)
  66. # QOSINE should not be imported as they clash; gamma, pi and zeta clash, too
  67. _greek = list(greeks) # make a copy, so we can mutate it
  68. # Note: We import lamda since lambda is a reserved keyword in Python
  69. _greek.remove("lambda")
  70. _greek.append("lamda")
  71. ns: tDict[str, Any] = {}
  72. exec('from sympy import *', ns)
  73. _clash1: tDict[str, Any] = {}
  74. _clash2: tDict[str, Any] = {}
  75. while ns:
  76. _k, _ = ns.popitem()
  77. if _k in _greek:
  78. _clash2[_k] = null
  79. _greek.remove(_k)
  80. elif _k in _latin:
  81. _clash1[_k] = null
  82. _latin.remove(_k)
  83. _clash = {}
  84. _clash.update(_clash1)
  85. _clash.update(_clash2)
  86. del _latin, _greek, Symbol, _k, null