Ticket #6559: trac_6559-domain-and-latex_name-for-variable.take2.3.patch

File trac_6559-domain-and-latex_name-for-variable.take2.3.patch, 7.7 KB (added by burcin, 3 years ago)

rebased to 4.3.1.rc0

  • sage/calculus/var.pyx

    # HG changeset patch
    # User Golam Mortuza Hossain <gmhossain@gmail.com>
    # Date 1252185702 10800
    # Node ID db8fbca6b7b549d6bbb00b832cbf13d04f7bab76
    # Parent  bed868ece57991000d41cb5605f94c70974c997c
    Allow users to specify domain and latex_name for symbolic variables
    
    diff --git a/sage/calculus/var.pyx b/sage/calculus/var.pyx
    a b  
    1010    - ``s`` - a string, either a single variable name, or a space or 
    1111      comma separated list of variable names. 
    1212 
     13    - ``kwds`` - keyword arguments can be given to specify domain and 
     14      custom latex_name for variables. See EXAMPLES for usage. 
     15 
    1316    .. note:: 
    1417 
    1518       The new variable is both returned and automatically injected 
    16        into the global namespace.  If you use var in library code, it 
    17        is better to use sage.symbolic.ring.var, since it won't 
    18        touch the global namespace. 
     19       into the global namespace. If you need symbolic variable in 
     20       library code, it is better to use either SR.var() or SR.new_var(). 
    1921 
    2022    EXAMPLES: 
    2123     
     
    2931        sage: f = xx^n + yy^n + zz^n; f 
    3032        xx^n + yy^n + zz^n 
    3133 
     34    By default, var returns complex variable. To define real or positive  
     35    variable we can specify its domain as:: 
     36 
     37        sage: x = var('x', domain=RR); x; x.conjugate() 
     38        x 
     39        x 
     40        sage: y = var('y', domain='real'); y.conjugate() 
     41        y 
     42        sage: y = var('y', domain='positive'); y.abs() 
     43        y 
     44 
     45    Custom latex expression can be assigned to variable:: 
     46 
     47        sage: x = var('sui', latex_name="s_{u,i}"); x._latex_() 
     48        's_{u,i}' 
     49 
     50    In notebook, we can also colorize latex expression:: 
     51 
     52        sage: x = var('sui', latex_name="\\color{red}{s_{u,i}}"); x._latex_() 
     53        '\\color{red}{s_{u,i}}' 
     54 
    3255    We can substitute a new variable name for n:: 
    3356     
    3457        sage: f(n = var('sigma')) 
     
    81104            deprecation("The new (Pynac) symbolics are now the only symbolics; please do not use keyword 'ns' any longer.") 
    82105        else: 
    83106            raise NotImplementedError, "The new (Pynac) symbolics are now the only symbolics; please do not use keyword `ns` any longer." 
    84     v = new_var(s) 
     107    v = new_var(s, **kwds) 
    85108    if isinstance(v, tuple): 
    86109        for x in v: 
    87110            G[repr(x)] = x 
  • sage/libs/ginac/decl.pxi

    diff --git a/sage/libs/ginac/decl.pxi b/sage/libs/ginac/decl.pxi
    a b  
    2525 
    2626 
    2727    ctypedef struct GSymbol "symbol": 
    28         pass 
    29  
     28        unsigned get_domain() 
     29        void set_domain(unsigned d) 
     30        void set_texname(char* t) 
     31  
    3032    GSymbol* GSymbol_construct_str "Construct_p<symbol, char*>" \ 
    3133            (void *mem, char* m) 
    3234 
     
    208210    GEx g_pow "pow" (GEx left, GEx exp)      except + 
    209211 
    210212    GSymbol get_symbol(char* s)              except + 
     213    GSymbol ginac_symbol "GiNaC::symbol" (char* s, char* t, unsigned d) except + 
     214    GSymbol ginac_new_symbol "GiNaC::symbol" () except + 
    211215    GEx g_collect_common_factors "collect_common_factors" (GEx e) except + 
    212216 
    213217    # standard library string 
  • sage/symbolic/ring.pyx

    diff --git a/sage/symbolic/ring.pyx b/sage/symbolic/ring.pyx
    a b  
    3131from sage.structure.parent_base import ParentWithBase 
    3232from sage.rings.ring cimport CommutativeRing 
    3333from sage.categories.morphism cimport Morphism 
     34             
     35pynac_symbol_registry = {} 
    3436 
    3537cdef class SymbolicRing(CommutativeRing): 
    3638    """ 
     
    412414        from sage.symbolic.constants import pi 
    413415        return self(pi) 
    414416 
    415     cpdef symbol(self, name): 
     417    cpdef symbol(self, name=None, latex_name=None, domain=None): 
    416418        """ 
    417419        EXAMPLES:: 
    418420 
    419             sage: SR.symbol("asdfasdfasdf") 
    420             asdfasdfasdf 
     421            sage: tmp0_var = SR.symbol("tmp0_var") 
     422            sage: tmp0_var.conjugate() 
     423            conjugate(tmp0_var) 
     424 
     425            sage: tmp0_var = SR.symbol("tmp0_var", domain='real') 
     426            sage: tmp0_var.conjugate() 
     427            tmp0_var 
     428 
     429            sage: tmp0_var.abs() 
     430            abs(tmp0_var) 
     431 
     432            sage: tmp0_var = SR.symbol("tmp0_var", domain='positive') 
     433            sage: tmp0_var.abs() 
     434            tmp0_var 
     435 
     436            sage: tmp0_var = SR.symbol(); tmp0_var  #random 
     437            symbol343 
    421438        """ 
    422         cdef GSymbol symb = get_symbol(name) 
     439        # Decide the domain 
     440        from sage.rings.all import RR 
     441        if domain is 'real' or domain is RR: 
     442            domain = domain_real 
     443        elif domain is 'positive': 
     444            domain = domain_positive 
     445        else: 
     446            domain = domain_complex 
     447        cdef GSymbol symb 
    423448        cdef Expression e 
    424         e = <Expression>PY_NEW(Expression) 
    425         GEx_construct_symbol(&e._gobj, symb) 
    426         e._parent = SR 
     449        # Check if there is already a symbol of same name 
     450        try: 
     451            e = pynac_symbol_registry[name] 
     452            symb = ex_to_symbol((<Expression>e)._gobj) 
     453            if symb.get_domain() is domain and latex_name is None: 
     454                return e 
     455            else: 
     456                if not symb.get_domain() is domain: 
     457                    symb.set_domain(domain) 
     458                if latex_name is not None: 
     459                    symb.set_texname(latex_name) 
     460                GEx_construct_symbol(&e._gobj, symb) 
     461        except KeyError: 
     462            # Check if we need a distinct new symbol 
     463            if name is None: 
     464                symb = ginac_new_symbol() 
     465                if not domain is domain_complex: 
     466                    symb.set_domain(domain) 
     467            else: 
     468                # Get a ginac symbol with given name 
     469                name = str(name) 
     470                if latex_name is None: 
     471                    from sage.misc.latex import latex_variable_name 
     472                    latex_name = latex_variable_name(name) 
     473                symb = ginac_symbol(name, latex_name, domain) 
     474            # Construct expression 
     475            e = <Expression>PY_NEW(Expression) 
     476            GEx_construct_symbol(&e._gobj, symb) 
     477            e._parent = SR 
     478            pynac_symbol_registry[repr(e)] = e 
    427479        return e 
    428480 
    429     def var(self, name): 
     481    def var(self, name, **kwds): 
    430482        """ 
    431483        Return the symbolic variable defined by x as an element of the 
    432484        symbolic ring. 
     
    445497        if not isinstance(name, str): 
    446498            name = repr(name) 
    447499        if ',' in name: 
    448             return tuple([self.symbol(s.strip()) for s in name.split(',')]) 
     500            return tuple([self.symbol(s.strip(), **kwds) for s in name.split(',')]) 
    449501        elif ' ' in name: 
    450             return tuple([self.symbol(s.strip()) for s in name.split(' ')]) 
     502            return tuple([self.symbol(s.strip(), **kwds) for s in name.split(' ')]) 
    451503        else: 
    452             return self.symbol(name) 
     504            return self.symbol(name, **kwds) 
     505 
     506    def new_var(self, domain=None): 
     507        """ 
     508        Returns a unique symbolic variable as an element of the symbolic ring. 
     509        This is useful for situation where a temporary but distinct variable 
     510        (apart from those already in use) is needed. 
     511         
     512        EXAMPLES:: 
     513         
     514            sage: tmpvar = SR.new_var(); tmpvar  #random 
     515            symbol343 
     516            sage: tmpvar = SR.new_var(domain='real'); tmpvar  #random 
     517            symbol347 
     518        """ 
     519        return self.symbol(name=None, domain=domain) 
    453520 
    454521    def _repr_element_(self, Expression x): 
    455522        """ 
     
    667734    """ 
    668735    return R is SR 
    669736 
    670 def var(name): 
     737def var(name, **kwds): 
    671738    """ 
    672739    EXAMPLES:: 
    673740     
     
    681748        sage: var("z") 
    682749        z 
    683750    """ 
    684     return SR.var(name) 
     751    return SR.var(name, **kwds) 
    685752 
    686753def is_SymbolicVariable(x): 
    687754    """