# 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 
3944  3944  return float(f._approx_(float(g))) 
3945  3945  
3946  3946  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))) 
3950  3959  
3951  3960  def _mpfr_(self, field): 
3952  3961  """ 
… 
… 
class PrimitiveFunction(SymbolicExpressi 
4088  4097  def _approx_(self, x): # must *always* be called with a float x as input. 
4089  4098  s = '%s(%s), numer'%(self._repr_(), float(x)) 
4090  4099  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  
4091  4125  
4092  4126  _syms = {} 
4093  4127  
… 
… 
class Function_abs(PrimitiveFunction): 
4149  4183  
4150  4184  def _approx_(self, x): 
4151  4185  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__()) 
4152  4196  
4153  4197  def __call__(self, x): # special case 
4154  4198  return SymbolicComposition(self, SR(x)) 
diff r c78de23b7bab r 9683a9f27c78 sage/interfaces/maxima.py
a

b

class MaximaElement(ExpectElement): 
1324  1324  from sage.calculus.calculus import symbolic_expression_from_maxima_string 
1325  1325  #return symbolic_expression_from_maxima_string(self.name(), maxima=self.parent()) 
1326  1326  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_()) 
1327  1335  
1328  1336  def _complex_mpfr_field_(self, C): 
1329  1337  """ 