[pypy-svn] r77705 - pypy/branch/fast-forward/lib_pypy

afa at codespeak.net afa at codespeak.net
Fri Oct 8 00:50:15 CEST 2010

```Author: afa
Date: Fri Oct  8 00:50:14 2010
New Revision: 77705

Modified:
pypy/branch/fast-forward/lib_pypy/cmath.py
Log:
coerce the argument of cmath function to a complex object
- accept a subclass
- refuse strings
- but accept all other types convertible to complex
Should fix most tests in test_cmath

Modified: pypy/branch/fast-forward/lib_pypy/cmath.py
==============================================================================
--- pypy/branch/fast-forward/lib_pypy/cmath.py	(original)
+++ pypy/branch/fast-forward/lib_pypy/cmath.py	Fri Oct  8 00:50:14 2010
@@ -16,28 +16,36 @@
_halfi = complex(0., 0.5)

-# internal function not available from Python
+
+# internal functions not available from Python
+def _to_complex(x):
+    if isinstance(x, complex):
+        return x
+    if isinstance(x, (str, unicode)):
+        return x          # and let it fail later
+    return complex(x)
+
def _prodi(x):
-    x = complex(x, 0)
+    x = _to_complex(x)
real = -x.imag
imag = x.real
return complex(real, imag)

+
def phase(x):
-    x = complex(x)
+    x = _to_complex(x)
return math.atan2(x.imag, x.real)

def polar(x):
-    x = complex(x)
+    x = _to_complex(x)
phi = math.atan2(x.imag, x.real)
r = abs(x)
return r, phi

def rect(r, phi):
-    r, phi = complex(r), complex(phi)
return complex(r * math.cos(phi), r * math.sin(phi))

@@ -45,7 +53,8 @@
"""acos(x)

Return the arc cosine of x."""
-
+
+    x = _to_complex(x)
return -(_prodi(log((x+(_i*sqrt((_one-(x*x))))))))

@@ -54,6 +63,7 @@

Return the hyperbolic arccosine of x."""

+    x = _to_complex(x)
z = log(_sqrt_half*(sqrt(x+_one)+sqrt(x-_one)))
return z+z

@@ -62,7 +72,8 @@
"""asin(x)

Return the arc sine of x."""
-
+
+    x = _to_complex(x)
# -i * log[(sqrt(1-x**2) + i*x]
squared = x*x
sqrt_1_minus_x_sq = sqrt(_one-squared)
@@ -73,7 +84,8 @@
"""asinh(x)

Return the hyperbolic arc sine of x."""
-
+
+    x = _to_complex(x)
z = log((_sqrt_half * (sqrt(x+_i)+sqrt((x-_i))) ))
return z+z

@@ -82,7 +94,8 @@
"""atan(x)

Return the arc tangent of x."""
-
+
+    x = _to_complex(x)
return _halfi*log(((_i+x)/(_i-x)))

@@ -90,7 +103,8 @@
"""atanh(x)

Return the hyperbolic arc tangent of x."""
-
+
+    x = _to_complex(x)
return _half*log((_one+x)/(_one-x))

@@ -99,7 +113,7 @@

Return the cosine of x."""

-    x = complex(x, 0)
+    x = _to_complex(x)
real = math.cos(x.real) * math.cosh(x.imag)
imag = -math.sin(x.real) * math.sinh(x.imag)
return complex(real, imag)
@@ -110,7 +124,7 @@

Return the hyperbolic cosine of x."""

-    x = complex(x, 0)
+    x = _to_complex(x)
real = math.cos(x.imag) * math.cosh(x.real)
imag = math.sin(x.imag) * math.sinh(x.real)
return complex(real, imag)
@@ -121,7 +135,7 @@

Return the exponential value e**x."""

-    x = complex(x, 0)
+    x = _to_complex(x)
l = math.exp(x.real)
real = l * math.cos(x.imag)
imag = l * math.sin(x.imag)
@@ -135,7 +149,7 @@

if base is not None:
return log(x) / log(base)
-    x = complex(x, 0)
+    x = _to_complex(x)
l = math.hypot(x.real,x.imag)
imag = math.atan2(x.imag, x.real)
real = math.log(l)
@@ -147,7 +161,7 @@

Return the base-10 logarithm of x."""

-    x = complex(x, 0)
+    x = _to_complex(x)
l = math.hypot(x.real, x.imag)
imag = math.atan2(x.imag, x.real)/math.log(10.)
real = math.log10(l)
@@ -159,7 +173,7 @@

Return the sine of x."""

-    x = complex(x, 0)
+    x = _to_complex(x)
real = math.sin(x.real) * math.cosh(x.imag)
imag = math.cos(x.real) * math.sinh(x.imag)
return complex(real, imag)
@@ -170,7 +184,7 @@

Return the hyperbolic sine of x."""

-    x = complex(x, 0)
+    x = _to_complex(x)
real = math.cos(x.imag) * math.sinh(x.real)
imag = math.sin(x.imag) * math.cosh(x.real)
return complex(real, imag)
@@ -181,7 +195,7 @@

Return the square root of x."""

-    x = complex(x, 0)
+    x = _to_complex(x)
if x.real == 0. and x.imag == 0.:
real, imag = 0, 0
else:
@@ -206,7 +220,7 @@

Return the tangent of x."""

-    x = complex(x, 0)
+    x = _to_complex(x)
sr = math.sin(x.real)
cr = math.cos(x.real)
shi = math.sinh(x.imag)
@@ -226,7 +240,7 @@

Return the hyperbolic tangent of x."""

-    x = complex(x, 0)
+    x = _to_complex(x)
si = math.sin(x.imag)
ci = math.cos(x.imag)
shr = math.sinh(x.real)
@@ -239,3 +253,15 @@
real = (rs*rc + is_*ic) / d
imag = (is_*rc - rs*ic) / d
return complex(real, imag)
+
+def isnan(x):
+    """isnan(z) -> bool
+    Checks if the real or imaginary part of z not a number (NaN)"""
+    x = _to_complex(x)
+    return math.isnan(x.real) or math.isnan(x.imag)
+
+def isinf(x):
+    """isnan(z) -> bool
+    Checks if the real or imaginary part of z is infinite"""
+    x = _to_complex(x)
+    return math.isinf(x.real) or math.isinf(x.imag)

```