Ticket #1174: trac-1174-part2.patch

File trac-1174-part2.patch, 3.5 KB (added by was, 12 years ago)

apply this after applying the other patch

  • sage/calculus/calculus.py

    # HG changeset patch
    # User William Stein <wstein@gmail.com>
    # Date 1196626996 28800
    # Node ID 9683a9f27c7866987ccb131ace38475efdb3f3ee
    # Parent  c78de23b7babc30a766b0458fb46a6022fce6e19
    trac #1174 -- take two -- doing something better for __complex__ coercion in response to cwitty's referee remarks.
    
    diff -r c78de23b7bab -r 9683a9f27c78 sage/calculus/calculus.py
    a b class SymbolicComposition(SymbolicOperat 
    39443944        return float(f._approx_(float(g)))
    39453945
    39463946    def __complex__(self):
    3947         f = self._operands[0]
    3948         g = self._operands[1]
    3949         return complex(f._approx_(complex(g)))
     3947        """
     3948        Convert this symbolic composition to a Python complex number.
     3949       
     3950        EXAMPLES:
     3951            sage: complex(cos(3))
     3952            (-0.98999249660044542+0j)
     3953            sage: complex(cos(3*I))
     3954            (10.067661995777771+0j)
     3955        """
     3956        f = self._operands[0]
     3957        g = self._operands[1]
     3958        return complex(f._complex_approx_(complex(g)))
    39503959
    39513960    def _mpfr_(self, field):
    39523961        """
    class PrimitiveFunction(SymbolicExpressi 
    40884097    def _approx_(self, x):  # must *always* be called with a float x as input.
    40894098        s = '%s(%s), numer'%(self._repr_(), float(x))
    40904099        return float(maxima.eval(s))
     4100
     4101    def _complex_approx_(self, x): # must be called with Python complex float as input
     4102        """
     4103        Given a Python complex x, evaluate self and return a complex value.
     4104
     4105        EXAMPLES:
     4106            sage: complex(cos(3*I))
     4107            (10.067661995777771+0j)
     4108
     4109        The following fails because we and Maxima haven't implemented
     4110        erf yet for complex values:
     4111            sage: complex(erf(3*I))
     4112            Traceback (most recent call last):
     4113            ...
     4114            TypeError: unable to simplify to complex approximation
     4115        """
     4116        if x.imag == 0:
     4117            return self._approx_(x.real)
     4118        s = '%s(%s+%s*%%i), numer'%(self._repr_(), x.real, x.imag)
     4119        a = maxima.eval(s).replace('%i', '1j')
     4120        if '(' in a:
     4121            # unable to simplify to a complex -- still function calls there.
     4122            raise TypeError, "unable to simplify to complex approximation"
     4123        return complex(eval(a))
     4124       
    40914125
    40924126_syms = {}
    40934127
    class Function_abs(PrimitiveFunction): 
    41494183
    41504184    def _approx_(self, x):
    41514185        return float(x.__abs__())
     4186
     4187    def _complex_approx_(self, x):
     4188        """
     4189        EXAMPLES:
     4190            sage: complex(abs(3*I))
     4191            (3+0j)
     4192            sage: abs_symbolic._complex_approx_(complex(3*I))
     4193            (3+0j)
     4194        """
     4195        return complex(x.__abs__())
    41524196
    41534197    def __call__(self, x): # special case
    41544198        return SymbolicComposition(self, SR(x))
  • sage/interfaces/maxima.py

    diff -r c78de23b7bab -r 9683a9f27c78 sage/interfaces/maxima.py
    a b class MaximaElement(ExpectElement): 
    13241324        from sage.calculus.calculus import symbolic_expression_from_maxima_string
    13251325        #return symbolic_expression_from_maxima_string(self.name(), maxima=self.parent())
    13261326        return symbolic_expression_from_maxima_string(repr(self))
     1327
     1328    def __complex__(self):
     1329        """
     1330        EXAMPLES:
     1331            sage: complex(maxima('sqrt(-2)+1'))
     1332            (1+1.4142135623730951j)
     1333        """
     1334        return complex(self._sage_())
    13271335
    13281336    def _complex_mpfr_field_(self, C):
    13291337        """