[Python-checkins] r60409 - in python/branches/trunk-math: Doc/library/cmath.rst Doc/library/stdtypes.rst Lib/test/test_cmath.py Lib/test/test_complex.py Misc/NEWS Modules/cmathmodule.c Objects/complexobject.c

christian.heimes python-checkins at python.org
Tue Jan 29 14:07:21 CET 2008


Author: christian.heimes
Date: Tue Jan 29 14:07:21 2008
New Revision: 60409

Modified:
   python/branches/trunk-math/Doc/library/cmath.rst
   python/branches/trunk-math/Doc/library/stdtypes.rst
   python/branches/trunk-math/Lib/test/test_cmath.py
   python/branches/trunk-math/Lib/test/test_complex.py
   python/branches/trunk-math/Misc/NEWS
   python/branches/trunk-math/Modules/cmathmodule.c
   python/branches/trunk-math/Objects/complexobject.c
Log:
Removed from/as_cis methods and added arg, polar and rect functions to the math module.

Modified: python/branches/trunk-math/Doc/library/cmath.rst
==============================================================================
--- python/branches/trunk-math/Doc/library/cmath.rst	(original)
+++ python/branches/trunk-math/Doc/library/cmath.rst	Tue Jan 29 14:07:21 2008
@@ -39,6 +39,13 @@
    from 1 along the real axis to -∞, continuous from above.
 
 
+.. function:: arg(x)
+
+   Return argument, also known as the phase angle, of a complex.
+
+   .. versionadded:: 2.6
+
+
 .. function:: asin(x)
 
    Return the arc sine of *x*. This has the same branch cuts as :func:`acos`.
@@ -108,6 +115,21 @@
    :func:`log`.
 
 
+.. function:: polar(x)
+
+   Convert a complex from rectangular coordinates to polar coordinates. r is
+   the distance from 0 and phi the phase angle.
+
+   .. versionadded:: 2.6
+
+
+.. function:: rect(r, phi)
+
+   Convert from polar coordinates to rectangular coordinates.
+
+   .. versionadded:: 2.6
+
+
 .. function:: sin(x)
 
    Return the sine of *x*.

Modified: python/branches/trunk-math/Doc/library/stdtypes.rst
==============================================================================
--- python/branches/trunk-math/Doc/library/stdtypes.rst	(original)
+++ python/branches/trunk-math/Doc/library/stdtypes.rst	Tue Jan 29 14:07:21 2008
@@ -302,7 +302,7 @@
 +--------------------+---------------------------------+--------+
 | ``float(x)``       | *x* converted to floating point | \(6)   |
 +--------------------+---------------------------------+--------+
-| ``complex(re,im)`` | a complex number with real part | \(8)   |
+| ``complex(re,im)`` | a complex number with real part |        |
 |                    | *re*, imaginary part *im*.      |        |
 |                    | *im* defaults to zero.          |        |
 +--------------------+---------------------------------+--------+
@@ -370,14 +370,6 @@
    Python defines ``pow(0, 0)`` and ``0 ** 0`` to be ``1``, as is common for
    programming languages.
 
-(8)
-   The complex type has also the constructor ``from_cis(r, phi)`` to create a
-   complex from the polar form ``r * (cos(phi) + j * sin(pih))`` or
-   expotentional form ``r * exp(j * phi)``. The method ``as_cis()`` returns the
-   radius *r* and angle *phi* of a complex number.
-
-   .. versionadded:: 2.6
-
 
 All :class:`numbers.Real` types (:class:`int`, :class:`long`, and
 :class:`float`) also include the following operations:

Modified: python/branches/trunk-math/Lib/test/test_cmath.py
==============================================================================
--- python/branches/trunk-math/Lib/test/test_cmath.py	(original)
+++ python/branches/trunk-math/Lib/test/test_cmath.py	Tue Jan 29 14:07:21 2008
@@ -3,6 +3,7 @@
 import unittest
 import os, sys
 import cmath, math
+from cmath import arg, polar, rect, pi
 
 class CMathTests(unittest.TestCase):
     # list of all functions in cmath
@@ -224,6 +225,38 @@
                 self.rAssertAlmostEqual(expected.real, actual.real)
             self.rAssertAlmostEqual(expected.imag, actual.imag)
 
+    def assertCISEqual(self, a, b):
+        eps = 1E-7
+        if abs(a[0] - b[0]) > eps or abs(a[1] - b[1]) > eps:
+            self.fail((a ,b))
+
+    def test_polar(self):
+        self.assertCISEqual(polar(0), (0., 0.))
+        self.assertCISEqual(polar(1.), (1., 0.))
+        self.assertCISEqual(polar(-1.), (1., pi))
+        self.assertCISEqual(polar(1j), (1., pi/2))
+        self.assertCISEqual(polar(-1j), (1., -pi/2))
+
+    def test_arg(self):
+        self.assertAlmostEqual(arg(0), 0.)
+        self.assertAlmostEqual(arg(1.), 0.)
+        self.assertAlmostEqual(arg(-1.), pi)
+        self.assertAlmostEqual(arg(1j), pi/2)
+        self.assertAlmostEqual(arg(-1j), -pi/2)
+
+    def assertCEqual(self, a, b):
+        eps = 1E-7
+        if abs(a.real - b[0]) > eps or abs(a.imag - b[1]) > eps:
+            self.fail((a ,b))
+
+    def test_rect(self):
+        self.assertCEqual(rect(0, 0), (0, 0))
+        self.assertCEqual(rect(1, 0), (1., 0))
+        self.assertCEqual(rect(1, -pi), (-1., 0))
+        self.assertCEqual(rect(1, pi/2), (0, 1.))
+        self.assertCEqual(rect(1, -pi/2), (0, -1.))
+
+
 def test_main():
     run_unittest(CMathTests)
 

Modified: python/branches/trunk-math/Lib/test/test_complex.py
==============================================================================
--- python/branches/trunk-math/Lib/test/test_complex.py	(original)
+++ python/branches/trunk-math/Lib/test/test_complex.py	Tue Jan 29 14:07:21 2008
@@ -359,30 +359,6 @@
             except (OSError, IOError):
                 pass
 
-    def assertCISEqual(self, a, b):
-        eps = 1E-7
-        if abs(a[0] - b[0]) > eps or abs(a[1] - b[1]) > eps:
-            self.fail((a ,b))
-
-    def test_as_cis(self):
-        self.assertCISEqual(complex().as_cis(), (0., 0.))
-        self.assertCISEqual(complex(1.).as_cis(), (1., 0.))
-        self.assertCISEqual(complex(-1.).as_cis(), (1., pi))
-        self.assertCISEqual(complex(0., 1).as_cis(), (1., pi/2))
-        self.assertCISEqual(complex(0., -1).as_cis(), (1., -pi/2))
-
-    def assertCEqual(self, a, b):
-        eps = 1E-7
-        if abs(a.real - b[0]) > eps or abs(a.imag - b[1]) > eps:
-            self.fail((a ,b))
-
-    def test_from_cis(self):
-        self.assertCEqual(complex.from_cis(0, 0), (0, 0))
-        self.assertCEqual(complex.from_cis(1, 0), (1., 0))
-        self.assertCEqual(complex.from_cis(1, -pi), (-1., 0))
-        self.assertCEqual(complex.from_cis(1, pi/2), (0, 1.))
-        self.assertCEqual(complex.from_cis(1, -pi/2), (0, -1.))
-
 def test_main():
     test_support.run_unittest(ComplexTest)
 

Modified: python/branches/trunk-math/Misc/NEWS
==============================================================================
--- python/branches/trunk-math/Misc/NEWS	(original)
+++ python/branches/trunk-math/Misc/NEWS	Tue Jan 29 14:07:21 2008
@@ -12,9 +12,8 @@
 Core and builtins
 -----------------
 
-- Added ``from_cis`` constructor to the ``complex`` type to create a complex
-  from a polar or exp notation. The new method ``as_cis`` returns radius *r*
-  and angle *phi* of a complex.
+- Added arg(z) -> phi, polar(z) -> r, phi and rect(r, phi) -> z to the cmath
+  module.
 
 - Issue #XXXX: Four new methods were added to the math and cmath modules:
   acosh, asinh, atanh and log1p. Replacement implementations for platforms

Modified: python/branches/trunk-math/Modules/cmathmodule.c
==============================================================================
--- python/branches/trunk-math/Modules/cmathmodule.c	(original)
+++ python/branches/trunk-math/Modules/cmathmodule.c	Tue Jan 29 14:07:21 2008
@@ -33,8 +33,6 @@
 #define CM_SCALE_UP 2*(DBL_MANT_DIG/2) + 1
 #define CM_SCALE_DOWN -(DBL_MANT_DIG/2 + 1)
 
-
-
 /* forward declarations */
 static Py_complex c_asinh(Py_complex);
 static Py_complex c_atanh(Py_complex);
@@ -609,6 +607,75 @@
 FUNC1(cmath_tan, c_tan)
 FUNC1(cmath_tanh, c_tanh)
 
+static PyObject *
+cmath_arg(PyObject *self, PyObject *args)
+{
+	Py_complex z;
+	double phi;
+	if (!PyArg_ParseTuple(args, "D:arg", &z))
+		return NULL;
+	errno = 0;
+	PyFPE_START_PROTECT("arg function", return 0)
+	phi = atan2(z.imag, z.real);
+	PyFPE_END_PROTECT(r)
+	if (errno != 0)
+		return math_error();
+	else
+		return PyFloat_FromDouble(phi);
+}
+
+PyDoc_STRVAR(cmath_arg_doc,
+"arg(z) -> float\n\n\
+Return argument, also known as the phase angle, of a complex.");
+
+static PyObject *
+cmath_polar(PyObject *self, PyObject *args)
+{
+	Py_complex z;
+	double r, phi;
+	if (!PyArg_ParseTuple(args, "D:polar", &z))
+		return NULL;
+	errno = 0;
+	PyFPE_START_PROTECT("polar function", return 0)
+	r = hypot(z.real, z.imag);
+	phi = atan2(z.imag, z.real);
+	PyFPE_END_PROTECT(r)
+	if (errno != 0)
+		return math_error();
+	else
+		return Py_BuildValue("dd", r, phi);
+}
+
+PyDoc_STRVAR(cmath_polar_doc,
+"polar(z) -> r: float, phi: float\n\n\
+Convert a complex from rectangular coordinates to polar coordinates. r is\n\
+the distance from 0 and phi the phase angle.");
+
+static PyObject *
+cmath_rect(PyObject *self, PyObject *args)
+{
+	Py_complex z;
+	double r, phi;
+	if (!PyArg_ParseTuple(args, "dd:rect", &r, &phi))
+		return NULL;
+	errno = 0;
+	if (r < 0.) {
+		errno = EDOM;
+		return math_error();
+	}
+	PyFPE_START_PROTECT("rect function", return 0)
+	z.real = r * cos(phi);
+	z.imag = r * sin(phi);
+	PyFPE_END_PROTECT(z)
+	if (errno != 0)
+		return math_error();
+	else
+		return PyComplex_FromCComplex(z);
+}
+
+PyDoc_STRVAR(cmath_rect_doc,
+"rect(r, phi) -> z: complex\n\n\
+Convert from polar coordinates to rectangular coordinates.");
 
 PyDoc_STRVAR(module_doc,
 "This module is always available. It provides access to mathematical\n"
@@ -617,6 +684,7 @@
 static PyMethodDef cmath_methods[] = {
 	{"acos",   cmath_acos,  METH_VARARGS, c_acos_doc},
 	{"acosh",  cmath_acosh, METH_VARARGS, c_acosh_doc},
+	{"arg",    cmath_arg,   METH_VARARGS, cmath_arg_doc},
 	{"asin",   cmath_asin,  METH_VARARGS, c_asin_doc},
 	{"asinh",  cmath_asinh, METH_VARARGS, c_asinh_doc},
 	{"atan",   cmath_atan,  METH_VARARGS, c_atan_doc},
@@ -626,6 +694,8 @@
 	{"exp",    cmath_exp,   METH_VARARGS, c_exp_doc},
 	{"log",    cmath_log,   METH_VARARGS, cmath_log_doc},
 	{"log10",  cmath_log10, METH_VARARGS, c_log10_doc},
+	{"polar",  cmath_polar, METH_VARARGS, cmath_polar_doc},
+	{"rect",   cmath_rect,  METH_VARARGS, cmath_rect_doc},
 	{"sin",    cmath_sin,   METH_VARARGS, c_sin_doc},
 	{"sinh",   cmath_sinh,  METH_VARARGS, c_sinh_doc},
 	{"sqrt",   cmath_sqrt,  METH_VARARGS, c_sqrt_doc},

Modified: python/branches/trunk-math/Objects/complexobject.c
==============================================================================
--- python/branches/trunk-math/Objects/complexobject.c	(original)
+++ python/branches/trunk-math/Objects/complexobject.c	Tue Jan 29 14:07:21 2008
@@ -183,31 +183,6 @@
 
 }
 
-static Py_complex
-c_from_cis(float r, float phi)
-{
-	Py_complex cn;
-
-	if (r <= 0.) {
-		if (r != 0.) {
-			errno = EDOM;
-		}
-		cn.real = cn.imag = 0.;
-		return cn;
-	}
-
-	cn.real = r * cos(phi);
-	cn.imag = r * sin(phi);
-	return cn;
-}
-
-static void
-c_as_cis(Py_complex cn, float *r, float *phi)
-{
-	*r = hypot(cn.real, cn.imag);
-	*phi = atan2(cn.imag, cn.real);
-}
-
 static PyObject *
 complex_subtype_from_c_complex(PyTypeObject *type, Py_complex cval)
 {
@@ -773,62 +748,9 @@
 	return Py_BuildValue("(D)", &v->cval);
 }
 
-static PyObject *
-complex_from_cis(PyObject *ignored, PyObject *args)
-{
-	Py_complex c;
-	float r, phi;
-
-	if (!PyArg_ParseTuple(args, "ff:fromcis", &r, &phi))
-		return NULL;
-
-	PyFPE_START_PROTECT("complex_from_cis", return NULL)
-	errno = 0;
-	c = c_from_cis(r, phi);
-	PyFPE_END_PROTECT(r)
-	Py_ADJUST_ERANGE2(c.real, c.imag);
-	if (errno == EDOM) {
-		PyErr_SetString(PyExc_ValueError,
-				"r must not be negative");
-		return NULL;
-	}
-	return PyComplex_FromCComplex(c);
-}
-
-PyDoc_STRVAR(complex_from_cis_doc,
-"complex.from_cis(r, phi) -> complex\n"
-"\n"
-"Creates a complex number from the polar form r * (cos(phi) + j * sin(phi))"
-"or exponential form r * exp(j * phi)");
-
-static PyObject *
-complex_as_cis(PyObject *self)
-{
-	Py_complex c;
-	float r, phi;
-
-	c = ((PyComplexObject *)self)->cval;
-	PyFPE_START_PROTECT("complex_as_cis", return NULL)
-	errno = 0;
-	c_as_cis(c, &r, &phi);
-	PyFPE_END_PROTECT(r)
-	return Py_BuildValue("ff", r, phi);
-}
-
-PyDoc_STRVAR(complex_as_cis_doc,
-"complex.as_cis() -> r, phi\n"
-"\n"
-"Converts a complex number to the polar form r * (cos(phi) + j * sin(phi))"
-"or exponential form r * exp(j * phi)");
-
-
 static PyMethodDef complex_methods[] = {
-	{"as_cis",	(PyCFunction)complex_as_cis,	METH_NOARGS,
-	 complex_as_cis_doc},
 	{"conjugate",	(PyCFunction)complex_conjugate,	METH_NOARGS,
 	 complex_conjugate_doc},
-	{"from_cis",	(PyCFunction)complex_from_cis,
-	 METH_VARARGS | METH_STATIC, complex_from_cis_doc},
 	{"__getnewargs__",	(PyCFunction)complex_getnewargs,	METH_NOARGS},
 	{NULL,		NULL}		/* sentinel */
 };


More information about the Python-checkins mailing list