def exp(x, err=defaultError): """ exp(x [,err]) is the exponential function. """ try: rx = rational.Rational(x) if isinstance(err, RelativeError): _err = real.RelativeError(0, err.relativeerrorrange) elif isinstance(err, AbsoluteError): _err = real.AbsoluteError(0, err.absoluteerrorrange) return real.exp(rx, _err) except TypeError: pass if (defaultError >= err) or isinstance(err, AbsoluteError): # divide real part and imaginary part if isinstance(err, RelativeError): _err = real.RelativeError(0, err.relativeerrorrange, 2) elif isinstance(err, AbsoluteError): _err = real.AbsoluteError(0, err.absoluteerrorrange, 2) radius = real.exp(x.real, _err) if isinstance(err, RelativeError): _err = RelativeError(err.relativeerrorrange / 2) elif isinstance(err, AbsoluteError): _err = AbsoluteError(err.absoluteerrorrange / 2) arg = expi(x.imag, _err) return radius * arg else: return Complex(cmath.exp(complex(x.real,x.imag)))
def expi(x, err=defaultError): """ expi(x [,err]) returns exp(i * x) where i is the imaginary unit and x must be a real number. """ if x == 0: return rational.Integer(1) if isinstance(err, RelativeError): _err = real.RelativeError(0, err.relativeerrorrange, 2) elif isinstance(err, AbsoluteError): _err = real.AbsoluteError(0, err.absoluteerrorrange, 2) re = real.cos(x, _err) im = real.sin(x, _err) return Complex(re, im)
def log(x, err=defaultError): """ log(x [,err]) returns the natural logarithm of x. There is one branch cut, from 0 along the negative real axis to -infinity, continuous from above. """ if (defaultError >= err) or isinstance(err, AbsoluteError): if isinstance(err, RelativeError): _err = real.RelativeError(0, err.relativeerrorrange, 2) elif isinstance(err, AbsoluteError): _err = real.AbsoluteError(0, err.absoluteerrorrange, 2) if x in real.theRealField: x = +x if x > 0: return real.log(x, err=_err) elif x < 0: return Complex(real.log(abs(x), _err), real.pi(_err)) return Complex(real.log(abs(x), err=_err), real.atan2(x.imag, x.real, _err)) else: return Complex(cmath.log(complex(x.real, x.imag)))