diff --git a/src/sage/calculus/calculus.py b/src/sage/calculus/calculus.py index c895e9cb135..72c96362200 100644 --- a/src/sage/calculus/calculus.py +++ b/src/sage/calculus/calculus.py @@ -1960,7 +1960,6 @@ def maxima_options(**kwds): # _find_var() and _find_func() functions below without extra arguments. _augmented_syms = {} -from sage.symbolic.ring import pynac_symbol_registry def _find_var(name): """ @@ -1976,13 +1975,17 @@ def _find_var(name): I """ try: - res = _augmented_syms.get(name) - if res is None: - return pynac_symbol_registry[name] + res = _augmented_syms[name] + except KeyError: + pass + else: # _augmented_syms might contain entries pointing to functions if # previous computations polluted the maxima workspace if not isinstance(res, Function): return res + + try: + return SR.symbols[name] except KeyError: pass diff --git a/src/sage/symbolic/callable.py b/src/sage/symbolic/callable.py index e64659eac01..e1900971143 100644 --- a/src/sage/symbolic/callable.py +++ b/src/sage/symbolic/callable.py @@ -61,7 +61,8 @@ SyntaxError: can't assign to function call """ -from sage.symbolic.ring import SymbolicRing, SR, ParentWithBase +from sage.structure.parent_base import ParentWithBase +from sage.symbolic.ring import SymbolicRing, SR from sage.categories.pushout import ConstructionFunctor ######################################################################################### @@ -283,6 +284,7 @@ def __init__(self, arguments): self._arguments = arguments ParentWithBase.__init__(self, SR) self._populate_coercion_lists_(coerce_list=[SR]) + self.symbols = SR.symbols # Use the same list of symbols as SR def __hash__(self): """ diff --git a/src/sage/symbolic/expression.pyx b/src/sage/symbolic/expression.pyx index 3809dc83610..a962be91946 100644 --- a/src/sage/symbolic/expression.pyx +++ b/src/sage/symbolic/expression.pyx @@ -11492,14 +11492,16 @@ cdef class Expression(CommutativeRingElement): ValueError: Expression cos(x)*sin(x) contains no y terms - TESTS:: + TESTS: - sage: var('x,y') # check that the pynac registry is not polluted + Check that the symbols registry is not polluted:: + + sage: var('x,y') (x, y) - sage: psr = copy(sage.symbolic.ring.pynac_symbol_registry) + sage: psr = copy(SR.symbols) sage: (x^6*y^5).implicit_derivative(y, x, 3) -792/125*y/x^3 + 12/25*(15*x^4*y^5 + 28*x^3*y^5)/(x^6*y^4) - 36/125*(20*x^5*y^4 + 43*x^4*y^4)/(x^7*y^3) - sage: psr == sage.symbolic.ring.pynac_symbol_registry + sage: psr == SR.symbols True """ from sage.symbolic.ring import SR diff --git a/src/sage/symbolic/ring.pxd b/src/sage/symbolic/ring.pxd new file mode 100644 index 00000000000..3f029346bba --- /dev/null +++ b/src/sage/symbolic/ring.pxd @@ -0,0 +1,7 @@ +from sage.symbolic.expression cimport Expression +from sage.rings.ring cimport CommutativeRing + +cdef class SymbolicRing(CommutativeRing): + cdef public dict symbols + + cpdef Expression symbol(self, name=*, latex_name=*, domain=*) diff --git a/src/sage/symbolic/ring.pyx b/src/sage/symbolic/ring.pyx index 239097c6be2..8341f2edae9 100644 --- a/src/sage/symbolic/ring.pyx +++ b/src/sage/symbolic/ring.pyx @@ -13,10 +13,6 @@ The symbolic ring # http://www.gnu.org/licenses/ #***************************************************************************** -################################################################# -# Initialize the library -################################################################# - from ginac cimport * from sage.rings.integer cimport Integer @@ -27,14 +23,11 @@ from sage.symbolic.expression cimport Expression, new_Expression_from_GEx, new_E from sage.libs.pari.pari_instance import PariInstance from sage.misc.latex import latex_variable_name from sage.structure.element cimport RingElement, Element, Matrix -from sage.structure.parent_base import ParentWithBase -from sage.rings.ring cimport CommutativeRing from sage.categories.morphism cimport Morphism from sage.structure.coerce cimport is_numpy_type from sage.rings.all import RR, CC, ZZ -pynac_symbol_registry = {} cdef class SymbolicRing(CommutativeRing): """ @@ -51,6 +44,7 @@ cdef class SymbolicRing(CommutativeRing): """ CommutativeRing.__init__(self, self) self._populate_coercion_lists_(convert_method_name='_symbolic_') + self.symbols = {} def __reduce__(self): """ @@ -501,7 +495,7 @@ cdef class SymbolicRing(CommutativeRing): sage: SR._an_element_() some_variable """ - return self.var('some_variable') + return self.symbol('some_variable') def is_field(self, proof = True): """ @@ -555,7 +549,7 @@ cdef class SymbolicRing(CommutativeRing): from sage.symbolic.constants import pi return self(pi) - cpdef symbol(self, name=None, latex_name=None, domain=None): + cpdef Expression symbol(self, name=None, latex_name=None, domain=None): """ EXAMPLES:: @@ -609,13 +603,13 @@ cdef class SymbolicRing(CommutativeRing): cdef Expression e # check if there is already a symbol with same name - e = pynac_symbol_registry.get(name) + e = self.symbols.get(name) # fast path to get an already existing variable if e is not None: if domain is None: if latex_name is None: - return self(e) + return e # get symbol symb = ex_to_symbol(e._gobj) @@ -627,7 +621,7 @@ cdef class SymbolicRing(CommutativeRing): if domain is not None: send_sage_domain_to_maxima(e, domain) - return self(e) + return e else: # initialize a new symbol # Construct expression @@ -646,7 +640,7 @@ cdef class SymbolicRing(CommutativeRing): else: ginac_domain = domain_complex symb = ginac_symbol(name, latex_name, ginac_domain) - pynac_symbol_registry[name] = e + self.symbols[name] = e GEx_construct_symbol(&e._gobj, symb) if domain is not None: @@ -654,7 +648,7 @@ cdef class SymbolicRing(CommutativeRing): return e - cpdef var(self, name, latex_name=None, domain=None): + def var(self, name, latex_name=None, domain=None): """ Return the symbolic variable defined by x as an element of the symbolic ring. @@ -1043,8 +1037,8 @@ def var(name, **kwds): TESTS: - These examples test that variables can only be made from - valid identifiers. See Trac 7496 (and 9724) for details:: + These examples test that variables can only be made from valid + identifiers. See :trac:`7496` (and :trac:`9724`) for details:: sage: var(' ') Traceback (most recent call last):