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