forked from simonegli/hobbyspline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmath.py
226 lines (155 loc) · 4.22 KB
/
cmath.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
"""This module is always available. It provides access to mathematical
functions for complex numbers."""
# Complex math module
# much code borrowed from mathmodule.c
import math
from math import e, pi
# constants
_one = complex(1., 0.)
_half = complex(0.5, 0.)
_i = complex(0., 1.)
_halfi = complex(0., 0.5)
# internal function not available from Python
def _prodi(x):
x = complex(x, 0)
real = -x.imag
imag = x.real
return complex(real, imag)
def acos(x):
"""acos(x)
Return the arc cosine of x."""
return -(_prodi(log((x+(_i*sqrt((_one-(x*x))))))))
def acosh(x):
"""acosh(x)
Return the hyperbolic arccosine of x."""
z = complex()
z = sqrt(_half)
z = log(z*(sqrt(x+_one)+sqrt(x-_one)))
return z+z
def asin(x):
"""asin(x)
Return the arc sine of x."""
# -i * log[(sqrt(1-x**2) + i*x]
squared = x*x
sqrt_1_minus_x_sq = sqrt(_one-squared)
return -(_prodi(log((sqrt_1_minus_x_sq+_prodi(x)))))
def asinh(x):
"""asinh(x)
Return the hyperbolic arc sine of x."""
z = complex()
z = sqrt(_half)
z = log((z * (sqrt(x+_i)+sqrt((x-_i))) ))
return z+z
def atan(x):
"""atan(x)
Return the arc tangent of x."""
return _halfi*log(((_i+x)/(_i-x)))
def atanh(x):
"""atanh(x)
Return the hyperbolic arc tangent of x."""
return _half*log((_one+x)/(_one-x))
def cos(x):
"""cos(x)
Return the cosine of x."""
x = complex(x, 0)
real = math.cos(x.real) * math.cosh(x.imag)
imag = -math.sin(x.real) * math.sinh(x.imag)
return complex(real, imag)
def cosh(x):
"""cosh(x)
Return the hyperbolic cosine of x."""
x = complex(x, 0)
real = math.cos(x.imag) * math.cosh(x.real)
imag = math.sin(x.imag) * math.sinh(x.real)
return complex(real, imag)
def exp(x):
"""exp(x)
Return the exponential value e**x."""
x = complex(x, 0)
l = math.exp(x.real)
real = l * math.cos(x.imag)
imag = l * math.sin(x.imag)
return complex(real, imag)
def log(x, base=None):
"""log(x)
Return the natural logarithm of x."""
if base is not None:
return log(x) / log(base)
x = complex(x, 0)
l = math.hypot(x.real,x.imag)
imag = math.atan2(x.imag, x.real)
real = math.log(l)
return complex(real, imag)
def log10(x):
"""log10(x)
Return the base-10 logarithm of x."""
x = complex(x, 0)
l = math.hypot(x.real, x.imag)
imag = math.atan2(x.imag, x.real)/math.log(10.)
real = math.log10(l)
return complex(real, imag)
def sin(x):
"""sin(x)
Return the sine of x."""
x = complex(x, 0)
real = math.sin(x.real) * math.cosh(x.imag)
imag = math.cos(x.real) * math.sinh(x.imag)
return complex(real, imag)
def sinh(x):
"""sinh(x)
Return the hyperbolic sine of x."""
x = complex(x, 0)
real = math.cos(x.imag) * math.sinh(x.real)
imag = math.sin(x.imag) * math.cosh(x.real)
return complex(real, imag)
def sqrt(x):
"""sqrt(x)
Return the square root of x."""
x = complex(x, 0)
if x.real == 0. and x.imag == 0.:
real, imag = 0, 0
else:
s = math.sqrt(0.5*(math.fabs(x.real) + math.hypot(x.real,x.imag)))
d = 0.5*x.imag/s
if x.real > 0.:
real = s
imag = d
elif x.imag >= 0.:
real = d
imag = s
else:
real = -d
imag = -s
return complex(real, imag)
def tan(x):
"""tan(x)
Return the tangent of x."""
x = complex(x, 0)
sr = math.sin(x.real)
cr = math.cos(x.real)
shi = math.sinh(x.imag)
chi = math.cosh(x.imag)
rs = sr * chi
is_ = cr * shi
rc = cr * chi
ic = -sr * shi
d = rc*rc + ic * ic
real = (rs*rc + is_*ic) / d
imag = (is_*rc - rs*ic) / d
return complex(real, imag)
def tanh(x):
"""tanh(x)
Return the hyperbolic tangent of x."""
x = complex(x, 0)
si = math.sin(x.imag)
ci = math.cos(x.imag)
shr = math.sinh(x.real)
chr = math.cosh(x.real)
rs = ci * shr
is_ = si * chr
rc = ci * chr
ic = si * shr
d = rc*rc + ic*ic
real = (rs*rc + is_*ic) / d
imag = (is_*rc - rs*ic) / d
return complex(real, imag)