# Ticket #1174: trac-1174-part2.patch

File trac-1174-part2.patch, 3.5 KB (added by was, 14 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 class SymbolicComposition(SymbolicOperat return float(f._approx_(float(g))) def __complex__(self): f = self._operands[0] g = self._operands[1] return complex(f._approx_(complex(g))) """ Convert this symbolic composition to a Python complex number. EXAMPLES: sage: complex(cos(3)) (-0.98999249660044542+0j) sage: complex(cos(3*I)) (10.067661995777771+0j) """ f = self._operands[0] g = self._operands[1] return complex(f._complex_approx_(complex(g))) def _mpfr_(self, field): """ class PrimitiveFunction(SymbolicExpressi def _approx_(self, x):  # must *always* be called with a float x as input. s = '%s(%s), numer'%(self._repr_(), float(x)) return float(maxima.eval(s)) def _complex_approx_(self, x): # must be called with Python complex float as input """ Given a Python complex x, evaluate self and return a complex value. EXAMPLES: sage: complex(cos(3*I)) (10.067661995777771+0j) The following fails because we and Maxima haven't implemented erf yet for complex values: sage: complex(erf(3*I)) Traceback (most recent call last): ... TypeError: unable to simplify to complex approximation """ if x.imag == 0: return self._approx_(x.real) s = '%s(%s+%s*%%i), numer'%(self._repr_(), x.real, x.imag) a = maxima.eval(s).replace('%i', '1j') if '(' in a: # unable to simplify to a complex -- still function calls there. raise TypeError, "unable to simplify to complex approximation" return complex(eval(a)) _syms = {} class Function_abs(PrimitiveFunction): def _approx_(self, x): return float(x.__abs__()) def _complex_approx_(self, x): """ EXAMPLES: sage: complex(abs(3*I)) (3+0j) sage: abs_symbolic._complex_approx_(complex(3*I)) (3+0j) """ return complex(x.__abs__()) def __call__(self, x): # special case return SymbolicComposition(self, SR(x))
• ## sage/interfaces/maxima.py

`diff -r c78de23b7bab -r 9683a9f27c78 sage/interfaces/maxima.py`
 a class MaximaElement(ExpectElement): from sage.calculus.calculus import symbolic_expression_from_maxima_string #return symbolic_expression_from_maxima_string(self.name(), maxima=self.parent()) return symbolic_expression_from_maxima_string(repr(self)) def __complex__(self): """ EXAMPLES: sage: complex(maxima('sqrt(-2)+1')) (1+1.4142135623730951j) """ return complex(self._sage_()) def _complex_mpfr_field_(self, C): """