|
| 1 | +package org.python.modules; |
| 2 | + |
| 3 | +import org.python.core.Py; |
| 4 | +import org.python.core.PyComplex; |
| 5 | +import org.python.core.PyException; |
| 6 | +import org.python.core.PyFloat; |
| 7 | +import org.python.core.PyObject; |
| 8 | +import org.python.modules.math; |
| 9 | + |
| 10 | +public class cmath { |
| 11 | + public static PyFloat pi = new PyFloat(Math.PI); |
| 12 | + public static PyFloat e = new PyFloat(Math.E); |
| 13 | + |
| 14 | + private static PyComplex one = new PyComplex(1.0, 0.0); |
| 15 | + private static PyComplex half = new PyComplex(0.5, 0.0); |
| 16 | + private static PyComplex i = new PyComplex(0.0, 1.0); |
| 17 | + private static PyComplex half_i = new PyComplex(0.0, 0.5); |
| 18 | + |
| 19 | + private static PyComplex c_prodi(PyComplex x) { |
| 20 | + return (new PyComplex(-x.imag, x.real)); |
| 21 | + } |
| 22 | + |
| 23 | + private static double hypot(double x, double y) { |
| 24 | + return (Math.sqrt(x * x + y * y)); |
| 25 | + } |
| 26 | + |
| 27 | + private static PyComplex complexFromPyObject(PyObject in) { |
| 28 | + try{ |
| 29 | + return(in.__complex__()); |
| 30 | + } catch(PyException e){ |
| 31 | + if(e.type == Py.AttributeError) { |
| 32 | + throw Py.TypeError("a float is required"); |
| 33 | + } |
| 34 | + throw e; |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + public static PyObject acos(PyObject in) { |
| 39 | + PyComplex x = complexFromPyObject(in); |
| 40 | + return (c_prodi(log(x.__add__(i |
| 41 | + .__mul__(sqrt(one.__sub__(x.__mul__(x))))))).__neg__()); |
| 42 | + } |
| 43 | + |
| 44 | + public static PyComplex acosh(PyObject in) { |
| 45 | + PyComplex x = complexFromPyObject(in); |
| 46 | + PyComplex r = null; |
| 47 | + |
| 48 | + PyComplex a = sqrt(x.__sub__(one)); |
| 49 | + PyComplex b = sqrt(x.__add__(one)); |
| 50 | + PyComplex c = sqrt(half); |
| 51 | + r = log(c.__mul__(b.__add__(a))); |
| 52 | + |
| 53 | + return ((PyComplex) r.__add__(r)); |
| 54 | + } |
| 55 | + |
| 56 | + public static PyComplex asin(PyObject in) { |
| 57 | + PyComplex x = complexFromPyObject(in); |
| 58 | + PyComplex r = null; |
| 59 | + |
| 60 | + PyComplex squared = (PyComplex) x.__mul__(x); |
| 61 | + PyComplex sq1_minus_xsq = sqrt(one.__sub__(squared)); |
| 62 | + |
| 63 | + r = (PyComplex) c_prodi(log(sq1_minus_xsq.__add__(c_prodi(x)))) |
| 64 | + .__neg__(); |
| 65 | + return (r); |
| 66 | + } |
| 67 | + |
| 68 | + public static PyComplex asinh(PyObject in) { |
| 69 | + PyComplex x = complexFromPyObject(in); |
| 70 | + PyComplex r = null; |
| 71 | + |
| 72 | + PyComplex a = sqrt(x.__add__(i)); |
| 73 | + PyComplex b = sqrt(x.__sub__(i)); |
| 74 | + PyComplex z = sqrt(half); |
| 75 | + r = log(z.__mul__(a.__add__(b))); |
| 76 | + |
| 77 | + return ((PyComplex) r.__add__(r)); |
| 78 | + } |
| 79 | + |
| 80 | + public static PyComplex atan(PyObject in) { |
| 81 | + PyComplex x = complexFromPyObject(in); |
| 82 | + PyComplex r = (PyComplex) half_i.__mul__(log(i.__add__(x).__div__( |
| 83 | + i.__sub__(x)))); |
| 84 | + |
| 85 | + return (r); |
| 86 | + } |
| 87 | + |
| 88 | + public static PyComplex atanh(PyObject in) { |
| 89 | + PyComplex x = complexFromPyObject(in); |
| 90 | + PyComplex r = (PyComplex) half.__mul__(log(one.__add__(x).__div__( |
| 91 | + one.__sub__(x)))); |
| 92 | + return (r); |
| 93 | + } |
| 94 | + |
| 95 | + public static PyComplex cos(PyObject in) { |
| 96 | + PyComplex x = complexFromPyObject(in); |
| 97 | + PyComplex r = new PyComplex(Math.cos(x.real) * math.cosh(x.imag), -Math |
| 98 | + .sin(x.real) |
| 99 | + * math.sinh(x.imag)); |
| 100 | + return (r); |
| 101 | + } |
| 102 | + |
| 103 | + public static PyComplex cosh(PyObject in) { |
| 104 | + PyComplex x = complexFromPyObject(in); |
| 105 | + PyComplex r = new PyComplex(Math.cos(x.imag) * math.cosh(x.real), Math |
| 106 | + .sin(x.imag) |
| 107 | + * math.sinh(x.real)); |
| 108 | + return (r); |
| 109 | + } |
| 110 | + |
| 111 | + public static PyComplex exp(PyObject in) { |
| 112 | + PyComplex x = complexFromPyObject(in); |
| 113 | + PyComplex r = new PyComplex(0.0, 0.0); |
| 114 | + double l = Math.exp(x.real); |
| 115 | + r.real = l * Math.cos(x.imag); |
| 116 | + r.imag = l * Math.sin(x.imag); |
| 117 | + return (r); |
| 118 | + } |
| 119 | + |
| 120 | + public static PyComplex log(PyObject in) { |
| 121 | + PyComplex r = new PyComplex(0.0, 0.0); |
| 122 | + PyComplex x = complexFromPyObject(in); |
| 123 | + r.imag = Math.atan2(x.imag, x.real); |
| 124 | + r.real = Math.log(hypot(x.real, x.imag)); |
| 125 | + return (r); |
| 126 | + } |
| 127 | + |
| 128 | + public static PyComplex log10(PyObject in) { |
| 129 | + PyComplex r = new PyComplex(0.0, 0.0); |
| 130 | + PyComplex x = complexFromPyObject(in); |
| 131 | + double l = hypot(x.real, x.imag); |
| 132 | + r.imag = Math.atan2(x.imag, x.real) / Math.log(10.0); |
| 133 | + r.real = math.log10(new PyFloat(l)); |
| 134 | + return (r); |
| 135 | + } |
| 136 | + |
| 137 | + public static PyComplex sin(PyObject in) { |
| 138 | + PyComplex r = new PyComplex(0.0, 0.0); |
| 139 | + PyComplex x = complexFromPyObject(in); |
| 140 | + r.real = Math.sin(x.real) * math.cosh(x.imag); |
| 141 | + r.imag = Math.cos(x.real) * math.sinh(x.imag); |
| 142 | + return (r); |
| 143 | + } |
| 144 | + |
| 145 | + public static PyComplex sinh(PyObject in) { |
| 146 | + PyComplex r = new PyComplex(0.0, 0.0); |
| 147 | + PyComplex x = complexFromPyObject(in); |
| 148 | + r.real = Math.cos(x.imag) * math.sinh(x.real); |
| 149 | + r.imag = Math.sin(x.imag) * math.cosh(x.real); |
| 150 | + return (r); |
| 151 | + } |
| 152 | + |
| 153 | + public static PyComplex sqrt(PyObject in) { |
| 154 | + PyComplex x = complexFromPyObject(in); |
| 155 | + PyComplex r = new PyComplex(0.0, 0.0); |
| 156 | + |
| 157 | + if ((x.real != 0.0) || (x.imag != 0.0)) { |
| 158 | + double s = Math |
| 159 | + .sqrt(0.5 * (Math.abs(x.real) + hypot(x.real, x.imag))); |
| 160 | + double d = 0.5 * x.imag / s; |
| 161 | + |
| 162 | + if (x.real > 0) { |
| 163 | + r.real = s; |
| 164 | + r.imag = d; |
| 165 | + } else if (x.imag >= 0) { |
| 166 | + r.real = d; |
| 167 | + r.imag = s; |
| 168 | + } else { |
| 169 | + r.real = -d; |
| 170 | + r.imag = -s; |
| 171 | + } |
| 172 | + } |
| 173 | + return (r); |
| 174 | + } |
| 175 | + |
| 176 | + public static PyComplex tan(PyObject in) { |
| 177 | + PyComplex x = complexFromPyObject(in); |
| 178 | + PyComplex r = new PyComplex(0.0, 0.0); |
| 179 | + |
| 180 | + double sr = Math.sin(x.real); |
| 181 | + double cr = Math.cos(x.real); |
| 182 | + double shi = math.sinh(x.imag); |
| 183 | + double chi = math.cosh(x.imag); |
| 184 | + double rs = sr * chi; |
| 185 | + double is = cr * shi; |
| 186 | + double rc = cr * chi; |
| 187 | + double ic = -sr * shi; |
| 188 | + double d = rc * rc + ic * ic; |
| 189 | + r.real = ((rs * rc) + (is * ic)) / d; |
| 190 | + r.imag = ((is * rc) - (rs * ic)) / d; |
| 191 | + |
| 192 | + return (r); |
| 193 | + } |
| 194 | + |
| 195 | + public static PyComplex tanh(PyObject in) { |
| 196 | + PyComplex x = complexFromPyObject(in); |
| 197 | + PyComplex r = new PyComplex(0.0, 0.0); |
| 198 | + |
| 199 | + double si = Math.sin(x.imag); |
| 200 | + double ci = Math.cos(x.imag); |
| 201 | + double shr = math.sinh(x.real); |
| 202 | + double chr = math.cosh(x.real); |
| 203 | + double rs = ci * shr; |
| 204 | + double is = si * chr; |
| 205 | + double rc = ci * chr; |
| 206 | + double ic = si * shr; |
| 207 | + double d = rc * rc + ic * ic; |
| 208 | + r.real = ((rs * rc) + (is * ic)) / d; |
| 209 | + r.imag = ((is * rc) - (rs * ic)) / d; |
| 210 | + |
| 211 | + return (r); |
| 212 | + } |
| 213 | +} |
0 commit comments