コード例 #1
0
    def __init__(self,
                 name,
                 conversions=None,
                 latex=None,
                 mathml="",
                 domain='complex'):
        """
        EXAMPLES::

            sage: from sage.symbolic.constants import Constant
            sage: p = Constant('p')
            sage: loads(dumps(p))
            p
        """
        self._conversions = conversions if conversions is not None else {}
        self._latex = latex if latex is not None else name
        self._mathml = mathml
        self._name = name
        self._domain = domain

        for system, value in self._conversions.items():
            setattr(self, "_%s_" % system,
                    partial(self._generic_interface, value))
            setattr(self, "_%s_init_" % system,
                    partial(self._generic_interface_init, value))

        from sage.symbolic.constants_c import PynacConstant
        self._pynac = PynacConstant(self._name, self._latex, self._domain)
        self._serial = self._pynac.serial()
        constants_table[self._serial] = self
        constants_name_table[self._name] = self

        from sage.symbolic.pynac import register_symbol
        register_symbol(self.expression(), self._conversions)
コード例 #2
0
ファイル: constants.py プロジェクト: dagss/sage
    def __init__(self, name, conversions=None, latex=None, mathml="",
                 domain='complex'):
        """
        EXAMPLES::

            sage: from sage.symbolic.constants import Constant
            sage: p = Constant('p')
            sage: loads(dumps(p))
            p
        """
        self._conversions = conversions if conversions is not None else {}
        self._latex = latex if latex is not None else name
        self._mathml = mathml
        self._name = name
        self._domain = domain

        for system, value in self._conversions.items():
            setattr(self, "_%s_"%system, partial(self._generic_interface, value))
            setattr(self, "_%s_init_"%system, partial(self._generic_interface_init, value))

        from sage.symbolic.constants_c import PynacConstant
        self._pynac = PynacConstant(self._name, self._latex, self._domain)
        self._serial = self._pynac.serial()
        constants_table[self._serial] = self
        constants_name_table[self._name] = self
        
        from sage.symbolic.pynac import register_symbol
        register_symbol(self.expression(), self._conversions)
コード例 #3
0
class Constant(object):
    def __init__(self,
                 name,
                 conversions=None,
                 latex=None,
                 mathml="",
                 domain='complex'):
        """
        EXAMPLES::

            sage: from sage.symbolic.constants import Constant
            sage: p = Constant('p')
            sage: loads(dumps(p))
            p
        """
        self._conversions = conversions if conversions is not None else {}
        self._latex = latex if latex is not None else name
        self._mathml = mathml
        self._name = name
        self._domain = domain

        for system, value in self._conversions.items():
            setattr(self, "_%s_" % system,
                    partial(self._generic_interface, value))
            setattr(self, "_%s_init_" % system,
                    partial(self._generic_interface_init, value))

        from sage.symbolic.constants_c import PynacConstant
        self._pynac = PynacConstant(self._name, self._latex, self._domain)
        self._serial = self._pynac.serial()
        constants_table[self._serial] = self
        constants_name_table[self._name] = self

        from sage.symbolic.pynac import register_symbol
        register_symbol(self.expression(), self._conversions)

    def __eq__(self, other):
        """
        EXAMPLES::

            sage: from sage.symbolic.constants import Constant
            sage: p = Constant('p')
            sage: s = Constant('s')
            sage: p == p
            True
            sage: p == s
            False
            sage: p != s
            True
        """
        return (self.__class__ == other.__class__
                and self._name == other._name)

    def __reduce__(self):
        """
        Adds support for pickling constants.

        EXAMPLES::

            sage: from sage.symbolic.constants import Constant
            sage: p = Constant('p')
            sage: p.__reduce__()
            (<function unpickle_Constant at 0x...>,
             ('Constant', 'p', {}, 'p', '', 'complex'))
            sage: loads(dumps(p))
            p

            sage: pi.pyobject().__reduce__()
            (<function unpickle_Constant at 0x...>,
             ('Pi',
              'pi',
              ...,
              '\\pi',
              '<mi>&pi;</mi>',
              'positive'))
            sage: loads(dumps(pi.pyobject()))
            pi

        """
        return (unpickle_Constant,
                (self.__class__.__name__, self._name, self._conversions,
                 self._latex, self._mathml, self._domain))

    def domain(self):
        """
        Returns the domain of this constant.  This is either positive,
        real, or complex, and is used by Pynac to make inferences
        about expressions containing this constant.

        EXAMPLES::

            sage: p = pi.pyobject(); p
            pi
            sage: type(_)
            <class 'sage.symbolic.constants.Pi'>
            sage: p.domain()
            'positive'
        """
        return self._domain

    def expression(self):
        """
        Returns an expression for this constant.

        EXAMPLES::

            sage: a = pi.pyobject()
            sage: pi2 = a.expression()
            sage: pi2
            pi
            sage: pi2 + 2
            pi + 2
            sage: pi - pi2
            0
        """
        return self._pynac.expression()

    def name(self):
        """
        Returns the name of this constant.

        EXAMPLES::

            sage: from sage.symbolic.constants import Constant
            sage: c = Constant('c')
            sage: c.name()
            'c'
        """
        return self._name

    def __repr__(self):
        """
        EXAMPLES::

            sage: from sage.symbolic.constants import Constant
            sage: c = Constant('c')
            sage: c
            c
        """
        return self._name

    def _latex_(self):
        r"""
        EXAMPLES::

            sage: from sage.symbolic.constants import Constant
            sage: c = Constant('c', latex=r'\xi')
            sage: latex(c)
            \xi
        """
        return self._latex

    def _mathml_(self):
        """
        EXAMPLES::

            sage: from sage.symbolic.constants import Constant
            sage: c = Constant('c', mathml=r'<mi>c</mi>')
            sage: mathml(c)
            <mi>c</mi>
        """
        return self._mathml

    def _generic_interface(self, value, I):
        """
        This is a helper method used in defining the ``_X_`` methods
        where ``X`` is the name of some interface.

        EXAMPLES::

            sage: from sage.symbolic.constants import Constant
            sage: p = Constant('p', conversions=dict(maxima='%pi'))
            sage: p._maxima_(maxima)
            %pi

        The above ``_maxima_`` is constructed like ``m`` below::

            sage: from functools import partial
            sage: m = partial(p._generic_interface, '%pi')
            sage: m(maxima)
            %pi

        """
        return I(value)

    def _generic_interface_init(self, value):
        """
        This is a helper method used in defining the ``_X_init_`` methods
        where ``X`` is the name of some interface.

        EXAMPLES::

            sage: from sage.symbolic.constants import Constant
            sage: p = Constant('p', conversions=dict(maxima='%pi'))
            sage: p._maxima_init_()
            '%pi'

        The above ``_maxima_init_`` is constructed like ``mi`` below::

            sage: from functools import partial
            sage: mi = partial(p._generic_interface_init, '%pi')
            sage: mi()
            '%pi'

        """
        return value

    def _interface_(self, I):
        """
        EXAMPLES::

            sage: from sage.symbolic.constants import Constant
            sage: p = Constant('p', conversions=dict(maxima='%pi'))
            sage: p._interface_(maxima)
            %pi
        """
        try:
            s = self._conversions[I.name()]
            return I(s)
        except KeyError:
            pass

        try:
            return getattr(self, "_%s_" % (I.name()))(I)
        except AttributeError:
            pass

        raise NotImplementedError

    def _gap_(self, gap):
        """
        Returns the constant as a string in GAP. Since GAP does not
        have floating point numbers, we simply return the constant as
        a string.

        EXAMPLES::

            sage: from sage.symbolic.constants import Constant
            sage: p = Constant('p')
            sage: gap(p)
            p
        """
        return gap('"%s"' % self)

    def _singular_(self, singular):
        """
        Returns the constant as a string in Singular. Since Singular
        does not always support floating point numbers, we simply
        return the constant as a string.  (Singular allows floating point
        numbers if the current ring has floating point coefficients,
        but not otherwise.)

        EXAMPLES::

            sage: from sage.symbolic.constants import Constant
            sage: p = Constant('p')
            sage: singular(p)
            p
        """
        return singular('"%s"' % self)
コード例 #4
0
ファイル: constants.py プロジェクト: dagss/sage
class Constant(object):
    def __init__(self, name, conversions=None, latex=None, mathml="",
                 domain='complex'):
        """
        EXAMPLES::

            sage: from sage.symbolic.constants import Constant
            sage: p = Constant('p')
            sage: loads(dumps(p))
            p
        """
        self._conversions = conversions if conversions is not None else {}
        self._latex = latex if latex is not None else name
        self._mathml = mathml
        self._name = name
        self._domain = domain

        for system, value in self._conversions.items():
            setattr(self, "_%s_"%system, partial(self._generic_interface, value))
            setattr(self, "_%s_init_"%system, partial(self._generic_interface_init, value))

        from sage.symbolic.constants_c import PynacConstant
        self._pynac = PynacConstant(self._name, self._latex, self._domain)
        self._serial = self._pynac.serial()
        constants_table[self._serial] = self
        constants_name_table[self._name] = self
        
        from sage.symbolic.pynac import register_symbol
        register_symbol(self.expression(), self._conversions)

    def __eq__(self, other):
        """
        EXAMPLES::

            sage: from sage.symbolic.constants import Constant
            sage: p = Constant('p')
            sage: s = Constant('s')
            sage: p == p
            True
            sage: p == s
            False
            sage: p != s
            True
        """
        return (self.__class__ == other.__class__ and
                self._name == other._name)

    def __reduce__(self):
        """
        Adds support for pickling constants.

        EXAMPLES::

            sage: from sage.symbolic.constants import Constant
            sage: p = Constant('p')
            sage: p.__reduce__()
            (<function unpickle_Constant at 0x...>,
             ('Constant', 'p', {}, 'p', '', 'complex'))
            sage: loads(dumps(p))
            p

            sage: pi.pyobject().__reduce__()
            (<function unpickle_Constant at 0x...>,
             ('Pi',
              'pi',
              ...,
              '\\pi',
              '<mi>&pi;</mi>',
              'positive'))
            sage: loads(dumps(pi.pyobject()))
            pi
            
        """
        return (unpickle_Constant, (self.__class__.__name__, self._name,
                                    self._conversions, self._latex,
                                    self._mathml, self._domain))
                                   

    def domain(self):
        """
        Returns the domain of this constant.  This is either positive,
        real, or complex, and is used by Pynac to make inferences
        about expressions containing this constant.

        EXAMPLES::

            sage: p = pi.pyobject(); p
            pi
            sage: type(_)
            <class 'sage.symbolic.constants.Pi'>
            sage: p.domain()
            'positive'
        """
        return self._domain

    def expression(self):
        """
        Returns an expression for this constant.
        
        EXAMPLES::

            sage: a = pi.pyobject()
            sage: pi2 = a.expression()
            sage: pi2
            pi
            sage: pi2 + 2
            pi + 2
            sage: pi - pi2
            0
        """
        return self._pynac.expression()

    def name(self):
        """
        Returns the name of this constant.

        EXAMPLES::
           
            sage: from sage.symbolic.constants import Constant
            sage: c = Constant('c')
            sage: c.name()
            'c'
        """
        return self._name

    def __repr__(self):
        """
        EXAMPLES::
           
            sage: from sage.symbolic.constants import Constant
            sage: c = Constant('c')
            sage: c
            c
        """
        return self._name

    def _latex_(self):
        r"""
        EXAMPLES::
           
            sage: from sage.symbolic.constants import Constant
            sage: c = Constant('c', latex=r'\xi')
            sage: latex(c)
            \xi
        """
        return self._latex

    def _mathml_(self):
        """
        EXAMPLES::
           
            sage: from sage.symbolic.constants import Constant
            sage: c = Constant('c', mathml=r'<mi>c</mi>')
            sage: mathml(c)
            <mi>c</mi>
        """
        return self._mathml

    def _generic_interface(self, value, I):
        """
        This is a helper method used in defining the ``_X_`` methods
        where ``X`` is the name of some interface.

        EXAMPLES::

            sage: from sage.symbolic.constants import Constant
            sage: p = Constant('p', conversions=dict(maxima='%pi'))
            sage: p._maxima_(maxima)
            %pi
            
        The above ``_maxima_`` is constructed like ``m`` below::

            sage: from functools import partial
            sage: m = partial(p._generic_interface, '%pi')
            sage: m(maxima)
            %pi

        """
        return I(value)

    def _generic_interface_init(self, value):
        """
        This is a helper method used in defining the ``_X_init_`` methods
        where ``X`` is the name of some interface.

        EXAMPLES::

            sage: from sage.symbolic.constants import Constant
            sage: p = Constant('p', conversions=dict(maxima='%pi'))
            sage: p._maxima_init_()
            '%pi'
            
        The above ``_maxima_init_`` is constructed like ``mi`` below::

            sage: from functools import partial
            sage: mi = partial(p._generic_interface_init, '%pi')
            sage: mi()
            '%pi'

        """
        return value

    def _interface_(self, I):
        """
        EXAMPLES::

            sage: from sage.symbolic.constants import Constant
            sage: p = Constant('p', conversions=dict(maxima='%pi'))
            sage: p._interface_(maxima)
            %pi
        """
        try:
            s = self._conversions[I.name()]
            return I(s)
        except KeyError:
            pass
        
        try:
            return getattr(self, "_%s_"%(I.name()))(I)
        except AttributeError:
            pass

        raise NotImplementedError

    def _gap_(self, gap):
        """
        Returns the constant as a string in GAP. Since GAP does not
        have floating point numbers, we simply return the constant as
        a string.

        EXAMPLES::

            sage: from sage.symbolic.constants import Constant
            sage: p = Constant('p')
            sage: gap(p)
            p
        """        
        return gap('"%s"'%self)

    def _singular_(self, singular):
        """
        Returns the constant as a string in Singular. Since Singular
        does not always support floating point numbers, we simply
        return the constant as a string.  (Singular allows floating point
        numbers if the current ring has floating point coefficients,
        but not otherwise.)

        EXAMPLES::

            sage: from sage.symbolic.constants import Constant
            sage: p = Constant('p')
            sage: singular(p)
            p
        """
        return singular('"%s"'%self)