Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
Move pynac_symbol_registry to cdef attribute SR.symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
jdemeyer committed Nov 2, 2015
1 parent e1aca77 commit 9ac89ae
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 25 deletions.
11 changes: 7 additions & 4 deletions src/sage/calculus/calculus.py
Expand Up @@ -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):
"""
Expand All @@ -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

Expand Down
4 changes: 3 additions & 1 deletion src/sage/symbolic/callable.py
Expand Up @@ -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

#########################################################################################
Expand Down Expand Up @@ -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):
"""
Expand Down
10 changes: 6 additions & 4 deletions src/sage/symbolic/expression.pyx
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions 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=*)
26 changes: 10 additions & 16 deletions src/sage/symbolic/ring.pyx
Expand Up @@ -13,10 +13,6 @@ The symbolic ring
# http://www.gnu.org/licenses/
#*****************************************************************************

#################################################################
# Initialize the library
#################################################################

from ginac cimport *

from sage.rings.integer cimport Integer
Expand All @@ -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):
"""
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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::
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -646,15 +640,15 @@ 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:
send_sage_domain_to_maxima(e, domain)

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.
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 9ac89ae

Please sign in to comment.