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 __future__ import annotations
  43. from typing import Any
  44. import string
  45. from .core import Symbol, symbols
  46. from .core.alphabets import greeks
  47. from sympy.parsing.sympy_parser import null
  48. ##### Symbol definitions #####
  49. # Implementation note: The easiest way to avoid typos in the symbols()
  50. # parameter is to copy it from the left-hand side of the assignment.
  51. a, b, c, d, e, f, g, h, i, j = symbols('a, b, c, d, e, f, g, h, i, j')
  52. k, l, m, n, o, p, q, r, s, t = symbols('k, l, m, n, o, p, q, r, s, t')
  53. u, v, w, x, y, z = symbols('u, v, w, x, y, z')
  54. A, B, C, D, E, F, G, H, I, J = symbols('A, B, C, D, E, F, G, H, I, J')
  55. K, L, M, N, O, P, Q, R, S, T = symbols('K, L, M, N, O, P, Q, R, S, T')
  56. U, V, W, X, Y, Z = symbols('U, V, W, X, Y, Z')
  57. alpha, beta, gamma, delta = symbols('alpha, beta, gamma, delta')
  58. epsilon, zeta, eta, theta = symbols('epsilon, zeta, eta, theta')
  59. iota, kappa, lamda, mu = symbols('iota, kappa, lamda, mu')
  60. nu, xi, omicron, pi = symbols('nu, xi, omicron, pi')
  61. rho, sigma, tau, upsilon = symbols('rho, sigma, tau, upsilon')
  62. phi, chi, psi, omega = symbols('phi, chi, psi, omega')
  63. ##### Clashing-symbols diagnostics #####
  64. # We want to know which names in SymPy collide with those in here.
  65. # This is mostly for diagnosing SymPy's namespace during SymPy development.
  66. _latin = list(string.ascii_letters)
  67. # QOSINE should not be imported as they clash; gamma, pi and zeta clash, too
  68. _greek = list(greeks) # make a copy, so we can mutate it
  69. # Note: We import lamda since lambda is a reserved keyword in Python
  70. _greek.remove("lambda")
  71. _greek.append("lamda")
  72. ns: dict[str, Any] = {}
  73. exec('from sympy import *', ns)
  74. _clash1: dict[str, Any] = {}
  75. _clash2: dict[str, Any] = {}
  76. while ns:
  77. _k, _ = ns.popitem()
  78. if _k in _greek:
  79. _clash2[_k] = null
  80. _greek.remove(_k)
  81. elif _k in _latin:
  82. _clash1[_k] = null
  83. _latin.remove(_k)
  84. _clash = {}
  85. _clash.update(_clash1)
  86. _clash.update(_clash2)
  87. del _latin, _greek, Symbol, _k, null