コード例 #1
0
ファイル: generalized.py プロジェクト: swewers/mein_sage
    def _evalf_(self, x, **kwds):
        """
        TESTS:

        Check that :trac:`16587` is fixed::

            sage: M = sgn(3/2, hold=True); M
            sgn(3/2)
            sage: M.n()
            1
            sage: h(x) = sgn(x)
            sage: h(pi).numerical_approx()
            1.00000000000000
        """
        if hasattr(x, 'sign'):  # First check if x has a sign method
            return x.sign()
        if hasattr(x, 'sgn'):  # or a sgn method
            return x.sgn()
        approx_x = ComplexIntervalField()(x)
        if bool(approx_x.imag() == 0):  # x is real
            if bool(approx_x.real() == 0):  # x is zero
                return ZZ(0)
            # Now we have a non-zero real
            if bool((approx_x**(0.5)).imag() == 0):  # Check: x > 0
                return ZZ(1)
            else:
                return ZZ(-1)
        raise ValueError("Numeric evaluation of symbolic expression")
コード例 #2
0
ファイル: generalized.py プロジェクト: sagemath/sage
    def _evalf_(self, x, **kwds):
        """
        TESTS:

        Check that :trac:`16587` is fixed::

            sage: M = sgn(3/2, hold=True); M
            sgn(3/2)
            sage: M.n()
            1
            sage: h(x) = sgn(x)
            sage: h(pi).numerical_approx()
            1.00000000000000
        """
        if hasattr(x,'sign'): # First check if x has a sign method
            return x.sign()
        if hasattr(x,'sgn'): # or a sgn method
            return x.sgn()
        approx_x = ComplexIntervalField()(x)
        if bool(approx_x.imag() == 0):      # x is real
            if bool(approx_x.real() == 0):  # x is zero
                return ZZ(0)
            # Now we have a non-zero real
            if bool((approx_x**(0.5)).imag() == 0): # Check: x > 0
                return ZZ(1)
            else:
                return ZZ(-1)
        raise ValueError("Numeric evaluation of symbolic expression")
コード例 #3
0
    def _eval_(self, x):
        """
        INPUT:
        
        -  ``x`` - a real number or a symbolic expression
        
        EXAMPLES::

            sage: dirac_delta(1)
            0
            sage: dirac_delta(0)
            dirac_delta(0)
            sage: dirac_delta(x)
            dirac_delta(x)
            sage: dirac_delta(exp(-10000000000000000000))
            0

        Evaluation test::
            
            sage: dirac_delta(x).subs(x=1)
            0
        """
        try:
            approx_x = ComplexIntervalField()(x)
            if bool(approx_x.imag() == 0):  # x is real
                if bool(approx_x.real() == 0):  # x is zero
                    return None
                else:
                    return 0
        except StandardError:  # x is symbolic
            pass
        return None
コード例 #4
0
ファイル: generalized.py プロジェクト: Etn40ff/sage
    def _eval_(self, x):
        """
        INPUT:

        -  ``x`` - a real number or a symbolic expression

        EXAMPLES::

            sage: dirac_delta(1)
            0
            sage: dirac_delta(0)
            dirac_delta(0)
            sage: dirac_delta(x)
            dirac_delta(x)
            sage: dirac_delta(exp(-10000000000000000000))
            0

        Evaluation test::

            sage: dirac_delta(x).subs(x=1)
            0
        """
        try:
            approx_x = ComplexIntervalField()(x)
            if bool(approx_x.imag() == 0):      # x is real
                if bool(approx_x.real() == 0):  # x is zero
                    return None
                else:
                    return 0
        except Exception:                     # x is symbolic
            pass
        return None
コード例 #5
0
def _is_numerically_zero_CIF(x):
    from sage.rings.all import ComplexIntervalField
    try:
        approx_x = ComplexIntervalField()(x)
        if bool(approx_x.imag() == 0) and bool(approx_x.real() == 0):
            return True
    except:
        return False
コード例 #6
0
    def _eval_(self, x):
        """
        
        EXAMPLES::

            sage: sgn(-1)
            -1
            sage: sgn(1)
            1
            sage: sgn(0)
            0
            sage: sgn(x)
            sgn(x)
            sage: sgn(-exp(-10000000000000000000))
            -1

        Evaluation test::
            
            sage: sgn(x).subs(x=1)
            1
            sage: sgn(x).subs(x=0)
            0
            sage: sgn(x).subs(x=-1)
            -1

        More tests::

            sage: sign(RR(2))
            1
            sage: sign(RDF(2))
            1
            sage: sign(AA(-2))
            -1
            sage: sign(AA(0))
            0
        """
        if hasattr(x, 'sign'):  # First check if x has a sign method
            return x.sign()
        if hasattr(x, 'sgn'):  # or a sgn method
            return x.sgn()
        try:
            approx_x = ComplexIntervalField()(x)
            if bool(approx_x.imag() == 0):  # x is real
                if bool(approx_x.real() == 0):  # x is zero
                    return ZZ(0)
                # Now we have a non-zero real
                if bool((approx_x**(0.5)).imag() == 0):  # Check: x > 0
                    return ZZ(1)
                else:
                    return ZZ(-1)
        except StandardError:  # x is symbolic
            pass
        return None
コード例 #7
0
ファイル: generalized.py プロジェクト: Etn40ff/sage
    def _eval_(self, x):
        """

        EXAMPLES::

            sage: sgn(-1)
            -1
            sage: sgn(1)
            1
            sage: sgn(0)
            0
            sage: sgn(x)
            sgn(x)
            sage: sgn(-exp(-10000000000000000000))
            -1

        Evaluation test::

            sage: sgn(x).subs(x=1)
            1
            sage: sgn(x).subs(x=0)
            0
            sage: sgn(x).subs(x=-1)
            -1

        More tests::

            sage: sign(RR(2))
            1
            sage: sign(RDF(2))
            1
            sage: sign(AA(-2))
            -1
            sage: sign(AA(0))
            0
        """
        if hasattr(x,'sign'): # First check if x has a sign method
            return x.sign()
        if hasattr(x,'sgn'): # or a sgn method
            return x.sgn()
        try:
            approx_x = ComplexIntervalField()(x)
            if bool(approx_x.imag() == 0):      # x is real
                if bool(approx_x.real() == 0):  # x is zero
                    return ZZ(0)
                # Now we have a non-zero real
                if bool((approx_x**(0.5)).imag() == 0): # Check: x > 0
                    return ZZ(1)
                else:
                    return ZZ(-1)
        except Exception:                     # x is symbolic
            pass
        return None
コード例 #8
0
ファイル: generalized.py プロジェクト: Etn40ff/sage
    def _eval_(self, x):
        """
        INPUT:

        -  ``x`` - a real number or a symbolic expression

        EXAMPLES::

            sage: heaviside(-1/2)
            0
            sage: heaviside(1)
            1
            sage: heaviside(0)
            heaviside(0)
            sage: heaviside(x)
            heaviside(x)
            sage: heaviside(exp(-1000000000000000000000))
            1

        Evaluation test::

            sage: heaviside(x).subs(x=1)
            1
            sage: heaviside(x).subs(x=-1)
            0

        ::

            sage: ex = heaviside(x)+1
            sage: t = loads(dumps(ex)); t
            heaviside(x) + 1
            sage: bool(t == ex)
            True
            sage: t.subs(x=1)
            2
        """
        try:
            approx_x = ComplexIntervalField()(x)
            if bool(approx_x.imag() == 0):      # x is real
                if bool(approx_x.real() == 0):  # x is zero
                    return None
                # Now we have a non-zero real
                if bool((approx_x**(0.5)).imag() == 0): # Check: x > 0
                    return 1
                else:
                    return 0
        except Exception:                     # x is symbolic
            pass
        return None
コード例 #9
0
    def _eval_(self, x):
        """
        INPUT:
        
        -  ``x`` - a real number or a symbolic expression
        
        EXAMPLES::

            sage: heaviside(-1/2)
            0
            sage: heaviside(1)
            1
            sage: heaviside(0)
            heaviside(0)
            sage: heaviside(x)
            heaviside(x)
            sage: heaviside(exp(-1000000000000000000000))
            1

        Evaluation test::
            
            sage: heaviside(x).subs(x=1)
            1
            sage: heaviside(x).subs(x=-1)
            0

        ::

            sage: ex = heaviside(x)+1
            sage: t = loads(dumps(ex)); t
            heaviside(x) + 1
            sage: bool(t == ex)
            True
            sage: t.subs(x=1)
            2
        """
        try:
            approx_x = ComplexIntervalField()(x)
            if bool(approx_x.imag() == 0):  # x is real
                if bool(approx_x.real() == 0):  # x is zero
                    return None
                # Now we have a non-zero real
                if bool((approx_x**(0.5)).imag() == 0):  # Check: x > 0
                    return 1
                else:
                    return 0
        except StandardError:  # x is symbolic
            pass
        return None
コード例 #10
0
ファイル: generalized.py プロジェクト: swewers/mein_sage
    def _evalf_(self, x, **kwds):
        """
        TESTS::

            sage: h(x) = dirac_delta(x)
            sage: h(pi).numerical_approx()
            0.000000000000000
        """
        approx_x = ComplexIntervalField()(x)
        if bool(approx_x.imag() == 0):  # x is real
            if bool(approx_x.real() == 0):  # x is zero
                return None
            else:
                return 0
        raise ValueError("Numeric evaluation of symbolic expression")
コード例 #11
0
ファイル: generalized.py プロジェクト: sagemath/sage
    def _evalf_(self, x, **kwds):
        """
        TESTS::

            sage: h(x) = dirac_delta(x)
            sage: h(pi).numerical_approx()
            0.000000000000000
        """
        approx_x = ComplexIntervalField()(x)
        if bool(approx_x.imag() == 0):      # x is real
            if bool(approx_x.real() == 0):  # x is zero
                return None
            else:
                return 0
        raise ValueError("Numeric evaluation of symbolic expression")
コード例 #12
0
    def _evalf_(self, m, n, **kwds):
        """
        TESTS::

            sage: h(x) = kronecker_delta(3,x)
            sage: h(pi).numerical_approx()
            0.000000000000000
        """
        if bool(repr(m) > repr(n)):
            return kronecker_delta(n, m)
        x = m - n
        approx_x = ComplexIntervalField()(x)
        if approx_x.imag() == 0:      # x is real
            if approx_x.real() == 0:  # x is zero
                return 1
            else:
                return 0
        return 0            # x is complex
コード例 #13
0
ファイル: generalized.py プロジェクト: yunboliu27/sage
    def _evalf_(self, x, **kwds):
        """
        TESTS::

            sage: h(x) = unit_step(x)
            sage: h(pi).numerical_approx()
            1.00000000000000
        """
        approx_x = ComplexIntervalField()(x)
        if bool(approx_x.imag() == 0):  # x is real
            if bool(approx_x.real() == 0):  # x is zero
                return 1
            # Now we have a non-zero real
            if bool((approx_x**(0.5)).imag() == 0):  # Check: x > 0
                return 1
            else:
                return 0
        raise ValueError("Numeric evaluation of symbolic expression")
コード例 #14
0
ファイル: generalized.py プロジェクト: robertwb/sage
    def _evalf_(self, x, **kwds):
        """
        TESTS::

            sage: h(x) = unit_step(x)
            sage: h(pi).numerical_approx()
            1.00000000000000
        """
        approx_x = ComplexIntervalField()(x)
        if bool(approx_x.imag() == 0):      # x is real
            if bool(approx_x.real() == 0):  # x is zero
                return 1
            # Now we have a non-zero real
            if bool((approx_x**(0.5)).imag() == 0): # Check: x > 0
                return 1
            else:
                return 0
        raise ValueError("Numeric evaluation of symbolic expression")
コード例 #15
0
ファイル: generalized.py プロジェクト: Etn40ff/sage
    def _eval_(self, m, n):
        """
        The Kronecker delta function.

        EXAMPLES::

            sage: kronecker_delta(1,2)
            0
            sage: kronecker_delta(1,1)
            1

        Kronecker delta is a symmetric function. We keep arguments sorted to
        ensure that k_d(m, n) - k_d(n, m) cancels automatically::

            sage: x,y=var('x,y')
            sage: kronecker_delta(x, y)
            kronecker_delta(x, y)
            sage: kronecker_delta(y, x)
            kronecker_delta(x, y)
            sage: kronecker_delta(x,2*x)
            kronecker_delta(2*x, x)

        Evaluation test::

            sage: kronecker_delta(1,x).subs(x=1)
            1
        """
        if bool(repr(m) > repr(n)):
            return kronecker_delta(n, m)

        x = m - n
        try:
            approx_x = ComplexIntervalField()(x)
            if bool(approx_x.imag() == 0):      # x is real
                if bool(approx_x.real() == 0):  # x is zero
                    return 1
                else:
                    return 0
            else:
                return 0            # x is complex
        except Exception:                     # x is symbolic
            pass
        return None
コード例 #16
0
    def _eval_(self, m, n):
        """
        The Kronecker delta function.

        EXAMPLES::

            sage: kronecker_delta(1,2)
            0
            sage: kronecker_delta(1,1)
            1

        Kronecker delta is a symmetric function. We keep arguments sorted to
        ensure that (k_d(m, n) - k_d(n, m) cancels automatically::

            sage: x,y=var('x,y')
            sage: kronecker_delta(x, y)
            kronecker_delta(x, y)
            sage: kronecker_delta(y, x)
            kronecker_delta(x, y)
            sage: kronecker_delta(x,2*x)
            kronecker_delta(2*x, x)

        Evaluation test::
            
            sage: kronecker_delta(1,x).subs(x=1)
            1
        """
        if bool(repr(m) > repr(n)):
            return kronecker_delta(n, m)

        x = m - n
        try:
            approx_x = ComplexIntervalField()(x)
            if bool(approx_x.imag() == 0):  # x is real
                if bool(approx_x.real() == 0):  # x is zero
                    return 1
                else:
                    return 0
            else:
                return 0  # x is complex
        except StandardError:  # x is symbolic
            pass
        return None
コード例 #17
0
ファイル: generalized.py プロジェクト: sagemath/sage
    def _evalf_(self, m, n, **kwds):
        """
        TESTS::

            sage: h(x) = kronecker_delta(3,x)
            sage: h(pi).numerical_approx()
            0.000000000000000
        """
        if bool(repr(m) > repr(n)):
            return kronecker_delta(n, m)

        x = m - n
        approx_x = ComplexIntervalField()(x)
        if bool(approx_x.imag() == 0):      # x is real
            if bool(approx_x.real() == 0):  # x is zero
                return 1
            else:
                return 0
        else:
            return 0            # x is complex
        raise ValueError("Numeric evaluation of symbolic expression")
コード例 #18
0
ファイル: generalized.py プロジェクト: swewers/mein_sage
    def _evalf_(self, m, n, **kwds):
        """
        TESTS::

            sage: h(x) = kronecker_delta(3,x)
            sage: h(pi).numerical_approx()
            0.000000000000000
        """
        if bool(repr(m) > repr(n)):
            return kronecker_delta(n, m)

        x = m - n
        approx_x = ComplexIntervalField()(x)
        if bool(approx_x.imag() == 0):  # x is real
            if bool(approx_x.real() == 0):  # x is zero
                return 1
            else:
                return 0
        else:
            return 0  # x is complex
        raise ValueError("Numeric evaluation of symbolic expression")
コード例 #19
0
ファイル: generalized.py プロジェクト: Etn40ff/sage
    def _eval_(self, x):
        """
        INPUT:

        -  ``x`` - a real number or a symbolic expression

        EXAMPLES::

            sage: unit_step(-1)
            0
            sage: unit_step(1)
            1
            sage: unit_step(0)
            1
            sage: unit_step(x)
            unit_step(x)
            sage: unit_step(-exp(-10000000000000000000))
            0

        Evaluation test::

            sage: unit_step(x).subs(x=1)
            1
            sage: unit_step(x).subs(x=0)
            1
        """
        try:
            approx_x = ComplexIntervalField()(x)
            if bool(approx_x.imag() == 0):      # x is real
                if bool(approx_x.real() == 0):  # x is zero
                    return 1
                # Now we have a non-zero real
                if bool((approx_x**(0.5)).imag() == 0): # Check: x > 0
                    return 1
                else:
                    return 0
        except Exception:                     # x is symbolic
            pass
        return None
コード例 #20
0
    def _eval_(self, x):
        """
        INPUT:
        
        -  ``x`` - a real number or a symbolic expression
        
        EXAMPLES::

            sage: unit_step(-1)
            0
            sage: unit_step(1)
            1
            sage: unit_step(0)
            1
            sage: unit_step(x)
            unit_step(x)
            sage: unit_step(-exp(-10000000000000000000))
            0

        Evaluation test::
            
            sage: unit_step(x).subs(x=1)
            1
            sage: unit_step(x).subs(x=0)
            1
        """
        try:
            approx_x = ComplexIntervalField()(x)
            if bool(approx_x.imag() == 0):  # x is real
                if bool(approx_x.real() == 0):  # x is zero
                    return 1
                # Now we have a non-zero real
                if bool((approx_x**(0.5)).imag() == 0):  # Check: x > 0
                    return 1
                else:
                    return 0
        except StandardError:  # x is symbolic
            pass
        return None