[Python-checkins] r60392 - in python/branches/trunk-math: Doc/library/stdtypes.rst Lib/test/test_complex.py Objects/complexobject.c
christian.heimes
python-checkins at python.org
Mon Jan 28 15:40:48 CET 2008
Author: christian.heimes
Date: Mon Jan 28 15:40:48 2008
New Revision: 60392
Modified:
python/branches/trunk-math/Doc/library/stdtypes.rst
python/branches/trunk-math/Lib/test/test_complex.py
python/branches/trunk-math/Objects/complexobject.c
Log:
Added from_cis and as_cis to the complex type. The functions creates / return the cis form of a complex (aka polar form or exp form) which is often used in engineering.
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 Mon Jan 28 15:40:48 2008
@@ -302,7 +302,7 @@
+--------------------+---------------------------------+--------+
| ``float(x)`` | *x* converted to floating point | \(6) |
+--------------------+---------------------------------+--------+
-| ``complex(re,im)`` | a complex number with real part | |
+| ``complex(re,im)`` | a complex number with real part | \(8) |
| | *re*, imaginary part *im*. | |
| | *im* defaults to zero. | |
+--------------------+---------------------------------+--------+
@@ -370,6 +370,13 @@
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.
+
+
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_complex.py
==============================================================================
--- python/branches/trunk-math/Lib/test/test_complex.py (original)
+++ python/branches/trunk-math/Lib/test/test_complex.py Mon Jan 28 15:40:48 2008
@@ -9,7 +9,7 @@
)
from random import random
-from math import atan2
+from math import atan2, pi
# These tests ensure that complex math does the right thing
@@ -359,6 +359,30 @@
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/Objects/complexobject.c
==============================================================================
--- python/branches/trunk-math/Objects/complexobject.c (original)
+++ python/branches/trunk-math/Objects/complexobject.c Mon Jan 28 15:40:48 2008
@@ -183,6 +183,31 @@
}
+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)
{
@@ -748,9 +773,62 @@
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