From: <cg...@us...> - 2006-12-05 02:58:38
|
Revision: 3006 http://svn.sourceforge.net/jython/?rev=3006&view=rev Author: cgroves Date: 2006-12-04 18:58:36 -0800 (Mon, 04 Dec 2006) Log Message: ----------- adding Tim Freund's cmath implementation from patch #1600162 Modified Paths: -------------- branches/2.3/src/org/python/modules/Setup.java Added Paths: ----------- branches/2.3/Lib/test/test_cmath.py branches/2.3/src/org/python/modules/cmath.java Added: branches/2.3/Lib/test/test_cmath.py =================================================================== --- branches/2.3/Lib/test/test_cmath.py (rev 0) +++ branches/2.3/Lib/test/test_cmath.py 2006-12-05 02:58:36 UTC (rev 3006) @@ -0,0 +1,109 @@ +#! /usr/bin/env python +""" Simple test script for cmathmodule.c + Roger E. Masse +""" +import cmath +import unittest +from test import test_support +from test.test_support import verbose + +p = cmath.pi +e = cmath.e +if verbose: + print 'PI = ', abs(p) + print 'E = ', abs(e) + + +class CmathTestCase(unittest.TestCase): + def assertAlmostEqual(self, x, y, places=5, msg=None): + unittest.TestCase.assertAlmostEqual(self, x.real, y.real, places, msg) + unittest.TestCase.assertAlmostEqual(self, x.imag, y.imag, places, msg) + + def test_acos(self): + self.assertAlmostEqual(complex(0.936812, -2.30551), + cmath.acos(complex(3, 4))) + + def test_acosh(self): + self.assertAlmostEqual(complex(2.30551, 0.93681), + cmath.acosh(complex(3, 4))) + + def test_asin(self): + self.assertAlmostEqual(complex(0.633984, 2.30551), + cmath.asin(complex(3, 4))) + + def test_asinh(self): + self.assertAlmostEqual(complex(2.29991, 0.917617), + cmath.asinh(complex(3, 4))) + + def test_atan(self): + self.assertAlmostEqual(complex(1.44831, 0.158997), + cmath.atan(complex(3, 4))) + + def test_atanh(self): + self.assertAlmostEqual(complex(0.11750, 1.40992), + cmath.atanh(complex(3, 4))) + + def test_cos(self): + self.assertAlmostEqual(complex(-27.03495, -3.851153), + cmath.cos(complex(3, 4))) + + def test_cosh(self): + self.assertAlmostEqual(complex(-6.58066, -7.58155), + cmath.cosh(complex(3, 4))) + + def test_exp(self): + self.assertAlmostEqual(complex(-13.12878, -15.20078), + cmath.exp(complex(3, 4))) + + def test_log(self): + self.assertAlmostEqual(complex(1.60944, 0.927295), + cmath.log(complex(3, 4))) + + def test_log10(self): + self.assertAlmostEqual(complex(0.69897, 0.40272), + cmath.log10(complex(3, 4))) + + def test_sin(self): + self.assertAlmostEqual(complex(3.853738, -27.01681), + cmath.sin(complex(3, 4))) + + def test_sinh(self): + self.assertAlmostEqual(complex(-6.54812, -7.61923), + cmath.sinh(complex(3, 4))) + + def test_sqrt_real_positive(self): + self.assertAlmostEqual(complex(2, 1), + cmath.sqrt(complex(3, 4))) + + def test_sqrt_real_zero(self): + self.assertAlmostEqual(complex(1.41421, 1.41421), + cmath.sqrt(complex(0, 4))) + + def test_sqrt_real_negative(self): + self.assertAlmostEqual(complex(1, 2), + cmath.sqrt(complex(-3, 4))) + + def test_sqrt_imaginary_zero(self): + self.assertAlmostEqual(complex(0.0, 1.73205), + cmath.sqrt(complex(-3, 0))) + + def test_sqrt_imaginary_negative(self): + self.assertAlmostEqual(complex(1.0, -2.0), + cmath.sqrt(complex(-3, -4))) + + def test_tan(self): + self.assertAlmostEqual(complex(-0.000187346, 0.999356), + cmath.tan(complex(3, 4))) + + def test_tanh(self): + self.assertAlmostEqual(complex(1.00071, 0.00490826), + cmath.tanh(complex(3, 4))) + +def test_main(): + test_support.run_unittest(CmathTestCase) + +if __name__ == "__main__": + test_main() + + + Modified: branches/2.3/src/org/python/modules/Setup.java =================================================================== --- branches/2.3/src/org/python/modules/Setup.java 2006-12-03 22:23:58 UTC (rev 3005) +++ branches/2.3/src/org/python/modules/Setup.java 2006-12-05 02:58:36 UTC (rev 3006) @@ -53,6 +53,7 @@ "errno", "array:org.python.modules.ArrayModule", "sets:org.python.modules.sets.Sets", - "_random:org.python.modules.random.RandomModule" + "_random:org.python.modules.random.RandomModule", + "cmath" }; } Added: branches/2.3/src/org/python/modules/cmath.java =================================================================== --- branches/2.3/src/org/python/modules/cmath.java (rev 0) +++ branches/2.3/src/org/python/modules/cmath.java 2006-12-05 02:58:36 UTC (rev 3006) @@ -0,0 +1,213 @@ +package org.python.modules; + +import org.python.core.Py; +import org.python.core.PyComplex; +import org.python.core.PyException; +import org.python.core.PyFloat; +import org.python.core.PyObject; +import org.python.modules.math; + +public class cmath { + public static PyFloat pi = new PyFloat(Math.PI); + public static PyFloat e = new PyFloat(Math.E); + + private static PyComplex one = new PyComplex(1.0, 0.0); + private static PyComplex half = new PyComplex(0.5, 0.0); + private static PyComplex i = new PyComplex(0.0, 1.0); + private static PyComplex half_i = new PyComplex(0.0, 0.5); + + private static PyComplex c_prodi(PyComplex x) { + return (new PyComplex(-x.imag, x.real)); + } + + private static double hypot(double x, double y) { + return (Math.sqrt(x * x + y * y)); + } + + private static PyComplex complexFromPyObject(PyObject in) { + try{ + return(in.__complex__()); + } catch(PyException e){ + if(e.type == Py.AttributeError) { + throw Py.TypeError("a float is required"); + } + throw e; + } + } + + public static PyObject acos(PyObject in) { + PyComplex x = complexFromPyObject(in); + return (c_prodi(log(x.__add__(i + .__mul__(sqrt(one.__sub__(x.__mul__(x))))))).__neg__()); + } + + public static PyComplex acosh(PyObject in) { + PyComplex x = complexFromPyObject(in); + PyComplex r = null; + + PyComplex a = sqrt(x.__sub__(one)); + PyComplex b = sqrt(x.__add__(one)); + PyComplex c = sqrt(half); + r = log(c.__mul__(b.__add__(a))); + + return ((PyComplex) r.__add__(r)); + } + + public static PyComplex asin(PyObject in) { + PyComplex x = complexFromPyObject(in); + PyComplex r = null; + + PyComplex squared = (PyComplex) x.__mul__(x); + PyComplex sq1_minus_xsq = sqrt(one.__sub__(squared)); + + r = (PyComplex) c_prodi(log(sq1_minus_xsq.__add__(c_prodi(x)))) + .__neg__(); + return (r); + } + + public static PyComplex asinh(PyObject in) { + PyComplex x = complexFromPyObject(in); + PyComplex r = null; + + PyComplex a = sqrt(x.__add__(i)); + PyComplex b = sqrt(x.__sub__(i)); + PyComplex z = sqrt(half); + r = log(z.__mul__(a.__add__(b))); + + return ((PyComplex) r.__add__(r)); + } + + public static PyComplex atan(PyObject in) { + PyComplex x = complexFromPyObject(in); + PyComplex r = (PyComplex) half_i.__mul__(log(i.__add__(x).__div__( + i.__sub__(x)))); + + return (r); + } + + public static PyComplex atanh(PyObject in) { + PyComplex x = complexFromPyObject(in); + PyComplex r = (PyComplex) half.__mul__(log(one.__add__(x).__div__( + one.__sub__(x)))); + return (r); + } + + public static PyComplex cos(PyObject in) { + PyComplex x = complexFromPyObject(in); + PyComplex r = new PyComplex(Math.cos(x.real) * math.cosh(x.imag), -Math + .sin(x.real) + * math.sinh(x.imag)); + return (r); + } + + public static PyComplex cosh(PyObject in) { + PyComplex x = complexFromPyObject(in); + PyComplex r = new PyComplex(Math.cos(x.imag) * math.cosh(x.real), Math + .sin(x.imag) + * math.sinh(x.real)); + return (r); + } + + public static PyComplex exp(PyObject in) { + PyComplex x = complexFromPyObject(in); + PyComplex r = new PyComplex(0.0, 0.0); + double l = Math.exp(x.real); + r.real = l * Math.cos(x.imag); + r.imag = l * Math.sin(x.imag); + return (r); + } + + public static PyComplex log(PyObject in) { + PyComplex r = new PyComplex(0.0, 0.0); + PyComplex x = complexFromPyObject(in); + r.imag = Math.atan2(x.imag, x.real); + r.real = Math.log(hypot(x.real, x.imag)); + return (r); + } + + public static PyComplex log10(PyObject in) { + PyComplex r = new PyComplex(0.0, 0.0); + PyComplex x = complexFromPyObject(in); + double l = hypot(x.real, x.imag); + r.imag = Math.atan2(x.imag, x.real) / Math.log(10.0); + r.real = math.log10(new PyFloat(l)); + return (r); + } + + public static PyComplex sin(PyObject in) { + PyComplex r = new PyComplex(0.0, 0.0); + PyComplex x = complexFromPyObject(in); + r.real = Math.sin(x.real) * math.cosh(x.imag); + r.imag = Math.cos(x.real) * math.sinh(x.imag); + return (r); + } + + public static PyComplex sinh(PyObject in) { + PyComplex r = new PyComplex(0.0, 0.0); + PyComplex x = complexFromPyObject(in); + r.real = Math.cos(x.imag) * math.sinh(x.real); + r.imag = Math.sin(x.imag) * math.cosh(x.real); + return (r); + } + + public static PyComplex sqrt(PyObject in) { + PyComplex x = complexFromPyObject(in); + PyComplex r = new PyComplex(0.0, 0.0); + + if ((x.real != 0.0) || (x.imag != 0.0)) { + double s = Math + .sqrt(0.5 * (Math.abs(x.real) + hypot(x.real, x.imag))); + double d = 0.5 * x.imag / s; + + if (x.real > 0) { + r.real = s; + r.imag = d; + } else if (x.imag >= 0) { + r.real = d; + r.imag = s; + } else { + r.real = -d; + r.imag = -s; + } + } + return (r); + } + + public static PyComplex tan(PyObject in) { + PyComplex x = complexFromPyObject(in); + PyComplex r = new PyComplex(0.0, 0.0); + + double sr = Math.sin(x.real); + double cr = Math.cos(x.real); + double shi = math.sinh(x.imag); + double chi = math.cosh(x.imag); + double rs = sr * chi; + double is = cr * shi; + double rc = cr * chi; + double ic = -sr * shi; + double d = rc * rc + ic * ic; + r.real = ((rs * rc) + (is * ic)) / d; + r.imag = ((is * rc) - (rs * ic)) / d; + + return (r); + } + + public static PyComplex tanh(PyObject in) { + PyComplex x = complexFromPyObject(in); + PyComplex r = new PyComplex(0.0, 0.0); + + double si = Math.sin(x.imag); + double ci = Math.cos(x.imag); + double shr = math.sinh(x.real); + double chr = math.cosh(x.real); + double rs = ci * shr; + double is = si * chr; + double rc = ci * chr; + double ic = si * shr; + double d = rc * rc + ic * ic; + r.real = ((rs * rc) + (is * ic)) / d; + r.imag = ((is * rc) - (rs * ic)) / d; + + return (r); + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |